OPEN - issue BATCH-673: Add new Java 5.0 features
http://jira.springframework.org/browse/BATCH-673 Modified job listeners to use same approach as step listeners. Fixed test errors in samples.
This commit is contained in:
@@ -15,11 +15,17 @@
|
||||
*/
|
||||
package org.springframework.batch.core.configuration.xml;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.annotation.AfterJob;
|
||||
import org.springframework.batch.core.annotation.BeforeJob;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -32,12 +38,35 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class JobExecutionListenerParserTests {
|
||||
|
||||
public static boolean beforeCalled = false;
|
||||
public static boolean afterCalled = false;
|
||||
|
||||
@Autowired
|
||||
Job job;
|
||||
|
||||
@Autowired
|
||||
JobRepository jobRepository;
|
||||
|
||||
@Test
|
||||
public void testListners(){
|
||||
Assert.assertNotNull(job);
|
||||
public void testListeners() throws Exception{
|
||||
JobExecution jobExecution = jobRepository.createJobExecution("testJob", new JobParametersBuilder().addLong("now",
|
||||
System.currentTimeMillis()).toJobParameters());
|
||||
job.execute(jobExecution);
|
||||
assertTrue(beforeCalled);
|
||||
assertTrue(afterCalled);
|
||||
}
|
||||
|
||||
public static class TestComponent{
|
||||
|
||||
@BeforeJob
|
||||
public void before(JobExecution jobExecution){
|
||||
beforeCalled = true;
|
||||
}
|
||||
|
||||
@AfterJob
|
||||
public void after(){
|
||||
afterCalled = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.core.listener;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.annotation.AfterJob;
|
||||
import org.springframework.batch.core.annotation.BeforeJob;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class JobExecutionListenerAdapterTests {
|
||||
|
||||
private TestClass testClass;
|
||||
private AnnotatedTestClass annotatedTestClass;
|
||||
private JobExecution jobExecution = new JobExecution(11L);
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp(){
|
||||
testClass = new TestClass();
|
||||
annotatedTestClass = new AnnotatedTestClass();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeforeJob() throws Exception{
|
||||
JobExecutionListenerAdapter adapter = new JobExecutionListenerAdapter(testClass);
|
||||
adapter.setBeforeMethod("beforeJob");
|
||||
adapter.afterPropertiesSet();
|
||||
adapter.beforeJob(jobExecution);
|
||||
adapter.afterJob(jobExecution);
|
||||
assertTrue(testClass.beforeJobCalled);
|
||||
assertFalse(testClass.afterJobCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAfterJob() throws Exception{
|
||||
JobExecutionListenerAdapter adapter = new JobExecutionListenerAdapter(testClass);
|
||||
adapter.setAfterMethod("afterJob");
|
||||
adapter.afterPropertiesSet();
|
||||
adapter.beforeJob(jobExecution);
|
||||
adapter.afterJob(jobExecution);
|
||||
assertFalse(testClass.beforeJobCalled);
|
||||
assertTrue(testClass.afterJobCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoth() throws Exception{
|
||||
JobExecutionListenerAdapter adapter = new JobExecutionListenerAdapter(testClass);
|
||||
adapter.setAfterMethod("afterJob");
|
||||
adapter.setBeforeMethod("beforeJob");
|
||||
adapter.afterPropertiesSet();
|
||||
adapter.beforeJob(jobExecution);
|
||||
adapter.afterJob(jobExecution);
|
||||
assertTrue(testClass.beforeJobCalled);
|
||||
assertTrue(testClass.afterJobCalled);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testNeither() throws Exception{
|
||||
JobExecutionListenerAdapter adapter = new JobExecutionListenerAdapter(testClass);
|
||||
adapter.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnnotation() throws Exception{
|
||||
JobExecutionListenerAdapter adapter = new JobExecutionListenerAdapter(annotatedTestClass);
|
||||
adapter.afterPropertiesSet();
|
||||
adapter.beforeJob(jobExecution);
|
||||
adapter.afterJob(jobExecution);
|
||||
assertTrue(annotatedTestClass.beforeJobCalled);
|
||||
assertTrue(annotatedTestClass.afterJobCalled);
|
||||
}
|
||||
|
||||
private class TestClass{
|
||||
|
||||
boolean beforeJobCalled = false;
|
||||
boolean afterJobCalled = false;
|
||||
|
||||
public void beforeJob(JobExecution jobExecution){
|
||||
beforeJobCalled = true;
|
||||
}
|
||||
|
||||
public void afterJob(JobExecution jobExecution){
|
||||
afterJobCalled = true;
|
||||
}
|
||||
}
|
||||
|
||||
private class AnnotatedTestClass extends TestClass{
|
||||
|
||||
@BeforeJob
|
||||
public void before(){
|
||||
super.beforeJobCalled = true;
|
||||
}
|
||||
|
||||
@AfterJob
|
||||
public void after(){
|
||||
super.afterJobCalled = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -28,20 +28,25 @@ import org.springframework.batch.core.annotation.BeforeJob;
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class JobExecutionListnerFactoryBeanTests {
|
||||
public class JobListenerFactoryBeanTests {
|
||||
|
||||
JobExecutionListenerFactoryBean factoryBean;
|
||||
JobListenerFactoryBean factoryBean;
|
||||
|
||||
@Before
|
||||
public void setUp(){
|
||||
factoryBean = new JobExecutionListenerFactoryBean();
|
||||
factoryBean = new JobListenerFactoryBean();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithInterface() throws Exception{
|
||||
JobListenerWithInterface delegate = new JobListenerWithInterface();
|
||||
factoryBean.setDelegate(delegate);
|
||||
assertEquals(delegate,factoryBean.getObject());
|
||||
JobExecutionListener listener = (JobExecutionListener) factoryBean.getObject();
|
||||
JobExecution jobExecution = new JobExecution(11L);
|
||||
listener.beforeJob(jobExecution);
|
||||
listener.afterJob(jobExecution);
|
||||
assertTrue(delegate.beforeJobCalled);
|
||||
assertTrue(delegate.afterJobCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -58,10 +63,15 @@ public class JobExecutionListnerFactoryBeanTests {
|
||||
|
||||
private class JobListenerWithInterface implements JobExecutionListener{
|
||||
|
||||
boolean beforeJobCalled = false;
|
||||
boolean afterJobCalled = false;
|
||||
|
||||
public void afterJob(JobExecution jobExecution) {
|
||||
beforeJobCalled = true;
|
||||
}
|
||||
|
||||
public void beforeJob(JobExecution jobExecution) {
|
||||
afterJobCalled = true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,7 +19,7 @@ import org.springframework.batch.core.configuration.util.SimpleMethodInvoker;
|
||||
|
||||
public class StepListenerMethodInterceptorTests {
|
||||
|
||||
StepListenerMethodInterceptor interceptor;
|
||||
MethodInvokerMethodInterceptor interceptor;
|
||||
TestClass testClass;
|
||||
|
||||
@Before
|
||||
@@ -34,7 +34,7 @@ public class StepListenerMethodInterceptorTests {
|
||||
for(Method method : TestClass.class.getMethods()){
|
||||
invokerMap.put(method.getName(), asSet( new SimpleMethodInvoker(testClass, method)));
|
||||
}
|
||||
interceptor = new StepListenerMethodInterceptor(invokerMap);
|
||||
interceptor = new MethodInvokerMethodInterceptor(invokerMap);
|
||||
interceptor.invoke(new StubMethodInvocation(TestClass.class.getMethod("method1")));
|
||||
assertEquals(1, testClass.method1Count);
|
||||
interceptor.invoke(new StubMethodInvocation(TestClass.class.getMethod("method2")));
|
||||
@@ -48,7 +48,7 @@ public class StepListenerMethodInterceptorTests {
|
||||
Set<MethodInvoker> invokers = asSet(MethodInvokerUtils.createMethodInvokerByName(testClass, "method1", false));
|
||||
invokers.add(MethodInvokerUtils.createMethodInvokerByName(testClass, "method2", false));
|
||||
invokerMap.put("method1", invokers);
|
||||
interceptor = new StepListenerMethodInterceptor(invokerMap);
|
||||
interceptor = new MethodInvokerMethodInterceptor(invokerMap);
|
||||
interceptor.invoke(new StubMethodInvocation(TestClass.class.getMethod("method1")));
|
||||
assertEquals(1, testClass.method1Count);
|
||||
interceptor.invoke(new StubMethodInvocation(TestClass.class.getMethod("method2")));
|
||||
@@ -61,7 +61,7 @@ public class StepListenerMethodInterceptorTests {
|
||||
Set<MethodInvoker> invokers = asSet(MethodInvokerUtils.createMethodInvokerByName(testClass, "method3", false));
|
||||
invokers.add(MethodInvokerUtils.createMethodInvokerByName(testClass, "method3", false));
|
||||
invokerMap.put("method3", invokers);
|
||||
interceptor = new StepListenerMethodInterceptor(invokerMap);
|
||||
interceptor = new MethodInvokerMethodInterceptor(invokerMap);
|
||||
assertEquals(ExitStatus.FINISHED, interceptor.invoke(new StubMethodInvocation(TestClass.class.getMethod("method3"))));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user