BATCH-1474: Added reload() feature to JobLoader.

This commit is contained in:
dsyer
2010-01-03 10:38:15 +00:00
parent 05821ca7d3
commit a08069a9c6
9 changed files with 266 additions and 28 deletions

View File

@@ -24,6 +24,7 @@ import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;
/**
@@ -36,30 +37,36 @@ public class ClassPathXmlApplicationContextFactoryTests {
@Test
public void testCreateJob() {
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "trivial-context.xml")));
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(),
"trivial-context.xml")));
assertNotNull(factory.createApplicationContext());
}
@Test
public void testGetJobName() {
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "trivial-context.xml")));
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(),
"trivial-context.xml")));
assertEquals("test-job", factory.createApplicationContext().getBeanNamesForType(Job.class)[0]);
}
@Test
public void testParentConfigurationInherited() {
factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(getClass(), "parent-context.xml")));
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml")));
factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(
getClass(), "parent-context.xml")));
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(),
"child-context.xml")));
ConfigurableApplicationContext context = factory.createApplicationContext();
assertEquals("test-job", context.getBeanNamesForType(Job.class)[0]);
assertEquals("bar", ((Job) context.getBean("test-job", Job.class)).getName());
assertEquals(4, ((Foo) context.getBean("foo", Foo.class)).values[1], 0.01);
}
@Test
public void testBeanFactoryPostProcessorsNotCopied() {
factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(getClass(), "parent-context.xml")));
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml")));
factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(
getClass(), "parent-context.xml")));
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(),
"child-context.xml")));
@SuppressWarnings("unchecked")
Class<? extends BeanFactoryPostProcessor>[] classes = (Class<? extends BeanFactoryPostProcessor>[]) new Class<?>[0];
factory.setBeanFactoryPostProcessorClasses(classes);
@@ -68,21 +75,36 @@ public class ClassPathXmlApplicationContextFactoryTests {
assertEquals("${foo}", ((Job) context.getBean("test-job", Job.class)).getName());
assertEquals(4, ((Foo) context.getBean("foo", Foo.class)).values[1], 0.01);
}
@Test
public void testBeanFactoryConfigurationNotCopied() {
factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(getClass(), "parent-context.xml")));
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml")));
factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(
getClass(), "parent-context.xml")));
factory.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(),
"child-context.xml")));
factory.setCopyConfiguration(false);
ConfigurableApplicationContext context = factory.createApplicationContext();
assertEquals("test-job", context.getBeanNamesForType(Job.class)[0]);
assertEquals("bar", ((Job) context.getBean("test-job", Job.class)).getName());
// The CustomEditorConfigurer is a BeanFactoryPostProcessor so the editor gets copied anyway!
// The CustomEditorConfigurer is a BeanFactoryPostProcessor so the
// editor gets copied anyway!
assertEquals(4, ((Foo) context.getBean("foo", Foo.class)).values[1], 0.01);
}
@Test
public void testEquals() throws Exception {
Resource resource = new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(),
"child-context.xml"));
factory.setResource(resource);
ClassPathXmlApplicationContextFactory other = new ClassPathXmlApplicationContextFactory();
other.setResource(resource);
assertEquals(other, factory);
assertEquals(other.hashCode(), factory.hashCode());
}
public static class Foo {
private double[] values;
public void setValues(double[] values) {
this.values = values;
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2006-2010 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.assertEquals;
import org.junit.Test;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.core.io.ClassPathResource;
/**
* @author Dave Syer
*
*/
public class DefaultJobLoaderTests {
private JobRegistry registry = new MapJobRegistry();
private DefaultJobLoader jobLoader = new DefaultJobLoader(registry);
@Test
public void testReload() throws Exception {
ClassPathXmlApplicationContextFactory factory = new ClassPathXmlApplicationContextFactory(
new ClassPathResource("trivial-context.xml", getClass()));
jobLoader.load(factory);
assertEquals(1, registry.getJobNames().size());
jobLoader.reload(factory);
assertEquals(1, registry.getJobNames().size());
}
@Test
public void testReloadWithAutoRegister() throws Exception {
ClassPathXmlApplicationContextFactory factory = new ClassPathXmlApplicationContextFactory(
new ClassPathResource("trivial-context-autoregister.xml", getClass()));
jobLoader.load(factory);
assertEquals(1, registry.getJobNames().size());
jobLoader.reload(factory);
assertEquals(1, registry.getJobNames().size());
}
}

View File

@@ -20,6 +20,7 @@ import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.osgi.framework.Bundle;
@@ -50,6 +51,15 @@ public class OsgiBundleXmlApplicationContextFactoryTests {
verify(bundleContext, bundle);
}
@Test
public void testEquals() throws Exception {
factory.setPath("child-context.xml");
OsgiBundleXmlApplicationContextFactory other = new OsgiBundleXmlApplicationContextFactory();
other.setPath("child-context.xml");
assertEquals(other, factory);
assertEquals(other.hashCode(), factory.hashCode());
}
/**
* Test method for {@link org.springframework.batch.core.configuration.support.OsgiBundleXmlApplicationContextFactory#setApplicationContext(org.springframework.context.ApplicationContext)}.
*/