OPEN - issue BATCH-1108: Add composite ItemWriter/Processor based on Classifier

First draft of classifier features added.  Also refactored MethodInvoker abstraction into infrastructure.
This commit is contained in:
dsyer
2009-03-03 11:55:03 +00:00
parent b8208dcd8a
commit 998f6f592e
25 changed files with 819 additions and 132 deletions

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2006-2007 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.support;
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.support.annotation.Classifier;
/**
* @author Dave Syer
*
*/
public class BackToBackPatternClassifierTests {
private BackToBackPatternClassifier<String, String> classifier = new BackToBackPatternClassifier<String, String>();
private Map<String, String> map;
@Before
public void createMap() {
map = new HashMap<String, String>();
map.put("foo", "bar");
map.put("*", "spam");
}
@Test(expected=NullPointerException.class)
public void testNoClassifiers() {
classifier.classify("foo");
}
@Test
public void testCreateFromConstructor() {
classifier = new BackToBackPatternClassifier<String, String>(new PatternMatchingClassifier<String>(Collections
.singletonMap("oof", "bucket")), new PatternMatchingClassifier<String>(map));
assertEquals("spam", classifier.classify("oof"));
}
@Test
public void testSetRouterDelegate() {
classifier.setRouterDelegate(new Object() {
@SuppressWarnings("unused")
@Classifier
public String convert(String value) {
return "bucket";
}
});
classifier.setMatcherMap(map);
assertEquals("spam", classifier.classify("oof"));
}
}

View File

@@ -0,0 +1,127 @@
/*
* Copyright 2006-2007 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.support;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.batch.support.annotation.Classifier;
/**
* @author Dave Syer
*
*/
public class ClassifierAdapterTests {
private ClassifierAdapter<String, Integer> adapter = new ClassifierAdapter<String, Integer>();
@Test
public void testClassifierAdapterObject() {
adapter = new ClassifierAdapter<String, Integer>(new Object() {
@SuppressWarnings("unused")
@Classifier
public Integer getValue(String key) {
return Integer.parseInt(key);
}
@SuppressWarnings("unused")
public Integer getAnother(String key) {
throw new UnsupportedOperationException("Not allowed");
}
});
assertEquals(23, adapter.classify("23").intValue());
}
@Test(expected = IllegalStateException.class)
public void testClassifierAdapterObjectWithNoAnnotation() {
adapter = new ClassifierAdapter<String, Integer>(new Object() {
@SuppressWarnings("unused")
public Integer getValue(String key) {
return Integer.parseInt(key);
}
@SuppressWarnings("unused")
public Integer getAnother(String key) {
throw new UnsupportedOperationException("Not allowed");
}
});
assertEquals(23, adapter.classify("23").intValue());
}
@Test
public void testClassifierAdapterObjectSingleMethodWithNoAnnotation() {
adapter = new ClassifierAdapter<String, Integer>(new Object() {
@SuppressWarnings("unused")
public Integer getValue(String key) {
return Integer.parseInt(key);
}
@SuppressWarnings("unused")
public void doNothing(String key) {
}
@SuppressWarnings("unused")
public String doNothing(String key, int value) {
return "foo";
}
});
assertEquals(23, adapter.classify("23").intValue());
}
@Test
public void testClassifierAdapterClassifier() {
adapter = new ClassifierAdapter<String, Integer>(
new org.springframework.batch.support.Classifier<String, Integer>() {
public Integer classify(String classifiable) {
return Integer.valueOf(classifiable);
}
});
assertEquals(23, adapter.classify("23").intValue());
}
@Test
public void testClassifyWithSetter() {
adapter.setDelegate(new Object() {
@SuppressWarnings("unused")
@Classifier
public Integer getValue(String key) {
return Integer.parseInt(key);
}
});
assertEquals(23, adapter.classify("23").intValue());
}
@Test(expected=IllegalArgumentException.class)
public void testClassifyWithWrongType() {
adapter.setDelegate(new Object() {
@SuppressWarnings("unused")
@Classifier
public String getValue(Integer key) {
return key.toString();
}
});
assertEquals(23, adapter.classify("23").intValue());
}
@Test
public void testClassifyWithClassifier() {
adapter.setDelegate(new org.springframework.batch.support.Classifier<String, Integer>() {
public Integer classify(String classifiable) {
return Integer.valueOf(classifiable);
}
});
assertEquals(23, adapter.classify("23").intValue());
}
}

View File

@@ -76,6 +76,26 @@ public class PatternMatcherTests {
assertTrue(PatternMatcher.match("a*c", "abdegc"));
}
@Test
public void testMatchTwoStars() {
assertTrue(PatternMatcher.match("a*d*", "abcdeg"));
}
@Test
public void testMatchPastEnd() {
assertFalse(PatternMatcher.match("a*de", "abcdeg"));
}
@Test
public void testMatchPastEndTwoStars() {
assertTrue(PatternMatcher.match("a*d*g*", "abcdeg"));
}
@Test
public void testMatchStarAtEnd() {
assertTrue(PatternMatcher.match("ab*", "ab"));
}
@Test
public void testMatchStarNo() {
assertFalse(PatternMatcher.match("a*c", "abdeg"));
@@ -83,36 +103,36 @@ public class PatternMatcherTests {
@Test
public void testMatchPrefixSubsumed() {
assertEquals(2, PatternMatcher.match("apple", map).intValue());
assertEquals(2, new PatternMatcher<Integer>(map).match("apple").intValue());
}
@Test
public void testMatchPrefixSubsuming() {
assertEquals(3, PatternMatcher.match("animal", map).intValue());
assertEquals(3, new PatternMatcher<Integer>(map).match("animal").intValue());
}
@Test
public void testMatchPrefixUnrelated() {
assertEquals(4, PatternMatcher.match("biggest", map).intValue());
assertEquals(4, new PatternMatcher<Integer>(map).match("biggest").intValue());
}
@Test(expected = IllegalStateException.class)
public void testMatchPrefix_noMatch() {
PatternMatcher.match("bat", map);
public void testMatchPrefixNoMatch() {
new PatternMatcher<Integer>(map).match("bat");
}
@Test
public void testMatchPrefixDefaultValueUnrelated() {
assertEquals(5, PatternMatcher.match("biggest", defaultMap).intValue());
assertEquals(5, new PatternMatcher<Integer>(defaultMap).match("biggest").intValue());
}
@Test
public void testMatchPrefixDefaultValueEmptyString() {
assertEquals(1, PatternMatcher.match("", defaultMap).intValue());
assertEquals(1, new PatternMatcher<Integer>(defaultMap).match("").intValue());
}
@Test
public void testMatchPrefixDefaultValueNoMatch() {
assertEquals(1, PatternMatcher.match("bat", defaultMap).intValue());
assertEquals(1, new PatternMatcher<Integer>(defaultMap).match("bat").intValue());
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2006-2007 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.support;
import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
/**
* @author Dave Syer
*
*/
public class PatternMatchingClassifierTests {
private PatternMatchingClassifier<String> classifier = new PatternMatchingClassifier<String>();
private Map<String, String> map;
@Before
public void createMap() {
map = new HashMap<String, String>();
map.put("foo", "bar");
map.put("*", "spam");
}
@Test
public void testSetPatternMap() {
classifier.setPatternMap(map);
assertEquals("bar", classifier.classify("foo"));
assertEquals("spam", classifier.classify("bucket"));
}
@Test
public void testCreateFromMap() {
classifier = new PatternMatchingClassifier<String>(map);
assertEquals("bar", classifier.classify("foo"));
assertEquals("spam", classifier.classify("bucket"));
}
}

View File

@@ -0,0 +1,143 @@
/*
* 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.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.Assert;
/**
* @author Lucas Ward
*
*/
public class SimpleMethodInvokerTests {
TestClass testClass;
String value = "foo";
@Before
public void setUp(){
testClass = new TestClass();
}
@Test
public void testMethod() throws Exception{
Method method = TestClass.class.getMethod("before");
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, method);
methodInvoker.invokeMethod(value);
assertTrue(testClass.beforeCalled);
}
@Test
public void testMethodByName() throws Exception{
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, "before", String.class);
methodInvoker.invokeMethod(value);
assertTrue(testClass.beforeCalled);
}
@Test
public void testMethodWithExecution() throws Exception{
Method method = TestClass.class.getMethod("beforeWithArgument", String.class);
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, method);
methodInvoker.invokeMethod(value);
assertTrue(testClass.beforeCalled);
}
@Test
public void testMethodByNameWithExecution() throws Exception{
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, "beforeWithArgument", String.class);
methodInvoker.invokeMethod(value);
assertTrue(testClass.beforeCalled);
}
@Test(expected=IllegalArgumentException.class)
public void testMethodWithTooManyArguments() throws Exception{
Method method = TestClass.class.getMethod("beforeWithTooManyArguments", String.class, int.class);
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, method);
methodInvoker.invokeMethod(value);
assertFalse(testClass.beforeCalled);
}
@Test(expected=IllegalArgumentException.class)
public void testMethodByNameWithTooManyArguments() throws Exception{
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, "beforeWithTooManyArguments", String.class);
methodInvoker.invokeMethod(value);
assertFalse(testClass.beforeCalled);
}
@Test
public void testMethodWithArgument() throws Exception{
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, "argumentTest", Object.class);
methodInvoker.invokeMethod(new Object());
assertTrue(testClass.argumentTestCalled);
}
@Test
public void testEquals() throws Exception{
Method method = TestClass.class.getMethod("beforeWithArgument", String.class);
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, method);
method = TestClass.class.getMethod("beforeWithArgument", String.class);
MethodInvoker methodInvoker2 = new SimpleMethodInvoker(testClass, method);
assertEquals(methodInvoker, methodInvoker2);
}
private class TestClass{
boolean beforeCalled = false;
boolean argumentTestCalled = false;
public void before(){
beforeCalled = true;
}
public void beforeWithArgument(String value){
beforeCalled = true;
}
public void beforeWithTooManyArguments(String value, int someInt){
beforeCalled = true;
}
public void argumentTest(Object object){
Assert.notNull(object);
argumentTestCalled = true;
}
}
}