diff --git a/execution/src/main/java/org/springframework/batch/execution/repository/dao/JobInstanceFilter.java b/execution/src/main/java/org/springframework/batch/execution/repository/dao/JobInstanceFilter.java new file mode 100644 index 000000000..1c919c7d7 --- /dev/null +++ b/execution/src/main/java/org/springframework/batch/execution/repository/dao/JobInstanceFilter.java @@ -0,0 +1,40 @@ +package org.springframework.batch.execution.repository.dao; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.springframework.batch.core.domain.JobInstance; +import org.springframework.batch.core.runtime.JobIdentifier; + +/** + * Shared utility class for {@link JobDao} implementations to allow a + * {@link JobInstance} to be identified from its {@link JobIdentifier}. + * + * @author Dave Syer + * + */ +class JobInstanceFilter { + + /** + * Filter the list and pull out a {@link JobInstance} with the supplied + * identifier. + * + * @param instances + * a collection of {@link JobInstance} + * @param identifier + * the required {@link JobIdentifier} + * @return another collection of {@link JobInstance}, all matching the the + * given {@link JobIdentifier} + */ + public List filter(List instances, JobIdentifier identifier) { + List result = new ArrayList(); + for (Iterator iterator = instances.iterator(); iterator.hasNext();) { + JobInstance job = (JobInstance) iterator.next(); + if (job.getIdentifier().equals(identifier)) { + result.add(job); + } + } + return result; + } +} diff --git a/execution/src/test/java/org/springframework/batch/execution/bootstrap/SimpleJobLauncherTests.java b/execution/src/test/java/org/springframework/batch/execution/bootstrap/SimpleJobLauncherTests.java index 0700d2b75..744c703dc 100644 --- a/execution/src/test/java/org/springframework/batch/execution/bootstrap/SimpleJobLauncherTests.java +++ b/execution/src/test/java/org/springframework/batch/execution/bootstrap/SimpleJobLauncherTests.java @@ -20,8 +20,7 @@ import junit.framework.TestCase; import org.springframework.batch.core.configuration.JobConfiguration; import org.springframework.batch.core.runtime.JobIdentifier; -import org.springframework.batch.core.runtime.JobIdentifierFactory; -import org.springframework.batch.core.runtime.SimpleJobIdentifier; +import org.springframework.batch.core.runtime.SimpleJobIdentifierFactory; import org.springframework.batch.execution.JobExecutorFacade; import org.springframework.batch.repeat.ExitStatus; import org.springframework.core.task.SimpleAsyncTaskExecutor; @@ -54,12 +53,7 @@ public class SimpleJobLauncherTests extends TestCase { public void testRunTwiceNotFatal() throws Exception { SimpleJobLauncher launcher = new SimpleJobLauncher(); - final SimpleJobIdentifier runtimeInformation = new SimpleJobIdentifier("foo"); - launcher.setJobIdentifierFactory(new JobIdentifierFactory() { - public JobIdentifier getJobIdentifier(String name) { - return runtimeInformation; - } - }); + launcher.setJobIdentifierFactory(new SimpleJobIdentifierFactory()); InterruptibleFacade jobExecutorFacade = new InterruptibleFacade(); launcher.setJobExecutorFacade(jobExecutorFacade); launcher.setJobConfigurationName(new JobConfiguration("foo").getName()); @@ -72,12 +66,7 @@ public class SimpleJobLauncherTests extends TestCase { public void testInterruptContainer() throws Exception { final SimpleJobLauncher launcher = new SimpleJobLauncher(); - final SimpleJobIdentifier runtimeInformation = new SimpleJobIdentifier("foo"); - launcher.setJobIdentifierFactory(new JobIdentifierFactory() { - public JobIdentifier getJobIdentifier(String name) { - return runtimeInformation; - } - }); + launcher.setJobIdentifierFactory(new SimpleJobIdentifierFactory()); InterruptibleFacade jobExecutorFacade = new InterruptibleFacade(); launcher.setJobExecutorFacade(jobExecutorFacade); diff --git a/execution/src/test/java/org/springframework/batch/execution/bootstrap/TaskExecutorJobLauncherTests.java b/execution/src/test/java/org/springframework/batch/execution/bootstrap/TaskExecutorJobLauncherTests.java index 821b3ee9c..901e0cbd8 100644 --- a/execution/src/test/java/org/springframework/batch/execution/bootstrap/TaskExecutorJobLauncherTests.java +++ b/execution/src/test/java/org/springframework/batch/execution/bootstrap/TaskExecutorJobLauncherTests.java @@ -28,8 +28,8 @@ import org.easymock.MockControl; import org.springframework.batch.core.configuration.JobConfiguration; import org.springframework.batch.core.configuration.NoSuchJobConfigurationException; import org.springframework.batch.core.runtime.JobIdentifier; -import org.springframework.batch.core.runtime.JobIdentifierFactory; import org.springframework.batch.core.runtime.SimpleJobIdentifier; +import org.springframework.batch.core.runtime.SimpleJobIdentifierFactory; import org.springframework.batch.execution.JobExecutorFacade; import org.springframework.batch.repeat.ExitStatus; import org.springframework.batch.repeat.interceptor.RepeatOperationsApplicationEvent; @@ -47,12 +47,8 @@ public class TaskExecutorJobLauncherTests extends TestCase { protected void setUp() throws Exception { super.setUp(); - final SimpleJobIdentifier runtimeInformation = new SimpleJobIdentifier("foo"); - launcher.setJobRuntimeInformationFactory(new JobIdentifierFactory() { - public JobIdentifier getJobIdentifier(String name) { - return runtimeInformation; - } - }); + launcher + .setJobRuntimeInformationFactory(new SimpleJobIdentifierFactory()); } public void testStopContainer() throws Exception { @@ -75,51 +71,62 @@ public class TaskExecutorJobLauncherTests extends TestCase { } public void testNormalApplicationEventNotRecognized() throws Exception { - launcher.onApplicationEvent(new ApplicationEvent("foo") {}); + launcher.onApplicationEvent(new ApplicationEvent("foo") { + }); // nothing happens } - + public void testRepeatOperationsBeforeNotUsed() throws Exception { final List list = new ArrayList(); launcher.setNotificationPublisher(new NotificationPublisher() { - public void sendNotification(Notification notification) throws UnableToSendNotificationException { + public void sendNotification(Notification notification) + throws UnableToSendNotificationException { list.add(notification); } }); - launcher.onApplicationEvent(new RepeatOperationsApplicationEvent(this, "foo", RepeatOperationsApplicationEvent.BEFORE) {}); + launcher.onApplicationEvent(new RepeatOperationsApplicationEvent(this, + "foo", RepeatOperationsApplicationEvent.BEFORE) { + }); assertEquals(0, list.size()); } public void testRepeatOperationsOpenUsed() throws Exception { final List list = new ArrayList(); launcher.setNotificationPublisher(new NotificationPublisher() { - public void sendNotification(Notification notification) throws UnableToSendNotificationException { + public void sendNotification(Notification notification) + throws UnableToSendNotificationException { list.add(notification); } }); - launcher.onApplicationEvent(new RepeatOperationsApplicationEvent(this, "foo", RepeatOperationsApplicationEvent.OPEN)); + launcher.onApplicationEvent(new RepeatOperationsApplicationEvent(this, + "foo", RepeatOperationsApplicationEvent.OPEN)); assertEquals(1, list.size()); - assertEquals("foo", ((Notification) list.get(0)).getMessage().substring(0, 3)); + assertEquals("foo", ((Notification) list.get(0)).getMessage() + .substring(0, 3)); } - + public void testStatisticsRetrieved() throws Exception { - MockControl control = MockControl.createControl(JobExecutorFacadeWithStatistics.class); - JobExecutorFacadeWithStatistics batchContainer = (JobExecutorFacadeWithStatistics) control.getMock(); + MockControl control = MockControl + .createControl(JobExecutorFacadeWithStatistics.class); + JobExecutorFacadeWithStatistics batchContainer = (JobExecutorFacadeWithStatistics) control + .getMock(); launcher.setBatchContainer(batchContainer); - + Properties properties = PropertiesConverter.stringToProperties("a=b"); control.expectAndReturn(batchContainer.getStatistics(), properties); - + control.replay(); assertEquals(properties, launcher.getStatistics()); control.verify(); } public void testStatisticsNotRetrieved() throws Exception { - MockControl control = MockControl.createControl(JobExecutorFacade.class); - JobExecutorFacade batchContainer = (JobExecutorFacade) control.getMock(); + MockControl control = MockControl + .createControl(JobExecutorFacade.class); + JobExecutorFacade batchContainer = (JobExecutorFacade) control + .getMock(); launcher.setBatchContainer(batchContainer); - + Properties properties = new Properties(); control.replay(); assertEquals(properties, launcher.getStatistics()); @@ -136,8 +143,7 @@ public class TaskExecutorJobLauncherTests extends TestCase { // started and // for interrupt to be called; Thread.sleep(300); - } - catch (InterruptedException ex) { + } catch (InterruptedException ex) { // thread intterrupted, allow to exit normally } } @@ -157,7 +163,7 @@ public class TaskExecutorJobLauncherTests extends TestCase { return false; } } - + public void testPublishApplicationEvent() throws Exception { final List list = new ArrayList(); launcher.setApplicationEventPublisher(new ApplicationEventPublisher() { @@ -166,10 +172,13 @@ public class TaskExecutorJobLauncherTests extends TestCase { } }); - MockControl control = MockControl.createControl(JobExecutorFacade.class); - JobExecutorFacade batchContainer = (JobExecutorFacade) control.getMock(); + MockControl control = MockControl + .createControl(JobExecutorFacade.class); + JobExecutorFacade batchContainer = (JobExecutorFacade) control + .getMock(); launcher.setBatchContainer(batchContainer); - SimpleJobIdentifier jobRuntimeInformation = new SimpleJobIdentifier("spam"); + SimpleJobIdentifier jobRuntimeInformation = new SimpleJobIdentifier( + "spam"); batchContainer.start(jobRuntimeInformation); control.setThrowable(new NoSuchJobConfigurationException("SPAM")); @@ -179,7 +188,8 @@ public class TaskExecutorJobLauncherTests extends TestCase { control.verify(); } - private interface JobExecutorFacadeWithStatistics extends JobExecutorFacade, StatisticsProvider { + private interface JobExecutorFacadeWithStatistics extends + JobExecutorFacade, StatisticsProvider { } }