Add checkstyle support
Fixes #481 Execute checkstyle validation at build time. Add minimal set of rules. Ensure that existing code conforms to the rules.
This commit is contained in:
committed by
Ilayaperumal Gopinathan
parent
0bbabaf705
commit
147a0cb0bc
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -29,105 +29,110 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Abstract base class for JUnit {@link Rule}s that detect the presence of some external resource. If the resource is
|
||||
* indeed present, it will be available during the test lifecycle through {@link #getResource()}. If it is not, tests
|
||||
* will either fail or be skipped, depending on the value of system property {@value #SCS_EXTERNAL_SERVERS_REQUIRED}.
|
||||
* Abstract base class for JUnit {@link Rule}s that detect the presence of some external
|
||||
* resource. If the resource is indeed present, it will be available during the test
|
||||
* lifecycle through {@link #getResource()}. If it is not, tests will either fail or be
|
||||
* skipped, depending on the value of system property
|
||||
* {@value #SCS_EXTERNAL_SERVERS_REQUIRED}.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
* @author Gary Russell
|
||||
*/
|
||||
public abstract class AbstractExternalResourceTestSupport<R> implements TestRule {
|
||||
|
||||
public static final String SCS_EXTERNAL_SERVERS_REQUIRED = "SCS_EXTERNAL_SERVERS_REQUIRED";
|
||||
public static final String SCS_EXTERNAL_SERVERS_REQUIRED = "SCS_EXTERNAL_SERVERS_REQUIRED";
|
||||
|
||||
protected R resource;
|
||||
protected R resource;
|
||||
|
||||
private String resourceDescription;
|
||||
private String resourceDescription;
|
||||
|
||||
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
protected AbstractExternalResourceTestSupport(String resourceDescription) {
|
||||
Assert.hasText(resourceDescription, "resourceDescription is required");
|
||||
this.resourceDescription = resourceDescription;
|
||||
}
|
||||
protected AbstractExternalResourceTestSupport(String resourceDescription) {
|
||||
Assert.hasText(resourceDescription, "resourceDescription is required");
|
||||
this.resourceDescription = resourceDescription;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement apply(final Statement base, Description description) {
|
||||
try {
|
||||
obtainResource();
|
||||
}
|
||||
catch (Exception e) {
|
||||
maybeCleanup();
|
||||
@Override
|
||||
public Statement apply(final Statement base, Description description) {
|
||||
try {
|
||||
obtainResource();
|
||||
}
|
||||
catch (Exception e) {
|
||||
maybeCleanup();
|
||||
|
||||
return failOrSkip(e);
|
||||
}
|
||||
return failOrSkip(e);
|
||||
}
|
||||
|
||||
return new Statement() {
|
||||
return new Statement() {
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
try {
|
||||
base.evaluate();
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
cleanupResource();
|
||||
}
|
||||
catch (Exception ignored) {
|
||||
logger.warn("Exception while trying to cleanup proper resource", ignored);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
try {
|
||||
base.evaluate();
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
cleanupResource();
|
||||
}
|
||||
catch (Exception ignored) {
|
||||
logger.warn("Exception while trying to cleanup proper resource",
|
||||
ignored);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private Statement failOrSkip(final Exception e) {
|
||||
String serversRequired = System.getenv(SCS_EXTERNAL_SERVERS_REQUIRED);
|
||||
if ("true".equalsIgnoreCase(serversRequired)) {
|
||||
logger.error(resourceDescription + " IS REQUIRED BUT NOT AVAILABLE", e);
|
||||
fail(resourceDescription + " IS NOT AVAILABLE");
|
||||
// Never reached, here to satisfy method signature
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
logger.error(resourceDescription + " IS NOT AVAILABLE, SKIPPING TESTS", e);
|
||||
return new Statement() {
|
||||
private Statement failOrSkip(final Exception e) {
|
||||
String serversRequired = System.getenv(SCS_EXTERNAL_SERVERS_REQUIRED);
|
||||
if ("true".equalsIgnoreCase(serversRequired)) {
|
||||
logger.error(resourceDescription + " IS REQUIRED BUT NOT AVAILABLE", e);
|
||||
fail(resourceDescription + " IS NOT AVAILABLE");
|
||||
// Never reached, here to satisfy method signature
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
logger.error(resourceDescription + " IS NOT AVAILABLE, SKIPPING TESTS", e);
|
||||
return new Statement() {
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
Assume.assumeTrue("Skipping test due to " + resourceDescription + " not being available " + e, false);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
Assume.assumeTrue("Skipping test due to " + resourceDescription
|
||||
+ " not being available " + e, false);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private void maybeCleanup() {
|
||||
if (resource != null) {
|
||||
try {
|
||||
cleanupResource();
|
||||
}
|
||||
catch (Exception ignored) {
|
||||
logger.warn("Exception while trying to cleanup failed resource", ignored);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void maybeCleanup() {
|
||||
if (resource != null) {
|
||||
try {
|
||||
cleanupResource();
|
||||
}
|
||||
catch (Exception ignored) {
|
||||
logger.warn("Exception while trying to cleanup failed resource", ignored);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public R getResource() {
|
||||
return resource;
|
||||
}
|
||||
public R getResource() {
|
||||
return resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform cleanup of the {@link #resource} field, which is guaranteed to be non null.
|
||||
*
|
||||
* @throws Exception any exception thrown by this method will be logged and swallowed
|
||||
*/
|
||||
protected abstract void cleanupResource() throws Exception;
|
||||
/**
|
||||
* Perform cleanup of the {@link #resource} field, which is guaranteed to be non null.
|
||||
*
|
||||
* @throws Exception any exception thrown by this method will be logged and swallowed
|
||||
*/
|
||||
protected abstract void cleanupResource() throws Exception;
|
||||
|
||||
/**
|
||||
* Try to obtain and validate a resource. Implementors should either set the {@link #resource} field with a valid
|
||||
* resource and return normally, or throw an exception.
|
||||
*/
|
||||
protected abstract void obtainResource() throws Exception;
|
||||
/**
|
||||
* Try to obtain and validate a resource. Implementors should either set the
|
||||
* {@link #resource} field with a valid resource and return normally, or throw an
|
||||
* exception.
|
||||
*/
|
||||
protected abstract void obtainResource() throws Exception;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,30 +16,15 @@
|
||||
|
||||
package org.springframework.cloud.stream.test.junit.kafka;
|
||||
|
||||
import kafka.admin.AdminUtils;
|
||||
import kafka.consumer.Consumer;
|
||||
import kafka.consumer.ConsumerConfig;
|
||||
import kafka.javaapi.consumer.ConsumerConnector;
|
||||
import kafka.server.KafkaConfig;
|
||||
import kafka.server.KafkaServerStartable;
|
||||
|
||||
import kafka.utils.TestUtils;
|
||||
import org.I0Itec.zkclient.ZkClient;
|
||||
import org.apache.curator.framework.CuratorFramework;
|
||||
import org.apache.curator.framework.CuratorFrameworkFactory;
|
||||
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
|
||||
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
|
||||
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
|
||||
import org.apache.curator.retry.RetryUntilElapsed;
|
||||
import org.apache.curator.test.TestingServer;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Properties;
|
||||
|
||||
import kafka.server.KafkaConfig;
|
||||
import kafka.server.KafkaServerStartable;
|
||||
import kafka.utils.TestUtils;
|
||||
import org.apache.curator.test.TestingServer;
|
||||
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
/**
|
||||
* A test Kafka + ZooKeeper pair for testing purposes.
|
||||
@@ -65,8 +50,8 @@ public class TestKafkaCluster {
|
||||
}
|
||||
|
||||
private static KafkaConfig getKafkaConfig(final String zkConnectString) {
|
||||
scala.collection.Iterator<Properties> propsI =
|
||||
TestUtils.createBrokerConfigs(1, false).iterator();
|
||||
scala.collection.Iterator<Properties> propsI = TestUtils
|
||||
.createBrokerConfigs(1, false).iterator();
|
||||
assert propsI.hasNext();
|
||||
Properties props = propsI.next();
|
||||
assert props.containsKey("zookeeper.connect");
|
||||
@@ -75,8 +60,7 @@ public class TestKafkaCluster {
|
||||
}
|
||||
|
||||
public String getKafkaBrokerString() {
|
||||
return String.format("localhost:%d",
|
||||
kafkaServer.serverConfig().port());
|
||||
return String.format("localhost:%d", kafkaServer.serverConfig().port());
|
||||
}
|
||||
|
||||
public void stop() throws IOException {
|
||||
@@ -84,8 +68,6 @@ public class TestKafkaCluster {
|
||||
zkServer.stop();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getZkConnectString() {
|
||||
return zkServer.getConnectString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user