BATCH-1701: Implementation of JobScope
* Updated Job and Step scopes to use common class introduced by jpraet * Updated Job and Step Synchronization Managers to use common class introduced by jpraet * Updated JobScope related code to not be concerned with Spring 2.5 (which is what batch was on when the PR was opened).
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2009 the original author or authors.
|
||||
* Copyright 2013 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,10 +30,10 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
* @author Jimmy Praet
|
||||
*/
|
||||
public class AutoRegisteringJobScopeTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void testJobElement() throws Exception {
|
||||
ConfigurableApplicationContext ctx =
|
||||
ConfigurableApplicationContext ctx =
|
||||
new ClassPathXmlApplicationContext(
|
||||
"org/springframework/batch/core/configuration/xml/AutoRegisteringJobScopeForJobElementTests-context.xml");
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -43,7 +43,7 @@ public class AutoRegisteringJobScopeTests {
|
||||
|
||||
@Test
|
||||
public void testStepElement() throws Exception {
|
||||
ConfigurableApplicationContext ctx =
|
||||
ConfigurableApplicationContext ctx =
|
||||
new ClassPathXmlApplicationContext(
|
||||
"org/springframework/batch/core/configuration/xml/AutoRegisteringJobScopeForStepElementTests-context.xml");
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@@ -22,6 +22,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class JobScopeIntegrationTests {
|
||||
|
||||
private static final String PROXY_TO_STRING_REGEX = "class .*\\$Proxy\\d+";
|
||||
|
||||
@Autowired
|
||||
@Qualifier("vanilla")
|
||||
private Job vanilla;
|
||||
@@ -64,7 +66,7 @@ public class JobScopeIntegrationTests {
|
||||
assertNotNull(collaborator);
|
||||
assertEquals("bar", collaborator);
|
||||
assertTrue("Scoped proxy not created", ((String) TestJob.getContext().getAttribute("collaborator.class"))
|
||||
.startsWith("class $Proxy"));
|
||||
.matches(PROXY_TO_STRING_REGEX));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -78,7 +80,7 @@ public class JobScopeIntegrationTests {
|
||||
assertNotNull(parent);
|
||||
assertEquals("bar", parent);
|
||||
assertTrue("Scoped proxy not created", ((String) TestJob.getContext().getAttribute("parent.class"))
|
||||
.startsWith("class $Proxy"));
|
||||
.matches(PROXY_TO_STRING_REGEX));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -37,10 +37,10 @@ public class StepScopePerformanceTests implements ApplicationContextAware {
|
||||
public void start() throws Exception {
|
||||
int count = doTest("vanilla", "warmup");
|
||||
logger.info("Item count: "+count);
|
||||
StepSynchronizationManager.close();
|
||||
StepSynchronizationManager.register(new StepExecution("step", new JobExecution(0L),1L));
|
||||
}
|
||||
|
||||
@Before
|
||||
@After
|
||||
public void cleanup() {
|
||||
StepSynchronizationManager.close();
|
||||
|
||||
@@ -24,6 +24,7 @@ import static org.junit.Assert.fail;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobInstance;
|
||||
@@ -37,11 +38,20 @@ import org.springframework.batch.item.ExecutionContext;
|
||||
*/
|
||||
public class JobContextTests {
|
||||
|
||||
private List<String> list = new ArrayList<String>();
|
||||
private List<String> list;
|
||||
|
||||
private JobExecution jobExecution = new JobExecution(new JobInstance(2L, null, "job"), 1L);
|
||||
private JobExecution jobExecution;
|
||||
|
||||
private JobContext context = new JobContext(jobExecution);
|
||||
private JobContext context;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
jobExecution = new JobExecution(1l);
|
||||
JobInstance jobInstance = new JobInstance(2l, "job");
|
||||
jobExecution.setJobInstance(jobInstance);
|
||||
context = new JobContext(jobExecution);
|
||||
list = new ArrayList<String>();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobExecution() {
|
||||
@@ -79,6 +89,7 @@ public class JobContextTests {
|
||||
public void testDestructionCallbackSunnyDay() throws Exception {
|
||||
context.setAttribute("foo", "FOO");
|
||||
context.registerDestructionCallback("foo", new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
list.add("bar");
|
||||
}
|
||||
@@ -91,6 +102,7 @@ public class JobContextTests {
|
||||
@Test
|
||||
public void testDestructionCallbackMissingAttribute() throws Exception {
|
||||
context.registerDestructionCallback("foo", new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
list.add("bar");
|
||||
}
|
||||
@@ -106,12 +118,14 @@ public class JobContextTests {
|
||||
context.setAttribute("foo", "FOO");
|
||||
context.setAttribute("bar", "BAR");
|
||||
context.registerDestructionCallback("bar", new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
list.add("spam");
|
||||
throw new RuntimeException("fail!");
|
||||
}
|
||||
});
|
||||
context.registerDestructionCallback("foo", new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
list.add("bar");
|
||||
throw new RuntimeException("fail!");
|
||||
@@ -152,8 +166,10 @@ public class JobContextTests {
|
||||
@Test
|
||||
public void testJobParameters() throws Exception {
|
||||
JobParameters jobParameters = new JobParametersBuilder().addString("foo", "bar").toJobParameters();
|
||||
JobInstance jobInstance = new JobInstance(0L, jobParameters, "foo");
|
||||
JobInstance jobInstance = new JobInstance(0L, "foo");
|
||||
jobExecution = new JobExecution(5l, jobParameters);
|
||||
jobExecution.setJobInstance(jobInstance);
|
||||
context = new JobContext(jobExecution);
|
||||
assertEquals("bar", context.getJobParameters().get("foo"));
|
||||
}
|
||||
|
||||
@@ -164,9 +180,7 @@ public class JobContextTests {
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testIllegalContextId() throws Exception {
|
||||
JobParameters jobParameters = new JobParametersBuilder().addString("foo", "bar").toJobParameters();
|
||||
JobInstance jobInstance = new JobInstance(0L, jobParameters, "foo");
|
||||
context = new JobContext(new JobExecution(jobInstance));
|
||||
context = new JobContext(new JobExecution(null));
|
||||
context.getId();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,10 +4,8 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
@@ -18,11 +16,10 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* JobSynchronizationManagerTests.
|
||||
*
|
||||
*
|
||||
* @author Jimmy Praet
|
||||
*/
|
||||
public class JobSynchronizationManagerTests {
|
||||
@@ -49,6 +46,7 @@ public class JobSynchronizationManagerTests {
|
||||
final List<String> list = new ArrayList<String>();
|
||||
JobContext context = JobSynchronizationManager.register(jobExecution);
|
||||
context.registerDestructionCallback("foo", new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
list.add("foo");
|
||||
}
|
||||
@@ -56,26 +54,14 @@ public class JobSynchronizationManagerTests {
|
||||
JobSynchronizationManager.close();
|
||||
assertNull(JobSynchronizationManager.getContext());
|
||||
assertEquals(0, list.size());
|
||||
// check for possible memory leak
|
||||
assertEquals(0, extractStaticMap("counts").size());
|
||||
assertEquals(0, extractStaticMap("contexts").size());
|
||||
}
|
||||
|
||||
private Map<?, ?> extractStaticMap(String name) throws IllegalAccessException {
|
||||
Field field = ReflectionUtils.findField(JobSynchronizationManager.class, "synchronizationManager");
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
SynchronizationManagerSupport<?, ?> synchronizationManager =
|
||||
(SynchronizationManagerSupport<?, ?>) field.get(JobSynchronizationManager.class);
|
||||
field = ReflectionUtils.findField(SynchronizationManagerSupport.class, name);
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
Map<?, ?> map = (Map<?, ?>) field.get(synchronizationManager);
|
||||
return map;
|
||||
}
|
||||
@Test
|
||||
public void testMultithreaded() throws Exception {
|
||||
JobContext context = JobSynchronizationManager.register(jobExecution);
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||
FutureTask<JobContext> task = new FutureTask<JobContext>(new Callable<JobContext>() {
|
||||
@Override
|
||||
public JobContext call() throws Exception {
|
||||
try {
|
||||
JobSynchronizationManager.register(jobExecution);
|
||||
@@ -100,6 +86,7 @@ public class JobSynchronizationManagerTests {
|
||||
JobContext context = JobSynchronizationManager.register(jobExecution);
|
||||
final List<String> list = new ArrayList<String>();
|
||||
context.registerDestructionCallback("foo", new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
list.add("foo");
|
||||
}
|
||||
@@ -130,4 +117,4 @@ public class JobSynchronizationManagerTests {
|
||||
assertNull(JobSynchronizationManager.getContext());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,10 +19,8 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
@@ -35,7 +33,6 @@ import org.junit.Test;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.jsr.configuration.support.BatchPropertyContext;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
public class StepSynchronizationManagerTests {
|
||||
|
||||
@@ -80,20 +77,6 @@ public class StepSynchronizationManagerTests {
|
||||
StepSynchronizationManager.close();
|
||||
assertNull(StepSynchronizationManager.getContext());
|
||||
assertEquals(0, list.size());
|
||||
// check for possible memory leak
|
||||
assertEquals(0, extractStaticMap("counts").size());
|
||||
assertEquals(0, extractStaticMap("contexts").size());
|
||||
}
|
||||
|
||||
private Map<?, ?> extractStaticMap(String name) throws IllegalAccessException {
|
||||
Field field = ReflectionUtils.findField(StepSynchronizationManager.class, "synchronizationManager");
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
SynchronizationManagerSupport<?, ?> synchronizationManager =
|
||||
(SynchronizationManagerSupport<?, ?>) field.get(StepSynchronizationManager.class);
|
||||
field = ReflectionUtils.findField(SynchronizationManagerSupport.class, name);
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
Map<?, ?> map = (Map<?, ?>) field.get(synchronizationManager);
|
||||
return map;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package org.springframework.batch.core.scope.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.scope.context.JobContext;
|
||||
import org.springframework.batch.core.scope.context.JobSynchronizationManager;
|
||||
|
||||
public class JobContextFactoryTests {
|
||||
|
||||
private JobContextFactory factory = new JobContextFactory();
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
JobSynchronizationManager.close();
|
||||
JobSynchronizationManager.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetContext() {
|
||||
JobExecution jobExecution = new JobExecution(11L);
|
||||
JobContext context = JobSynchronizationManager.register(jobExecution);
|
||||
assertEquals(context, factory.getContext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetContextId() {
|
||||
JobSynchronizationManager.register(new JobExecution(11L));
|
||||
Object id1 = factory.getContextId();
|
||||
JobSynchronizationManager.register(new JobExecution(12L));
|
||||
Object id2 = factory.getContextId();
|
||||
assertFalse(id2.equals(id1));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user