Moved tests over from testsuite to core

This commit is contained in:
Arjen Poutsma
2008-10-29 17:10:09 +00:00
parent 6849cbd5ba
commit 6b80dbb4a9
3 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
/*
* 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.core;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import junit.framework.TestCase;
import org.springframework.beans.TestBean;
/**
* @author Rob Harrop
*/
public class ConventionsTests extends TestCase {
public void testSimpleObject() {
TestBean testBean = new TestBean();
assertEquals("Incorrect singular variable name", "testBean", Conventions.getVariableName(testBean));
}
public void testArray() {
TestBean[] testBeans = new TestBean[0];
assertEquals("Incorrect plural array form", "testBeanList", Conventions.getVariableName(testBeans));
}
public void testCollections() {
List list = new ArrayList();
list.add(new TestBean());
assertEquals("Incorrect plural List form", "testBeanList", Conventions.getVariableName(list));
Set set = new HashSet();
set.add(new TestBean());
assertEquals("Incorrect plural Set form", "testBeanList", Conventions.getVariableName(set));
List emptyList = new ArrayList();
try {
Conventions.getVariableName(emptyList);
fail("Should not be able to generate name for empty collection");
}
catch(IllegalArgumentException ex) {
// success
}
}
public void testAttributeNameToPropertyName() throws Exception {
assertEquals("transactionManager", Conventions.attributeNameToPropertyName("transaction-manager"));
assertEquals("pointcutRef", Conventions.attributeNameToPropertyName("pointcut-ref"));
assertEquals("lookupOnStartup", Conventions.attributeNameToPropertyName("lookup-on-startup"));
}
public void testGetQualifiedAttributeName() throws Exception {
String baseName = "foo";
Class cls = String.class;
String desiredResult = "java.lang.String.foo";
assertEquals(desiredResult, Conventions.getQualifiedAttributeName(cls, baseName));
}
}

View File

@@ -0,0 +1,201 @@
/*
* 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.core;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import junit.framework.TestCase;
import org.springframework.beans.TestBean;
/**
* @author Adrian Colyer
*/
public class LocalVariableTableParameterNameDiscovererTests extends TestCase {
private LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
public void testMethodParameterNameDiscoveryNoArgs() throws NoSuchMethodException {
Method getName = TestBean.class.getMethod("getName", new Class[0]);
String[] names = discoverer.getParameterNames(getName);
assertNotNull("should find method info", names);
assertEquals("no argument names", 0, names.length);
}
public void testMethodParameterNameDiscoveryWithArgs() throws NoSuchMethodException {
Method setName = TestBean.class.getMethod("setName", new Class[]{String.class});
String[] names = discoverer.getParameterNames(setName);
assertNotNull("should find method info", names);
assertEquals("one argument", 1, names.length);
assertEquals("name", names[0]);
}
public void testConsParameterNameDiscoveryNoArgs() throws NoSuchMethodException {
Constructor noArgsCons = TestBean.class.getConstructor(new Class[0]);
String[] names = discoverer.getParameterNames(noArgsCons);
assertNotNull("should find cons info", names);
assertEquals("no argument names", 0, names.length);
}
public void testConsParameterNameDiscoveryArgs() throws NoSuchMethodException {
Constructor twoArgCons = TestBean.class.getConstructor(new Class[]{String.class, int.class});
String[] names = discoverer.getParameterNames(twoArgCons);
assertNotNull("should find cons info", names);
assertEquals("one argument", 2, names.length);
assertEquals("name", names[0]);
assertEquals("age", names[1]);
}
public void testStaticMethodParameterNameDiscoveryNoArgs() throws NoSuchMethodException {
Method m = getClass().getMethod("staticMethodNoLocalVars", new Class[0]);
String[] names = discoverer.getParameterNames(m);
assertNotNull("should find method info", names);
assertEquals("no argument names", 0, names.length);
}
public void testOverloadedStaticMethod() throws Exception {
Class clazz = this.getClass();
Method m1 = clazz.getMethod("staticMethod", new Class[]{Long.TYPE, Long.TYPE});
String[] names = discoverer.getParameterNames(m1);
assertNotNull("should find method info", names);
assertEquals("two arguments", 2, names.length);
assertEquals("x", names[0]);
assertEquals("y", names[1]);
Method m2 = clazz.getMethod("staticMethod", new Class[]{Long.TYPE, Long.TYPE, Long.TYPE});
names = discoverer.getParameterNames(m2);
assertNotNull("should find method info", names);
assertEquals("three arguments", 3, names.length);
assertEquals("x", names[0]);
assertEquals("y", names[1]);
assertEquals("z", names[2]);
}
public void testOverloadedStaticMethodInInnerClass() throws Exception {
Class clazz = InnerClass.class;
Method m1 = clazz.getMethod("staticMethod", new Class[]{Long.TYPE});
String[] names = discoverer.getParameterNames(m1);
assertNotNull("should find method info", names);
assertEquals("one argument", 1, names.length);
assertEquals("x", names[0]);
Method m2 = clazz.getMethod("staticMethod", new Class[]{Long.TYPE, Long.TYPE});
names = discoverer.getParameterNames(m2);
assertNotNull("should find method info", names);
assertEquals("two arguments", 2, names.length);
assertEquals("x", names[0]);
assertEquals("y", names[1]);
}
public void testOverloadedMethod() throws Exception {
Class clazz = this.getClass();
Method m1 = clazz.getMethod("instanceMethod", new Class[]{Double.TYPE, Double.TYPE});
String[] names = discoverer.getParameterNames(m1);
assertNotNull("should find method info", names);
assertEquals("two arguments", 2, names.length);
assertEquals("x", names[0]);
assertEquals("y", names[1]);
Method m2 = clazz.getMethod("instanceMethod", new Class[]{Double.TYPE, Double.TYPE, Double.TYPE});
names = discoverer.getParameterNames(m2);
assertNotNull("should find method info", names);
assertEquals("three arguments", 3, names.length);
assertEquals("x", names[0]);
assertEquals("y", names[1]);
assertEquals("z", names[2]);
}
public void testOverloadedMethodInInnerClass() throws Exception {
Class clazz = InnerClass.class;
Method m1 = clazz.getMethod("instanceMethod", new Class[]{String.class});
String[] names = discoverer.getParameterNames(m1);
assertNotNull("should find method info", names);
assertEquals("one argument", 1, names.length);
assertEquals("aa", names[0]);
Method m2 = clazz.getMethod("instanceMethod", new Class[]{String.class, String.class});
names = discoverer.getParameterNames(m2);
assertNotNull("should find method info", names);
assertEquals("two arguments", 2, names.length);
assertEquals("aa", names[0]);
assertEquals("bb", names[1]);
}
public static void staticMethodNoLocalVars() {
}
public static long staticMethod(long x, long y) {
long u = x * y;
return u;
}
public static long staticMethod(long x, long y, long z) {
long u = x * y * z;
return u;
}
public double instanceMethod(double x, double y) {
double u = x * y;
return u;
}
public double instanceMethod(double x, double y, double z) {
double u = x * y * z;
return u;
}
public static class InnerClass {
public int waz = 0;
public InnerClass() {
}
public InnerClass(String firstArg, long secondArg, Object thirdArg) {
long foo = 0;
short bar = 10;
this.waz = (int) (foo + bar);
}
public String instanceMethod(String aa) {
return aa;
}
public String instanceMethod(String aa, String bb) {
return aa + bb;
}
public static long staticMethod(long x) {
long u = x;
return u;
}
public static long staticMethod(long x, long y) {
long u = x * y;
return u;
}
}
}

View File

@@ -0,0 +1,84 @@
/*
* 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.core;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Arrays;
import junit.framework.TestCase;
import org.springframework.beans.TestBean;
public class PrioritizedParameterNameDiscovererTests extends TestCase {
private static final String[] FOO_BAR = new String[] { "foo", "bar" };
private static final String[] SOMETHING_ELSE = new String[] { "something", "else" };
ParameterNameDiscoverer returnsFooBar = new ParameterNameDiscoverer() {
public String[] getParameterNames(Method m) {
return FOO_BAR;
}
public String[] getParameterNames(Constructor ctor) {
return FOO_BAR;
}
};
ParameterNameDiscoverer returnsSomethingElse = new ParameterNameDiscoverer() {
public String[] getParameterNames(Method m) {
return SOMETHING_ELSE;
}
public String[] getParameterNames(Constructor ctor) {
return SOMETHING_ELSE;
}
};
private final Method anyMethod;
private final Class anyClass = Object.class;
public PrioritizedParameterNameDiscovererTests() throws SecurityException, NoSuchMethodException {
anyMethod = TestBean.class.getMethod("getAge", (Class[]) null);
}
public void testNoParametersDiscoverers() {
ParameterNameDiscoverer pnd = new PrioritizedParameterNameDiscoverer();
assertNull(pnd.getParameterNames(anyMethod));
assertNull(pnd.getParameterNames((Constructor) null));
}
public void testOrderedParameterDiscoverers1() {
PrioritizedParameterNameDiscoverer pnd = new PrioritizedParameterNameDiscoverer();
pnd.addDiscoverer(returnsFooBar);
assertTrue(Arrays.equals(FOO_BAR, pnd.getParameterNames(anyMethod)));
assertTrue(Arrays.equals(FOO_BAR, pnd.getParameterNames((Constructor) null)));
pnd.addDiscoverer(returnsSomethingElse);
assertTrue(Arrays.equals(FOO_BAR, pnd.getParameterNames(anyMethod)));
assertTrue(Arrays.equals(FOO_BAR, pnd.getParameterNames((Constructor) null)));
}
public void testOrderedParameterDiscoverers2() {
PrioritizedParameterNameDiscoverer pnd = new PrioritizedParameterNameDiscoverer();
pnd.addDiscoverer(returnsSomethingElse);
assertTrue(Arrays.equals(SOMETHING_ELSE, pnd.getParameterNames(anyMethod)));
assertTrue(Arrays.equals(SOMETHING_ELSE, pnd.getParameterNames((Constructor) null)));
pnd.addDiscoverer(returnsFooBar);
assertTrue(Arrays.equals(SOMETHING_ELSE, pnd.getParameterNames(anyMethod)));
assertTrue(Arrays.equals(SOMETHING_ELSE, pnd.getParameterNames((Constructor) null)));
}
}