Updating BatchListener Unit Tests

resolves #211
 Please enter the commit message for your changes. Lines starting
This commit is contained in:
Glenn Renfro
2016-09-19 14:55:52 -04:00
committed by Michael Minella
parent d657b22b79
commit 2bfc4780c7
12 changed files with 941 additions and 82 deletions

View File

@@ -73,5 +73,10 @@
<artifactId>spring-cloud-deployer-resource-support</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -270,15 +270,6 @@ public class JobExecutionEvent extends Entity {
return new ArrayList<>(allExceptions);
}
/**
* Deserialize and ensure transient fields are re-instantiated when read
* back
*/
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
this.failureExceptions = new ArrayList<>();
}
/*
* (non-Javadoc)
*

View File

@@ -417,15 +417,6 @@ public class StepExecutionEvent extends Entity {
&& getId().equals(other.getId());
}
/**
* Deserialize and ensure transient fields are re-instantiated when read
* back
*/
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
this.failureExceptions = new ArrayList<>();
}
/*
* (non-Javadoc)
*

View File

@@ -0,0 +1,236 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* 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.
*/
package org.springframework.cloud.task.batch.listener;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.cloud.task.batch.listener.support.JobExecutionEvent;
import org.springframework.cloud.task.batch.listener.support.StepExecutionEvent;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.messaging.Message;
import static org.junit.Assert.assertEquals;
/**
* @author Glenn Renfro
*/
public class EventListenerTests {
private QueueChannel queueChannel;
private EventEmittingSkipListener eventEmittingSkipListener;
private EventEmittingItemProcessListener eventEmittingItemProcessListener;
private EventEmittingItemReadListener eventEmittingItemReadListener;
private EventEmittingItemWriteListener eventEmittingItemWriteListener;
private EventEmittingJobExecutionListener eventEmittingJobExecutionListener;
private EventEmittingStepExecutionListener eventEmittingStepExecutionListener;
@Before
public void beforeTests() {
queueChannel = new QueueChannel(1);
eventEmittingSkipListener = new EventEmittingSkipListener(queueChannel);
eventEmittingItemProcessListener = new EventEmittingItemProcessListener(queueChannel);
eventEmittingItemReadListener = new EventEmittingItemReadListener(queueChannel);
eventEmittingItemWriteListener = new EventEmittingItemWriteListener(queueChannel);
eventEmittingJobExecutionListener = new EventEmittingJobExecutionListener(queueChannel);
eventEmittingStepExecutionListener = new EventEmittingStepExecutionListener(queueChannel);
}
@Test
public void testItemProcessListenerOnProcessorError() {
RuntimeException exeption = new RuntimeException("Test Exception");
eventEmittingItemProcessListener.onProcessError("HELLO", exeption);
assertEquals(1, queueChannel.getQueueSize());
Message msg = queueChannel.receive();
assertEquals("Exception while item was being processed", msg.getPayload());
}
@Test
public void testItemProcessListenerAfterProcess() {
eventEmittingItemProcessListener.afterProcess("HELLO_AFTER_PROCESS_EQUAL",
"HELLO_AFTER_PROCESS_EQUAL");
assertEquals(1, queueChannel.getQueueSize());
Message msg = queueChannel.receive();
assertEquals("item equaled result after processing", msg.getPayload());
eventEmittingItemProcessListener.afterProcess("HELLO_NOT_EQUAL", "WORLD");
assertEquals(1, queueChannel.getQueueSize());
msg = queueChannel.receive();
assertEquals("item did not equal result after processing", msg.getPayload());
eventEmittingItemProcessListener.afterProcess("HELLO_AFTER_PROCESS", null);
assertEquals(1, queueChannel.getQueueSize());
msg = queueChannel.receive();
assertEquals("1 item was filtered", msg.getPayload());
}
@Test
public void testItemProcessBeforeProcessor() {
eventEmittingItemProcessListener.beforeProcess("HELLO_BEFORE_PROCESS");
assertEquals(0, queueChannel.getQueueSize());
}
@Test
public void EventEmittingSkipListenerSkipRead() {
RuntimeException exeption = new RuntimeException("Text Exception");
eventEmittingSkipListener.onSkipInRead(exeption);
assertEquals(1, queueChannel.getQueueSize());
Message msg = queueChannel.receive();
assertEquals("Skipped when reading.", msg.getPayload());
}
@Test
public void EventEmittingSkipListenerSkipWrite() {
final String MESSAGE = "HELLO_SKIP_WRITE";
RuntimeException exeption = new RuntimeException("Text Exception");
eventEmittingSkipListener.onSkipInWrite(MESSAGE, exeption);
assertEquals(1, queueChannel.getQueueSize());
Message msg = queueChannel.receive();
assertEquals(MESSAGE, msg.getPayload());
}
@Test
public void EventEmittingSkipListenerSkipProcess() {
final String MESSAGE = "HELLO_SKIP_PROCESS";
RuntimeException exeption = new RuntimeException("Text Exception");
eventEmittingSkipListener.onSkipInProcess(MESSAGE, exeption);
assertEquals(1, queueChannel.getQueueSize());
Message msg = queueChannel.receive();
assertEquals(MESSAGE, msg.getPayload());
}
@Test
public void EventEmittingItemReadListener() {
RuntimeException exeption = new RuntimeException("Text Exception");
eventEmittingItemReadListener.onReadError(exeption);
assertEquals(1, queueChannel.getQueueSize());
Message msg = queueChannel.receive();
assertEquals("Exception while item was being read", msg.getPayload());
}
@Test
public void EventEmittingItemReadListenerBeforeRead() {
eventEmittingItemReadListener.beforeRead();
assertEquals(0, queueChannel.getQueueSize());
}
@Test
public void EventEmittingItemReadListenerAfterRead() {
eventEmittingItemReadListener.afterRead("HELLO_AFTER_READ");
assertEquals(0, queueChannel.getQueueSize());
}
@Test
public void EventEmittingItemWriteListenerBeforeWrite() {
eventEmittingItemWriteListener.beforeWrite(getSampleList());
assertEquals(1, queueChannel.getQueueSize());
Message msg = queueChannel.receive();
assertEquals("3 items to be written.", msg.getPayload());
}
@Test
public void EventEmittingItemWriteListenerAfterWrite() {
eventEmittingItemWriteListener.afterWrite(getSampleList());
assertEquals(1, queueChannel.getQueueSize());
Message msg = queueChannel.receive();
assertEquals("3 items have been written.", msg.getPayload());
}
@Test
public void EventEmittingItemWriteListenerWriteError() {
RuntimeException exeption = new RuntimeException("Text Exception");
eventEmittingItemWriteListener.onWriteError(exeption, getSampleList());
assertEquals(1, queueChannel.getQueueSize());
Message msg = queueChannel.receive();
assertEquals("Exception while 3 items are attempted to be written.", msg.getPayload());
}
@Test
public void EventEmittingJobExecutionListenerBeforeJob() {
JobExecution jobExecution = getJobExecution();
eventEmittingJobExecutionListener.beforeJob(jobExecution);
assertEquals(1, queueChannel.getQueueSize());
Message msg = queueChannel.receive();
JobExecutionEvent jobEvent = (JobExecutionEvent) msg.getPayload();
assertEquals(jobExecution.getJobInstance().getJobName(),
jobEvent.getJobInstance().getJobName());
}
@Test
public void EventEmittingJobExecutionListenerAfterJob() {
JobExecution jobExecution = getJobExecution();
eventEmittingJobExecutionListener.afterJob(jobExecution);
assertEquals(1, queueChannel.getQueueSize());
Message msg = queueChannel.receive();
JobExecutionEvent jobEvent = (JobExecutionEvent) msg.getPayload();
assertEquals(jobExecution.getJobInstance().getJobName(),
jobEvent.getJobInstance().getJobName());
}
@Test
public void EventEmittingStepExecutionListenerBeforeStep() {
final String STEP_MESSAGE = "BEFORE_STEP_MESSAGE";
JobExecution jobExecution = getJobExecution();
StepExecution stepExecution = new StepExecution(STEP_MESSAGE,jobExecution);
eventEmittingStepExecutionListener.beforeStep(stepExecution);
assertEquals(1, queueChannel.getQueueSize());
Message msg = queueChannel.receive();
StepExecutionEvent stepExecutionEvent = (StepExecutionEvent) msg.getPayload();
assertEquals(STEP_MESSAGE,
stepExecutionEvent.getStepName());
}
@Test
public void EventEmittingStepExecutionListenerAfterStep() {
final String STEP_MESSAGE = "AFTER_STEP_MESSAGE";
JobExecution jobExecution = getJobExecution();
StepExecution stepExecution = new StepExecution(STEP_MESSAGE,jobExecution);
eventEmittingStepExecutionListener.afterStep(stepExecution);
assertEquals(1, queueChannel.getQueueSize());
Message msg = queueChannel.receive();
StepExecutionEvent stepExecutionEvent = (StepExecutionEvent) msg.getPayload();
assertEquals(STEP_MESSAGE,
stepExecutionEvent.getStepName());
}
private JobExecution getJobExecution() {
final String JOB_NAME = UUID.randomUUID().toString();
JobInstance jobInstance = new JobInstance(1L, JOB_NAME);
return new JobExecution(jobInstance, 1L,
new JobParameters(), UUID.randomUUID().toString());
}
private List<String> getSampleList() {
List<String> testList = new ArrayList<>(3);
testList.add("Hello");
testList.add("World");
testList.add("foo");
return testList;
}
}

View File

@@ -1,63 +0,0 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* 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.
*/
package org.springframework.cloud.task.batch.listener;
import org.junit.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.cloud.task.batch.listener.support.StepExecutionEvent;
import static org.junit.Assert.assertEquals;
/**
* @author Glenn Renfro
*/
public class EventStepExecutionTests {
private static final String JOB_NAME = "FOO_JOB";
private static final String STEP_NAME = "STEP_NAME";
private static final Long JOB_INSTANCE_ID = 1l;
private static final Long JOB_EXECUTION_ID = 2l;
private static final String JOB_CONFIGURATION_NAME = "FOO_JOB_CONFIG";
@Test
public void testBasic(){
JobInstance jobInstance = new JobInstance(JOB_INSTANCE_ID, JOB_NAME);
JobParameters jobParameters = new JobParameters();
JobExecution jobExecution = new JobExecution(jobInstance, JOB_EXECUTION_ID, jobParameters, JOB_CONFIGURATION_NAME);
StepExecution stepExecution = new StepExecution(STEP_NAME, jobExecution);
stepExecution.setCommitCount(1);
stepExecution.setReadCount(2);
stepExecution.setWriteCount(3);
stepExecution.setReadSkipCount(4);
stepExecution.setWriteSkipCount(5);
StepExecutionEvent stepExecutionEvent = new StepExecutionEvent(stepExecution);
assertEquals("stepName result was not as expected", STEP_NAME, stepExecutionEvent.getStepName());
assertEquals("startTime result was not as expected", stepExecution.getStartTime(), stepExecutionEvent.getStartTime());
assertEquals("endTime result was not as expected", stepExecution.getEndTime(), stepExecutionEvent.getEndTime());
assertEquals("lastUpdated result was not as expected", stepExecution.getLastUpdated(), stepExecutionEvent.getLastUpdated());
assertEquals("commitCount result was not as expected", stepExecution.getCommitCount(), stepExecutionEvent.getCommitCount());
assertEquals("readCount result was not as expected", stepExecution.getReadCount(), stepExecutionEvent.getReadCount());
assertEquals("readSkipCount result was not as expected", stepExecution.getReadSkipCount(), stepExecutionEvent.getReadSkipCount());
assertEquals("writeCount result was not as expected", stepExecution.getWriteCount(), stepExecutionEvent.getWriteCount());
assertEquals("writeSkipCount result was not as expected", stepExecution.getWriteSkipCount(), stepExecutionEvent.getWriteSkipCount());
assertEquals("skipCount result was not as expected", stepExecution.getSkipCount(), stepExecutionEvent.getSkipCount());
}
}

View File

@@ -26,16 +26,19 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration;
import org.springframework.cloud.task.batch.listener.support.JobExecutionEvent;
import org.springframework.cloud.task.batch.listener.support.JobInstanceEvent;
import org.springframework.cloud.task.batch.listener.support.StepExecutionEvent;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.context.ConfigurableApplicationContext;
@@ -43,12 +46,13 @@ import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* @author Glenn Renfro.
*/
public class EventJobExecutionTests {
public class JobExecutionEventTests {
private static final String JOB_NAME = "FOODJOB";
private static final Long JOB_INSTANCE_ID = 1l;
@@ -173,6 +177,99 @@ public class EventJobExecutionTests {
BatchEventAutoConfiguration.SKIP_EVENTS_LISTENER);
}
@Test
public void testDefaultConstructor() {
JobExecutionEvent jobExecutionEvent = new JobExecutionEvent();
assertEquals("UNKNOWN", jobExecutionEvent.getExitStatus().getExitCode());
}
@Test
public void testFailureExceptions() {
final String EXCEPTION_MESSAGE = "TEST EXCEPTION";
JobExecutionEvent jobExecutionEvent = new JobExecutionEvent();
assertEquals(0, jobExecutionEvent.getFailureExceptions().size());
jobExecutionEvent.addFailureException(new IllegalStateException(EXCEPTION_MESSAGE));
assertEquals(1, jobExecutionEvent.getFailureExceptions().size());
assertEquals(1, jobExecutionEvent.getAllFailureExceptions().size());
assertEquals(jobExecutionEvent.getFailureExceptions().get(0).getMessage(), EXCEPTION_MESSAGE);
assertEquals(jobExecutionEvent.getAllFailureExceptions().get(0).getMessage(), EXCEPTION_MESSAGE);
}
@Test
public void testToString() {
JobExecutionEvent jobExecutionEvent = new JobExecutionEvent();
assertTrue(jobExecutionEvent.toString().startsWith("JobExecutionEvent:"));
}
@Test
public void testGetterSetters() {
Date date = new Date();
JobExecutionEvent jobExecutionEvent = new JobExecutionEvent();
jobExecutionEvent.setLastUpdated(date);
assertEquals(date, jobExecutionEvent.getLastUpdated());
jobExecutionEvent.setCreateTime(date);
assertEquals(date, jobExecutionEvent.getCreateTime());
jobExecutionEvent.setEndTime(date);
assertEquals(date, jobExecutionEvent.getEndTime());
jobExecutionEvent.setStartTime(date);
assertEquals(date, jobExecutionEvent.getStartTime());
}
@Test
public void testExitStatus() {
final String EXIT_CODE = "KNOWN";
JobExecutionEvent jobExecutionEvent = new JobExecutionEvent();
assertEquals("UNKNOWN", jobExecutionEvent.getExitStatus().getExitCode());
org.springframework.cloud.task.batch.listener.support.ExitStatus expectedExitStatus =
new org.springframework.cloud.task.batch.listener.support.ExitStatus();
expectedExitStatus.setExitCode(EXIT_CODE);
jobExecutionEvent.setExitStatus(expectedExitStatus);
assertEquals(EXIT_CODE, jobExecutionEvent.getExitStatus().getExitCode());
}
@Test
public void testJobInstance() {
final String JOB_NAME = "KNOWN";
JobExecutionEvent jobExecutionEvent = new JobExecutionEvent();
assertNull(jobExecutionEvent.getJobInstance());
assertNull(jobExecutionEvent.getJobId());
JobInstanceEvent expectedJobInstanceEvent = new JobInstanceEvent(1L,
JOB_NAME);
jobExecutionEvent.setJobInstance(expectedJobInstanceEvent);
assertEquals(expectedJobInstanceEvent.getJobName(),
jobExecutionEvent.getJobInstance().getJobName());
assertEquals(expectedJobInstanceEvent.getId(),
jobExecutionEvent.getJobId());
}
@Test
public void testExecutionContext() {
ExecutionContext executionContext = new ExecutionContext();
executionContext.put("hello", "world");
JobExecutionEvent jobExecutionEvent = new JobExecutionEvent();
assertNotNull(jobExecutionEvent.getExecutionContext());
jobExecutionEvent.setExecutionContext(executionContext);
assertEquals("world", jobExecutionEvent.getExecutionContext().getString("hello"));
}
@Test
public void testBatchStatus() {
JobExecutionEvent jobExecutionEvent = new JobExecutionEvent();
assertEquals(BatchStatus.STARTING, jobExecutionEvent.getStatus());
jobExecutionEvent.setStatus(BatchStatus.ABANDONED);
assertEquals(BatchStatus.ABANDONED, jobExecutionEvent.getStatus());
}
@Test
public void testUpgradeBatchStatus() {
JobExecutionEvent jobExecutionEvent = new JobExecutionEvent();
assertEquals(BatchStatus.STARTING, jobExecutionEvent.getStatus());
jobExecutionEvent.upgradeStatus(BatchStatus.FAILED);
assertEquals(BatchStatus.FAILED, jobExecutionEvent.getStatus());
jobExecutionEvent.upgradeStatus(BatchStatus.COMPLETED);
assertEquals(BatchStatus.FAILED, jobExecutionEvent.getStatus());
}
public void testDisabledConfiguration(String property, String disabledListener) {
boolean exceptionThrown = false;
String disabledPropertyArg = (property != null) ? "--" + property + "=false" : "";

View File

@@ -0,0 +1,39 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* 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.
*/
package org.springframework.cloud.task.batch.listener;
import org.junit.Test;
import org.springframework.cloud.task.batch.listener.support.JobInstanceEvent;
import static org.junit.Assert.assertEquals;
/**
* @author Glenn Renfro
*/
public class JobInstanceEventTests {
public static final long INSTANCE_ID = 1;
public static final String JOB_NAME = "FOOBAR";
@Test
public void testConstructor() {
JobInstanceEvent jobInstanceEvent = new JobInstanceEvent(INSTANCE_ID, JOB_NAME);
assertEquals(INSTANCE_ID, jobInstanceEvent.getInstanceId());
assertEquals(JOB_NAME, jobInstanceEvent.getJobName());
}
}

View File

@@ -0,0 +1,94 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* 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.
*/
package org.springframework.cloud.task.batch.listener;
import java.util.Date;
import org.junit.Test;
import org.springframework.batch.core.JobParameter;
import org.springframework.cloud.task.batch.listener.support.JobParameterEvent;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Glenn Renfro
*/
public class JobParameterEventTests {
@Test
public void testDefaultConstructor() {
JobParameterEvent jobParameterEvent = new JobParameterEvent();
assertNull(jobParameterEvent.getValue());
assertNull(jobParameterEvent.getType());
assertFalse(jobParameterEvent.isIdentifying());
assertEquals(new JobParameterEvent(), jobParameterEvent);
}
@Test
public void testConstructor() {
final String EXPECTED_VALUE = "FOO";
final Date EXPECTED_DATE_VALUE = new Date();
JobParameter jobParameter = new JobParameter(EXPECTED_VALUE, true);
JobParameterEvent jobParameterEvent = new JobParameterEvent(jobParameter);
assertEquals(EXPECTED_VALUE, jobParameterEvent.getValue());
assertEquals(JobParameterEvent.ParameterType.STRING, jobParameterEvent.getType());
assertTrue(jobParameterEvent.isIdentifying());
jobParameter = new JobParameter(EXPECTED_DATE_VALUE, true);
jobParameterEvent = new JobParameterEvent(jobParameter);
assertEquals(EXPECTED_DATE_VALUE, jobParameterEvent.getValue());
assertEquals(JobParameterEvent.ParameterType.DATE, jobParameterEvent.getType());
assertTrue(jobParameterEvent.isIdentifying());
assertTrue(new JobParameterEvent(jobParameter).equals(jobParameterEvent));
}
@Test
public void testEquals() {
final String EXPECTED_VALUE = "FOO";
JobParameter jobParameter = new JobParameter(EXPECTED_VALUE, true);
JobParameterEvent jobParameterEvent = new JobParameterEvent(jobParameter);
JobParameterEvent anotherJobParameterEvent = new JobParameterEvent(jobParameter);
assertTrue(jobParameterEvent.equals(jobParameterEvent));
assertFalse(jobParameterEvent.equals("nope"));
assertTrue(jobParameterEvent.equals(anotherJobParameterEvent));
}
@Test(expected = NullPointerException.class)
public void testInvalidHashCode() {
JobParameterEvent jobParameterEvent = new JobParameterEvent();
assertNull(jobParameterEvent.hashCode());
final String EXPECTED_VALUE = "FOO";
JobParameter jobParameter = new JobParameter(EXPECTED_VALUE, true);
jobParameterEvent = new JobParameterEvent(jobParameter);
assertNotNull(jobParameterEvent.hashCode());
}
@Test
public void testValidHashCode() {
final String EXPECTED_VALUE = "FOO";
JobParameter jobParameter = new JobParameter(EXPECTED_VALUE, true);
JobParameterEvent jobParameterEvent = new JobParameterEvent(jobParameter);
assertNotNull(jobParameterEvent.hashCode());
}
}

View File

@@ -0,0 +1,130 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* 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.
*/
package org.springframework.cloud.task.batch.listener;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.junit.Test;
import org.springframework.batch.core.JobParameter;
import org.springframework.cloud.task.batch.listener.support.JobParametersEvent;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Glenn Renfro
*/
public class JobParametersEventTests {
private final static JobParameter STRING_PARAM = new JobParameter("FOO", true);
private final static JobParameter DATE_PARAM = new JobParameter(new Date(), true);
private final static JobParameter LONG_PARAM = new JobParameter(1L, true);
private final static JobParameter DOUBLE_PARAM = new JobParameter(2D, true);
private final static String DATE_KEY = "DATE_KEY";
private final static String STRING_KEY = "STRING_KEY";
private final static String LONG_KEY = "LONG_KEY";
private final static String DOUBLE_KEY = "DOUBLE_KEY";
@Test
public void testDefaultConstructor() {
JobParametersEvent jobParametersEvent = new JobParametersEvent();
assertEquals(0, jobParametersEvent.getParameters().size());
assertTrue(jobParametersEvent.isEmpty());
}
@Test
public void testConstructor() {
JobParametersEvent jobParametersEvent = getPopulatedParametersEvent();
assertEquals(STRING_PARAM.getValue(), jobParametersEvent.getString(STRING_KEY));
assertEquals(LONG_PARAM.getValue(), jobParametersEvent.getLong(LONG_KEY));
assertEquals(DATE_PARAM.getValue(), jobParametersEvent.getDate(DATE_KEY));
assertEquals(DOUBLE_PARAM.getValue(), jobParametersEvent.getDouble(DOUBLE_KEY));
JobParametersEvent jobParametersEventNew = getPopulatedParametersEvent();
assertEquals(jobParametersEventNew, jobParametersEvent);
}
@Test
public void testEquals() {
assertTrue(getPopulatedParametersEvent().equals(getPopulatedParametersEvent()));
JobParametersEvent jobParametersEvent = getPopulatedParametersEvent();
assertFalse(jobParametersEvent.equals("FOO"));
assertTrue(jobParametersEvent.equals(jobParametersEvent));
}
@Test
public void testHashCode() {
JobParametersEvent jobParametersEvent = new JobParametersEvent();
assertNotNull(jobParametersEvent.hashCode());
JobParametersEvent jobParametersEventPopulated = getPopulatedParametersEvent();
assertNotNull(jobParametersEvent);
assertNotEquals(jobParametersEvent.hashCode(), jobParametersEventPopulated.hashCode());
}
@Test
public void testToProperties() {
JobParametersEvent jobParametersEvent = getPopulatedParametersEvent();
Properties properties = jobParametersEvent.toProperties();
assertEquals(properties.getProperty(DATE_KEY), jobParametersEvent.getString(DATE_KEY));
assertEquals(properties.getProperty(STRING_KEY), jobParametersEvent.getString(STRING_KEY));
assertEquals(properties.getProperty(LONG_KEY), jobParametersEvent.getString(LONG_KEY));
assertEquals(properties.getProperty(DOUBLE_KEY), jobParametersEvent.getString(DOUBLE_KEY));
}
@Test
public void testToString() {
JobParametersEvent jobParametersEvent = getPopulatedParametersEvent();
assertNotNull(toString());
}
@Test
public void testGetterSetterDefaults() {
JobParametersEvent jobParametersEvent = getPopulatedParametersEvent();
assertEquals(new Double(0), jobParametersEvent.getDouble("FOOBAR"));
assertEquals(new Long(0), jobParametersEvent.getLong("FOOBAR"));
assertEquals(new Double(5), jobParametersEvent.getDouble("FOOBAR", 5));
assertEquals(DOUBLE_PARAM.getValue(), jobParametersEvent.getDouble(DOUBLE_KEY, 0));
assertEquals(new Long(5), jobParametersEvent.getLong("FOOBAR", 5));
assertEquals(LONG_PARAM.getValue(), jobParametersEvent.getLong(LONG_KEY, 5));
assertEquals("TESTVAL", jobParametersEvent.getString("FOOBAR","TESTVAL"));
assertEquals(STRING_PARAM.getValue(),
jobParametersEvent.getString(STRING_KEY,"TESTVAL"));
Date date = new Date();
assertEquals(date, jobParametersEvent.getDate("FOOBAR", date));
assertEquals(DATE_PARAM.getValue(),
jobParametersEvent.getDate(DATE_KEY, date));
}
public JobParametersEvent getPopulatedParametersEvent() {
Map<String, JobParameter> jobParameters = new HashMap<>();
jobParameters.put(DATE_KEY, DATE_PARAM);
jobParameters.put(STRING_KEY,STRING_PARAM);
jobParameters.put(LONG_KEY,LONG_PARAM);
jobParameters.put(DOUBLE_KEY,DOUBLE_PARAM);
return new JobParametersEvent(jobParameters);
}
}

View File

@@ -0,0 +1,215 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* 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.
*/
package org.springframework.cloud.task.batch.listener;
import java.util.Date;
import org.junit.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.cloud.task.batch.listener.support.ExitStatus;
import org.springframework.cloud.task.batch.listener.support.StepExecutionEvent;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* @author Glenn Renfro
*/
public class StepExecutionEventTests {
private static final String JOB_NAME = "FOO_JOB";
private static final String STEP_NAME = "STEP_NAME";
private static final Long JOB_INSTANCE_ID = 1l;
private static final Long JOB_EXECUTION_ID = 2l;
private static final String JOB_CONFIGURATION_NAME = "FOO_JOB_CONFIG";
@Test
public void testBasic(){
StepExecution stepExecution = getBasicStepExecution();
stepExecution.setCommitCount(1);
stepExecution.setReadCount(2);
stepExecution.setWriteCount(3);
stepExecution.setReadSkipCount(4);
stepExecution.setWriteSkipCount(5);
StepExecutionEvent stepExecutionEvent = new StepExecutionEvent(stepExecution);
assertEquals("stepName result was not as expected", STEP_NAME, stepExecutionEvent.getStepName());
assertEquals("startTime result was not as expected", stepExecution.getStartTime(), stepExecutionEvent.getStartTime());
assertEquals("endTime result was not as expected", stepExecution.getEndTime(), stepExecutionEvent.getEndTime());
assertEquals("lastUpdated result was not as expected", stepExecution.getLastUpdated(), stepExecutionEvent.getLastUpdated());
assertEquals("commitCount result was not as expected", stepExecution.getCommitCount(), stepExecutionEvent.getCommitCount());
assertEquals("readCount result was not as expected", stepExecution.getReadCount(), stepExecutionEvent.getReadCount());
assertEquals("readSkipCount result was not as expected", stepExecution.getReadSkipCount(), stepExecutionEvent.getReadSkipCount());
assertEquals("writeCount result was not as expected", stepExecution.getWriteCount(), stepExecutionEvent.getWriteCount());
assertEquals("writeSkipCount result was not as expected", stepExecution.getWriteSkipCount(), stepExecutionEvent.getWriteSkipCount());
assertEquals("skipCount result was not as expected", stepExecution.getSkipCount(), stepExecutionEvent.getSkipCount());
}
@Test
public void testException() {
RuntimeException exception = new RuntimeException("EXPECTED EXCEPTION");
StepExecution stepExecution = getBasicStepExecution();
stepExecution.addFailureException(exception);
StepExecutionEvent stepExecutionEvent = new StepExecutionEvent(stepExecution);
assertEquals(1, stepExecutionEvent.getFailureExceptions().size());
assertEquals(exception, stepExecution.getFailureExceptions().get(0));
}
@Test
public void testGetSummary() {
StepExecution stepExecution = getBasicStepExecution();
StepExecutionEvent stepExecutionEvent = new StepExecutionEvent(stepExecution);
assertEquals("StepExecutionEvent: id=null, version=null, name=STEP_NAME, status=STARTING,"
+ " exitStatus=EXECUTING, readCount=0, filterCount=0, writeCount=0 readSkipCount=0,"
+ " writeSkipCount=0, processSkipCount=0, commitCount=0, rollbackCount=0",
stepExecutionEvent.getSummary());
}
@Test
public void testHashCode() {
StepExecution stepExecution = getBasicStepExecution();
StepExecutionEvent stepExecutionEvent =
new StepExecutionEvent(stepExecution);
assertEquals("StepExecutionEvent: id=null, version=null, "
+ "name=STEP_NAME, status=STARTING, exitStatus=EXECUTING, "
+ "readCount=0, filterCount=0, writeCount=0 readSkipCount=0, "
+ "writeSkipCount=0, processSkipCount=0, commitCount=0, "
+ "rollbackCount=0, exitDescription=", stepExecutionEvent.toString());
}
@Test
public void testToString() {
StepExecution stepExecution = getBasicStepExecution();
StepExecutionEvent stepExecutionEvent = new StepExecutionEvent(stepExecution);
assertNotNull(stepExecutionEvent.hashCode());
}
@Test
public void testEquals() {
StepExecution stepExecution = getBasicStepExecution();
stepExecution.setId(1L);
StepExecutionEvent stepExecutionEvent = new StepExecutionEvent(stepExecution);
assertFalse(stepExecutionEvent.equals(getBasicStepExecution()));
assertTrue(stepExecutionEvent.equals(stepExecution));
}
@Test
public void testSettersGetters() {
StepExecutionEvent stepExecutionEvent = new StepExecutionEvent(getBasicStepExecution());
Date date = new Date();
stepExecutionEvent.setLastUpdated(date);
assertEquals(date, stepExecutionEvent.getLastUpdated());
stepExecutionEvent.setProcessSkipCount(55);
assertEquals(55, stepExecutionEvent.getProcessSkipCount());
stepExecutionEvent.setWriteSkipCount(47);
assertEquals(47, stepExecutionEvent.getWriteSkipCount());
stepExecutionEvent.setReadSkipCount(49);
assertEquals(49, stepExecutionEvent.getReadSkipCount());
assertEquals(0, stepExecutionEvent.getCommitCount());
stepExecutionEvent.incrementCommitCount();
assertEquals(1, stepExecutionEvent.getCommitCount());
assertFalse(stepExecutionEvent.isTerminateOnly());
stepExecutionEvent.setTerminateOnly();
assertTrue(stepExecutionEvent.isTerminateOnly());
stepExecutionEvent.setStepName("FOOBAR");
assertEquals("FOOBAR", stepExecutionEvent.getStepName());
stepExecutionEvent.setStartTime(date);
assertEquals(date, stepExecutionEvent.getStartTime());
assertEquals(0, stepExecutionEvent.getRollbackCount());
stepExecutionEvent.setRollbackCount(33);
assertEquals(33, stepExecutionEvent.getRollbackCount());
stepExecutionEvent.setFilterCount(23);
assertEquals(23, stepExecutionEvent.getFilterCount());
stepExecutionEvent.setWriteCount(11);
assertEquals(11,stepExecutionEvent.getWriteCount());
stepExecutionEvent.setReadCount(12);
assertEquals(12, stepExecutionEvent.getReadCount());
stepExecutionEvent.setEndTime(date);
assertEquals(date, stepExecutionEvent.getEndTime());
stepExecutionEvent.setCommitCount(29);
assertEquals(29, stepExecutionEvent.getCommitCount());
}
@Test
public void testExitStatus() {
StepExecutionEvent stepExecutionEvent = new StepExecutionEvent(getBasicStepExecution());
final String EXIT_CODE = "1";
final String EXIT_DESCRIPTION = "EXPECTED FAILURE";
ExitStatus exitStatus = new ExitStatus();
exitStatus.setExitCode(EXIT_CODE);
exitStatus.setExitDescription(EXIT_DESCRIPTION);
stepExecutionEvent.setExitStatus(exitStatus);
ExitStatus actualExitStatus = stepExecutionEvent.getExitStatus();
assertNotNull(actualExitStatus);
assertEquals(exitStatus.getExitCode(), actualExitStatus.getExitCode());
assertEquals(exitStatus.getExitDescription(), actualExitStatus.getExitDescription());
}
@Test
public void testBatchStatus() {
StepExecutionEvent stepExecutionEvent = new StepExecutionEvent(getBasicStepExecution());
assertEquals(BatchStatus.STARTING, stepExecutionEvent.getStatus());
stepExecutionEvent.setStatus(BatchStatus.ABANDONED);
assertEquals(BatchStatus.ABANDONED, stepExecutionEvent.getStatus());
}
@Test
public void testDefaultConstructor() {
StepExecutionEvent stepExecutionEvent = new StepExecutionEvent();
assertEquals(BatchStatus.STARTING, stepExecutionEvent.getStatus());
assertNotNull(stepExecutionEvent.getExitStatus());
assertEquals("EXECUTING", stepExecutionEvent.getExitStatus().getExitCode());
}
@Test
public void testExecutionContext() {
ExecutionContext executionContext = new ExecutionContext();
executionContext.put("hello", "world");
StepExecutionEvent stepExecutionEvent = new StepExecutionEvent(getBasicStepExecution());
assertNotNull(stepExecutionEvent.getExecutionContext());
stepExecutionEvent.setExecutionContext(executionContext);
assertEquals("world", stepExecutionEvent.getExecutionContext().getString("hello"));
}
private StepExecution getBasicStepExecution() {
JobInstance jobInstance = new JobInstance(JOB_INSTANCE_ID, JOB_NAME);
JobParameters jobParameters = new JobParameters();
JobExecution jobExecution = new JobExecution(jobInstance, JOB_EXECUTION_ID, jobParameters, JOB_CONFIGURATION_NAME);
return new StepExecution(STEP_NAME, jobExecution);
}
}

View File

@@ -0,0 +1,123 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* 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.
*/
package org.springframework.cloud.task.batch.listener;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.ChunkListener;
import org.springframework.batch.core.ItemProcessListener;
import org.springframework.batch.core.ItemReadListener;
import org.springframework.batch.core.ItemWriteListener;
import org.springframework.batch.core.SkipListener;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.step.item.ChunkOrientedTasklet;
import org.springframework.batch.core.step.item.SimpleChunkProcessor;
import org.springframework.batch.core.step.item.SimpleChunkProvider;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.cloud.task.batch.listener.support.TaskBatchEventListenerBeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
/**
* @author Glenn Renfro
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class TaskBatchEventListenerBeanPostProcessorTests {
@MockBean
private TaskletStep taskletStep;
@MockBean
private SimpleChunkProvider chunkProvider;
@MockBean
private SimpleChunkProcessor chunkProcessor;
@MockBean
ItemProcessListener itemProcessListener;
@MockBean
StepExecutionListener stepExecutionListener;
@MockBean
ChunkListener chunkListener;
@MockBean
ItemReadListener itemReadListener;
@MockBean
ItemWriteListener itemWriteListener;
@MockBean
SkipListener skipListener;
@Autowired
private GenericApplicationContext context;
@Before
public void setupMock() {
when(taskletStep.getTasklet()).thenReturn(
new ChunkOrientedTasklet(chunkProvider, chunkProcessor));
when(taskletStep.getName()).thenReturn("FOOOBAR");
registerAlias(ItemProcessListener.class, BatchEventAutoConfiguration.ITEM_PROCESS_EVENTS_LISTENER);
registerAlias(StepExecutionListener.class, BatchEventAutoConfiguration.STEP_EXECUTION_EVENTS_LISTENER);
registerAlias(ChunkListener.class, BatchEventAutoConfiguration.CHUNK_EVENTS_LISTENER);
registerAlias(ItemReadListener.class, BatchEventAutoConfiguration.ITEM_READ_EVENTS_LISTENER);
registerAlias(ItemWriteListener.class, BatchEventAutoConfiguration.ITEM_WRITE_EVENTS_LISTENER);
registerAlias(SkipListener.class, BatchEventAutoConfiguration.SKIP_EVENTS_LISTENER);
}
@Test
public void testPostProcessor() {
TaskBatchEventListenerBeanPostProcessor postProcessor =
context.getBean(TaskBatchEventListenerBeanPostProcessor.class);
assertNotNull(postProcessor);
TaskletStep updatedTaskletStep = (TaskletStep)
postProcessor.postProcessBeforeInitialization(taskletStep, "FOO");
assertEquals(taskletStep, updatedTaskletStep);
}
@Configuration
@EnableAutoConfiguration
public static class TestConfiguration {
@Bean
public TaskBatchEventListenerBeanPostProcessor taskBatchEventListenerBeanPostProcessor() {
return new TaskBatchEventListenerBeanPostProcessor();
}
}
private void registerAlias(Class clazz, String name) {
assertEquals(1, context.getBeanNamesForType(clazz).length);
context.registerAlias(context.getBeanNamesForType(clazz)[0],name);
}
}

View File

@@ -1 +1,2 @@
maven.remoteRepositories.springRepo.url=https://repo.spring.io/libs-snapshot
logging.level.org.springframework.cloud.task.batch.listener=DEBUG