@Test public void testMultithreaded() throws Exception { JobContext context = JobSynchronizationManager.register(jobExecution); ExecutorService executorService = Executors.newFixedThreadPool(2); FutureTask<JobContext> task = new FutureTask<>(new Callable<JobContext>() { @Override public JobContext call() throws Exception { try { JobSynchronizationManager.register(jobExecution); JobContext context = JobSynchronizationManager.getContext(); context.setAttribute("foo", "bar"); return context; } finally { JobSynchronizationManager.close(); } } }); executorService.execute(task); executorService.awaitTermination(1, TimeUnit.SECONDS); assertEquals(context.attributeNames().length, task.get().attributeNames().length); JobSynchronizationManager.close(); assertNull(JobSynchronizationManager.getContext()); }
@Test public void testScopedProxy() throws Exception { proxied.execute(new JobExecution(11L)); assertTrue(TestJob.getContext().attributeNames().length > 0); String collaborator = (String) TestJob.getContext().getAttribute("collaborator"); assertNotNull(collaborator); assertEquals("bar", collaborator); assertTrue("Scoped proxy not created", ((String) TestJob.getContext().getAttribute("collaborator.class")) .matches(PROXY_TO_STRING_REGEX)); }
@Test public void testScopedProxyForReference() throws Exception { enhanced.execute(new JobExecution(11L)); assertTrue(TestJob.getContext().attributeNames().length > 0); String collaborator = (String) TestJob.getContext().getAttribute("collaborator"); assertNotNull(collaborator); assertEquals("bar", collaborator); }
@Test public void testScopedProxyForSecondReference() throws Exception { doubleEnhanced.execute(new JobExecution(11L)); assertTrue(TestJob.getContext().attributeNames().length > 0); String collaborator = (String) TestJob.getContext().getAttribute("collaborator"); assertNotNull(collaborator); assertEquals("bar", collaborator); }
@Test public void testNestedScopedProxy() throws Exception { nested.execute(new JobExecution(11L)); assertTrue(TestJob.getContext().attributeNames().length > 0); String collaborator = (String) TestJob.getContext().getAttribute("collaborator"); assertNotNull(collaborator); assertEquals("foo", collaborator); String parent = (String) TestJob.getContext().getAttribute("parent"); assertNotNull(parent); assertEquals("bar", parent); assertTrue("Scoped proxy not created", ((String) TestJob.getContext().getAttribute("parent.class")) .matches(PROXY_TO_STRING_REGEX)); }
@Test public void testExecutionContext() throws Exception { JobExecution stepExecution = new JobExecution(11L); ExecutionContext executionContext = new ExecutionContext(); executionContext.put("name", "spam"); stepExecution.setExecutionContext(executionContext); proxied.execute(stepExecution); assertTrue(TestJob.getContext().attributeNames().length > 0); String collaborator = (String) TestJob.getContext().getAttribute("collaborator"); assertNotNull(collaborator); assertEquals("bar", collaborator); }