Moved Spring Web Flow to a top level project

This commit is contained in:
Ben Hale
2006-12-21 16:02:27 +00:00
commit e7ecbbc7e5
1083 changed files with 80775 additions and 0 deletions

View File

@@ -0,0 +1,119 @@
/*
* Copyright 2002-2006 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.binding.collection;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import junit.framework.TestCase;
/**
* Test case for {@link CompositeIterator}.
*
* @author Erwin Vervaet
*/
public class CompositeIteratorTests extends TestCase {
public void testNoIterators() {
CompositeIterator it = new CompositeIterator();
assertFalse(it.hasNext());
try {
it.next();
fail();
}
catch (NoSuchElementException e) {
// expected
}
}
public void testSingleIterator() {
CompositeIterator it = new CompositeIterator();
it.add(Arrays.asList(new String[] { "0", "1" }).iterator());
for (int i = 0; i < 2; i++) {
assertTrue(it.hasNext());
assertEquals(String.valueOf(i), it.next());
}
assertFalse(it.hasNext());
try {
it.next();
fail();
}
catch (NoSuchElementException e) {
// expected
}
}
public void testMultipleIterators() {
CompositeIterator it = new CompositeIterator();
it.add(Arrays.asList(new String[] { "0", "1" }).iterator());
it.add(Arrays.asList(new String[] { "2" }).iterator());
it.add(Arrays.asList(new String[] { "3", "4" }).iterator());
for (int i = 0; i < 5; i++) {
assertTrue(it.hasNext());
assertEquals(String.valueOf(i), it.next());
}
assertFalse(it.hasNext());
try {
it.next();
fail();
}
catch (NoSuchElementException e) {
// expected
}
}
public void testInUse() {
List list = Arrays.asList(new String[] { "0", "1" });
CompositeIterator it = new CompositeIterator();
it.add(list.iterator());
it.hasNext();
try {
it.add(list.iterator());
fail();
}
catch (IllegalStateException e) {
// expected
}
it = new CompositeIterator();
it.add(list.iterator());
it.next();
try {
it.add(list.iterator());
fail();
}
catch (IllegalStateException e) {
// expected
}
}
public void testDuplicateIterators() {
List list = Arrays.asList(new String[] { "0", "1" });
Iterator iterator = list.iterator();
CompositeIterator it = new CompositeIterator();
it.add(iterator);
it.add(list.iterator());
try {
it.add(iterator);
fail();
}
catch (IllegalArgumentException e) {
// expected
}
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2002-2006 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.binding.collection;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import junit.framework.TestCase;
/**
* Unit tests for {@link org.springframework.binding.collection.SharedMapDecorator}.
*/
public class SharedMapDecoratorTests extends TestCase {
private SharedMapDecorator map = new SharedMapDecorator(new HashMap());
public void testGetPutRemove() {
assertTrue(map.size() == 0);
assertTrue(map.isEmpty());
assertNull(map.get("foo"));
assertFalse(map.containsKey("foo"));
map.put("foo", "bar");
assertTrue(map.size() == 1);
assertFalse(map.isEmpty());
assertNotNull(map.get("foo"));
assertTrue(map.containsKey("foo"));
assertTrue(map.containsValue("bar"));
assertEquals("bar", map.get("foo"));
map.remove("foo");
assertTrue(map.size() == 0);
assertNull(map.get("foo"));
}
public void testPutAll() {
Map all = new HashMap();
all.put("foo", "bar");
all.put("bar", "baz");
map.putAll(all);
assertTrue(map.size() == 2);
}
public void testEntrySet() {
map.put("foo", "bar");
map.put("bar", "baz");
Set entrySet = map.entrySet();
assertTrue(entrySet.size() == 2);
}
public void testKeySet() {
map.put("foo", "bar");
map.put("bar", "baz");
Set keySet = map.keySet();
assertTrue(keySet.size() == 2);
}
public void testValues() {
map.put("foo", "bar");
map.put("bar", "baz");
Collection values = map.values();
assertTrue(values.size() == 2);
}
}

View File

@@ -0,0 +1,97 @@
/*
* Copyright 2002-2006 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.binding.collection;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import junit.framework.TestCase;
/**
* Unit tests for {@link org.springframework.binding.collection.StringKeyedMapAdapter}.
*/
public class StringKeyedMapAdapterTests extends TestCase {
private Map contents = new HashMap();
private StringKeyedMapAdapter map = new StringKeyedMapAdapter() {
protected Object getAttribute(String key) {
return contents.get(key);
}
protected Iterator getAttributeNames() {
return contents.keySet().iterator();
}
protected void removeAttribute(String key) {
contents.remove(key);
}
protected void setAttribute(String key, Object value) {
contents.put(key, value);
}
};
public void testGetPutRemove() {
assertTrue(map.size() == 0);
assertTrue(map.isEmpty());
assertNull(map.get("foo"));
assertFalse(map.containsKey("foo"));
map.put("foo", "bar");
assertTrue(map.size() == 1);
assertFalse(map.isEmpty());
assertNotNull(map.get("foo"));
assertTrue(map.containsKey("foo"));
assertTrue(map.containsValue("bar"));
assertEquals("bar", map.get("foo"));
map.remove("foo");
assertTrue(map.size() == 0);
assertNull(map.get("foo"));
}
public void testPutAll() {
Map all = new HashMap();
all.put("foo", "bar");
all.put("bar", "baz");
map.putAll(all);
assertTrue(map.size() == 2);
}
public void testEntrySet() {
map.put("foo", "bar");
map.put("bar", "baz");
Set entrySet = map.entrySet();
assertTrue(entrySet.size() == 2);
}
public void testKeySet() {
map.put("foo", "bar");
map.put("bar", "baz");
Set keySet = map.keySet();
assertTrue(keySet.size() == 2);
}
public void testValues() {
map.put("foo", "bar");
map.put("bar", "baz");
Collection values = map.values();
assertTrue(values.size() == 2);
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2002-2006 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.binding.convert.support;
import java.util.Date;
import junit.framework.TestCase;
import org.springframework.binding.convert.ConversionException;
import org.springframework.binding.convert.ConversionExecutor;
import org.springframework.binding.convert.ConversionService;
/**
* Test case for the {@link CompositeConversionService}.
*
* @author Erwin Vervaet
*/
public class CompositeConversionServiceTests extends TestCase {
private CompositeConversionService service;
protected void setUp() throws Exception {
GenericConversionService first = new GenericConversionService();
first.addConverter(new TextToClass());
first.addConverter(new TextToBoolean("ja", "nee"));
first.addDefaultAlias(Boolean.class);
GenericConversionService second = new GenericConversionService();
second.addConverter(new TextToNumber());
second.addConverter(new TextToBoolean());
second.addDefaultAlias(Integer.class);
service = new CompositeConversionService(new ConversionService[] { first, second });
}
public void testGetConversionExecutor() {
assertNotNull(service.getConversionExecutor(String.class, Class.class));
assertNotNull(service.getConversionExecutor(String.class, Boolean.class));
assertEquals(Boolean.TRUE, service.getConversionExecutor(String.class, Boolean.class).execute("ja"));
assertNotNull(service.getConversionExecutor(String.class, Integer.class));
try {
service.getConversionExecutor(String.class, Date.class);
fail();
}
catch (ConversionException e) {
// expected
}
}
public void testGetConversionExecutorByTargetAlias() {
assertNotNull(service.getConversionExecutorByTargetAlias(String.class, "boolean"));
assertEquals(Boolean.TRUE, service.getConversionExecutorByTargetAlias(String.class, "boolean").execute("ja"));
assertNotNull(service.getConversionExecutorByTargetAlias(String.class, "integer"));
assertNull(service.getConversionExecutorByTargetAlias(String.class, "class"));
}
public void testGetConversionExecutorsForSource() {
assertEquals(
new TextToClass().getTargetClasses().length +
new TextToBoolean().getTargetClasses().length +
new TextToNumber().getTargetClasses().length,
service.getConversionExecutorsForSource(String.class).length);
assertEquals(0, service.getConversionExecutorsForSource(Date.class).length);
ConversionExecutor[] fromStringConversionExecutors = service.getConversionExecutorsForSource(String.class);
ConversionExecutor booleanConversionExecutor = null;
for (int i = 0; i < fromStringConversionExecutors.length; i++) {
if (fromStringConversionExecutors[i].getConverter() instanceof TextToBoolean) {
booleanConversionExecutor = fromStringConversionExecutors[i];
}
}
assertEquals(Boolean.TRUE, booleanConversionExecutor.execute("ja"));
}
public void testGetClassByAlias() {
assertEquals(Boolean.class, service.getClassByAlias("boolean"));
assertEquals(Integer.class, service.getClassByAlias("integer"));
assertNull(service.getClassByAlias("class"));
}
}

View File

@@ -0,0 +1,100 @@
/*
* Copyright 2002-2006 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.binding.convert.support;
import java.util.HashMap;
import junit.framework.TestCase;
import org.springframework.binding.convert.ConversionException;
import org.springframework.binding.convert.ConversionExecutor;
import org.springframework.binding.convert.Converter;
import org.springframework.core.enums.ShortCodedLabeledEnum;
/**
* Test case for the default conversion service.
*
* @author Keith Donald
*/
public class DefaultConversionServiceTests extends TestCase {
public void testOverrideConverter() {
Converter customConverter = new TextToBoolean("ja", "nee");
DefaultConversionService service = new DefaultConversionService();
ConversionExecutor executor = service.getConversionExecutor(String.class, Boolean.class);
assertNotSame(customConverter, executor.getConverter());
try {
executor.execute("ja");
fail();
}
catch (ConversionException e) {
// expected
}
service.addConverter(customConverter);
executor = service.getConversionExecutor(String.class, Boolean.class);
assertSame(customConverter, executor.getConverter());
assertTrue(((Boolean)executor.execute("ja")).booleanValue());
}
public void testTargetClassNotSupported() {
DefaultConversionService service = new DefaultConversionService();
try {
service.getConversionExecutor(String.class, HashMap.class);
fail("Should have thrown an exception");
}
catch (ConversionException e) {
}
}
public void testValidConversion() {
DefaultConversionService service = new DefaultConversionService();
ConversionExecutor executor = service.getConversionExecutor(String.class, Integer.class);
Integer three = (Integer)executor.execute("3");
assertEquals(3, three.intValue());
}
public void testLabeledEnumConversionNoSuchEnum() {
DefaultConversionService service = new DefaultConversionService();
ConversionExecutor executor = service.getConversionExecutor(String.class, MyEnum.class);
try {
executor.execute("My Invalid Label");
fail("Should have failed");
}
catch (ConversionException e) {
}
}
public void testValidLabeledEnumConversion() {
DefaultConversionService service = new DefaultConversionService();
ConversionExecutor executor = service.getConversionExecutor(String.class, MyEnum.class);
MyEnum myEnum = (MyEnum)executor.execute("My Label 1");
assertEquals(MyEnum.ONE, myEnum);
}
public static class MyEnum extends ShortCodedLabeledEnum {
public static MyEnum ONE = new MyEnum(0, "My Label 1");
public static MyEnum TWO = new MyEnum(1, "My Label 2");
private MyEnum(int code, String label) {
super(code, label);
}
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2002-2006 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.binding.expression.support;
import java.util.ArrayList;
import junit.framework.TestCase;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.ExpressionParser;
/**
* Unit tests for {@link org.springframework.binding.expression.support.CollectionAddingExpression}.
*/
public class CollectionAddingExpressionTests extends TestCase {
ExpressionParser parser = new BeanWrapperExpressionParser();
TestBean bean = new TestBean();
Expression exp = parser.parseExpression("list");
public void testEvaluation() {
ArrayList list = new ArrayList();
bean.setList(list);
CollectionAddingExpression colExp = new CollectionAddingExpression(exp);
assertSame(list, colExp.evaluate(bean, null));
}
public void testAddToCollection() {
CollectionAddingExpression colExp = new CollectionAddingExpression(exp);
colExp.evaluateToSet(bean, "1", null);
colExp.evaluateToSet(bean, "2", null);
assertEquals("1", bean.getList().get(0));
assertEquals("2", bean.getList().get(1));
}
public void testNotACollection() {
Expression exp = parser.parseExpression("flag");
CollectionAddingExpression colExp = new CollectionAddingExpression(exp);
try {
colExp.evaluateToSet(bean, "1", null);
fail("not a collection");
}
catch (IllegalArgumentException e) {
}
}
public void testNoAddOnNullValue() {
CollectionAddingExpression colExp = new CollectionAddingExpression(exp);
colExp.evaluateToSet(bean, null, null);
colExp.evaluateToSet(bean, "2", null);
assertEquals("2", bean.getList().get(0));
}
}

View File

@@ -0,0 +1,110 @@
/*
* Copyright 2002-2006 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.binding.expression.support;
import junit.framework.TestCase;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.ParserException;
/**
* Unit tests for {@link org.springframework.binding.expression.support.OgnlExpressionParser}.
*/
public class OgnlExpressionParserTests extends TestCase {
private OgnlExpressionParser parser = new OgnlExpressionParser();
private TestBean bean = new TestBean();
public void testParseSimpleDelimited() {
String exp = "${flag}";
Expression e = parser.parseExpression(exp);
assertNotNull(e);
Boolean b = (Boolean)e.evaluate(bean, null);
assertFalse(b.booleanValue());
}
public void testParseSimple() {
String exp = "flag";
Expression e = parser.parseExpression(exp);
assertNotNull(e);
Boolean b = (Boolean)e.evaluate(bean, null);
assertFalse(b.booleanValue());
}
public void testParseNull() {
Expression e = parser.parseExpression(null);
assertNotNull(e);
assertNull(e.evaluate(bean, null));
}
public void testParseEmpty() {
Expression e = parser.parseExpression("");
assertNotNull(e);
assertEquals("", e.evaluate(bean, null));
}
public void testParseComposite() {
String exp = "hello ${flag} ${flag} ${flag}";
Expression e = parser.parseExpression(exp);
assertNotNull(e);
String str = (String)e.evaluate(bean, null);
assertEquals("hello false false false", str);
}
public void testEnclosedCompositeNotSupported() {
String exp = "${hello ${flag} ${flag} ${flag}}";
try {
parser.parseExpression(exp);
fail("Should've failed - not intended use");
}
catch (ParserException e) {
}
}
public void testSyntaxError1() {
try {
String exp = "hello ${flag} ${abcd defg";
parser.parseExpression(exp);
fail("Should've failed - not intended use");
}
catch (ParserException e) {
}
}
public void testSyntaxError2() {
try {
String exp = "hello ${flag} ${}";
parser.parseExpression(exp);
fail("Should've failed - not intended use");
}
catch (ParserException e) {
}
}
public void testIsDelimitedExpression() {
assertTrue(parser.isDelimitedExpression("${foo}"));
assertTrue(parser.isDelimitedExpression("${foo ${foo}}"));
assertTrue(parser.isDelimitedExpression("foo ${bar}"));
}
public void testIsNotDelimitedExpression() {
assertFalse(parser.isDelimitedExpression("foo"));
assertFalse(parser.isDelimitedExpression("foo ${"));
assertFalse(parser.isDelimitedExpression("$foo}"));
assertFalse(parser.isDelimitedExpression("foo ${}"));
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2002-2006 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.binding.expression.support;
import java.util.ArrayList;
import java.util.List;
public class TestBean {
private boolean flag;
private List list = new ArrayList();
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2002-2006 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.binding.format.support;
import java.math.BigDecimal;
import java.util.Locale;
import org.springframework.binding.format.Formatter;
import junit.framework.TestCase;
/**
* Unit tests for {@link NumberFormatterTests}.
*
* @author Erwin Vervaet
*/
public class NumberFormatterTests extends TestCase {
private Locale systemDefaultLocale;
protected void setUp() throws Exception {
systemDefaultLocale = Locale.getDefault();
}
protected void tearDown() throws Exception {
// restore default
Locale.setDefault(systemDefaultLocale);
}
public void testParseBigDecimalInUs() {
Locale.setDefault(Locale.US);
Formatter formatter = new SimpleFormatterFactory().getNumberFormatter(BigDecimal.class);
assertEquals(new BigDecimal("123.45"), formatter.parseValue("123.45", BigDecimal.class));
}
public void testParseBigDecimalInGermany() {
Locale.setDefault(Locale.GERMANY);
Formatter formatter = new SimpleFormatterFactory().getNumberFormatter(BigDecimal.class);
assertEquals(new BigDecimal("123.45"), formatter.parseValue("123.45", BigDecimal.class));
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2002-2006 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.binding.mapping;
import java.util.HashMap;
import junit.framework.TestCase;
import org.springframework.binding.expression.support.OgnlExpressionParser;
/**
* Unit tests for the {@link org.springframework.binding.mapping.RequiredMapping}.
*/
public class RequiredMappingTests extends TestCase {
public void testRequired() {
MappingBuilder builder = new MappingBuilder(new OgnlExpressionParser());
Mapping mapping = builder.source("foo").target("bar").required().value();
HashMap source = new HashMap();
source.put("foo", "baz");
HashMap target = new HashMap();
mapping.map(source, target, null);
assertEquals("baz", target.get("bar"));
}
public void testRequiredExceptionOnNull() {
MappingBuilder builder = new MappingBuilder(new OgnlExpressionParser());
Mapping mapping = builder.source("foo").target("bar").required().value();
HashMap source = new HashMap();
source.put("foo", null);
HashMap target = new HashMap();
try {
mapping.map(source, target, null);
}
catch (RequiredMappingException e) {
}
}
public void testRequiredExceptionOnNoKey() {
MappingBuilder builder = new MappingBuilder(new OgnlExpressionParser());
Mapping mapping = builder.source("foo").target("bar").required().value();
HashMap source = new HashMap();
HashMap target = new HashMap();
try {
mapping.map(source, target, null);
}
catch (RequiredMappingException e) {
}
}
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright 2002-2006 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.binding.method;
import junit.framework.TestCase;
import org.springframework.binding.expression.support.StaticExpression;
/**
* Unit tests for {@link org.springframework.binding.method.MethodInvoker}.
*
* @author Erwin Vervaet
*/
public class MethodInvokerTests extends TestCase {
private MethodInvoker methodInvoker;
protected void setUp() throws Exception {
this.methodInvoker = new MethodInvoker();
}
public void testInvocationTargetException() {
try {
methodInvoker.invoke(new MethodSignature("test"), new TestObject(), null);
fail();
}
catch (MethodInvocationException e) {
assertTrue(e.getTargetException() instanceof IllegalArgumentException);
assertEquals("just testing", e.getTargetException().getMessage());
}
}
public void testInvalidMethod() {
try {
methodInvoker.invoke(new MethodSignature("bogus"), new TestObject(), null);
fail();
}
catch (MethodInvocationException e) {
assertTrue(e.getTargetException() instanceof InvalidMethodKeyException);
}
}
public void testBeanArg() {
Parameters parameters = new Parameters();
Bean bean = new Bean();
parameters.add(new Parameter(Bean.class, new StaticExpression(bean)));
MethodSignature method = new MethodSignature("testBeanArg", parameters);
assertSame(bean, methodInvoker.invoke(method, new TestObject(), null));
}
public void testPrimitiveArg() {
Parameters parameters = new Parameters();
parameters.add(new Parameter(Boolean.class, new StaticExpression(Boolean.TRUE)));
MethodSignature method = new MethodSignature("testPrimitiveArg", parameters);
assertEquals(Boolean.TRUE, methodInvoker.invoke(method, new TestObject(), null));
}
private static class TestObject {
public void test() {
throw new IllegalArgumentException("just testing");
}
public Object testBeanArg(Bean bean) {
return bean;
}
public boolean testPrimitiveArg(boolean primitive) {
return primitive;
}
}
private static class Bean {
String value;
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2002-2006 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.binding.method;
import java.io.File;
import java.io.FilenameFilter;
import java.lang.reflect.Method;
import junit.framework.TestCase;
/**
* @author Rob Harrop
* @since 1.0
*/
public class MethodKeyTests extends TestCase {
private static final Method LIST_NO_ARGS = safeGetMethod(File.class, "list", null);
private static final Method LIST_FILENAME_FILTER = safeGetMethod(File.class, "list",
new Class[] { FilenameFilter.class });
public void testGetMethodWithNoArgs() throws Exception {
MethodKey key = new MethodKey(File.class, "list", new Class[0]);
Method m = key.getMethod();
assertEquals(LIST_NO_ARGS, m);
}
public void testGetMoreGenericMethod() throws Exception {
MethodKey key = new MethodKey(Object.class, "equals", new Class[] { Long.class });
assertEquals(safeGetMethod(Object.class, "equals", new Class[] { Object.class }), key.getMethod());
}
public void testGetMethodWithSingleArg() throws Exception {
MethodKey key = new MethodKey(File.class, "list", new Class[] { FilenameFilter.class });
Method m = key.getMethod();
assertEquals(LIST_FILENAME_FILTER, m);
}
public void testGetMethodWithSingleNullArgAndValidMatch() throws Exception {
MethodKey key = new MethodKey(File.class, "list", new Class[] { null });
Method m = key.getMethod();
assertEquals(LIST_FILENAME_FILTER, m);
}
public void testGetMethodWithSingleNullAndUnclearMatch() throws Exception {
new MethodKey(File.class, "listFiles", new Class[] { null });
}
private static final Method safeGetMethod(Class type, String name, Class[] argTypes) {
try {
return type.getMethod(name, argTypes);
}
catch (NoSuchMethodException e) {
throw new IllegalStateException("Unable to safely access a known method via reflection. " + e.getMessage());
}
}
}