BATCH-891: Created JobExecutionListenerAdapter which supports @BeforeJob and @AfterJob annotation. Also added listners tag to XSD.
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.configuration.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobExecutionListener;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class SimpleMethodInvokerTests {
|
||||
|
||||
TestClass testClass;
|
||||
JobExecution jobExecution = new JobExecution(11L);
|
||||
|
||||
@Before
|
||||
public void setUp(){
|
||||
testClass = new TestClass();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethod() throws Exception{
|
||||
|
||||
Method method = TestClass.class.getMethod("beforeJob");
|
||||
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, method);
|
||||
methodInvoker.invokeMethod(jobExecution);
|
||||
assertTrue(testClass.beforeJobCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodByName() throws Exception{
|
||||
|
||||
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, "beforeJob", JobExecutionListener.class);
|
||||
methodInvoker.invokeMethod(jobExecution);
|
||||
assertTrue(testClass.beforeJobCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodWithExecution() throws Exception{
|
||||
Method method = TestClass.class.getMethod("beforeJobWithExecution", JobExecution.class);
|
||||
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, method);
|
||||
methodInvoker.invokeMethod(jobExecution);
|
||||
assertTrue(testClass.beforeJobCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodByNameWithExecution() throws Exception{
|
||||
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, "beforeJobWithExecution", JobExecution.class);
|
||||
methodInvoker.invokeMethod(jobExecution);
|
||||
assertTrue(testClass.beforeJobCalled);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testMethodWithTooManyArguments() throws Exception{
|
||||
Method method = TestClass.class.getMethod("beforeJobWithTooManyArguments", JobExecution.class, int.class);
|
||||
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, method);
|
||||
methodInvoker.invokeMethod(jobExecution);
|
||||
assertFalse(testClass.beforeJobCalled);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testMethodByNameWithTooManyArguments() throws Exception{
|
||||
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, "beforeJobWithTooManyArguments", JobExecution.class);
|
||||
methodInvoker.invokeMethod(jobExecution);
|
||||
assertFalse(testClass.beforeJobCalled);
|
||||
}
|
||||
|
||||
private class TestClass{
|
||||
|
||||
boolean beforeJobCalled = false;
|
||||
|
||||
public void beforeJob(){
|
||||
beforeJobCalled = true;
|
||||
}
|
||||
|
||||
public void beforeJobWithExecution(JobExecution jobExecution){
|
||||
beforeJobCalled = true;
|
||||
}
|
||||
|
||||
public void beforeJobWithTooManyArguments(JobExecution jobExecution, int someInt){
|
||||
beforeJobCalled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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.configuration.xml;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class JobExecutionListenerParserTests {
|
||||
|
||||
@Autowired
|
||||
Job job;
|
||||
|
||||
@Test
|
||||
public void testListners(){
|
||||
Assert.assertNotNull(job);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.springframework.batch.core.configuration.xml;
|
||||
|
||||
import org.springframework.batch.core.annotation.BeforeJob;
|
||||
|
||||
public class TestJobListener {
|
||||
|
||||
@BeforeJob
|
||||
public void beforeJob(){
|
||||
|
||||
}
|
||||
|
||||
public void afterJob(){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user