OPEN - issue BATCH-1338: Allow segregation of jobs by type or origin
Add group aware job factory and re-org interfaces in JobLocator-JobRegistry (not backwards compatible but probably not a change for anyone outside the framework)
This commit is contained in:
@@ -9,7 +9,7 @@ import org.springframework.context.support.StaticApplicationContext;
|
||||
|
||||
public class ApplicationContextJobFactoryTests {
|
||||
|
||||
private ApplicationContextJobFactory factory = new ApplicationContextJobFactory(
|
||||
private AbstractGroupAwareJobFactory factory = new ApplicationContextJobFactory(
|
||||
new StubApplicationContextFactory(), "job");
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.configuration.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.job.JobSupport;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class GroupAwareJobFactoryTests {
|
||||
|
||||
private Job job = new JobSupport("foo");
|
||||
|
||||
@Test
|
||||
public void testCreateJob() {
|
||||
AbstractGroupAwareJobFactory factory = new AbstractGroupAwareJobFactory(job.getName()) {
|
||||
@Override
|
||||
protected Job doCreateJob(String jobName) {
|
||||
return job;
|
||||
}
|
||||
};
|
||||
assertEquals(factory.getJobName(), factory.createJob().getName());
|
||||
assertEquals(job, factory.createJob());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJobName() {
|
||||
AbstractGroupAwareJobFactory factory = new AbstractGroupAwareJobFactory("jobs", job.getName()) {
|
||||
@Override
|
||||
protected Job doCreateJob(String jobName) {
|
||||
return job;
|
||||
}
|
||||
};
|
||||
assertEquals(factory.getJobName(), factory.createJob().getName());
|
||||
assertEquals("jobs$foo", factory.getJobName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,8 +20,6 @@ import java.util.Collection;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.core.configuration.DuplicateJobException;
|
||||
import org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor;
|
||||
import org.springframework.batch.core.configuration.support.MapJobRegistry;
|
||||
import org.springframework.batch.core.job.JobSupport;
|
||||
import org.springframework.batch.core.launch.NoSuchJobException;
|
||||
import org.springframework.beans.FatalBeanException;
|
||||
@@ -39,22 +37,21 @@ public class JobRegistryBeanPostProcessorTests extends TestCase {
|
||||
try {
|
||||
processor.afterPropertiesSet();
|
||||
fail("Expected IllegalArgumentException");
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
assertTrue(e.getMessage().indexOf("JobConfigurationRegistry") >= 0);
|
||||
assertTrue(e.getMessage().contains("JobRegistry"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testBeforeInitialization() throws Exception {
|
||||
// should be a no-op
|
||||
assertEquals("foo", processor.postProcessAfterInitialization("foo",
|
||||
"bar"));
|
||||
assertEquals("foo", processor.postProcessAfterInitialization("foo", "bar"));
|
||||
}
|
||||
|
||||
public void testAfterInitializationWithWrongType() throws Exception {
|
||||
// should be a no-op
|
||||
assertEquals("foo", processor.postProcessAfterInitialization("foo",
|
||||
"bar"));
|
||||
assertEquals("foo", processor.postProcessAfterInitialization("foo", "bar"));
|
||||
}
|
||||
|
||||
public void testAfterInitializationWithCorrectType() throws Exception {
|
||||
@@ -62,9 +59,8 @@ public class JobRegistryBeanPostProcessorTests extends TestCase {
|
||||
processor.setJobRegistry(registry);
|
||||
JobSupport configuration = new JobSupport();
|
||||
configuration.setBeanName("foo");
|
||||
assertEquals(configuration, processor.postProcessAfterInitialization(
|
||||
configuration, "bar"));
|
||||
assertEquals(configuration, registry.getJob("foo"));
|
||||
assertEquals(configuration, processor.postProcessAfterInitialization(configuration, "bar"));
|
||||
assertEquals(configuration.getName(), registry.getJob("foo").getName());
|
||||
}
|
||||
|
||||
public void testAfterInitializationWithDuplicate() throws Exception {
|
||||
@@ -76,7 +72,8 @@ public class JobRegistryBeanPostProcessorTests extends TestCase {
|
||||
try {
|
||||
processor.postProcessAfterInitialization(configuration, "spam");
|
||||
fail("Expected FatalBeanException");
|
||||
} catch (FatalBeanException e) {
|
||||
}
|
||||
catch (FatalBeanException e) {
|
||||
// Expected
|
||||
assertTrue(e.getCause() instanceof DuplicateJobException);
|
||||
}
|
||||
@@ -87,22 +84,20 @@ public class JobRegistryBeanPostProcessorTests extends TestCase {
|
||||
processor.setJobRegistry(registry);
|
||||
JobSupport configuration = new JobSupport();
|
||||
configuration.setBeanName("foo");
|
||||
assertEquals(configuration, processor.postProcessAfterInitialization(
|
||||
configuration, "bar"));
|
||||
assertEquals(configuration, processor.postProcessAfterInitialization(configuration, "bar"));
|
||||
processor.destroy();
|
||||
try {
|
||||
assertEquals(null, registry.getJob("foo"));
|
||||
fail("Expected NoSuchJobConfigurationException");
|
||||
} catch (NoSuchJobException e) {
|
||||
}
|
||||
catch (NoSuchJobException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testExecutionWithApplicationContext() throws Exception {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"test-context.xml", getClass());
|
||||
MapJobRegistry registry = (MapJobRegistry) context
|
||||
.getBean("registry");
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("test-context.xml", getClass());
|
||||
MapJobRegistry registry = (MapJobRegistry) context.getBean("registry");
|
||||
Collection<String> configurations = registry.getJobNames();
|
||||
// System.err.println(configurations);
|
||||
String[] names = context.getBeanNamesForType(JobSupport.class);
|
||||
@@ -114,20 +109,12 @@ public class JobRegistryBeanPostProcessorTests extends TestCase {
|
||||
// child beans will have the same name and will be re-registered (and
|
||||
// override, if the registry supports that).
|
||||
assertNotNull(registry.getJob("test-job"));
|
||||
assertEquals(context.getBean("test-job-with-name"), registry
|
||||
.getJob("foo"));
|
||||
assertEquals(context.getBean("test-job-with-bean-name"), registry
|
||||
.getJob("bar"));
|
||||
assertEquals(context.getBean("test-job-with-parent-and-name"), registry
|
||||
.getJob("spam"));
|
||||
assertEquals(context.getBean("test-job-with-parent-and-bean-name"),
|
||||
registry.getJob("bucket"));
|
||||
assertEquals(context.getBean("test-job-with-concrete-parent"), registry
|
||||
.getJob("maps"));
|
||||
assertEquals(context.getBean("test-job-with-concrete-parent-and-name"),
|
||||
registry.getJob("oof"));
|
||||
assertEquals(context
|
||||
.getBean("test-job-with-concrete-parent-and-bean-name"),
|
||||
registry.getJob("rab"));
|
||||
assertEquals(context.getBean("test-job-with-name"), registry.getJob("foo"));
|
||||
assertEquals(context.getBean("test-job-with-bean-name"), registry.getJob("bar"));
|
||||
assertEquals(context.getBean("test-job-with-parent-and-name"), registry.getJob("spam"));
|
||||
assertEquals(context.getBean("test-job-with-parent-and-bean-name"), registry.getJob("bucket"));
|
||||
assertEquals(context.getBean("test-job-with-concrete-parent"), registry.getJob("maps"));
|
||||
assertEquals(context.getBean("test-job-with-concrete-parent-and-name"), registry.getJob("oof"));
|
||||
assertEquals(context.getBean("test-job-with-concrete-parent-and-bean-name"), registry.getJob("rab"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.configuration.ListableJobRegistry;
|
||||
import org.springframework.batch.core.configuration.ListableJobLocator;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -38,7 +38,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
public class JobRegistryJobParserTests implements ApplicationContextAware {
|
||||
|
||||
@Autowired
|
||||
private ListableJobRegistry jobRegistry;
|
||||
private ListableJobLocator jobRegistry;
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user