@PostConstruct public void initialize() { registrar.addApplicationContextFactory(new GenericApplicationContextFactory(TestConfiguration.class)); registrar.addApplicationContextFactory(new GenericApplicationContextFactory(VanillaConfiguration.class)); }
private void setUpApplicationContextFactories(Resource[] jobPaths, ApplicationContext parent) { Collection<ApplicationContextFactory> applicationContextFactories = new ArrayList<>(); for (Resource resource : jobPaths) { GenericApplicationContextFactory factory = new GenericApplicationContextFactory(resource); factory.setApplicationContext(parent); applicationContextFactories.add(factory); } registrar.setApplicationContextFactories(applicationContextFactories .toArray(new ApplicationContextFactory[jobPaths.length])); }
@Test(expected = IllegalArgumentException.class) public void testDifferentResourceTypes() throws Exception { Resource resource1 = new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "abstract-context.xml")); GenericApplicationContextFactory factory = new GenericApplicationContextFactory(resource1, Configuration1.class); factory.createApplicationContext(); }
@Test public void testEqualsMultipleConfigs() throws Exception { Resource resource1 = new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "abstract-context.xml")); Resource resource2 = new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context-with-abstract-job.xml")); GenericApplicationContextFactory factory = new GenericApplicationContextFactory(resource1, resource2); GenericApplicationContextFactory other = new GenericApplicationContextFactory(resource1, resource2); assertEquals(other, factory); assertEquals(other.hashCode(), factory.hashCode()); }
@Test public void testNoStepRegistryAvailable() throws DuplicateJobException { final JobLoader loader = new DefaultJobLoader(jobRegistry); GenericApplicationContextFactory factory = new GenericApplicationContextFactory( new ClassPathResource("job-context-with-steps.xml", getClass())); loader.load(factory); // No step registry available so just registering the jobs assertEquals(2, jobRegistry.getJobNames().size()); }
@Test public void testRegistryUpdated() throws DuplicateJobException { GenericApplicationContextFactory factory = new GenericApplicationContextFactory( new ClassPathResource("trivial-context.xml", getClass())); jobLoader.load(factory); assertEquals(1, jobRegistry.getJobNames().size()); assertStepExist(TEST_JOB_NAME, TEST_STEP_NAME); }
@Test public void testEquals() throws Exception { Resource resource = new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml")); GenericApplicationContextFactory factory = new GenericApplicationContextFactory(resource); GenericApplicationContextFactory other = new GenericApplicationContextFactory(resource); assertEquals(other, factory); assertEquals(other.hashCode(), factory.hashCode()); }
@Test public void testGetJobName() { GenericApplicationContextFactory factory = new GenericApplicationContextFactory( new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "trivial-context.xml"))); assertEquals("test-job", factory.createApplicationContext().getBeanNamesForType(Job.class)[0]); }
@Test public void testMultipleConfigurationClasses() throws Exception { GenericApplicationContextFactory factory = new GenericApplicationContextFactory(Configuration1.class, Configuration2.class); ConfigurableApplicationContext context = factory.createApplicationContext(); assertEquals(context.getBean("bean1"), "bean1"); assertEquals(context.getBean("bean2"), "bean2"); assertEquals(context.getBean("bean3"), "bean3"); assertEquals(context.getBean("bean4"), "bean4"); }
@Test public void testCreateJob() { GenericApplicationContextFactory factory = new GenericApplicationContextFactory( new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "trivial-context.xml"))); @SuppressWarnings("resource") ConfigurableApplicationContext context = factory.createApplicationContext(); assertNotNull(context); assertTrue("Wrong id: " + context, context.getId().contains("trivial-context.xml")); }
@SuppressWarnings("resource") @Test public void testParentConfigurationInherited() { GenericApplicationContextFactory factory = new GenericApplicationContextFactory( new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "child-context.xml"))); factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath( getClass(), "parent-context.xml"))); ConfigurableApplicationContext context = factory.createApplicationContext(); assertEquals("test-job", context.getBeanNamesForType(Job.class)[0]); assertEquals("bar", context.getBean("test-job", Job.class).getName()); assertEquals(4, context.getBean("foo", Foo.class).values[1], 0.01); }
@Test public void testParentChildLifecycleEvents() throws InterruptedException { AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(ParentContext.class); GenericApplicationContextFactory child = new GenericApplicationContextFactory(ChildContextConfiguration.class); child.setApplicationContext(parent); ApplicationContext context = child.createApplicationContext(); ChildBean bean = context.getBean(ChildBean.class); assertEquals(1, bean.counter1); assertEquals(1, bean.counter2); }
@SuppressWarnings("resource") @Test public void testBeanFactoryPostProcessorOrderRespected() { GenericApplicationContextFactory factory = new GenericApplicationContextFactory( new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "placeholder-context.xml"))); factory.setApplicationContext(new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath( getClass(), "parent-context.xml"))); ConfigurableApplicationContext context = factory.createApplicationContext(); assertEquals("test-job", context.getBeanNamesForType(Job.class)[0]); assertEquals("spam", context.getBean("test-job", Job.class).getName()); }
@Test public void testReload() throws Exception { GenericApplicationContextFactory factory = new GenericApplicationContextFactory(new ClassPathResource( "trivial-context.xml", getClass())); jobLoader.load(factory); assertEquals(1, jobRegistry.getJobNames().size()); assertStepExist(TEST_JOB_NAME, TEST_STEP_NAME); jobLoader.reload(factory); assertEquals(1, jobRegistry.getJobNames().size()); assertStepExist(TEST_JOB_NAME, TEST_STEP_NAME); }
@Test public void testReloadWithAutoRegister() throws Exception { GenericApplicationContextFactory factory = new GenericApplicationContextFactory(new ClassPathResource( "trivial-context-autoregister.xml", getClass())); jobLoader.load(factory); assertEquals(1, jobRegistry.getJobNames().size()); assertStepExist(TEST_JOB_NAME, TEST_STEP_NAME); jobLoader.reload(factory); assertEquals(1, jobRegistry.getJobNames().size()); assertStepExist(TEST_JOB_NAME, TEST_STEP_NAME); }
@Test public void testMultipleJobsInTheSameContext() throws DuplicateJobException { GenericApplicationContextFactory factory = new GenericApplicationContextFactory( new ClassPathResource("job-context-with-steps.xml", getClass())); jobLoader.load(factory); assertEquals(2, jobRegistry.getJobNames().size()); assertStepExist("job1", "step11", "step12"); assertStepDoNotExist("job1", "step21", "step22"); assertStepExist("job2", "step21", "step22"); assertStepDoNotExist("job2", "step11", "step12"); }
@Test public void testMultipleJobsInTheSameContextWithSeparateSteps() throws DuplicateJobException { GenericApplicationContextFactory factory = new GenericApplicationContextFactory( new ClassPathResource("job-context-with-separate-steps.xml", getClass())); jobLoader.load(factory); assertEquals(2, jobRegistry.getJobNames().size()); assertStepExist("job1", "step11", "step12", "genericStep1", "genericStep2"); assertStepDoNotExist("job1", "step21", "step22"); assertStepExist("job2", "step21", "step22", "genericStep1", "genericStep2"); assertStepDoNotExist("job2", "step11", "step12"); }
@Test public void testLoadWithJobThatIsNotAStepLocatorNoStepRegistry() throws DuplicateJobException { final JobLoader loader = new DefaultJobLoader(jobRegistry); GenericApplicationContextFactory factory = new GenericApplicationContextFactory( new ByteArrayResource(BASIC_JOB_XML.getBytes())); try { loader.load(factory); } catch (UnsupportedOperationException e) { fail("Should not have failed with a [" + UnsupportedOperationException.class.getName() + "] as " + "stepRegistry is not available for this JobLoader instance."); } }
@Test public void testLoadWithExplicitName() throws Exception { GenericApplicationContextFactory factory = new GenericApplicationContextFactory(new ByteArrayResource( JOB_XML.getBytes())); jobLoader.load(factory); assertEquals(1, jobRegistry.getJobNames().size()); jobLoader.reload(factory); assertEquals(1, jobRegistry.getJobNames().size()); }
@Test public void testClear() throws Exception { GenericApplicationContextFactory factory = new GenericApplicationContextFactory(new ByteArrayResource( JOB_XML.getBytes())); jobLoader.load(factory); assertEquals(1, ((Map) ReflectionTestUtils.getField(jobLoader, "contexts")).size()); assertEquals(1, ((Map) ReflectionTestUtils.getField(jobLoader, "contextToJobNames")).size()); jobLoader.clear(); assertEquals(0, ((Map) ReflectionTestUtils.getField(jobLoader, "contexts")).size()); assertEquals(0, ((Map) ReflectionTestUtils.getField(jobLoader, "contextToJobNames")).size()); }