@Override public String toString() { return super.toString() + ", jobExecutionContext=" + getJobExecutionContext() + ", jobParameters=" + getJobParameters(); }
@Test(expected = IllegalStateException.class) public void testIllegalContextId() throws Exception { context = new JobContext(new JobExecution((Long) null)); context.getId(); }
@Test public void testDestructionCallbackSunnyDay() throws Exception { context.setAttribute("foo", "FOO"); context.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("bar"); } }); context.close(); assertEquals(1, list.size()); assertEquals("bar", list.get(0)); }
/** * @see Scope#get(String, ObjectFactory) */ @Override public Object get(String name, ObjectFactory<?> objectFactory) { JobContext context = getContext(); Object scopedObject = context.getAttribute(name); if (scopedObject == null) { synchronized (mutex) { scopedObject = context.getAttribute(name); if (scopedObject == null) { if (logger.isDebugEnabled()) { logger.debug(String.format("Creating object in scope=%s, name=%s", this.getName(), name)); } scopedObject = objectFactory.getObject(); context.setAttribute(name, scopedObject); } } } return scopedObject; }
@Test public void testRegisterDestructionCallback() { final List<String> list = new ArrayList<>(); context.setAttribute("foo", "bar"); scope.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("foo"); } }); assertEquals(0, list.size()); // When the context is closed, provided the attribute exists the // callback is called... context.close(); assertEquals(1, list.size()); }
@Test public void testDestructionCallbackMissingAttribute() throws Exception { context.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("bar"); } }); context.close(); // Yes the callback should be called even if the attribute is missing - // for inner beans assertEquals(1, list.size()); }
@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 testJobParameters() throws Exception { JobParameters jobParameters = new JobParametersBuilder().addString("foo", "bar").toJobParameters(); JobInstance jobInstance = new JobInstance(0L, "foo"); jobExecution = new JobExecution(5L, jobParameters); jobExecution.setJobInstance(jobInstance); context = new JobContext(jobExecution); assertEquals("bar", context.getJobParameters().get("foo")); }
@Override protected JobContext createNewContext(JobExecution execution, BatchPropertyContext args) { return new JobContext(execution); }
/** * @see Scope#registerDestructionCallback(String, Runnable) */ @Override public void registerDestructionCallback(String name, Runnable callback) { JobContext context = getContext(); if (logger.isDebugEnabled()) { logger.debug(String.format("Registered destruction callback in scope=%s, name=%s", this.getName(), name)); } context.registerDestructionCallback(name, callback); }
@Test public void testJobExecutionContext() throws Exception { ExecutionContext executionContext = jobExecution.getExecutionContext(); executionContext.put("foo", "bar"); assertEquals("bar", context.getJobExecutionContext().get("foo")); }
/** * @see Scope#getConversationId() */ @Override public String getConversationId() { JobContext context = getContext(); return context.getId(); }
@Override protected void close(JobContext context) { context.close(); } };
@Test public void testRemove() { context.setAttribute("foo", "bar"); scope.remove("foo"); assertFalse(context.hasAttribute("foo")); }
@Test public void testGetJobExecution() { context = new JobContext(jobExecution); assertNotNull(context.getJobExecution()); }
/** * @see Scope#remove(String) */ @Override public Object remove(String name) { JobContext context = getContext(); if (logger.isDebugEnabled()) { logger.debug(String.format("Removing from scope=%s, name=%s", this.getName(), name)); } return context.removeAttribute(name); }
/** * Override base class behaviour to ensure destruction callbacks are * unregistered as well as the default behaviour. * * @see SynchronizedAttributeAccessor#removeAttribute(String) */ @Override @Nullable public Object removeAttribute(String name) { unregisterDestructionCallbacks(name); return super.removeAttribute(name); }
private void setContextFromCollaborator() { if (context != null) { context.setAttribute("collaborator", collaborator.getName()); context.setAttribute("collaborator.class", collaborator.getClass().toString()); if (collaborator.getParent()!=null) { context.setAttribute("parent", collaborator.getParent().getName()); context.setAttribute("parent.class", collaborator.getParent().getClass().toString()); } } }
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()); }
/** * @see Scope#get(String, ObjectFactory) */ @Override public Object get(String name, ObjectFactory<?> objectFactory) { JobContext context = getContext(); Object scopedObject = context.getAttribute(name); if (scopedObject == null) { synchronized (mutex) { scopedObject = context.getAttribute(name); if (scopedObject == null) { logger.debug(String.format("Creating object in scope=%s, name=%s", this.getName(), name)); scopedObject = objectFactory.getObject(); context.setAttribute(name, scopedObject); } } } return scopedObject; }