Added checkstyle

This commit is contained in:
Marcin Grzejszczak
2019-02-03 19:27:07 +01:00
parent 4d0acf120c
commit 60f1e21d03
249 changed files with 7450 additions and 5857 deletions

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package configuration;
@@ -52,16 +52,15 @@ public class JobConfiguration {
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(step1()).next(step2())
.build();
return this.jobBuilderFactory.get("job").start(step1()).next(step2()).build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1").tasklet(new Tasklet() {
return this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
@@ -70,15 +69,15 @@ public class JobConfiguration {
@Bean
public Step step2() {
return stepBuilderFactory.get("step2").<String, String>chunk(DEFAULT_CHUNK_COUNT)
return this.stepBuilderFactory.get("step2")
.<String, String>chunk(DEFAULT_CHUNK_COUNT)
.reader(new ListItemReader<>(Arrays.asList("1", "2", "3", "4", "5", "6")))
.processor(new ItemProcessor<String, String>() {
@Override
public String process(String item) throws Exception {
return String.valueOf(Integer.parseInt(item) * -1);
}
})
.writer(new ItemWriter<String>() {
}).writer(new ItemWriter<String>() {
@Override
public void write(List<? extends String> items) throws Exception {
for (Object item : items) {

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package configuration;
@@ -36,6 +36,7 @@ import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
public class JobSkipConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@@ -44,16 +45,15 @@ public class JobSkipConfiguration {
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(step1()).next(step2())
.build();
return this.jobBuilderFactory.get("job").start(step1()).next(step2()).build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1").tasklet(new Tasklet() {
return this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
@@ -62,15 +62,15 @@ public class JobSkipConfiguration {
@Bean
public Step step2() {
return stepBuilderFactory.get("step2").chunk(3).faultTolerant().skip(IllegalStateException.class).skipLimit(100)
return this.stepBuilderFactory.get("step2").chunk(3).faultTolerant()
.skip(IllegalStateException.class).skipLimit(100)
.reader(new SkipItemReader())
.processor(new ItemProcessor<Object, Object>() {
@Override
public String process(Object item) throws Exception {
return String.valueOf(Integer.parseInt((String) item) * -1);
}
})
.writer( new SkipItemWriter() ).build();
}).writer(new SkipItemWriter()).build();
}
}

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package configuration;
@@ -24,22 +24,25 @@ import org.springframework.batch.item.UnexpectedInputException;
/**
* @author Glenn Renfro
*/
public class SkipItemReader implements ItemReader{
public class SkipItemReader implements ItemReader {
int failCount = 0;
boolean finished = false;
@Override
public Object read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
public Object read() throws Exception, UnexpectedInputException, ParseException,
NonTransientResourceException {
String result = "1";
if(failCount < 2) {
failCount++;
if (this.failCount < 2) {
this.failCount++;
throw new IllegalStateException("Reader FOOBAR");
}
if (finished){
if (this.finished) {
result = null;
}
finished = true;
this.finished = true;
return result;
}
}

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package configuration;
@@ -29,12 +29,13 @@ public class SkipItemWriter implements ItemWriter {
@Override
public void write(List items) throws Exception {
if(failCount < 2) {
failCount++;
if (this.failCount < 2) {
this.failCount++;
throw new IllegalStateException("Writer FOOBAR");
}
for (Object item : items) {
System.out.println(">> " + item);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -38,9 +38,11 @@ public class TaskStartApplication {
return new CommandLineRunner() {
@Override
public void run(String... strings) throws Exception {
for(String s : strings)
for (String s : strings) {
System.out.println("Test" + s);
}
}
};
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -61,29 +61,35 @@ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.SocketUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {TaskStartTests.TaskLauncherConfiguration.class})
@SpringBootTest(classes = { TaskStartTests.TaskLauncherConfiguration.class })
public class TaskStartTests {
private final static int WAIT_INTERVAL = 500;
private final static int MAX_WAIT_TIME = 5000;
private final static String URL = "maven://io.spring.cloud:"
+ "timestamp-task:jar:1.1.0.RELEASE";
private final static String DATASOURCE_URL;
private final static String DATASOURCE_USER_NAME = "SA";
private final static String DATASOURCE_USER_PASSWORD = "''";
private final static String DATASOURCE_DRIVER_CLASS_NAME = "org.h2.Driver";
private final static String TASK_NAME = "TASK_LAUNCHER_SINK_TEST";
private static int randomPort;
static {
randomPort = SocketUtils.findAvailableTcpPort();
DATASOURCE_URL = "jdbc:h2:tcp://localhost:" + randomPort + "/mem:dataflow;DB_CLOSE_DELAY=-1;"
+ "DB_CLOSE_ON_EXIT=FALSE";
DATASOURCE_URL = "jdbc:h2:tcp://localhost:" + randomPort
+ "/mem:dataflow;DB_CLOSE_DELAY=-1;" + "DB_CLOSE_ON_EXIT=FALSE";
}
private DataSource dataSource;
@@ -98,7 +104,7 @@ public class TaskStartTests {
@After
public void tearDown() {
if (this.applicationContext != null && this.applicationContext.isActive() ) {
if (this.applicationContext != null && this.applicationContext.isActive()) {
this.applicationContext.close();
}
}
@@ -106,20 +112,22 @@ public class TaskStartTests {
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(dataSource);
taskExplorer = new SimpleTaskExplorer(factoryBean);
taskRepository = new SimpleTaskRepository(factoryBean);
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(
dataSource);
this.taskExplorer = new SimpleTaskExplorer(factoryBean);
this.taskRepository = new SimpleTaskRepository(factoryBean);
}
@Before
public void setup() {
properties = new HashMap<>();
properties.put("spring.datasource.url", DATASOURCE_URL);
properties.put("spring.datasource.username", DATASOURCE_USER_NAME);
properties.put("spring.datasource.password", DATASOURCE_USER_PASSWORD);
properties.put("spring.datasource.driverClassName", DATASOURCE_DRIVER_CLASS_NAME);
properties.put("spring.application.name",TASK_NAME);
properties.put("spring.cloud.task.initialize.enable", "false");
this.properties = new HashMap<>();
this.properties.put("spring.datasource.url", DATASOURCE_URL);
this.properties.put("spring.datasource.username", DATASOURCE_USER_NAME);
this.properties.put("spring.datasource.password", DATASOURCE_USER_PASSWORD);
this.properties.put("spring.datasource.driverClassName",
DATASOURCE_DRIVER_CLASS_NAME);
this.properties.put("spring.application.name", TASK_NAME);
this.properties.put("spring.cloud.task.initialize.enable", "false");
JdbcTemplate template = new JdbcTemplate(this.dataSource);
template.execute("DROP TABLE IF EXISTS TASK_TASK_BATCH");
@@ -140,41 +148,52 @@ public class TaskStartTests {
DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSource);
initializer.setDataSource(this.dataSource);
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
databasePopulator.addScript(new ClassPathResource("/org/springframework/cloud/task/schema-h2.sql"));
databasePopulator.addScript(
new ClassPathResource("/org/springframework/cloud/task/schema-h2.sql"));
initializer.setDatabasePopulator(databasePopulator);
initializer.afterPropertiesSet();
}
@Test
public void testWithGeneratedTaskExecution() throws Exception {
taskRepository.createTaskExecution();
assertEquals("Only one row is expected", 1, taskExplorer.getTaskExecutionCount());
this.taskRepository.createTaskExecution();
assertThat(this.taskExplorer.getTaskExecutionCount())
.as("Only one row is expected").isEqualTo(1);
this.applicationContext = getTaskApplication(1).run(new String[0]);
assertTrue(waitForDBToBePopulated());
assertThat(waitForDBToBePopulated()).isTrue();
Page<TaskExecution> taskExecutions = taskExplorer.findAll(PageRequest.of(0, 10));
Page<TaskExecution> taskExecutions = this.taskExplorer
.findAll(PageRequest.of(0, 10));
TaskExecution te = taskExecutions.iterator().next();
assertEquals("Only one row is expected", 1, taskExecutions.getTotalElements());
assertEquals("return code should be 0", 0, taskExecutions.iterator().next().getExitCode().intValue());
assertThat(taskExecutions.getTotalElements()).as("Only one row is expected")
.isEqualTo(1);
assertThat(taskExecutions.iterator().next().getExitCode().intValue())
.as("return code should be 0").isEqualTo(0);
}
@Test
public void testWithGeneratedTaskExecutionWithName() throws Exception {
final String TASK_EXECUTION_NAME = "PRE-EXECUTION-TEST-NAME";
taskRepository.createTaskExecution(TASK_EXECUTION_NAME);
assertEquals("Only one row is expected", 1, taskExplorer.getTaskExecutionCount());
assertEquals(TASK_EXECUTION_NAME, taskExplorer.getTaskExecution(1).getTaskName());
this.taskRepository.createTaskExecution(TASK_EXECUTION_NAME);
assertThat(this.taskExplorer.getTaskExecutionCount())
.as("Only one row is expected").isEqualTo(1);
assertThat(this.taskExplorer.getTaskExecution(1).getTaskName())
.isEqualTo(TASK_EXECUTION_NAME);
this.applicationContext = getTaskApplication(1).run(new String[0]);
assertTrue(waitForDBToBePopulated());
assertThat(waitForDBToBePopulated()).isTrue();
Page<TaskExecution> taskExecutions = taskExplorer.findAll(PageRequest.of(0, 10));
Page<TaskExecution> taskExecutions = this.taskExplorer
.findAll(PageRequest.of(0, 10));
TaskExecution te = taskExecutions.iterator().next();
assertEquals("Only one row is expected", 1, taskExecutions.getTotalElements());
assertEquals("return code should be 0", 0, taskExecutions.iterator().next().getExitCode().intValue());
assertEquals("batchEvents", taskExplorer.getTaskExecution(1).getTaskName());
assertThat(taskExecutions.getTotalElements()).as("Only one row is expected")
.isEqualTo(1);
assertThat(taskExecutions.iterator().next().getExitCode().intValue())
.as("return code should be 0").isEqualTo(0);
assertThat(this.taskExplorer.getTaskExecution(1).getTaskName())
.isEqualTo("batchEvents");
}
@Test(expected = ApplicationContextException.class)
@@ -184,64 +203,67 @@ public class TaskStartTests {
@Test(expected = ApplicationContextException.class)
public void testCompletedTaskExecution() throws Exception {
taskRepository.createTaskExecution();
assertEquals("Only one row is expected", 1, taskExplorer.getTaskExecutionCount());
taskRepository.completeTaskExecution(1, 0, new Date(),"");
this.taskRepository.createTaskExecution();
assertThat(this.taskExplorer.getTaskExecutionCount())
.as("Only one row is expected").isEqualTo(1);
this.taskRepository.completeTaskExecution(1, 0, new Date(), "");
this.applicationContext = getTaskApplication(1).run(new String[0]);
}
@Test
public void testDuplicateTaskExecutionWithSingleInstanceEnabled() throws Exception {
String params[] = {"--spring.cloud.task.single-instance-enabled=true",
"--spring.cloud.task.name=foo"};
String[] params = { "--spring.cloud.task.single-instance-enabled=true",
"--spring.cloud.task.name=foo" };
boolean testFailed = false;
try {
taskRepository.createTaskExecution();
assertEquals("Only one row is expected", 1, taskExplorer.getTaskExecutionCount());
this.taskRepository.createTaskExecution();
assertThat(this.taskExplorer.getTaskExecutionCount())
.as("Only one row is expected").isEqualTo(1);
enableLock("foo");
getTaskApplication(1).run(params);
}
catch (ApplicationContextException taskException) {
assertEquals("Failed to start bean 'taskLifecycleListener'; nested " +
"exception is org.springframework.cloud.task." +
"listener.TaskExecutionException: Failed to process " +
"@BeforeTask or @AfterTask annotation because: Task with name \"foo\" is already running.",
taskException.getMessage());
assertThat(taskException.getMessage())
.isEqualTo("Failed to start bean 'taskLifecycleListener'; nested "
+ "exception is org.springframework.cloud.task."
+ "listener.TaskExecutionException: Failed to process "
+ "@BeforeTask or @AfterTask annotation because: Task with name \"foo\" is already running.");
testFailed = true;
}
assertTrue("Expected TaskExecutionException for because of " +
"single-instance-enabled is enabled", testFailed);
assertThat(testFailed).as("Expected TaskExecutionException for because of "
+ "single-instance-enabled is enabled").isTrue();
}
@Test
public void testDuplicateTaskExecutionWithSingleInstanceDisabled() throws Exception {
taskRepository.createTaskExecution();
TaskExecution execution = taskRepository.createTaskExecution();
taskRepository.startTaskExecution(execution.getExecutionId(), "bar",
this.taskRepository.createTaskExecution();
TaskExecution execution = this.taskRepository.createTaskExecution();
this.taskRepository.startTaskExecution(execution.getExecutionId(), "bar",
new Date(), new ArrayList<>(), "");
String params[] = {"--spring.cloud.task.name=bar"};
String[] params = { "--spring.cloud.task.name=bar" };
enableLock("bar");
this.applicationContext = getTaskApplication(1).run(params);
assertTrue(waitForDBToBePopulated());
assertThat(waitForDBToBePopulated()).isTrue();
}
private SpringApplication getTaskApplication(Integer executionId) {
SpringApplication myapp = new SpringApplication(TaskStartApplication.class);
Map<String,Object> myMap = new HashMap<>();
Map<String, Object> myMap = new HashMap<>();
ConfigurableEnvironment environment = new StandardEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
myMap.put("spring.cloud.task.executionid", executionId);
propertySources.addFirst(new MapPropertySource("EnvrionmentTestPropsource", myMap));
propertySources
.addFirst(new MapPropertySource("EnvrionmentTestPropsource", myMap));
myapp.setEnvironment(environment);
return myapp;
}
private boolean tableExists() throws SQLException {
boolean result;
try (Connection conn = dataSource.getConnection();
ResultSet res = conn.getMetaData().getTables(null, null, "TASK_EXECUTION",
new String[]{"TABLE"})) {
try (Connection conn = this.dataSource.getConnection();
ResultSet res = conn.getMetaData().getTables(null, null, "TASK_EXECUTION",
new String[] { "TABLE" })) {
result = res.next();
}
return result;
@@ -251,7 +273,7 @@ public class TaskStartTests {
boolean isDbPopulated = false;
for (int waitTime = 0; waitTime <= MAX_WAIT_TIME; waitTime += WAIT_INTERVAL) {
Thread.sleep(WAIT_INTERVAL);
if (tableExists() && taskExplorer.getTaskExecutionCount() > 0) {
if (tableExists() && this.taskExplorer.getTaskExecutionCount() > 0) {
isDbPopulated = true;
break;
}
@@ -260,9 +282,11 @@ public class TaskStartTests {
}
private void enableLock(String lockKey) {
SimpleJdbcInsert taskLockInsert = new SimpleJdbcInsert(this.dataSource).withTableName("TASK_LOCK");
SimpleJdbcInsert taskLockInsert = new SimpleJdbcInsert(this.dataSource)
.withTableName("TASK_LOCK");
Map<String, Object> taskLockParams = new HashMap<>();
taskLockParams.put("LOCK_KEY", UUID.nameUUIDFromBytes(lockKey.getBytes()).toString());
taskLockParams.put("LOCK_KEY",
UUID.nameUUIDFromBytes(lockKey.getBytes()).toString());
taskLockParams.put("REGION", "DEFAULT");
taskLockParams.put("CLIENT_ID", "aClientID");
taskLockParams.put("CREATED_DATE", new Date());
@@ -271,13 +295,14 @@ public class TaskStartTests {
@Configuration
public static class TaskLauncherConfiguration {
private static Server defaultServer;
@Bean()
@Bean
public Server initH2TCPServer() {
Server server = null;
try {
if(defaultServer == null) {
if (defaultServer == null) {
server = Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort",
String.valueOf(randomPort)).start();
defaultServer = server;
@@ -298,6 +323,7 @@ public class TaskStartTests {
dataSource.setPassword(DATASOURCE_USER_PASSWORD);
return dataSource;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -21,7 +21,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.sql.DataSource;
import org.h2.tools.Server;
@@ -55,35 +55,42 @@ import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.SocketUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {TaskLauncherSinkApplication.class, TaskLauncherSinkTests.TaskLauncherConfiguration.class},
properties = {"maven.remote-repositories.repo1.url=https://repo.spring.io/libs-milestone"})
@SpringBootTest(classes = { TaskLauncherSinkApplication.class,
TaskLauncherSinkTests.TaskLauncherConfiguration.class }, properties = {
"maven.remote-repositories.repo1.url=https://repo.spring.io/libs-milestone" })
public class TaskLauncherSinkTests {
private final static int WAIT_INTERVAL = 500;
private final static int MAX_WAIT_TIME = 120000;
private final static String URL = "maven://org.springframework.cloud.task.app:"
+ "timestamp-task:2.0.0.RELEASE";
private final static String DATASOURCE_URL;
private final static String DATASOURCE_USER_NAME = "SA";
private final static String DATASOURCE_USER_PASSWORD = "''";
private final static String DATASOURCE_DRIVER_CLASS_NAME = "org.h2.Driver";
private final static String TASK_NAME = "TASK_LAUNCHER_SINK_TEST";
@ClassRule
public static RabbitTestSupport rabbitTestSupport = new RabbitTestSupport();
private static int randomPort;
static {
randomPort = SocketUtils.findAvailableTcpPort();
DATASOURCE_URL = "jdbc:h2:tcp://localhost:" + randomPort + "/mem:dataflow;DB_CLOSE_DELAY=-1;"
+ "DB_CLOSE_ON_EXIT=FALSE";
DATASOURCE_URL = "jdbc:h2:tcp://localhost:" + randomPort
+ "/mem:dataflow;DB_CLOSE_DELAY=-1;" + "DB_CLOSE_ON_EXIT=FALSE";
}
@ClassRule
public static RabbitTestSupport rabbitTestSupport = new RabbitTestSupport();
@Autowired
private Sink sink;
@@ -96,27 +103,30 @@ public class TaskLauncherSinkTests {
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
taskExplorer = new SimpleTaskExplorer(new TaskExecutionDaoFactoryBean(dataSource));
this.taskExplorer = new SimpleTaskExplorer(
new TaskExecutionDaoFactoryBean(dataSource));
}
@Before
public void setup() {
properties = new HashMap<>();
properties.put("spring.datasource.url", DATASOURCE_URL);
properties.put("spring.datasource.username", DATASOURCE_USER_NAME);
properties.put("spring.datasource.password", DATASOURCE_USER_PASSWORD);
properties.put("spring.datasource.driverClassName", DATASOURCE_DRIVER_CLASS_NAME);
properties.put("spring.application.name",TASK_NAME);
properties.put("spring.cloud.task.initialize.enable", "false");
this.properties = new HashMap<>();
this.properties.put("spring.datasource.url", DATASOURCE_URL);
this.properties.put("spring.datasource.username", DATASOURCE_USER_NAME);
this.properties.put("spring.datasource.password", DATASOURCE_USER_PASSWORD);
this.properties.put("spring.datasource.driverClassName",
DATASOURCE_DRIVER_CLASS_NAME);
this.properties.put("spring.application.name", TASK_NAME);
this.properties.put("spring.cloud.task.initialize.enable", "false");
JdbcTemplate template = new JdbcTemplate(this.dataSource);
template.execute("DROP ALL OBJECTS");
DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSource);
initializer.setDataSource(this.dataSource);
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
databasePopulator.addScript(new ClassPathResource("/org/springframework/cloud/task/schema-h2.sql"));
databasePopulator.addScript(
new ClassPathResource("/org/springframework/cloud/task/schema-h2.sql"));
initializer.setDatabasePopulator(databasePopulator);
initializer.afterPropertiesSet();
@@ -125,20 +135,22 @@ public class TaskLauncherSinkTests {
@Test
public void testWithLocalDeployer() throws Exception {
launchTask(URL);
assertTrue(waitForDBToBePopulated());
assertThat(waitForDBToBePopulated()).isTrue();
Page<TaskExecution> taskExecutions = taskExplorer.findAll(PageRequest.of(0, 10));
assertEquals("Only one row is expected", 1, taskExecutions.getTotalElements());
assertTrue(waitForTaskToComplete());
assertEquals("return code should be 0", 0, taskExecutions.iterator().next().getExitCode().intValue());
Page<TaskExecution> taskExecutions = this.taskExplorer
.findAll(PageRequest.of(0, 10));
assertThat(taskExecutions.getTotalElements()).as("Only one row is expected")
.isEqualTo(1);
assertThat(waitForTaskToComplete()).isTrue();
assertThat(taskExecutions.iterator().next().getExitCode().intValue())
.as("return code should be 0").isEqualTo(0);
}
private boolean tableExists() throws SQLException {
boolean result;
try (
Connection conn = dataSource.getConnection();
try (Connection conn = this.dataSource.getConnection();
ResultSet res = conn.getMetaData().getTables(null, null, "TASK_EXECUTION",
new String[]{"TABLE"})) {
new String[] { "TABLE" })) {
result = res.next();
}
return result;
@@ -148,7 +160,7 @@ public class TaskLauncherSinkTests {
boolean isDbPopulated = false;
for (int waitTime = 0; waitTime <= MAX_WAIT_TIME; waitTime += WAIT_INTERVAL) {
Thread.sleep(WAIT_INTERVAL);
if (tableExists() && taskExplorer.getTaskExecutionCount() > 0) {
if (tableExists() && this.taskExplorer.getTaskExecutionCount() > 0) {
isDbPopulated = true;
break;
}
@@ -156,18 +168,18 @@ public class TaskLauncherSinkTests {
return isDbPopulated;
}
private boolean waitForTaskToComplete() throws Exception {
boolean istTaskComplete = false;
for (int waitTime = 0; waitTime <= MAX_WAIT_TIME; waitTime += WAIT_INTERVAL) {
Thread.sleep(WAIT_INTERVAL);
TaskExecution taskExecution = taskExplorer.getTaskExecution(1);
if (taskExecution.getExitCode() != null) {
istTaskComplete = true;
break;
}
private boolean waitForTaskToComplete() throws Exception {
boolean istTaskComplete = false;
for (int waitTime = 0; waitTime <= MAX_WAIT_TIME; waitTime += WAIT_INTERVAL) {
Thread.sleep(WAIT_INTERVAL);
TaskExecution taskExecution = this.taskExplorer.getTaskExecution(1);
if (taskExecution.getExitCode() != null) {
istTaskComplete = true;
break;
}
return istTaskComplete;
}
return istTaskComplete;
}
private void launchTask(String artifactURL) {
@@ -179,6 +191,7 @@ public class TaskLauncherSinkTests {
@Configuration
public static class TaskLauncherConfiguration {
@Bean
public TaskLauncher taskLauncher() {
LocalDeployerProperties props = new LocalDeployerProperties();
@@ -209,6 +222,7 @@ public class TaskLauncherSinkTests {
dataSource.setPassword(DATASOURCE_USER_PASSWORD);
return dataSource;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -30,4 +30,5 @@ public class TaskLauncherSinkApplication {
public static void main(String[] args) {
SpringApplication.run(TaskLauncherSinkApplication.class, args);
}
}

View File

@@ -1,18 +1,17 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.listener;
@@ -23,6 +22,7 @@ import java.util.concurrent.TimeUnit;
import configuration.JobConfiguration;
import configuration.JobSkipConfiguration;
import org.assertj.core.api.Assertions;
import org.junit.After;
import org.junit.ClassRule;
import org.junit.Test;
@@ -41,14 +41,15 @@ import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.PropertySource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Glenn Renfro
*/
public class BatchExecutionEventTests {
private static final String TASK_NAME = "jobEventTest";
@ClassRule
public static RabbitTestSupport rabbitTestSupport = new RabbitTestSupport();
@@ -77,82 +78,135 @@ public class BatchExecutionEventTests {
static int readSkipCount = 0;
static int writeSkipCount = 0;
private static final String TASK_NAME = "jobEventTest";
private ConfigurableApplicationContext applicationContext;
@After
public void tearDown() {
if (applicationContext != null && applicationContext.isActive() ) {
applicationContext.close();
if (this.applicationContext != null && this.applicationContext.isActive()) {
this.applicationContext.close();
}
}
@Test
public void testContext() {
applicationContext = new SpringApplicationBuilder()
.sources(this.getConfigurations(BatchExecutionEventTests.ListenerBinding.class, JobConfiguration.class))
.build().run(getCommandLineParams("--spring.cloud.stream.bindings.job-execution-events.destination=bazbar"));
this.applicationContext = new SpringApplicationBuilder()
.sources(this.getConfigurations(
BatchExecutionEventTests.ListenerBinding.class,
JobConfiguration.class))
.build().run(getCommandLineParams(
"--spring.cloud.stream.bindings.job-execution-events.destination=bazbar"));
assertNotNull(applicationContext.getBean("jobExecutionEventsListener"));
assertNotNull(applicationContext.getBean("stepExecutionEventsListener"));
assertNotNull(applicationContext.getBean("chunkEventsListener"));
assertNotNull(applicationContext.getBean("itemReadEventsListener"));
assertNotNull(applicationContext.getBean("itemWriteEventsListener"));
assertNotNull(applicationContext.getBean("itemProcessEventsListener"));
assertNotNull(applicationContext.getBean("skipEventsListener"));
assertNotNull(applicationContext.getBean(BatchEventAutoConfiguration.BatchEventsChannels.class));
assertThat(this.applicationContext.getBean("jobExecutionEventsListener"))
.isNotNull();
assertThat(this.applicationContext.getBean("stepExecutionEventsListener"))
.isNotNull();
assertThat(this.applicationContext.getBean("chunkEventsListener")).isNotNull();
assertThat(this.applicationContext.getBean("itemReadEventsListener")).isNotNull();
assertThat(this.applicationContext.getBean("itemWriteEventsListener"))
.isNotNull();
assertThat(this.applicationContext.getBean("itemProcessEventsListener"))
.isNotNull();
assertThat(this.applicationContext.getBean("skipEventsListener")).isNotNull();
assertThat(this.applicationContext
.getBean(BatchEventAutoConfiguration.BatchEventsChannels.class))
.isNotNull();
}
@Test
public void testJobEventListener() throws Exception {
testListener("--spring.cloud.stream.bindings.job-execution-events.destination=foobar",
testListener(
"--spring.cloud.stream.bindings.job-execution-events.destination=foobar",
jobExecutionLatch, BatchExecutionEventTests.ListenerBinding.class);
}
@Test
public void testStepEventListener() throws Exception {
testListener("--spring.cloud.stream.bindings.step-execution-events.destination=step-execution-foobar",
testListener(
"--spring.cloud.stream.bindings.step-execution-events.destination=step-execution-foobar",
stepExecutionLatch, BatchExecutionEventTests.StepListenerBinding.class);
assertEquals("the number of step1 events did not match", 2, stepOneCount);
assertEquals("the number of step2 events did not match", 2, stepTwoCount);
assertThat(stepOneCount).as("the number of step1 events did not match")
.isEqualTo(2);
assertThat(stepTwoCount).as("the number of step2 events did not match")
.isEqualTo(2);
}
@Test
public void testItemProcessEventListener() throws Exception {
testListener("--spring.cloud.stream.bindings.item-process-events.destination=item-process-foobar",
itemProcessLatch, BatchExecutionEventTests.ItemProcessListenerBinding.class);
testListener(
"--spring.cloud.stream.bindings.item-process-events.destination=item-process-foobar",
itemProcessLatch,
BatchExecutionEventTests.ItemProcessListenerBinding.class);
}
@Test
public void testChunkListener() throws Exception {
testListener("--spring.cloud.stream.bindings.chunk-events.destination=chunk-events-foobar",
chunkEventsLatch, BatchExecutionEventTests.ChunkEventsListenerBinding.class);
testListener(
"--spring.cloud.stream.bindings.chunk-events.destination=chunk-events-foobar",
chunkEventsLatch,
BatchExecutionEventTests.ChunkEventsListenerBinding.class);
}
@Test
public void testItemReadListener() throws Exception {
testListener("--spring.cloud.stream.bindings.item-read-events.destination=item-read-events-foobar",
itemReadEventsLatch, BatchExecutionEventTests.ItemReadEventsListenerBinding.class);
testListener(
"--spring.cloud.stream.bindings.item-read-events.destination=item-read-events-foobar",
itemReadEventsLatch,
BatchExecutionEventTests.ItemReadEventsListenerBinding.class);
}
@Test
public void testWriteListener() throws Exception {
testListener("--spring.cloud.stream.bindings.item-write-events.destination=item-write-events-foobar",
itemWriteEventsLatch, BatchExecutionEventTests.ItemWriteEventsListenerBinding.class);
testListener(
"--spring.cloud.stream.bindings.item-write-events.destination=item-write-events-foobar",
itemWriteEventsLatch,
BatchExecutionEventTests.ItemWriteEventsListenerBinding.class);
}
@Test
public void testSkipEventListener() throws Exception {
testListenerSkip("--spring.cloud.stream.bindings.skip-events.destination=skip-event-foobar",
skipEventsLatch, BatchExecutionEventTests.SkipEventsListenerBinding.class);
assertEquals("read skip count did not match expected result", 2, readSkipCount);
assertEquals("write skip count did not match expected result", 1, writeSkipCount);
testListenerSkip(
"--spring.cloud.stream.bindings.skip-events.destination=skip-event-foobar",
skipEventsLatch,
BatchExecutionEventTests.SkipEventsListenerBinding.class);
assertThat(readSkipCount).as("read skip count did not match expected result")
.isEqualTo(2);
assertThat(writeSkipCount).as("write skip count did not match expected result")
.isEqualTo(1);
}
private Class[] getConfigurations(Class sinkClazz, Class jobConfigurationClazz) {
return new Class[] { PropertyPlaceholderAutoConfiguration.class,
jobConfigurationClazz, sinkClazz };
}
private String[] getCommandLineParams(String sinkChannelParam) {
return new String[] { "--spring.cloud.task.closecontext_enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false",
"--spring.cloud.stream.defaultBinder=rabbit",
"--spring.cloud.stream.bindings.task-events.destination=test",
"foo=" + UUID.randomUUID().toString(), sinkChannelParam };
}
private void testListener(String channelBinding, CountDownLatch latch, Class<?> clazz)
throws Exception {
this.applicationContext = new SpringApplicationBuilder()
.sources(this.getConfigurations(clazz, JobConfiguration.class)).build()
.run(getCommandLineParams(channelBinding));
assertThat(latch.await(1, TimeUnit.SECONDS)).isTrue();
}
private void testListenerSkip(String channelBinding, CountDownLatch latch,
Class<?> clazz) throws Exception {
this.applicationContext = new SpringApplicationBuilder()
.sources(this.getConfigurations(clazz, JobSkipConfiguration.class))
.build().run(getCommandLineParams(channelBinding));
assertThat(latch.await(1, TimeUnit.SECONDS)).isTrue();
}
@EnableBinding(Sink.class)
@@ -163,9 +217,11 @@ public class BatchExecutionEventTests {
@StreamListener(Sink.INPUT)
public void receive(JobExecutionEvent execution) {
assertEquals("Job name should be job", "job", execution.getJobInstance().getJobName());
Assertions.assertThat(execution.getJobInstance().getJobName())
.isEqualTo("job").as("Job name should be job");
jobExecutionLatch.countDown();
}
}
@EnableBinding(Sink.class)
@@ -176,15 +232,16 @@ public class BatchExecutionEventTests {
@StreamListener(Sink.INPUT)
public void receive(StepExecutionEvent execution) {
if(execution.getStepName().equals("step1")) {
if (execution.getStepName().equals("step1")) {
stepOneCount++;
}
if(execution.getStepName().equals("step2")) {
if (execution.getStepName().equals("step2")) {
stepTwoCount++;
}
stepExecutionLatch.countDown();
}
}
@EnableBinding(Sink.class)
@@ -197,6 +254,7 @@ public class BatchExecutionEventTests {
public void receive(String object) {
itemProcessLatch.countDown();
}
}
@EnableBinding(Sink.class)
@@ -209,6 +267,7 @@ public class BatchExecutionEventTests {
public void receive(String message) {
chunkEventsLatch.countDown();
}
}
@EnableBinding(Sink.class)
@@ -221,6 +280,7 @@ public class BatchExecutionEventTests {
public void receive(Object itemRead) {
itemReadEventsLatch.countDown();
}
}
@EnableBinding(Sink.class)
@@ -228,18 +288,22 @@ public class BatchExecutionEventTests {
@EnableAutoConfiguration
@EnableTask
public static class SkipEventsListenerBinding {
private static final String SKIPPING_READ_MESSAGE = "Skipped when reading.";
private static final String SKIPPING_WRITE_CONTENT = "-1";
@StreamListener(Sink.INPUT)
public void receive(String exceptionMessage) {
if(exceptionMessage.toString().equals(SKIPPING_READ_MESSAGE)){
if (exceptionMessage.toString().equals(SKIPPING_READ_MESSAGE)) {
readSkipCount++;
}
if(exceptionMessage.toString().equals(SKIPPING_WRITE_CONTENT)){
if (exceptionMessage.toString().equals(SKIPPING_WRITE_CONTENT)) {
writeSkipCount++;
}
skipEventsLatch.countDown();
}
}
@EnableBinding(Sink.class)
@@ -250,41 +314,13 @@ public class BatchExecutionEventTests {
@StreamListener(Sink.INPUT)
public void receive(String itemWrite) {
assertTrue("Message should start with '3 items'", itemWrite.toString().startsWith("3 items "));
assertTrue("Message should end with ' written.'", itemWrite.toString().endsWith(" written."));
Assertions.assertThat(itemWrite).startsWith("3 items ")
.as("Message should start with '3 items'");
Assertions.assertThat(itemWrite).endsWith("written")
.as("Message should end with ' written.'");
itemWriteEventsLatch.countDown();
}
}
private Class[] getConfigurations(Class sinkClazz, Class jobConfigurationClazz) {
return new Class[]{PropertyPlaceholderAutoConfiguration.class,
jobConfigurationClazz,
sinkClazz };
}
private String[] getCommandLineParams(String sinkChannelParam) {
return new String[]{ "--spring.cloud.task.closecontext_enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false",
"--spring.cloud.stream.defaultBinder=rabbit",
"--spring.cloud.stream.bindings.task-events.destination=test",
"foo=" + UUID.randomUUID().toString(),
sinkChannelParam };
}
private void testListener(String channelBinding, CountDownLatch latch, Class<?> clazz) throws Exception{
applicationContext = new SpringApplicationBuilder()
.sources(this.getConfigurations(clazz, JobConfiguration.class))
.build().run(getCommandLineParams(channelBinding));
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
private void testListenerSkip(String channelBinding, CountDownLatch latch, Class<?> clazz) throws Exception{
applicationContext = new SpringApplicationBuilder()
.sources(this.getConfigurations(clazz, JobSkipConfiguration.class))
.build().run(getCommandLineParams(channelBinding));
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,11 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.listener;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.assertj.core.api.Assertions;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -36,37 +38,34 @@ import org.springframework.cloud.stream.config.BindingServiceConfiguration;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration;
import org.springframework.cloud.task.configuration.SingleTaskConfiguration;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Michael Minella
* @author Ilayaperumal Gopinathan
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {TaskEventTests.ListenerBinding.class})
@SpringBootTest(classes = { TaskEventTests.ListenerBinding.class })
public class TaskEventTests {
private static final String TASK_NAME = "taskEventTest";
@ClassRule
public static RabbitTestSupport rabbitTestSupport = new RabbitTestSupport();
// Count for two task execution events per task
static CountDownLatch latch = new CountDownLatch(2);
private static final String TASK_NAME = "taskEventTest";
@Test
public void testTaskEventListener() throws Exception {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
TaskEventAutoConfiguration.class,
.withConfiguration(AutoConfigurations.of(TaskEventAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
RabbitServiceAutoConfiguration.class,
SimpleTaskAutoConfiguration.class,
@@ -78,15 +77,18 @@ public class TaskEventTests {
"--spring.cloud.stream.defaultBinder=rabbit",
"--spring.cloud.stream.bindings.task-events.destination=test");
applicationContextRunner.run((context) -> {
assertNotNull(context.getBean("taskEventListener"));
assertNotNull(context.getBean(TaskEventAutoConfiguration.TaskEventChannels.class));
assertThat(context.getBean("taskEventListener")).isNotNull();
assertThat(
context.getBean(TaskEventAutoConfiguration.TaskEventChannels.class))
.isNotNull();
});
assertTrue(latch.await(1, TimeUnit.SECONDS));
assertThat(latch.await(1, TimeUnit.SECONDS)).isTrue();
}
@EnableTask
@Configuration
public static class TaskEventsConfiguration {
}
@EnableBinding(Sink.class)
@@ -96,12 +98,13 @@ public class TaskEventTests {
@StreamListener(Sink.INPUT)
public void receive(TaskExecution execution) {
assertTrue(String.format("Task name should be '%s'", TASK_NAME), execution.getTaskName().equals(TASK_NAME));
Assertions.assertThat(execution.getTaskName()).isEqualTo(TASK_NAME)
.as(String.format("Task name should be '%s'", TASK_NAME));
latch.countDown();
}
@Bean
public CommandLineRunner commandLineRunner(){
public CommandLineRunner commandLineRunner() {
return new CommandLineRunner() {
@Override
public void run(String... args) throws Exception {
@@ -109,5 +112,7 @@ public class TaskEventTests {
}
};
}
}
}