@Bean public JobRegistry jobRegistry() throws Exception { return new MapJobRegistry(); }
/** * Test method for {@link org.springframework.batch.core.configuration.support.MapJobRegistry#unregister(String)}. * @throws Exception */ public void testUnregister() throws Exception { registry.register(new ReferenceJobFactory(new JobSupport("foo"))); assertNotNull(registry.getJob("foo")); registry.unregister("foo"); try { assertNull(registry.getJob("foo")); fail("Expected NoSuchJobConfigurationException"); } catch (NoSuchJobException e) { // expected assertTrue(e.getMessage().indexOf("foo")>=0); } }
@Test public void testAfterInitializationWithCorrectType() throws Exception { MapJobRegistry registry = new MapJobRegistry(); processor.setJobRegistry(registry); JobSupport job = new JobSupport(); job.setBeanName("foo"); assertNotNull(processor.postProcessAfterInitialization(job, "bar")); assertEquals("[foo]", registry.getJobNames().toString()); }
@Test @SuppressWarnings("resource") public void testExecutionWithApplicationContext() throws Exception { 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); int count = names.length; // Each concrete bean of type JobConfiguration is registered... assertEquals(count, configurations.size()); // N.B. there is a failure / wonky mode where a parent bean is given an // explicit name or beanName (using property setter): in this case then // 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")); }
/** * Test method for {@link org.springframework.batch.core.configuration.support.MapJobRegistry#getJobNames()}. * @throws Exception */ public void testGetJobConfigurations() throws Exception { JobFactory jobFactory = new ReferenceJobFactory(new JobSupport("foo")); registry.register(jobFactory); registry.register(new ReferenceJobFactory(new JobSupport("bar"))); Collection<String> configurations = registry.getJobNames(); assertEquals(2, configurations.size()); assertTrue(configurations.contains(jobFactory.getJobName())); }
@Test public void testClear() throws Exception { Resource[] jobPaths = new Resource[] { new ClassPathResource( "org/springframework/batch/core/launch/support/2jobs.xml") }; setUpApplicationContextFactories(jobPaths, null); registrar.start(); assertEquals(2, registry.getJobNames().size()); registrar.stop(); assertEquals(0, registry.getJobNames().size()); }
/** * Test method for {@link org.springframework.batch.core.configuration.support.MapJobRegistry#getJob(java.lang.String)}. */ public void testRealDuplicateConfiguration() throws Exception { JobFactory jobFactory = new ReferenceJobFactory(new JobSupport("foo")); registry.register(jobFactory); try { registry.register(jobFactory); fail("Unexpected DuplicateJobConfigurationException"); } catch (DuplicateJobException e) { // expected assertTrue(e.getMessage().indexOf("foo")>=0); } }
@Test public void testLocateJob() throws Exception { Resource[] jobPaths = new Resource[] { new ClassPathResource("org/springframework/batch/core/launch/support/job.xml"), new ClassPathResource("org/springframework/batch/core/launch/support/job2.xml") }; @SuppressWarnings("resource") GenericApplicationContext applicationContext = new GenericApplicationContext(); applicationContext.refresh(); setUpApplicationContextFactories(jobPaths, applicationContext); registrar.setApplicationContext(applicationContext); registrar.start(); Collection<String> names = registry.getJobNames(); assertEquals(2, names.size()); assertTrue(names.contains("test-job")); assertTrue(names.contains("test-job2")); Job job = registry.getJob("test-job"); assertEquals("test-job", job.getName()); job = registry.getJob("test-job2"); assertEquals("test-job2", job.getName()); }
@Test public void testChildContextOverridesBeanPostProcessor() throws Exception { Resource[] jobPaths = new Resource[] { new ClassPathResource( "org/springframework/batch/core/launch/support/2jobs.xml") }; @SuppressWarnings("resource") ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext( "/org/springframework/batch/core/launch/support/test-environment-with-registry-and-auto-register.xml"); registrar.setApplicationContext(applicationContext); setUpApplicationContextFactories(jobPaths, applicationContext); registrar.start(); assertEquals(2, registry.getJobNames().size()); }
/** * Test method for {@link org.springframework.batch.core.configuration.support.MapJobRegistry#getJob(java.lang.String)}. */ public void testReplaceDuplicateConfiguration() throws Exception { registry.register(new ReferenceJobFactory(new JobSupport("foo"))); try { registry.register(new ReferenceJobFactory(new JobSupport("foo"))); fail("Expected DuplicateJobConfigurationException"); } catch (DuplicateJobException e) { // unexpected: even if the job is different we want a DuplicateJobException assertTrue(e.getMessage().indexOf("foo")>=0); } }
/** * Sets up the basic components by extracting them from the {@link BatchConfigurer configurer}, defaulting to some * sensible values as long as a unique DataSource is available. * * @throws Exception if there is a problem in the configurer */ protected void initialize() throws Exception { if (initialized) { return; } BatchConfigurer configurer = getConfigurer(context.getBeansOfType(BatchConfigurer.class).values()); jobRepository.set(configurer.getJobRepository()); jobLauncher.set(configurer.getJobLauncher()); transactionManager.set(configurer.getTransactionManager()); jobRegistry.set(new MapJobRegistry()); jobExplorer.set(configurer.getJobExplorer()); initialized = true; }
@Test public void testUnregisterOnDestroy() throws Exception { MapJobRegistry registry = new MapJobRegistry(); processor.setJobRegistry(registry); JobSupport job = new JobSupport(); job.setBeanName("foo"); assertNotNull(processor.postProcessAfterInitialization(job, "bar")); processor.destroy(); assertEquals("[]", registry.getJobNames().toString()); }
@Test public void testStartStopRunningWithCallback() throws Exception { Runnable callback = Mockito.mock(Runnable.class); Resource[] jobPaths = new Resource[] { new ClassPathResource( "org/springframework/batch/core/launch/support/2jobs.xml") }; setUpApplicationContextFactories(jobPaths, null); registrar.start(); assertTrue(registrar.isRunning()); registrar.start(); assertEquals(2, registry.getJobNames().size()); registrar.stop(callback); assertFalse(registrar.isRunning()); assertEquals(0, registry.getJobNames().size()); Mockito.verify(callback, Mockito.times(1)).run(); }
@Test public void testAfterInitializationWithDuplicate() throws Exception { MapJobRegistry registry = new MapJobRegistry(); processor.setJobRegistry(registry); JobSupport job = new JobSupport(); job.setBeanName("foo"); processor.postProcessAfterInitialization(job, "bar"); try { processor.postProcessAfterInitialization(job, "spam"); fail("Expected FatalBeanException"); } catch (FatalBeanException e) { // Expected assertTrue(e.getCause() instanceof DuplicateJobException); } }
@Test public void testAfterInitializationWithGroupName() throws Exception { MapJobRegistry registry = new MapJobRegistry(); processor.setJobRegistry(registry); processor.setGroupName("jobs"); JobSupport job = new JobSupport(); job.setBeanName("foo"); assertNotNull(processor.postProcessAfterInitialization(job, "bar")); assertEquals("[jobs.foo]", registry.getJobNames().toString()); }
/** * Test method for * {@link org.springframework.batch.core.configuration.support.JobFactoryRegistrationListener#unbind(org.springframework.batch.core.configuration.JobFactory, java.util.Map)}. * @throws Exception */ @Test public void testUnbind() throws Exception { testBind(); listener.unbind(new JobFactory() { @Override public Job createJob() { return null; } @Override public String getJobName() { return "foo"; } }, null); assertEquals(0, registry.getJobNames().size()); }
@Bean public JobRegistry jobRegistry() throws Exception { return new MapJobRegistry(); }
/** * Test method for * {@link org.springframework.batch.core.configuration.support.JobFactoryRegistrationListener#bind(org.springframework.batch.core.configuration.JobFactory, java.util.Map)}. * @throws Exception */ @Test public void testBind() throws Exception { listener.setJobRegistry(registry); listener.bind(new JobFactory() { @Override public Job createJob() { return null; } @Override public String getJobName() { return "foo"; } }, null); assertEquals(1, registry.getJobNames().size()); }
@Bean public JobRegistry jobRegistry() throws Exception { return new MapJobRegistry(); }
@Test public void testDuplicateJobsInFile() throws Exception { Resource[] jobPaths = new Resource[] { new ClassPathResource( "org/springframework/batch/core/launch/support/2jobs.xml") }; @SuppressWarnings("resource") GenericApplicationContext applicationContext = new GenericApplicationContext(); applicationContext.refresh(); setUpApplicationContextFactories(jobPaths, applicationContext); registrar.setApplicationContext(applicationContext); registrar.start(); assertEquals(2, registry.getJobNames().size()); }