/** * Get an attribute accessor in the form of a {@link JobContext} that can * be used to store scoped bean instances. * * @return the current job context which we can use as a scope storage * medium */ private JobContext getContext() { JobContext context = JobSynchronizationManager.getContext(); if (context == null) { throw new IllegalStateException("No context holder available for job scope"); } return context; }
@Test public void testRegisterNull() { assertNull(JobSynchronizationManager.getContext()); JobSynchronizationManager.register(null); assertNull(JobSynchronizationManager.getContext()); }
@Test public void testGetContext() { assertNull(JobSynchronizationManager.getContext()); JobSynchronizationManager.register(jobExecution); assertNotNull(JobSynchronizationManager.getContext()); }
@Before @After public void start() { while (JobSynchronizationManager.getContext() != null) { JobSynchronizationManager.close(); } }
@Test public void testDefaultJobContext() throws Exception { TestContext testContext = getTestContext(new Object()); listener.prepareTestInstance(testContext); listener.beforeTestMethod(testContext); JobContext context = JobSynchronizationManager.getContext(); assertNotNull(context); listener.afterTestMethod(testContext); assertNull(JobSynchronizationManager.getContext()); }
@Override public JobContext call() throws Exception { try { JobSynchronizationManager.register(jobExecution); JobContext context = JobSynchronizationManager.getContext(); context.setAttribute("foo", "bar"); return context; } finally { JobSynchronizationManager.close(); } } });
@Test public void testRegisterTwice() { JobSynchronizationManager.register(jobExecution); JobSynchronizationManager.register(jobExecution); JobSynchronizationManager.close(); // if someone registers you have to assume they are going to close, so // the last thing you want is for the close to remove another context // that someone else has registered assertNotNull(JobSynchronizationManager.getContext()); JobSynchronizationManager.close(); assertNull(JobSynchronizationManager.getContext()); }
private void testExecutionContext(Object target) throws Exception { TestContext testContext = getTestContext(target); listener.prepareTestInstance(testContext); try { listener.beforeTestMethod(testContext); JobContext context = JobSynchronizationManager.getContext(); assertNotNull(context); assertEquals("bar", context.getJobExecutionContext().get("foo")); } finally { listener.afterTestMethod(testContext); } assertNull(JobSynchronizationManager.getContext()); }
private void testJobParameters(Object target) throws Exception { TestContext testContext = getTestContext(target); listener.prepareTestInstance(testContext); try { listener.beforeTestMethod(testContext); JobContext context = JobSynchronizationManager.getContext(); assertNotNull(context); assertEquals("spam", context.getJobParameters().get("foo")); } finally { listener.afterTestMethod(testContext); } assertNull(JobSynchronizationManager.getContext()); }
@Override public void execute(JobExecution stepExecution) { context = JobSynchronizationManager.getContext(); setContextFromCollaborator(); stepExecution.getExecutionContext().put("foo", "changed but it shouldn't affect the collaborator"); setContextFromCollaborator(); }
@Test public void testClose() throws Exception { final List<String> list = new ArrayList<>(); JobContext context = JobSynchronizationManager.register(jobExecution); context.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("foo"); } }); JobSynchronizationManager.close(); assertNull(JobSynchronizationManager.getContext()); assertEquals(0, list.size()); }
@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 testRelease() { JobContext context = JobSynchronizationManager.register(jobExecution); final List<String> list = new ArrayList<>(); context.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("foo"); } }); // On release we expect the destruction callbacks to be called JobSynchronizationManager.release(); assertNull(JobSynchronizationManager.getContext()); assertEquals(1, list.size()); }
@Test public void testScopeCreation() throws Exception { vanilla.execute(new JobExecution(11L)); assertNotNull(TestJob.getContext()); assertNull(JobSynchronizationManager.getContext()); }
/** * Get an attribute accessor in the form of a {@link JobContext} that can * be used to store scoped bean instances. * * @return the current job context which we can use as a scope storage * medium */ private JobContext getContext() { JobContext context = JobSynchronizationManager.getContext(); if (context == null) { throw new IllegalStateException("No context holder available for job scope"); } return context; }
/** * Get an attribute accessor in the form of a {@link JobContext} that can * be used to store scoped bean instances. * * @return the current job context which we can use as a scope storage * medium */ private JobContext getContext() { JobContext context = JobSynchronizationManager.getContext(); if (context == null) { throw new IllegalStateException("No context holder available for job scope"); } return context; }
/** * Get an attribute accessor in the form of a {@link JobContext} that can * be used to store scoped bean instances. * * @return the current job context which we can use as a scope storage * medium */ private JobContext getContext() { JobContext context = JobSynchronizationManager.getContext(); if (context == null) { throw new IllegalStateException("No context holder available for job scope"); } return context; }