From 152829ba5ff0101352f7fd6b3d4e2060c0d80e67 Mon Sep 17 00:00:00 2001 From: dsyer Date: Wed, 30 Jan 2008 08:02:29 +0000 Subject: [PATCH] Send test coverage in core up to the high 90s. --- .../batch/core/domain/Entity.java | 2 +- .../batch/core/domain/JobExecution.java | 34 ++- .../batch/core/domain/JobParameters.java | 1 - .../batch/core/domain/JobSupport.java | 2 +- .../batch/core/domain/StepExecution.java | 2 +- .../batch/core/domain/EntityTests.java | 9 + .../batch/core/domain/JobExecutionTests.java | 29 +-- .../batch/core/domain/JobInstanceTests.java | 6 + .../batch/core/domain/JobParametersTests.java | 197 +++++++++++------- .../batch/core/domain/JobSupportTests.java | 15 ++ .../core/domain/StepContributionTests.java | 66 ++++++ .../batch/core/domain/StepExecutionTests.java | 52 +++++ .../batch/core/domain/StepSupportTests.java | 27 +++ .../launch/NoSuchJobExecutionException.java | 32 --- .../execution/launch/SimpleJobLauncher.java | 7 +- .../launch/SimpleJobLauncherTests.java | 95 +++++++-- .../batch/support/PropertiesConverter.java | 11 +- .../support/PropertiesConverterTests.java | 44 +++- 18 files changed, 472 insertions(+), 159 deletions(-) create mode 100644 spring-batch-core/src/test/java/org/springframework/batch/core/domain/StepContributionTests.java delete mode 100644 spring-batch-execution/src/main/java/org/springframework/batch/execution/launch/NoSuchJobExecutionException.java diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/Entity.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/Entity.java index 598b40ad1..241a9651a 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/Entity.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/Entity.java @@ -98,7 +98,7 @@ public class Entity implements Serializable { } Entity entity = (Entity) other; if (id == null || entity.getId() == null) { - return entity == this; + return false; } return id.equals(entity.getId()); } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobExecution.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobExecution.java index 78e9e2147..3b7c25f4e 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobExecution.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobExecution.java @@ -51,14 +51,13 @@ public class JobExecution extends Entity { * Because a JobExecution isn't valid unless the job is set, this * constructor is the only valid one from a modelling point of view. * - * @param job - * the job of which this execution is a part + * @param job the job of which this execution is a part */ public JobExecution(JobInstance job, Long id) { super(id); this.jobInstance = job; } - + /** * Constructor for transient (unsaved) instances. * @@ -145,34 +144,33 @@ public class JobExecution extends Entity { this.stepExecutions.add(stepExecution); return stepExecution; } - - /* (non-Javadoc) + + /* + * (non-Javadoc) * @see org.springframework.batch.core.domain.Entity#toString() */ public String toString() { - return super.toString()+", startTime="+startTime+", endTime="+endTime+", job=["+jobInstance+"]"; + return super.toString() + ", startTime=" + startTime + ", endTime=" + endTime + ", job=[" + jobInstance + "]"; } /** - * Test if this {@link JobExecution} indicates that it is running. It should - * be noted that this does not necessarily mean that it has been - * persisted as such yet. + * Test if this {@link JobExecution} indicates that it is running. It should + * be noted that this does not necessarily mean that it has been persisted + * as such yet. * @return true if the end time is null */ public boolean isRunning() { - return endTime==null; + return endTime == null; } - + /** - * Stop the JobExecution, each StepExecution will be iterated through, calling - * setTermianteOnly, signaling to the StepExecutor currently running that it should - * be terminated. + * Signal the {@link JobExecution} to stop. Iterates through the associated + * {@link StepExecution}s, calling {@link StepExecution#setTerminateOnly()}. * */ - public void stop(){ - - for(Iterator it = stepExecutions.iterator();it.hasNext();){ - StepExecution stepExecution = (StepExecution)it.next(); + public void stop() { + for (Iterator it = stepExecutions.iterator(); it.hasNext();) { + StepExecution stepExecution = (StepExecution) it.next(); stepExecution.setTerminateOnly(); } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobParameters.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobParameters.java index 914ab4dbc..2c4d515a0 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobParameters.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobParameters.java @@ -196,7 +196,6 @@ public class JobParameters { } public int hashCode() { - return new HashCodeBuilder(7, 21). append(stringMap). append(longMap). diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobSupport.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobSupport.java index ef183782c..db479b1f7 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobSupport.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobSupport.java @@ -139,6 +139,6 @@ public class JobSupport implements BeanNameAware, Job { } public String toString() { - return ClassUtils.getShortName(JobSupport.class) + ": [name=" + name + "]"; + return ClassUtils.getShortName(getClass()) + ": [name=" + name + "]"; } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepExecution.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepExecution.java index 4a333507b..ddc0cd5b7 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepExecution.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepExecution.java @@ -169,7 +169,7 @@ public class StepExecution extends Entity { public boolean equals(Object obj) { Object stepId = getStepId(); Object jobExecutionId = getJobExecutionId(); - if (stepId == null && jobExecutionId == null || !(obj instanceof StepExecution) || getId() != null) { + if (stepId == null && jobExecutionId == null || !(obj instanceof StepExecution) || getId() == null) { return super.equals(obj); } StepExecution other = (StepExecution) obj; diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/EntityTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/EntityTests.java index f4c85cace..2ba5dbd2d 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/EntityTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/EntityTests.java @@ -57,6 +57,15 @@ public class EntityTests extends TestCase { assertEquals(new Integer(0), entity.getVersion()); } + /** + * Test method for {@link org.springframework.batch.core.domain.Entity#getVersion()}. + */ + public void testIncrementVersionTwice() { + entity.incrementVersion(); + entity.incrementVersion(); + assertEquals(new Integer(1), entity.getVersion()); + } + /** * @throws Exception */ diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobExecutionTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobExecutionTests.java index 543ec82ce..4aee15cf5 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobExecutionTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobExecutionTests.java @@ -27,9 +27,7 @@ import org.springframework.batch.repeat.ExitStatus; */ public class JobExecutionTests extends TestCase { - private JobExecution execution = new JobExecution(new JobInstance(new Long(11), new JobParameters()), new Long(12)); - - private JobExecution context = new JobExecution(new JobInstance(new Long(11), new JobParameters(), new JobSupport("foo")), new Long(12)); + private JobExecution execution = new JobExecution(new JobInstance(new Long(11), new JobParameters(), new JobSupport("foo")), new Long(12)); /** * Test method for @@ -117,23 +115,30 @@ public class JobExecutionTests extends TestCase { } public void testContextContainsInfo() throws Exception { - assertEquals("foo", context.getJobInstance().getJobName()); + assertEquals("foo", execution.getJobInstance().getJobName()); } public void testAddAndRemoveStepExecution() throws Exception { - assertEquals(0, context.getStepExecutions().size()); - context.createStepExecution(new StepInstance(null, null)); - assertEquals(1, context.getStepExecutions().size()); + assertEquals(0, execution.getStepExecutions().size()); + execution.createStepExecution(new StepInstance(null, null)); + assertEquals(1, execution.getStepExecutions().size()); + } + + public void testStop() throws Exception { + StepExecution stepExecution = execution.createStepExecution(null); + assertFalse(stepExecution.isTerminateOnly()); + execution.stop(); + assertTrue(stepExecution.isTerminateOnly()); } public void testToString() throws Exception { - assertTrue("JobExecution string does not contain id", context.toString().indexOf("id=") >= 0); - assertTrue("JobExecution string does not contain name: " + context, context.toString().indexOf("foo") >= 0); + assertTrue("JobExecution string does not contain id", execution.toString().indexOf("id=") >= 0); + assertTrue("JobExecution string does not contain name: " + execution, execution.toString().indexOf("foo") >= 0); } public void testToStringWithNullJob() throws Exception { - context = new JobExecution(); - assertTrue("JobExecution string does not contain id", context.toString().indexOf("id=") >= 0); - assertTrue("JobExecution string does not contain job: " + context, context.toString().indexOf("job=") >= 0); + execution = new JobExecution(); + assertTrue("JobExecution string does not contain id", execution.toString().indexOf("id=") >= 0); + assertTrue("JobExecution string does not contain job: " + execution, execution.toString().indexOf("job=") >= 0); } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobInstanceTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobInstanceTests.java index 0f9b48e1e..9d23833b4 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobInstanceTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobInstanceTests.java @@ -72,6 +72,12 @@ public class JobInstanceTests extends TestCase { public void testGetJob(){ assertEquals("job", instance.getJob().getName()); + instance.setJob(null); + assertEquals(null, instance.getJob()); + } + + public void testCreateJobExecution(){ + assertNotNull(instance.createJobExecution()); } public void testCreateWithNulls(){ diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobParametersTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobParametersTests.java index 588ff7908..ca8fa62d3 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobParametersTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobParametersTests.java @@ -13,151 +13,202 @@ import junit.framework.TestCase; /** * @author Lucas Ward - * + * */ public class JobParametersTests extends TestCase { JobParameters parameters; - + Map stringMap; - + Map longMap; - + Map dateMap; + Date date1 = new Date(4321431242L); + Date date2 = new Date(7809089900L); - + protected void setUp() throws Exception { super.setUp(); - parameters = getNewParameters(); } - - private JobParameters getNewParameters(){ - + + private JobParameters getNewParameters() { + stringMap = new HashMap(); stringMap.put("string.key1", "value1"); stringMap.put("string.key2", "value2"); - + longMap = new HashMap(); longMap.put("long.key1", new Long(1)); longMap.put("long.key2", new Long(2)); - + dateMap = new HashMap(); - dateMap.put("date.key1", date1 ); - dateMap.put("date.key2", date2 ); - + dateMap.put("date.key1", date1); + dateMap.put("date.key2", date2); + return new JobParameters(stringMap, longMap, dateMap); } - - public void testBadLongConstructorException() throws Exception{ - + + public void testBadLongKeyException() throws Exception { + Map badLongMap = new HashMap(); - badLongMap.put("key", "bad long"); - - try{ + badLongMap.put(new Long(0), new Long(1)); + + try { new JobParameters(stringMap, badLongMap, dateMap); fail(); } - catch(IllegalArgumentException ex){ - //expected + catch (IllegalArgumentException ex) { + // expected } } - - public void testBadStringConstructorException() throws Exception{ - + + public void testBadLongConstructorException() throws Exception { + + Map badLongMap = new HashMap(); + badLongMap.put("key", "bad long"); + + try { + new JobParameters(stringMap, badLongMap, dateMap); + fail(); + } + catch (IllegalArgumentException ex) { + // expected + } + } + + public void testBadStringConstructorException() throws Exception { + Map badMap = new HashMap(); badMap.put("key", new Integer(2)); - - try{ + + try { new JobParameters(badMap, longMap, dateMap); fail(); } - catch(IllegalArgumentException ex){ - //expected + catch (IllegalArgumentException ex) { + // expected } } - - public void testBadDateConstructorException() throws Exception{ - + + public void testBadDateConstructorException() throws Exception { + Map badMap = new HashMap(); badMap.put("key", new java.sql.Date(System.currentTimeMillis())); - - try{ + + try { new JobParameters(stringMap, longMap, badMap); fail(); } - catch(IllegalArgumentException ex){ - //expected + catch (IllegalArgumentException ex) { + // expected } } - - public void testGetString(){ - + + public void testGetString() { assertEquals("value1", parameters.getString("string.key1")); assertEquals("value2", parameters.getString("string.key2")); } - - public void testGetLong(){ - + + public void testGetStringParameters() { + assertEquals("value1", parameters.getStringParameters().get("string.key1")); + assertEquals("value2", parameters.getStringParameters().get("string.key2")); + } + + public void testGetLong() { assertEquals(new Long(1), parameters.getLong("long.key1")); assertEquals(new Long(2), parameters.getLong("long.key2")); } - - public void testGetDate(){ - + + public void testGetLongParameters() { + assertEquals(new Long(1), parameters.getLongParameters().get("long.key1")); + assertEquals(new Long(2), parameters.getLongParameters().get("long.key2")); + } + + public void testGetDate() { assertEquals(date1, parameters.getDate("date.key1")); assertEquals(date2, parameters.getDate("date.key2")); } - - public void testEquals(){ - - JobParameters testParameters = getNewParameters(); + + public void testGetDateParameters() { + assertEquals(date1, parameters.getDateParameters().get("date.key1")); + assertEquals(date2, parameters.getDateParameters().get("date.key2")); + } + + public void testIsEmptyWhenEmpty() throws Exception { + assertTrue(new JobParameters().isEmpty()); + } + + public void testIsEmptyWhenNotEmpty() throws Exception { + assertFalse(parameters.isEmpty()); + } + + public void testEquals() { + JobParameters testParameters = getNewParameters(); assertTrue(testParameters.equals(parameters)); } - - public void testToStringOrder(){ - + + public void testEqualsSelf() { + assertTrue(parameters.equals(parameters)); + } + + public void testEqualsDifferent() { + assertFalse(parameters.equals(new JobParameters())); + } + + public void testEqualsWrongType() { + assertFalse(parameters.equals("foo")); + } + + public void testEqualsNull() { + assertFalse(parameters.equals(null)); + } + + public void testToStringOrder() { + Map props = parameters.getParameters(); StringBuilder stringBuilder = new StringBuilder(); - for(Iterator it = props.entrySet().iterator();it.hasNext();){ - Entry entry = (Entry)it.next(); + for (Iterator it = props.entrySet().iterator(); it.hasNext();) { + Entry entry = (Entry) it.next(); stringBuilder.append(entry.toString() + ";"); } - + String string1 = stringBuilder.toString(); - + stringMap = new HashMap(); stringMap.put("string.key2", "value2"); stringMap.put("string.key1", "value1"); - + longMap = new HashMap(); longMap.put("long.key2", new Long(2)); longMap.put("long.key1", new Long(1)); - - + dateMap = new HashMap(); - dateMap.put("date.key2", date2 ); - dateMap.put("date.key1", date1 ); - + dateMap.put("date.key2", date2); + dateMap.put("date.key1", date1); + JobParameters testProps = new JobParameters(stringMap, longMap, dateMap); - + props = testProps.getParameters(); stringBuilder = new StringBuilder(); - for(Iterator it = props.entrySet().iterator();it.hasNext();){ - Entry entry = (Entry)it.next(); + for (Iterator it = props.entrySet().iterator(); it.hasNext();) { + Entry entry = (Entry) it.next(); stringBuilder.append(entry.toString() + ";"); } String string2 = stringBuilder.toString(); - + assertEquals(string1, string2); } -// Not sure how to properly test this since there is no order garuntee, commenting out for now. -// -// public void testToString(){ -// -// assertEquals("{string.key1=value1, string.key2=value2}{long.key1=1, long.key2=2}" + -// "{date.key2=Wed Apr 01 03:11:29 CST 1970, date.key1=Thu Feb 19 18:23:51 CST 1970}", parameters.toString()); -// } + public void testHashCodeEqualWhenEmpty() throws Exception { + int code = new JobParameters().hashCode(); + assertEquals(code, new JobParameters().hashCode()); + } + + public void testHashCodeEqualWhenNotEmpty() throws Exception { + int code = getNewParameters().hashCode(); + assertEquals(code, parameters.hashCode()); + } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobSupportTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobSupportTests.java index d0c42b326..07c2b57d4 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobSupportTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobSupportTests.java @@ -96,5 +96,20 @@ public class JobSupportTests extends TestCase { job.setRestartable(true); assertTrue(job.isRestartable()); } + + public void testToString() throws Exception { + String value = job.toString(); + assertTrue("Should contain name: "+value, value.indexOf("name=")>=0); + } + + public void testRunNotSupported() throws Exception { + try { + job.run(null); + } catch (UnsupportedOperationException e) { + // expected + String message = e.getMessage(); + assertTrue("Message should contain JobSupport: "+message, message.contains("JobSupport")); + } + } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/StepContributionTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/StepContributionTests.java new file mode 100644 index 000000000..2c7c0ce57 --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/StepContributionTests.java @@ -0,0 +1,66 @@ +/* + * Copyright 2006-2007 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.batch.core.domain; + +import junit.framework.TestCase; + +import org.springframework.batch.support.PropertiesConverter; + +/** + * @author Dave Syer + * + */ +public class StepContributionTests extends TestCase { + + private StepExecution execution = new StepExecution(); + private StepContribution contribution = new StepContribution(execution); + /** + * Test method for {@link org.springframework.batch.core.domain.StepContribution#incrementTaskCount()}. + */ + public void testIncrementTaskCount() { + assertEquals(0, contribution.getTaskCount()); + contribution.incrementTaskCount(); + assertEquals(1, contribution.getTaskCount()); + } + + /** + * Test method for {@link org.springframework.batch.core.domain.StepContribution#setStatistics(java.util.Properties)}. + */ + public void testSetStatistics() { + assertEquals(null, contribution.getStatistics()); + contribution.setStatistics(PropertiesConverter.stringToProperties("foo=bar")); + assertEquals(1, contribution.getStatistics().size()); + } + + /** + * Test method for {@link org.springframework.batch.core.domain.StepContribution#incrementCommitCount()}. + */ + public void testIncrementCommitCount() { + assertEquals(0, contribution.getCommitCount()); + contribution.incrementCommitCount(); + assertEquals(1, contribution.getCommitCount()); + } + + /** + * Test method for {@link org.springframework.batch.core.domain.StepContribution#isTerminateOnly()}. + */ + public void testIsTerminateOnly() { + assertFalse(contribution.isTerminateOnly()); + execution.setTerminateOnly(); + assertTrue(contribution.isTerminateOnly()); + } + +} diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/StepExecutionTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/StepExecutionTests.java index 1168359bc..436fd3126 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/StepExecutionTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/StepExecutionTests.java @@ -39,6 +39,14 @@ public class StepExecutionTests extends TestCase { assertNull(new StepExecution().getId()); } + /** + * Test method for + * {@link org.springframework.batch.core.domain.JobExecution#JobExecution()}. + */ + public void testStepExecutionWithNullId() { + assertNull(new StepExecution(null, null).getId()); + } + /** * Test method for * {@link org.springframework.batch.core.domain.JobExecution#getEndTime()}. @@ -154,6 +162,32 @@ public class StepExecutionTests extends TestCase { public void testGetStepId() { assertEquals(11, execution.getStepId().longValue()); } + + public void testGetStep() throws Exception { + assertNotNull(execution.getStep()); + } + + public void testGetJobExecution() throws Exception { + assertNotNull(execution.getJobExecution()); + } + + public void testApplyContribution() throws Exception { + StepContribution contribution = execution.createStepContribution(); + contribution.incrementCommitCount(); + execution.apply(contribution); + assertEquals(new Integer(1), execution.getCommitCount()); + } + + public void testTerminateOnly() throws Exception { + assertFalse(execution.isTerminateOnly()); + execution.setTerminateOnly(); + assertTrue(execution.isTerminateOnly()); + } + + public void testToStringWithNullName() throws Exception { + String value = new StepExecution().toString(); + assertTrue("Should contain name=null: "+value, value.indexOf("name=null")>=0); + } public void testToString() throws Exception { assertTrue("Should contain task count: " + execution.toString(), @@ -200,6 +234,23 @@ public class StepExecutionTests extends TestCase { assertFalse(step.equals(new StepExecution())); } + public void testEqualsWithSelf() throws Exception { + assertTrue(execution.equals(execution)); + } + + public void testEqualsWithDifferent() throws Exception { + StepExecution step = newStepExecution(new Long(43), new Long(13)); + assertFalse(execution.equals(step)); + } + + public void testEqualsWithNullStepId() throws Exception { + execution = newStepExecution(null, new Long(31)); + assertEquals(null, execution.getStepId()); + StepExecution step = newStepExecution(null, new Long(31)); + assertEquals(step.getJobExecutionId(), execution.getJobExecutionId()); + assertTrue(execution.equals(step)); + } + public void testHashCode() throws Exception { assertTrue("Hash code same as parent", new Entity(execution.getId()) .hashCode() != execution.hashCode()); @@ -217,4 +268,5 @@ public class StepExecutionTests extends TestCase { StepExecution execution = new StepExecution(step, new JobExecution(job, long2), new Long(4)); return execution; } + } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/StepSupportTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/StepSupportTests.java index 1ab5815b6..993253ad2 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/StepSupportTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/StepSupportTests.java @@ -40,6 +40,33 @@ public class StepSupportTests extends TestCase { */ public void testGetName() { assertEquals("step", configuration.getName()); + configuration.setName("bar"); + assertEquals("bar", configuration.getName()); + } + + /** + * Test method for {@link org.springframework.batch.core.domain.StepSupport#getName()}. + */ + public void testBeanNameAlreadySet() { + assertEquals("step", configuration.getName()); + configuration.setBeanName("bar"); + assertEquals("step", configuration.getName()); + } + + /** + * Test method for {@link org.springframework.batch.core.domain.StepSupport#getName()}. + */ + public void testBeanNameOnNew() { + configuration = new StepSupport(); + assertEquals(null, configuration.getName()); + configuration.setBeanName("bar"); + assertEquals("bar", configuration.getName()); + } + + public void testSaveRestartFlag() throws Exception { + assertEquals(false, configuration.isSaveRestartData()); + configuration.setSaveRestartData(true); + assertEquals(true, configuration.isSaveRestartData()); } /** diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/launch/NoSuchJobExecutionException.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/launch/NoSuchJobExecutionException.java deleted file mode 100644 index aaef66f50..000000000 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/launch/NoSuchJobExecutionException.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2006-2007 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.batch.execution.launch; - -/** - * @author Dave Syer - * - */ -public class NoSuchJobExecutionException extends Exception { - - /** - * @param message - */ - public NoSuchJobExecutionException(String message) { - super(message); - } - - -} diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/launch/SimpleJobLauncher.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/launch/SimpleJobLauncher.java index 3fec38a78..87f4622b5 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/launch/SimpleJobLauncher.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/launch/SimpleJobLauncher.java @@ -84,8 +84,8 @@ public class SimpleJobLauncher implements JobLauncher, InitializingBean { try { logger.info("Job: [" + job + "] launched with the following parameters: [" + jobParameters + "]"); ExitStatus exitStatus = job.run(jobExecution); - // shouldn't need to set the exit status like this, I'm - // leaving it to make the latest change easier + // The exit status should be set by the Job. TODO: remove + // this line... jobExecution.setExitStatus(exitStatus); logger.info("Job: [" + job + "] completed successfully with the following parameters: [" + jobParameters + "]"); @@ -126,7 +126,8 @@ public class SimpleJobLauncher implements JobLauncher, InitializingBean { } /** - * Ensure the required dependencies of a {@link JobRepository} have been set. + * Ensure the required dependencies of a {@link JobRepository} have been + * set. */ public void afterPropertiesSet() throws Exception { Assert.state(jobRepository != null, "A JobRepository has not been set."); diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/launch/SimpleJobLauncherTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/launch/SimpleJobLauncherTests.java index 8d5c3a07d..10c178e8e 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/launch/SimpleJobLauncherTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/launch/SimpleJobLauncherTests.java @@ -16,6 +16,9 @@ package org.springframework.batch.execution.launch; +import java.util.ArrayList; +import java.util.List; + import junit.framework.TestCase; import org.easymock.MockControl; @@ -24,50 +27,112 @@ import org.springframework.batch.core.domain.JobExecution; import org.springframework.batch.core.domain.JobParameters; import org.springframework.batch.core.domain.JobSupport; import org.springframework.batch.core.repository.JobRepository; -import org.springframework.batch.io.exception.BatchCriticalException; import org.springframework.batch.repeat.ExitStatus; +import org.springframework.core.task.TaskExecutor; /** * @author Lucas Ward - * + * */ public class SimpleJobLauncherTests extends TestCase { private SimpleJobLauncher jobLauncher; - + private MockControl repositoryControl = MockControl.createControl(JobRepository.class); - + private Job job = new JobSupport("foo") { - public ExitStatus run(JobExecution execution) throws BatchCriticalException { + public ExitStatus run(JobExecution execution) { return ExitStatus.FINISHED; } }; + private JobParameters jobParameters = new JobParameters(); private JobRepository jobRepository; - + protected void setUp() throws Exception { super.setUp(); - - jobLauncher = new SimpleJobLauncher(); + + jobLauncher = new SimpleJobLauncher(); jobRepository = (JobRepository) repositoryControl.getMock(); jobLauncher.setJobRepository(jobRepository); - + } + public void testRun() throws Exception { - public void testRun() throws Exception{ - JobExecution jobExecution = new JobExecution(null); - + jobRepository.createJobExecution(job, jobParameters); repositoryControl.setReturnValue(jobExecution); - + repositoryControl.replay(); - + jobLauncher.run(job, jobParameters); assertEquals(ExitStatus.FINISHED, jobExecution.getExitStatus()); - + repositoryControl.verify(); } + + public void testTaskExecutor() throws Exception { + final List list = new ArrayList(); + jobLauncher.setTaskExecutor(new TaskExecutor() { + public void execute(Runnable task) { + list.add("execute"); + task.run(); + } + }); + testRun(); + assertEquals(1, list.size()); + } + + public void testRunWithException() throws Exception { + job = new JobSupport() { + public ExitStatus run(JobExecution execution) { + execution.setExitStatus(ExitStatus.FAILED); + throw new RuntimeException("foo"); + } + }; + try { + testRun(); + fail("Expected RuntimeException"); + } + catch (RuntimeException e) { + assertEquals("foo", e.getMessage()); + } + } + + public void testRunWithError() throws Exception { + job = new JobSupport() { + public ExitStatus run(JobExecution execution) { + execution.setExitStatus(ExitStatus.FAILED); + throw new Error("foo"); + } + }; + try { + testRun(); + fail("Expected Error"); + } + catch (RuntimeException e) { + assertEquals("foo", e.getCause().getMessage()); + } + } + + public void testInitialiseWithoutRepository() throws Exception { + try { + new SimpleJobLauncher().afterPropertiesSet(); + fail("Expected IllegalArgumentException"); + } + catch (IllegalStateException e) { + // expected + assertTrue("Message did not contain repository: " + e.getMessage(), e.getMessage().toLowerCase().contains( + "repository")); + } + } + + public void testInitialiseWithRepository() throws Exception { + jobLauncher = new SimpleJobLauncher(); + jobLauncher.setJobRepository(jobRepository); + jobLauncher.afterPropertiesSet(); // no error + } } \ No newline at end of file diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/PropertiesConverter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/PropertiesConverter.java index bd8e135fc..63173e917 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/PropertiesConverter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/PropertiesConverter.java @@ -23,6 +23,7 @@ import java.util.Properties; import org.springframework.util.DefaultPropertiesPersister; import org.springframework.util.PropertiesPersister; +import org.springframework.util.StringUtils; /** * Utility to convert a Properties object to a String and back. Ideally this @@ -48,7 +49,10 @@ public final class PropertiesConverter { /** * Parse a String to a Properties object. If string is null, an empty - * Properties object will be returned. + * Properties object will be returned. The input String is a set of + * name=value pairs, delimited by either newline or comma (for brevity). If + * the input String contains a newline it is assumed that the separator is + * newline, otherwise comma. * * @param stringToParse String to parse. * @return Properties parsed from each string. @@ -61,6 +65,11 @@ public final class PropertiesConverter { return new Properties(); } + if (!stringToParse.contains("\n")) { + return StringUtils.splitArrayElementsIntoProperties(StringUtils + .commaDelimitedListToStringArray(stringToParse), "="); + } + StringReader stringReader = new StringReader(stringToParse); Properties properties = new Properties(); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/PropertiesConverterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/PropertiesConverterTests.java index c38e4b48d..c388ff0d1 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/PropertiesConverterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/PropertiesConverterTests.java @@ -36,7 +36,7 @@ public class PropertiesConverterTests extends TestCase { /** * Check that Properties can be converted to String and back correctly. */ - public void testRegularConversion() { + public void testTwoWayRegularConversion() { Properties storedProps = new Properties(); storedProps.setProperty("key1", "value1"); @@ -47,6 +47,48 @@ public class PropertiesConverterTests extends TestCase { assertEquals(storedProps, props); } + /** + * Check that Properties can be comma delimited. + */ + public void testRegularConversionWithComma() { + + Properties storedProps = new Properties(); + storedProps.setProperty("key1", "value1"); + storedProps.setProperty("key2", "value2"); + + props = PropertiesConverter.stringToProperties("key1=value1,key2=value2"); + + assertEquals(storedProps, props); + } + + /** + * Check that Properties can be comma delimited with extra whitespace. + */ + public void testRegularConversionWithCommaAndWhitespace() { + + Properties storedProps = new Properties(); + storedProps.setProperty("key1", "value1"); + storedProps.setProperty("key2", "value2"); + + props = PropertiesConverter.stringToProperties("key1=value1, key2=value2"); + + assertEquals(storedProps, props); + } + + /** + * Check that Properties can be newline delimited. + */ + public void testRegularConversionWithCommaAndNewline() { + + Properties storedProps = new Properties(); + storedProps.setProperty("key1", "value1"); + storedProps.setProperty("key2", "value2"); + + props = PropertiesConverter.stringToProperties("key1=value1\n key2=value2"); + + assertEquals(storedProps, props); + } + /** * Converting a String to Properties and back does not return equal String! * See {@link PropertiesConverter} javadoc for more details.