Moved tests from testsuite to core

This commit is contained in:
Arjen Poutsma
2008-11-03 10:34:10 +00:00
parent 7ecdf96db5
commit 88efc06a23
13 changed files with 469 additions and 7 deletions

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2002-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.beans;
/**
* @author Juergen Hoeller
*/
public enum CustomEnum {
VALUE_1, VALUE_2;
public String toString() {
return "CustomEnum: " + name();
}
}

View File

@@ -0,0 +1,237 @@
/*
* 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.beans;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.core.io.Resource;
/**
* @author Juergen Hoeller
*/
public class GenericBean<T> {
private Set<Integer> integerSet;
private List<Resource> resourceList;
private List<List<Integer>> listOfLists;
private ArrayList<String[]> listOfArrays;
private List<Map<Integer, Long>> listOfMaps;
private Map plainMap;
private Map<Short, Integer> shortMap;
private HashMap<Long, ?> longMap;
private Map<Number, Collection<? extends Object>> collectionMap;
private Map<String, Map<Integer, Long>> mapOfMaps;
private Map<Integer, List<Integer>> mapOfLists;
private CustomEnum customEnum;
private T genericProperty;
private List<T> genericListProperty;
public GenericBean() {
}
public GenericBean(Set<Integer> integerSet) {
this.integerSet = integerSet;
}
public GenericBean(Set<Integer> integerSet, List<Resource> resourceList) {
this.integerSet = integerSet;
this.resourceList = resourceList;
}
public GenericBean(HashSet<Integer> integerSet, Map<Short, Integer> shortMap) {
this.integerSet = integerSet;
this.shortMap = shortMap;
}
public GenericBean(Map<Short, Integer> shortMap, Resource resource) {
this.shortMap = shortMap;
this.resourceList = Collections.singletonList(resource);
}
public GenericBean(Map plainMap, Map<Short, Integer> shortMap) {
this.plainMap = plainMap;
this.shortMap = shortMap;
}
public GenericBean(HashMap<Long, ?> longMap) {
this.longMap = longMap;
}
public GenericBean(boolean someFlag, Map<Number, Collection<? extends Object>> collectionMap) {
this.collectionMap = collectionMap;
}
public Set<Integer> getIntegerSet() {
return integerSet;
}
public void setIntegerSet(Set<Integer> integerSet) {
this.integerSet = integerSet;
}
public List<Resource> getResourceList() {
return resourceList;
}
public void setResourceList(List<Resource> resourceList) {
this.resourceList = resourceList;
}
public List<List<Integer>> getListOfLists() {
return listOfLists;
}
public ArrayList<String[]> getListOfArrays() {
return listOfArrays;
}
public void setListOfArrays(ArrayList<String[]> listOfArrays) {
this.listOfArrays = listOfArrays;
}
public void setListOfLists(List<List<Integer>> listOfLists) {
this.listOfLists = listOfLists;
}
public List<Map<Integer, Long>> getListOfMaps() {
return listOfMaps;
}
public void setListOfMaps(List<Map<Integer, Long>> listOfMaps) {
this.listOfMaps = listOfMaps;
}
public Map getPlainMap() {
return plainMap;
}
public Map<Short, Integer> getShortMap() {
return shortMap;
}
public void setShortMap(Map<Short, Integer> shortMap) {
this.shortMap = shortMap;
}
public HashMap<Long, ?> getLongMap() {
return longMap;
}
public void setLongMap(HashMap<Long, ?> longMap) {
this.longMap = longMap;
}
public Map<Number, Collection<? extends Object>> getCollectionMap() {
return collectionMap;
}
public void setCollectionMap(Map<Number, Collection<? extends Object>> collectionMap) {
this.collectionMap = collectionMap;
}
public Map<String, Map<Integer, Long>> getMapOfMaps() {
return mapOfMaps;
}
public void setMapOfMaps(Map<String, Map<Integer, Long>> mapOfMaps) {
this.mapOfMaps = mapOfMaps;
}
public Map<Integer, List<Integer>> getMapOfLists() {
return mapOfLists;
}
public void setMapOfLists(Map<Integer, List<Integer>> mapOfLists) {
this.mapOfLists = mapOfLists;
}
public T getGenericProperty() {
return genericProperty;
}
public void setGenericProperty(T genericProperty) {
this.genericProperty = genericProperty;
}
public List<T> getGenericListProperty() {
return genericListProperty;
}
public void setGenericListProperty(List<T> genericListProperty) {
this.genericListProperty = genericListProperty;
}
public CustomEnum getCustomEnum() {
return customEnum;
}
public void setCustomEnum(CustomEnum customEnum) {
this.customEnum = customEnum;
}
public static GenericBean createInstance(Set<Integer> integerSet) {
return new GenericBean(integerSet);
}
public static GenericBean createInstance(Set<Integer> integerSet, List<Resource> resourceList) {
return new GenericBean(integerSet, resourceList);
}
public static GenericBean createInstance(HashSet<Integer> integerSet, Map<Short, Integer> shortMap) {
return new GenericBean(integerSet, shortMap);
}
public static GenericBean createInstance(Map<Short, Integer> shortMap, Resource resource) {
return new GenericBean(shortMap, resource);
}
public static GenericBean createInstance(Map map, Map<Short, Integer> shortMap) {
return new GenericBean(map, shortMap);
}
public static GenericBean createInstance(HashMap<Long, ?> longMap) {
return new GenericBean(longMap);
}
public static GenericBean createInstance(boolean someFlag, Map<Number, Collection<? extends Object>> collectionMap) {
return new GenericBean(someFlag, collectionMap);
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2002-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.context.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Scope {
/**
* Specifies the scope to use for instances of the annotated class.
* @return the desired scope
*/
public abstract String value() default "singleton";
}

View File

@@ -0,0 +1,51 @@
/*
* 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.Method;
import java.lang.reflect.Type;
import junit.framework.TestCase;
/**
* @author Serge Bogatyrjov
*/
public abstract class AbstractGenericsTests extends TestCase {
protected Class<?> targetClass;
protected String methods[];
protected Type expectedResults[];
protected void executeTest() throws NoSuchMethodException {
String methodName = getName().substring(4);
methodName = methodName.substring(0, 1).toLowerCase() + methodName.substring(1);
for (int i = 0; i < this.methods.length; i++) {
if (methodName.equals(this.methods[i])) {
Method method = this.targetClass.getMethod(methodName);
Type type = getType(method);
assertEquals(this.expectedResults[i], type);
return;
}
}
throw new IllegalStateException("Bad test data");
}
protected abstract Type getType(Method method);
}

View File

@@ -0,0 +1,138 @@
/*
* 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.Method;
import java.lang.reflect.Type;
import java.util.AbstractMap;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import junit.framework.Assert;
import org.springframework.beans.GenericBean;
import org.springframework.core.io.Resource;
/**
* @author Serge Bogatyrjov
* @author Juergen Hoeller
*/
public class GenericCollectionTypeResolverTests extends AbstractGenericsTests {
protected void setUp() throws Exception {
this.targetClass = Foo.class;
this.methods = new String[] {"a", "b", "b2", "b3", "c", "d", "d2", "d3", "e", "e2", "e3"};
this.expectedResults = new Class[] {
Integer.class, null, null, Set.class, null, Integer.class,
Integer.class, Integer.class, Integer.class, Integer.class, Integer.class};
}
protected Type getType(Method method) {
return GenericCollectionTypeResolver.getMapValueReturnType(method);
}
public void testA() throws Exception {
executeTest();
}
public void testB() throws Exception {
executeTest();
}
public void testB2() throws Exception {
executeTest();
}
public void testB3() throws Exception {
executeTest();
}
public void testC() throws Exception {
executeTest();
}
public void testD() throws Exception {
executeTest();
}
public void testD2() throws Exception {
executeTest();
}
public void testD3() throws Exception {
executeTest();
}
public void testE() throws Exception {
executeTest();
}
public void testE2() throws Exception {
executeTest();
}
public void testE3() throws Exception {
executeTest();
}
public void testProgrammaticListIntrospection() throws Exception {
Method setter = GenericBean.class.getMethod("setResourceList", List.class);
Assert.assertEquals(Resource.class,
GenericCollectionTypeResolver.getCollectionParameterType(new MethodParameter(setter, 0)));
Method getter = GenericBean.class.getMethod("getResourceList");
Assert.assertEquals(Resource.class,
GenericCollectionTypeResolver.getCollectionReturnType(getter));
}
private abstract class CustomMap <T> extends AbstractMap<String, Integer> {
}
private abstract class OtherCustomMap <T> implements Map<String, Integer> {
}
private interface Foo {
Map<String, Integer> a();
Map<?, ?> b();
Map<?, ? extends Set> b2();
Map<?, ? super Set> b3();
Map c();
CustomMap<Date> d();
CustomMap<?> d2();
CustomMap d3();
OtherCustomMap<Date> e();
OtherCustomMap<?> e2();
OtherCustomMap e3();
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2002-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.core.type;
import java.io.IOException;
import java.io.Serializable;
import java.util.Map;
import junit.framework.TestCase;
import org.springframework.context.annotation.Scope;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import org.springframework.stereotype.Component;
/**
* @author Juergen Hoeller
*/
public class AnnotationMetadataTests extends TestCase {
public void testStandardAnnotationMetadata() throws IOException {
StandardAnnotationMetadata annInfo = new StandardAnnotationMetadata(AnnotatedComponent.class);
doTestAnnotationInfo(annInfo);
}
public void testAsmAnnotationMetadata() throws IOException {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(AnnotatedComponent.class.getName());
doTestAnnotationInfo(metadataReader.getAnnotationMetadata());
}
private void doTestAnnotationInfo(AnnotationMetadata metadata) {
assertEquals(AnnotatedComponent.class.getName(), metadata.getClassName());
assertFalse(metadata.isInterface());
assertFalse(metadata.isAbstract());
assertTrue(metadata.isConcrete());
assertTrue(metadata.hasSuperClass());
assertEquals(Object.class.getName(), metadata.getSuperClassName());
assertEquals(1, metadata.getInterfaceNames().length);
assertEquals(Serializable.class.getName(), metadata.getInterfaceNames()[0]);
assertTrue(metadata.hasAnnotation(Component.class.getName()));
assertTrue(metadata.hasAnnotation(Scope.class.getName()));
assertEquals(2, metadata.getAnnotationTypes().size());
assertTrue(metadata.getAnnotationTypes().contains(Component.class.getName()));
assertTrue(metadata.getAnnotationTypes().contains(Scope.class.getName()));
Map<String, Object> cattrs = metadata.getAnnotationAttributes(Component.class.getName());
assertEquals(1, cattrs.size());
assertEquals("myName", cattrs.get("value"));
Map<String, Object> sattrs = metadata.getAnnotationAttributes(Scope.class.getName());
assertEquals(1, sattrs.size());
assertEquals("myScope", sattrs.get("value"));
}
@Component("myName")
@Scope("myScope")
private static class AnnotatedComponent implements Serializable {
}
}

View File

@@ -0,0 +1,130 @@
/*
* Copyright 2002-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.core.type;
import java.lang.annotation.Inherited;
import junit.framework.TestCase;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.stereotype.Component;
/**
* @author Ramnivas Laddad
* @author Juergen Hoeller
*/
public class AnnotationTypeFilterTests extends TestCase {
public void testDirectAnnotationMatch() throws Exception {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeComponent";
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class);
assertTrue(filter.match(metadataReader, metadataReaderFactory));
ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
public void testInheritedAnnotationFromInterfaceDoesNotMatch() throws Exception {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubClassOfSomeComponentInterface";
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class);
// Must fail as annotation on interfaces should not be considered a match
assertFalse(filter.match(metadataReader, metadataReaderFactory));
ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
public void testInheritedAnnotationFromBaseClassDoesMatch() throws Exception {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubClassOfSomeComponent";
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class);
assertTrue(filter.match(metadataReader, metadataReaderFactory));
ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
public void testNonInheritedAnnotationDoesNotMatch() throws Exception {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubclassOfSomeClassMarkedWithNonInheritedAnnotation";
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
AnnotationTypeFilter filter = new AnnotationTypeFilter(NonInheritedAnnotation.class);
// Must fail as annotation isn't inherited
assertFalse(filter.match(metadataReader, metadataReaderFactory));
ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
public void testNonAnnotatedClassDoesntMatch() throws Exception {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeNonCandidateClass";
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
AnnotationTypeFilter filter = new AnnotationTypeFilter(Component.class);
assertFalse(filter.match(metadataReader, metadataReaderFactory));
ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
// We must use a standalone set of types to ensure that no one else is loading them
// and interfering with ClassloadingAssertions.assertClassNotLoaded()
@Inherited
private static @interface InheritedAnnotation {
}
@InheritedAnnotation
private static class SomeComponent {
}
@InheritedAnnotation
private static interface SomeComponentInterface {
}
private static class SomeSubClassOfSomeComponentInterface implements SomeComponentInterface {
}
private static class SomeSubClassOfSomeComponent extends SomeComponent {
}
private static @interface NonInheritedAnnotation {
}
@NonInheritedAnnotation
private static class SomeClassMarkedWithNonInheritedAnnotation {
}
private static class SomeSubclassOfSomeClassMarkedWithNonInheritedAnnotation extends SomeClassMarkedWithNonInheritedAnnotation {
}
private static class SomeNonCandidateClass {
}
}

View File

@@ -0,0 +1,164 @@
/*
* Copyright 2002-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.core.type;
import junit.framework.TestCase;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import org.springframework.core.type.filter.AspectJTypeFilter;
import org.springframework.stereotype.Component;
/**
* @author Ramnivas Laddad
*/
public class AspectJTypeFilterTests extends TestCase {
public void testNamePatternMatches() throws Exception {
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClass",
"org.springframework.core.type.AspectJTypeFilterTests.SomeClass");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClass",
"*");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClass",
"*..SomeClass");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClass",
"org..SomeClass");
}
public void testNamePatternNoMatches() throws Exception {
assertNoMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClass",
"org.springframework.core.type.AspectJTypeFilterTests.SomeClassX");
}
public void testSubclassPatternMatches() throws Exception {
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassExtendingSomeClass",
"org.springframework.core.type.AspectJTypeFilterTests.SomeClass+");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassExtendingSomeClass",
"*+");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassExtendingSomeClass",
"java.lang.Object+");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassImplementingSomeInterface",
"org.springframework.core.type.AspectJTypeFilterTests.SomeInterface+");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassImplementingSomeInterface",
"*+");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassImplementingSomeInterface",
"java.lang.Object+");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassExtendingSomeClassExtendingSomeClassAndImplemnentingSomeInterface",
"org.springframework.core.type.AspectJTypeFilterTests.SomeInterface+");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassExtendingSomeClassExtendingSomeClassAndImplemnentingSomeInterface",
"org.springframework.core.type.AspectJTypeFilterTests.SomeClassExtendingSomeClass+");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassExtendingSomeClassExtendingSomeClassAndImplemnentingSomeInterface",
"org.springframework.core.type.AspectJTypeFilterTests.SomeClass+");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassExtendingSomeClassExtendingSomeClassAndImplemnentingSomeInterface",
"*+");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassExtendingSomeClassExtendingSomeClassAndImplemnentingSomeInterface",
"java.lang.Object+");
}
public void testSubclassPatternNoMatches() throws Exception {
assertNoMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassExtendingSomeClass",
"java.lang.String+");
}
public void testAnnotationPatternMathces() throws Exception {
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassAnnotatedWithComponent",
"@org.springframework.stereotype.Component *..*");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassAnnotatedWithComponent",
"@* *..*");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassAnnotatedWithComponent",
"@*..* *..*");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassAnnotatedWithComponent",
"@*..*Component *..*");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassAnnotatedWithComponent",
"@org.springframework.stereotype.Component *..*Component");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassAnnotatedWithComponent",
"@org.springframework.stereotype.Component *");
}
public void testAnnotationPatternNoMathces() throws Exception {
assertNoMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassAnnotatedWithComponent",
"@org.springframework.stereotype.Repository *..*");
}
public void testCompositionPatternMatches() throws Exception {
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClass",
"!*..SomeOtherClass");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassExtendingSomeClassExtendingSomeClassAndImplemnentingSomeInterface",
"org.springframework.core.type.AspectJTypeFilterTests.SomeInterface+ " +
"&& org.springframework.core.type.AspectJTypeFilterTests.SomeClass+ " +
"&& org.springframework.core.type.AspectJTypeFilterTests.SomeClassExtendingSomeClass+");
assertMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClassExtendingSomeClassExtendingSomeClassAndImplemnentingSomeInterface",
"org.springframework.core.type.AspectJTypeFilterTests.SomeInterface+ " +
"|| org.springframework.core.type.AspectJTypeFilterTests.SomeClass+ " +
"|| org.springframework.core.type.AspectJTypeFilterTests.SomeClassExtendingSomeClass+");
}
public void testCompositionPatternNoMatches() throws Exception {
assertNoMatch("org.springframework.core.type.AspectJTypeFilterTests$SomeClass",
"*..Bogus && org.springframework.core.type.AspectJTypeFilterTests.SomeClass");
}
private void assertMatch(String type, String typePattern) throws Exception {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(type);
AspectJTypeFilter filter = new AspectJTypeFilter(typePattern, getClass().getClassLoader());
assertTrue(filter.match(metadataReader, metadataReaderFactory));
ClassloadingAssertions.assertClassNotLoaded(type);
}
private void assertNoMatch(String type, String typePattern) throws Exception {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(type);
AspectJTypeFilter filter = new AspectJTypeFilter(typePattern, getClass().getClassLoader());
assertFalse(filter.match(metadataReader, metadataReaderFactory));
ClassloadingAssertions.assertClassNotLoaded(type);
}
// We must use a standalone set of types to ensure that no one else is loading them
// and interfering with ClassloadingAssertions.assertClassNotLoaded()
static interface SomeInterface {
}
static class SomeClass {
}
static class SomeClassExtendingSomeClass extends SomeClass {
}
static class SomeClassImplementingSomeInterface implements SomeInterface {
}
static class SomeClassExtendingSomeClassExtendingSomeClassAndImplemnentingSomeInterface
extends SomeClassExtendingSomeClass implements SomeInterface {
}
@Component
static class SomeClassAnnotatedWithComponent {
}
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright 2002-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.core.type;
import junit.framework.TestCase;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import org.springframework.core.type.filter.AssignableTypeFilter;
/**
* @author Ramnivas Laddad
* @author Juergen Hoeller
*/
public class AssignableTypeFilterTests extends TestCase {
public void testDirectMatch() throws Exception {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
String classUnderTest = "org.springframework.core.type.AssignableTypeFilterTests$TestNonInheritingClass";
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
AssignableTypeFilter matchingFilter = new AssignableTypeFilter(TestNonInheritingClass.class);
AssignableTypeFilter notMatchingFilter = new AssignableTypeFilter(TestInterface.class);
assertFalse(notMatchingFilter.match(metadataReader, metadataReaderFactory));
assertTrue(matchingFilter.match(metadataReader, metadataReaderFactory));
}
public void testInterfaceMatch() throws Exception {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
String classUnderTest = "org.springframework.core.type.AssignableTypeFilterTests$TestInterfaceImpl";
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
AssignableTypeFilter filter = new AssignableTypeFilter(TestInterface.class);
assertTrue(filter.match(metadataReader, metadataReaderFactory));
ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
public void testSuperClassMatch() throws Exception {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
String classUnderTest = "org.springframework.core.type.AssignableTypeFilterTests$SomeDaoLikeImpl";
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
AssignableTypeFilter filter = new AssignableTypeFilter(SimpleJdbcDaoSupport.class);
assertTrue(filter.match(metadataReader, metadataReaderFactory));
ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
public void testInterfaceThroughSuperClassMatch() throws Exception {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
String classUnderTest = "org.springframework.core.type.AssignableTypeFilterTests$SomeDaoLikeImpl";
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
AssignableTypeFilter filter = new AssignableTypeFilter(JdbcDaoSupport.class);
assertTrue(filter.match(metadataReader, metadataReaderFactory));
ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
// We must use a standalone set of types to ensure that no one else is loading them
// and interfere with ClassloadingAssertions.assertClassNotLoaded()
private static class TestNonInheritingClass {
}
private static interface TestInterface {
}
private static class TestInterfaceImpl implements TestInterface {
}
private static interface SomeDaoLikeInterface {
}
private static class SomeDaoLikeImpl extends SimpleJdbcDaoSupport implements SomeDaoLikeInterface {
}
private static interface JdbcDaoSupport {
}
private static class SimpleJdbcDaoSupport implements JdbcDaoSupport {
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2002-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.core.type;
import java.lang.reflect.Method;
import junit.framework.TestCase;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
*
* @author Ramnivas Laddad
*
*/
public class ClassloadingAssertions {
public static boolean isClassLoaded(String className) {
ClassLoader cl = ClassUtils.getDefaultClassLoader();
Method findLoadeClassMethod = ReflectionUtils.findMethod(cl.getClass(), "findLoadedClass", new Class[]{String.class});
findLoadeClassMethod.setAccessible(true);
Class loadedClass = (Class)ReflectionUtils.invokeMethod(findLoadeClassMethod, cl, new Object[]{className});
return loadedClass != null;
}
public static void assertClassLoaded(String className) {
}
public static void assertClassNotLoaded(String className) {
TestCase.assertFalse("Class shouldn't have been loaded", isClassLoaded(className));
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2002-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.stereotype;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicates that an annotated class is a "component".
* Such classes are considered as candidates for auto-detection
* when using annotation-based configuration and classpath scanning.
*
* <p>Other class-level annotations may be considered as identifying
* a component as well, typically a special kind of component:
* e.g. the {@link Repository @Repository} annotation or AspectJ's
* {@link org.aspectj.lang.annotation.Aspect @Aspect} annotation.
*
* @author Mark Fisher
* @since 2.5
* @see Repository
* @see Service
* @see Controller
* @see org.springframework.context.annotation.ClassPathBeanDefinitionScanner
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any
*/
public abstract String value() default "";
}