Merge branch '3.2.x' into master
Conflicts: gradle.properties spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheManagerFactoryBean.java spring-core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java spring-core/src/main/java/org/springframework/core/env/ReadOnlySystemAttributesMap.java spring-jdbc/src/main/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxy.java spring-jdbc/src/main/java/org/springframework/jdbc/support/lob/AbstractLobHandler.java spring-web/src/main/java/org/springframework/http/client/BufferingClientHttpRequestWrapper.java spring-web/src/main/java/org/springframework/http/client/SimpleBufferingClientHttpRequest.java spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -25,7 +25,6 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
@@ -145,7 +144,7 @@ public abstract class BridgeMethodResolver {
|
||||
private static Method findGenericDeclaration(Method bridgeMethod) {
|
||||
// Search parent types for method that has same signature as bridge.
|
||||
Class superclass = bridgeMethod.getDeclaringClass().getSuperclass();
|
||||
while (!Object.class.equals(superclass)) {
|
||||
while (superclass != null && !Object.class.equals(superclass)) {
|
||||
Method method = searchForMatch(superclass, bridgeMethod);
|
||||
if (method != null && !method.isBridge()) {
|
||||
return method;
|
||||
@@ -219,8 +218,6 @@ public abstract class BridgeMethodResolver {
|
||||
* @return whether signatures match as described
|
||||
*/
|
||||
public static boolean isVisibilityBridgeMethodPair(Method bridgeMethod, Method bridgedMethod) {
|
||||
Assert.isTrue(bridgeMethod != null);
|
||||
Assert.isTrue(bridgedMethod != null);
|
||||
if (bridgeMethod == bridgedMethod) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -26,11 +26,7 @@ import java.lang.reflect.WildcardType;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.ConcurrentReferenceHashMap;
|
||||
|
||||
/**
|
||||
@@ -47,12 +43,11 @@ import org.springframework.util.ConcurrentReferenceHashMap;
|
||||
*/
|
||||
public abstract class GenericTypeResolver {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(GenericTypeResolver.class);
|
||||
|
||||
/** Cache from Class to TypeVariable Map */
|
||||
private static final Map<Class, Map<TypeVariable, Type>> typeVariableCache =
|
||||
new ConcurrentReferenceHashMap<Class, Map<TypeVariable,Type>>();
|
||||
|
||||
|
||||
/**
|
||||
* Determine the target type for the given parameter specification.
|
||||
* @param methodParam the method parameter specification
|
||||
@@ -93,7 +88,6 @@ public abstract class GenericTypeResolver {
|
||||
/**
|
||||
* Determine the target type for the generic return type of the given method,
|
||||
* where formal type variables are declared on the given class.
|
||||
*
|
||||
* @param method the method to introspect
|
||||
* @param clazz the class to resolve type variables against
|
||||
* @return the corresponding generic parameter or return type
|
||||
@@ -112,15 +106,12 @@ public abstract class GenericTypeResolver {
|
||||
* Determine the target type for the generic return type of the given
|
||||
* <em>generic method</em>, where formal type variables are declared on
|
||||
* the given method itself.
|
||||
*
|
||||
* <p>For example, given a factory method with the following signature,
|
||||
* if {@code resolveReturnTypeForGenericMethod()} is invoked with the reflected
|
||||
* method for {@code creatProxy()} and an {@code Object[]} array containing
|
||||
* {@code MyService.class}, {@code resolveReturnTypeForGenericMethod()} will
|
||||
* infer that the target return type is {@code MyService}.
|
||||
*
|
||||
* <pre>{@code public static <T> T createProxy(Class<T> clazz)}</pre>
|
||||
*
|
||||
* <h4>Possible Return Values</h4>
|
||||
* <ul>
|
||||
* <li>the target return type, if it can be inferred</li>
|
||||
@@ -134,27 +125,20 @@ public abstract class GenericTypeResolver {
|
||||
* Method#getGenericParameterTypes() formal argument list} for the given
|
||||
* method</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param method the method to introspect, never {@code null}
|
||||
* @param args the arguments that will be supplied to the method when it is
|
||||
* invoked, never {@code null}
|
||||
* @return the resolved target return type, the standard return type, or
|
||||
* {@code null}
|
||||
* @return the resolved target return type, the standard return type, or {@code null}
|
||||
* @since 3.2
|
||||
* @see #resolveReturnType
|
||||
*/
|
||||
public static Class<?> resolveReturnTypeForGenericMethod(Method method, Object[] args) {
|
||||
Assert.notNull(method, "method must not be null");
|
||||
Assert.notNull(args, "args must not be null");
|
||||
Assert.notNull(method, "Method must not be null");
|
||||
Assert.notNull(args, "Argument array must not be null");
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format("Resolving return type for [%s] with concrete method arguments [%s].",
|
||||
method.toGenericString(), ObjectUtils.nullSafeToString(args)));
|
||||
}
|
||||
|
||||
final TypeVariable<Method>[] declaredTypeVariables = method.getTypeParameters();
|
||||
final Type genericReturnType = method.getGenericReturnType();
|
||||
final Type[] methodArgumentTypes = method.getGenericParameterTypes();
|
||||
TypeVariable<Method>[] declaredTypeVariables = method.getTypeParameters();
|
||||
Type genericReturnType = method.getGenericReturnType();
|
||||
Type[] methodArgumentTypes = method.getGenericParameterTypes();
|
||||
|
||||
// No declared type variables to inspect, so just return the standard return type.
|
||||
if (declaredTypeVariables.length == 0) {
|
||||
@@ -172,11 +156,6 @@ public abstract class GenericTypeResolver {
|
||||
boolean locallyDeclaredTypeVariableMatchesReturnType = false;
|
||||
for (TypeVariable<Method> currentTypeVariable : declaredTypeVariables) {
|
||||
if (currentTypeVariable.equals(genericReturnType)) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format(
|
||||
"Found declared type variable [%s] that matches the target return type [%s].",
|
||||
currentTypeVariable, genericReturnType));
|
||||
}
|
||||
locallyDeclaredTypeVariableMatchesReturnType = true;
|
||||
break;
|
||||
}
|
||||
@@ -184,39 +163,20 @@ public abstract class GenericTypeResolver {
|
||||
|
||||
if (locallyDeclaredTypeVariableMatchesReturnType) {
|
||||
for (int i = 0; i < methodArgumentTypes.length; i++) {
|
||||
final Type currentMethodArgumentType = methodArgumentTypes[i];
|
||||
|
||||
Type currentMethodArgumentType = methodArgumentTypes[i];
|
||||
if (currentMethodArgumentType.equals(genericReturnType)) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format(
|
||||
"Found method argument type at index [%s] that matches the target return type.", i));
|
||||
}
|
||||
return args[i].getClass();
|
||||
}
|
||||
|
||||
if (currentMethodArgumentType instanceof ParameterizedType) {
|
||||
ParameterizedType parameterizedType = (ParameterizedType) currentMethodArgumentType;
|
||||
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
|
||||
|
||||
for (int j = 0; j < actualTypeArguments.length; j++) {
|
||||
final Type typeArg = actualTypeArguments[j];
|
||||
|
||||
for (Type typeArg : actualTypeArguments) {
|
||||
if (typeArg.equals(genericReturnType)) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format(
|
||||
"Found method argument type at index [%s] that is parameterized with a type argument that matches the target return type.",
|
||||
i));
|
||||
}
|
||||
|
||||
if (args[i] instanceof Class) {
|
||||
return (Class<?>) args[i];
|
||||
} else {
|
||||
// Consider adding logic to determine the class of the
|
||||
// J'th typeArg, if possible.
|
||||
logger.info(String.format(
|
||||
"Could not determine the target type for type argument [%s] for method [%s].",
|
||||
typeArg, method.toGenericString()));
|
||||
|
||||
}
|
||||
else {
|
||||
// Consider adding logic to determine the class of the typeArg, if possible.
|
||||
// For now, just fall back...
|
||||
return method.getReturnType();
|
||||
}
|
||||
@@ -409,8 +369,7 @@ public abstract class GenericTypeResolver {
|
||||
* all super types, enclosing types and interfaces.
|
||||
*/
|
||||
public static Map<TypeVariable, Type> getTypeVariableMap(Class clazz) {
|
||||
Map<TypeVariable, Type> ref = typeVariableCache.get(clazz);
|
||||
Map<TypeVariable, Type> typeVariableMap = (ref != null ? ref : null);
|
||||
Map<TypeVariable, Type> typeVariableMap = typeVariableCache.get(clazz);
|
||||
|
||||
if (typeVariableMap == null) {
|
||||
typeVariableMap = new HashMap<TypeVariable, Type>();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -65,15 +65,15 @@ public class LocalVariableTableParameterNameDiscoverer implements ParameterNameD
|
||||
|
||||
|
||||
public String[] getParameterNames(Method method) {
|
||||
Class<?> declaringClass = method.getDeclaringClass();
|
||||
Method originalMethod = BridgeMethodResolver.findBridgedMethod(method);
|
||||
Class<?> declaringClass = originalMethod.getDeclaringClass();
|
||||
Map<Member, String[]> map = this.parameterNamesCache.get(declaringClass);
|
||||
if (map == null) {
|
||||
// initialize cache
|
||||
map = inspectClass(declaringClass);
|
||||
this.parameterNamesCache.put(declaringClass, map);
|
||||
}
|
||||
if (map != NO_DEBUG_INFO_MAP) {
|
||||
return map.get(method);
|
||||
return map.get(originalMethod);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -82,14 +82,12 @@ public class LocalVariableTableParameterNameDiscoverer implements ParameterNameD
|
||||
Class<?> declaringClass = ctor.getDeclaringClass();
|
||||
Map<Member, String[]> map = this.parameterNamesCache.get(declaringClass);
|
||||
if (map == null) {
|
||||
// initialize cache
|
||||
map = inspectClass(declaringClass);
|
||||
this.parameterNamesCache.put(declaringClass, map);
|
||||
}
|
||||
if (map != NO_DEBUG_INFO_MAP) {
|
||||
return map.get(ctor);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -45,4 +45,4 @@ public class NamedInheritableThreadLocal<T> extends InheritableThreadLocal<T> {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.core.annotation;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.OrderComparator;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
@@ -37,7 +41,7 @@ public class AnnotationAwareOrderComparator extends OrderComparator {
|
||||
/**
|
||||
* Shared default instance of AnnotationAwareOrderComparator.
|
||||
*/
|
||||
public static AnnotationAwareOrderComparator INSTANCE = new AnnotationAwareOrderComparator();
|
||||
public static final AnnotationAwareOrderComparator INSTANCE = new AnnotationAwareOrderComparator();
|
||||
|
||||
|
||||
@Override
|
||||
@@ -46,7 +50,8 @@ public class AnnotationAwareOrderComparator extends OrderComparator {
|
||||
return ((Ordered) obj).getOrder();
|
||||
}
|
||||
if (obj != null) {
|
||||
Order order = obj.getClass().getAnnotation(Order.class);
|
||||
Class<?> clazz = (obj instanceof Class ? (Class) obj : obj.getClass());
|
||||
Order order = clazz.getAnnotation(Order.class);
|
||||
if (order != null) {
|
||||
return order.value();
|
||||
}
|
||||
@@ -54,4 +59,31 @@ public class AnnotationAwareOrderComparator extends OrderComparator {
|
||||
return Ordered.LOWEST_PRECEDENCE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sort the given List with a default AnnotationAwareOrderComparator.
|
||||
* <p>Optimized to skip sorting for lists with size 0 or 1,
|
||||
* in order to avoid unnecessary array extraction.
|
||||
* @param list the List to sort
|
||||
* @see java.util.Collections#sort(java.util.List, java.util.Comparator)
|
||||
*/
|
||||
public static void sort(List<?> list) {
|
||||
if (list.size() > 1) {
|
||||
Collections.sort(list, INSTANCE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort the given array with a default AnnotationAwareOrderComparator.
|
||||
* <p>Optimized to skip sorting for lists with size 0 or 1,
|
||||
* in order to avoid unnecessary array extraction.
|
||||
* @param array the array to sort
|
||||
* @see java.util.Arrays#sort(Object[], java.util.Comparator)
|
||||
*/
|
||||
public static void sort(Object[] array) {
|
||||
if (array.length > 1) {
|
||||
Arrays.sort(array, INSTANCE);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -19,7 +19,7 @@ package org.springframework.core.annotation;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
@@ -255,8 +255,9 @@ public abstract class AnnotationUtils {
|
||||
* declared locally on the supplied {@code clazz}. The supplied {@link Class}
|
||||
* may represent any type.
|
||||
* <p>Note: This method does <strong>not</strong> determine if the annotation is
|
||||
* {@link java.lang.annotation.Inherited inherited}. For greater clarity regarding inherited
|
||||
* annotations, consider using {@link #isAnnotationInherited(Class, Class)} instead.
|
||||
* {@linkplain java.lang.annotation.Inherited inherited}. For greater clarity
|
||||
* regarding inherited annotations, consider using
|
||||
* {@link #isAnnotationInherited(Class, Class)} instead.
|
||||
* @param annotationType the Class object corresponding to the annotation type
|
||||
* @param clazz the Class object corresponding to the class on which to check for the annotation
|
||||
* @return {@code true} if an annotation for the specified {@code annotationType}
|
||||
@@ -268,7 +269,7 @@ public abstract class AnnotationUtils {
|
||||
Assert.notNull(annotationType, "Annotation type must not be null");
|
||||
Assert.notNull(clazz, "Class must not be null");
|
||||
boolean declaredLocally = false;
|
||||
for (Annotation annotation : Arrays.asList(clazz.getDeclaredAnnotations())) {
|
||||
for (Annotation annotation : clazz.getDeclaredAnnotations()) {
|
||||
if (annotation.annotationType().equals(annotationType)) {
|
||||
declaredLocally = true;
|
||||
break;
|
||||
@@ -279,16 +280,16 @@ public abstract class AnnotationUtils {
|
||||
|
||||
/**
|
||||
* Determine whether an annotation for the specified {@code annotationType} is present
|
||||
* on the supplied {@code clazz} and is {@link java.lang.annotation.Inherited inherited}
|
||||
* i.e., not declared locally for the class).
|
||||
* on the supplied {@code clazz} and is {@linkplain java.lang.annotation.Inherited inherited}
|
||||
* (i.e., not declared locally for the class).
|
||||
* <p>If the supplied {@code clazz} is an interface, only the interface itself will be checked.
|
||||
* In accordance with standard meta-annotation semantics, the inheritance hierarchy for interfaces
|
||||
* will not be traversed. See the {@link java.lang.annotation.Inherited JavaDoc} for the
|
||||
* @Inherited meta-annotation for further details regarding annotation inheritance.
|
||||
* will not be traversed. See the {@linkplain java.lang.annotation.Inherited Javadoc} for the
|
||||
* {@code @Inherited} meta-annotation for further details regarding annotation inheritance.
|
||||
* @param annotationType the Class object corresponding to the annotation type
|
||||
* @param clazz the Class object corresponding to the class on which to check for the annotation
|
||||
* @return {@code true} if an annotation for the specified {@code annotationType} is present
|
||||
* on the supplied {@code clazz} and is {@link java.lang.annotation.Inherited inherited}
|
||||
* on the supplied {@code clazz} and is <em>inherited</em>
|
||||
* @see Class#isAnnotationPresent(Class)
|
||||
* @see #isAnnotationDeclaredLocally(Class, Class)
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.core.convert;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
@@ -33,6 +34,8 @@ import org.springframework.util.ObjectUtils;
|
||||
* @author Keith Donald
|
||||
* @author Andy Clement
|
||||
* @author Juergen Hoeller
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
* @since 3.0
|
||||
*/
|
||||
public class TypeDescriptor {
|
||||
@@ -75,7 +78,8 @@ public class TypeDescriptor {
|
||||
|
||||
/**
|
||||
* Create a new type descriptor from a {@link MethodParameter}.
|
||||
* Use this constructor when a source or target conversion point is a constructor parameter, method parameter, or method return value.
|
||||
* <p>Use this constructor when a source or target conversion point is a
|
||||
* constructor parameter, method parameter, or method return value.
|
||||
* @param methodParameter the method parameter
|
||||
*/
|
||||
public TypeDescriptor(MethodParameter methodParameter) {
|
||||
@@ -84,7 +88,7 @@ public class TypeDescriptor {
|
||||
|
||||
/**
|
||||
* Create a new type descriptor from a {@link Field}.
|
||||
* Use this constructor when source or target conversion point is a field.
|
||||
* <p>Use this constructor when a source or target conversion point is a field.
|
||||
* @param field the field
|
||||
*/
|
||||
public TypeDescriptor(Field field) {
|
||||
@@ -93,7 +97,8 @@ public class TypeDescriptor {
|
||||
|
||||
/**
|
||||
* Create a new type descriptor from a {@link Property}.
|
||||
* Use this constructor when a source or target conversion point is a property on a Java class.
|
||||
* <p>Use this constructor when a source or target conversion point is a
|
||||
* property on a Java class.
|
||||
* @param property the property
|
||||
*/
|
||||
public TypeDescriptor(Property property) {
|
||||
@@ -103,8 +108,11 @@ public class TypeDescriptor {
|
||||
|
||||
/**
|
||||
* Create a new type descriptor from the given type.
|
||||
* Use this to instruct the conversion system to convert an object to a specific target type, when no type location such as a method parameter or field is available to provide additional conversion context.
|
||||
* Generally prefer use of {@link #forObject(Object)} for constructing type descriptors from source objects, as it handles the null object case.
|
||||
* <p>Use this to instruct the conversion system to convert an object to a
|
||||
* specific target type, when no type location such as a method parameter or
|
||||
* field is available to provide additional conversion context.
|
||||
* <p>Generally prefer use of {@link #forObject(Object)} for constructing type
|
||||
* descriptors from source objects, as it handles the {@code null} object case.
|
||||
* @param type the class
|
||||
* @return the type descriptor
|
||||
*/
|
||||
@@ -114,12 +122,15 @@ public class TypeDescriptor {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new type descriptor from a java.util.Collection type.
|
||||
* Useful for converting to typed Collections.
|
||||
* For example, a List<String> could be converted to a List<EmailAddress> by converting to a targetType built with this method.
|
||||
* The method call to construct such a TypeDescriptor would look something like: collection(List.class, TypeDescriptor.valueOf(EmailAddress.class));
|
||||
* Create a new type descriptor from a {@link java.util.Collection} type.
|
||||
* <p>Useful for converting to typed Collections.
|
||||
* <p>For example, a {@code List<String>} could be converted to a
|
||||
* {@code List<EmailAddress>} by converting to a targetType built with this method.
|
||||
* The method call to construct such a {@code TypeDescriptor} would look something
|
||||
* like: {@code collection(List.class, TypeDescriptor.valueOf(EmailAddress.class));}
|
||||
* @param collectionType the collection type, which must implement {@link Collection}.
|
||||
* @param elementTypeDescriptor a descriptor for the collection's element type, used to convert collection elements
|
||||
* @param elementTypeDescriptor a descriptor for the collection's element type,
|
||||
* used to convert collection elements
|
||||
* @return the collection type descriptor
|
||||
*/
|
||||
public static TypeDescriptor collection(Class<?> collectionType, TypeDescriptor elementTypeDescriptor) {
|
||||
@@ -130,9 +141,9 @@ public class TypeDescriptor {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new type descriptor from a java.util.Map type.
|
||||
* Useful for Converting to typed Maps.
|
||||
* For example, a Map<String, String> could be converted to a Map<Id, EmailAddress> by converting to a targetType built with this method:
|
||||
* Create a new type descriptor from a {@link java.util.Map} type.
|
||||
* <p>Useful for converting to typed Maps.
|
||||
* <p>For example, a Map<String, String> could be converted to a Map<Id, EmailAddress> by converting to a targetType built with this method:
|
||||
* The method call to construct such a TypeDescriptor would look something like: map(Map.class, TypeDescriptor.valueOf(Id.class), TypeDescriptor.valueOf(EmailAddress.class));
|
||||
* @param mapType the map type, which must implement {@link Map}
|
||||
* @param keyTypeDescriptor a descriptor for the map's key type, used to convert map keys
|
||||
@@ -146,19 +157,44 @@ public class TypeDescriptor {
|
||||
return new TypeDescriptor(mapType, keyTypeDescriptor, valueTypeDescriptor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new type descriptor as an array of the specified type.
|
||||
* <p>For example to create a {@code Map<String,String>[]} use
|
||||
* {@code TypeDescriptor.array(TypeDescriptor.map(Map.class, TypeDescriptor.value(String.class), TypeDescriptor.value(String.class)))}.
|
||||
* @param elementTypeDescriptor the {@link TypeDescriptor} of the array element or {@code null}
|
||||
* @return an array {@link TypeDescriptor} or {@code null} if {@code elementTypeDescriptor} is {@code null}
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public static TypeDescriptor array(TypeDescriptor elementTypeDescriptor) {
|
||||
if(elementTypeDescriptor == null) {
|
||||
return null;
|
||||
}
|
||||
Class<?> type = Array.newInstance(elementTypeDescriptor.getType(), 0).getClass();
|
||||
return new TypeDescriptor(type, elementTypeDescriptor, null, null, elementTypeDescriptor.getAnnotations());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a type descriptor for a nested type declared within the method parameter.
|
||||
* For example, if the methodParameter is a List<String> and the nestingLevel is 1, the nested type descriptor will be String.class.
|
||||
* If the methodParameter is a List<List<String>> and the nestingLevel is 2, the nested type descriptor will also be a String.class.
|
||||
* If the methodParameter is a Map<Integer, String> and the nesting level is 1, the nested type descriptor will be String, derived from the map value.
|
||||
* If the methodParameter is a List<Map<Integer, String>> and the nesting level is 2, the nested type descriptor will be String, derived from the map value.
|
||||
* Returns null if a nested type cannot be obtained because it was not declared.
|
||||
* For example, if the method parameter is a List<?>, the nested type descriptor returned will be <tt>null</tt>.
|
||||
* <p>For example, if the methodParameter is a {@code List<String>} and the
|
||||
* nesting level is 1, the nested type descriptor will be String.class.
|
||||
* <p>If the methodParameter is a {@code List<List<String>>} and the nesting
|
||||
* level is 2, the nested type descriptor will also be a String.class.
|
||||
* <p>If the methodParameter is a {@code Map<Integer, String>} and the nesting
|
||||
* level is 1, the nested type descriptor will be String, derived from the map value.
|
||||
* <p>If the methodParameter is a {@code List<Map<Integer, String>>} and the
|
||||
* nesting level is 2, the nested type descriptor will be String, derived from the map value.
|
||||
* <p>Returns {@code null} if a nested type cannot be obtained because it was not declared.
|
||||
* For example, if the method parameter is a {@code List<?>}, the nested type
|
||||
* descriptor returned will be {@code null}.
|
||||
* @param methodParameter the method parameter with a nestingLevel of 1
|
||||
* @param nestingLevel the nesting level of the collection/array element or map key/value declaration within the method parameter
|
||||
* @return the nested type descriptor at the specified nesting level, or null if it could not be obtained
|
||||
* @throws IllegalArgumentException if the nesting level of the input {@link MethodParameter} argument is not 1
|
||||
* @throws IllegalArgumentException if the types up to the specified nesting level are not of collection, array, or map types
|
||||
* @param nestingLevel the nesting level of the collection/array element or
|
||||
* map key/value declaration within the method parameter
|
||||
* @return the nested type descriptor at the specified nesting level, or null
|
||||
* if it could not be obtained
|
||||
* @throws IllegalArgumentException if the nesting level of the input
|
||||
* {@link MethodParameter} argument is not 1
|
||||
* @throws IllegalArgumentException if the types up to the specified nesting
|
||||
* level are not of collection, array, or map types
|
||||
*/
|
||||
public static TypeDescriptor nested(MethodParameter methodParameter, int nestingLevel) {
|
||||
if (methodParameter.getNestingLevel() != 1) {
|
||||
@@ -169,16 +205,23 @@ public class TypeDescriptor {
|
||||
|
||||
/**
|
||||
* Creates a type descriptor for a nested type declared within the field.
|
||||
* <p>For example, if the field is a {@code List<String>} and the nestingLevel is 1, the nested type descriptor will be {@code String.class}.
|
||||
* If the field is a {@code List<List<String>>} and the nestingLevel is 2, the nested type descriptor will also be a {@code String.class}.
|
||||
* If the field is a {@code Map<Integer, String>} and the nestingLevel is 1, the nested type descriptor will be String, derived from the map value.
|
||||
* If the field is a {@code List<Map<Integer, String>>} and the nestingLevel is 2, the nested type descriptor will be String, derived from the map value.
|
||||
* Returns {@code null} if a nested type cannot be obtained because it was not declared.
|
||||
* For example, if the field is a {@code List<?>}, the nested type descriptor returned will be {@code null}.
|
||||
* <p>For example, if the field is a {@code List<String>} and the nesting
|
||||
* level is 1, the nested type descriptor will be {@code String.class}.
|
||||
* <p>If the field is a {@code List<List<String>>} and the nesting level is
|
||||
* 2, the nested type descriptor will also be a {@code String.class}.
|
||||
* <p>If the field is a {@code Map<Integer, String>} and the nesting level
|
||||
* is 1, the nested type descriptor will be String, derived from the map value.
|
||||
* <p>If the field is a {@code List<Map<Integer, String>>} and the nesting
|
||||
* level is 2, the nested type descriptor will be String, derived from the map value.
|
||||
* <p>Returns {@code null} if a nested type cannot be obtained because it was not declared.
|
||||
* For example, if the field is a {@code List<?>}, the nested type descriptor returned will be {@code null}.
|
||||
* @param field the field
|
||||
* @param nestingLevel the nesting level of the collection/array element or map key/value declaration within the field
|
||||
* @return the nested type descriptor at the specified nestingLevel, or null if it could not be obtained
|
||||
* @throws IllegalArgumentException if the types up to the specified nesting level are not of collection, array, or map types
|
||||
* @param nestingLevel the nesting level of the collection/array element or
|
||||
* map key/value declaration within the field
|
||||
* @return the nested type descriptor at the specified nesting level, or null
|
||||
* if it could not be obtained
|
||||
* @throws IllegalArgumentException if the types up to the specified nesting
|
||||
* level are not of collection, array, or map types
|
||||
*/
|
||||
public static TypeDescriptor nested(Field field, int nestingLevel) {
|
||||
return nested(new FieldDescriptor(field), nestingLevel);
|
||||
@@ -186,16 +229,24 @@ public class TypeDescriptor {
|
||||
|
||||
/**
|
||||
* Creates a type descriptor for a nested type declared within the property.
|
||||
* <p>For example, if the property is a {@code List<String>} and the nestingLevel is 1, the nested type descriptor will be {@code String.class}.
|
||||
* If the property is a {@code List<List<String>>} and the nestingLevel is 2, the nested type descriptor will also be a {@code String.class}.
|
||||
* If the property is a {@code Map<Integer, String>} and the nestingLevel is 1, the nested type descriptor will be String, derived from the map value.
|
||||
* If the property is a {@code List<Map<Integer, String>>} and the nestingLevel is 2, the nested type descriptor will be String, derived from the map value.
|
||||
* Returns {@code null} if a nested type cannot be obtained because it was not declared.
|
||||
* For example, if the property is a {@code List<?>}, the nested type descriptor returned will be {@code null}.
|
||||
* <p>For example, if the property is a {@code List<String>} and the nesting
|
||||
* level is 1, the nested type descriptor will be {@code String.class}.
|
||||
* <p>If the property is a {@code List<List<String>>} and the nesting level
|
||||
* is 2, the nested type descriptor will also be a {@code String.class}.
|
||||
* <p>If the property is a {@code Map<Integer, String>} and the nesting level
|
||||
* is 1, the nested type descriptor will be String, derived from the map value.
|
||||
* <p>If the property is a {@code List<Map<Integer, String>>} and the nesting
|
||||
* level is 2, the nested type descriptor will be String, derived from the map value.
|
||||
* <p>Returns {@code null} if a nested type cannot be obtained because it was not declared.
|
||||
* For example, if the property is a {@code List<?>}, the nested type descriptor
|
||||
* returned will be {@code null}.
|
||||
* @param property the property
|
||||
* @param nestingLevel the nesting level of the collection/array element or map key/value declaration within the property
|
||||
* @return the nested type descriptor at the specified nestingLevel, or {@code null} if it could not be obtained
|
||||
* @throws IllegalArgumentException if the types up to the specified nesting level are not of collection, array, or map types
|
||||
* @param nestingLevel the nesting level of the collection/array element or
|
||||
* map key/value declaration within the property
|
||||
* @return the nested type descriptor at the specified nesting level, or
|
||||
* {@code null} if it could not be obtained
|
||||
* @throws IllegalArgumentException if the types up to the specified nesting
|
||||
* level are not of collection, array, or map types
|
||||
*/
|
||||
public static TypeDescriptor nested(Property property, int nestingLevel) {
|
||||
return nested(new BeanPropertyDescriptor(property), nestingLevel);
|
||||
@@ -203,8 +254,8 @@ public class TypeDescriptor {
|
||||
|
||||
/**
|
||||
* Create a new type descriptor for an object.
|
||||
* Use this factory method to introspect a source object before asking the conversion system to convert it to some another type.
|
||||
* If the provided object is null, returns null, else calls {@link #valueOf(Class)} to build a TypeDescriptor from the object's class.
|
||||
* <p>Use this factory method to introspect a source object before asking the conversion system to convert it to some another type.
|
||||
* <p>If the provided object is null, returns null, else calls {@link #valueOf(Class)} to build a TypeDescriptor from the object's class.
|
||||
* @param source the source object
|
||||
* @return the type descriptor
|
||||
*/
|
||||
@@ -214,7 +265,7 @@ public class TypeDescriptor {
|
||||
|
||||
/**
|
||||
* The type of the backing class, method parameter, field, or property described by this TypeDescriptor.
|
||||
* Returns primitive types as-is.
|
||||
* <p>Returns primitive types as-is.
|
||||
* <p>See {@link #getObjectType()} for a variation of this operation that resolves primitive types
|
||||
* to their corresponding Object types if necessary.
|
||||
* @return the type, or {@code null}
|
||||
@@ -235,7 +286,7 @@ public class TypeDescriptor {
|
||||
|
||||
/**
|
||||
* Narrows this {@link TypeDescriptor} by setting its type to the class of the provided value.
|
||||
* If the value is {@code null}, no narrowing is performed and this TypeDescriptor is returned unchanged.
|
||||
* <p>If the value is {@code null}, no narrowing is performed and this TypeDescriptor is returned unchanged.
|
||||
* <p>Designed to be called by binding frameworks when they read property, field, or method return values.
|
||||
* Allows such frameworks to narrow a TypeDescriptor built from a declared property, field, or method return value type.
|
||||
* For example, a field declared as {@code java.lang.Object} would be narrowed to {@code java.util.HashMap}
|
||||
@@ -255,7 +306,6 @@ public class TypeDescriptor {
|
||||
/**
|
||||
* Cast this {@link TypeDescriptor} to a superclass or implemented interface
|
||||
* preserving annotations and nested type context.
|
||||
*
|
||||
* @param superType the super type to cast to (can be {@code null}
|
||||
* @return a new TypeDescriptor for the up-cast type
|
||||
* @throws IllegalArgumentException if this type is not assignable to the super-type
|
||||
@@ -324,7 +374,7 @@ public class TypeDescriptor {
|
||||
|
||||
/**
|
||||
* Returns true if an object of this type descriptor can be assigned to the location described by the given type descriptor.
|
||||
* For example, valueOf(String.class).isAssignableTo(valueOf(CharSequence.class)) returns true because a String value can be assigned to a CharSequence variable.
|
||||
* <p>For example, valueOf(String.class).isAssignableTo(valueOf(CharSequence.class)) returns true because a String value can be assigned to a CharSequence variable.
|
||||
* On the other hand, valueOf(Number.class).isAssignableTo(valueOf(Integer.class)) returns false because, while all Integers are Numbers, not all Numbers are Integers.
|
||||
* <p>
|
||||
* For arrays, collections, and maps, element and key/value types are checked if declared.
|
||||
@@ -382,10 +432,10 @@ public class TypeDescriptor {
|
||||
|
||||
/**
|
||||
* If this type is a {@link Collection} or an Array, creates a element TypeDescriptor from the provided collection or array element.
|
||||
* Narrows the {@link #getElementTypeDescriptor() elementType} property to the class of the provided collection or array element.
|
||||
* <p>Narrows the {@link #getElementTypeDescriptor() elementType} property to the class of the provided collection or array element.
|
||||
* For example, if this describes a java.util.List<java.lang.Number< and the element argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer.
|
||||
* If this describes a java.util.List<?> and the element argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer as well.
|
||||
* Annotation and nested type context will be preserved in the narrowed TypeDescriptor that is returned.
|
||||
* <p>Annotation and nested type context will be preserved in the narrowed TypeDescriptor that is returned.
|
||||
* @param element the collection or array element
|
||||
* @return a element type descriptor, narrowed to the type of the provided element
|
||||
* @throws IllegalStateException if this type is not a java.util.Collection or Array type
|
||||
@@ -417,10 +467,10 @@ public class TypeDescriptor {
|
||||
|
||||
/**
|
||||
* If this type is a {@link Map}, creates a mapKey {@link TypeDescriptor} from the provided map key.
|
||||
* Narrows the {@link #getMapKeyTypeDescriptor() mapKeyType} property to the class of the provided map key.
|
||||
* <p>Narrows the {@link #getMapKeyTypeDescriptor() mapKeyType} property to the class of the provided map key.
|
||||
* For example, if this describes a java.util.Map<java.lang.Number, java.lang.String< and the key argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer.
|
||||
* If this describes a java.util.Map<?, ?> and the key argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer as well.
|
||||
* Annotation and nested type context will be preserved in the narrowed TypeDescriptor that is returned.
|
||||
* <p>If this describes a java.util.Map<?, ?> and the key argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer as well.
|
||||
* <p>Annotation and nested type context will be preserved in the narrowed TypeDescriptor that is returned.
|
||||
* @param mapKey the map key
|
||||
* @return the map key type descriptor
|
||||
* @throws IllegalStateException if this type is not a java.util.Map
|
||||
@@ -432,7 +482,7 @@ public class TypeDescriptor {
|
||||
|
||||
/**
|
||||
* If this type is a {@link Map} and its value type is parameterized, returns the map's value type.
|
||||
* If the Map's value type is not parameterized, returns null indicating the value type is not declared.
|
||||
* <p>If the Map's value type is not parameterized, returns null indicating the value type is not declared.
|
||||
* @return the Map value type, or {@code null} if this type is a Map but its value type is not parameterized
|
||||
* @throws IllegalStateException if this type is not a java.util.Map
|
||||
*/
|
||||
@@ -443,10 +493,10 @@ public class TypeDescriptor {
|
||||
|
||||
/**
|
||||
* If this type is a {@link Map}, creates a mapValue {@link TypeDescriptor} from the provided map value.
|
||||
* Narrows the {@link #getMapValueTypeDescriptor() mapValueType} property to the class of the provided map value.
|
||||
* <p>Narrows the {@link #getMapValueTypeDescriptor() mapValueType} property to the class of the provided map value.
|
||||
* For example, if this describes a java.util.Map<java.lang.String, java.lang.Number< and the value argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer.
|
||||
* If this describes a java.util.Map<?, ?> and the value argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer as well.
|
||||
* Annotation and nested type context will be preserved in the narrowed TypeDescriptor that is returned.
|
||||
* <p>Annotation and nested type context will be preserved in the narrowed TypeDescriptor that is returned.
|
||||
* @param mapValue the map value
|
||||
* @return the map value type descriptor
|
||||
* @throws IllegalStateException if this type is not a java.util.Map
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -18,6 +18,7 @@ package org.springframework.core.convert.support;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.converter.ConverterFactory;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Converts from a String to a java.lang.Enum by calling {@link Enum#valueOf(Class, String)}.
|
||||
@@ -29,7 +30,13 @@ import org.springframework.core.convert.converter.ConverterFactory;
|
||||
final class StringToEnumConverterFactory implements ConverterFactory<String, Enum> {
|
||||
|
||||
public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
|
||||
return new StringToEnum(targetType);
|
||||
Class<?> enumType = targetType;
|
||||
while(enumType != null && !enumType.isEnum()) {
|
||||
enumType = enumType.getSuperclass();
|
||||
}
|
||||
Assert.notNull(enumType, "The target type " + targetType.getName()
|
||||
+ " does not refer to an enum");
|
||||
return new StringToEnum(enumType);
|
||||
}
|
||||
|
||||
private class StringToEnum<T extends Enum> implements Converter<String, T> {
|
||||
|
||||
@@ -41,4 +41,4 @@ public class MapPropertySource extends EnumerablePropertySource<Map<String, Obje
|
||||
return this.source.keySet().toArray(EMPTY_NAMES_ARRAY);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -31,13 +31,12 @@ import java.util.Properties;
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @since 3.1
|
||||
* @see org.springframework.mock.env.MockPropertySource
|
||||
*/
|
||||
public class PropertiesPropertySource extends MapPropertySource {
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public PropertiesPropertySource(String name, Properties source) {
|
||||
super(name, (Map)source);
|
||||
super(name, (Map) source);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -17,17 +17,21 @@
|
||||
package org.springframework.core.env;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Read-only {@code Map<String, String>} implementation that is backed by system properties or environment
|
||||
* variables.
|
||||
* Read-only {@code Map<String, String>} implementation that is backed by system
|
||||
* properties or environment variables.
|
||||
*
|
||||
* <p>Used by {@link AbstractApplicationContext} when a {@link SecurityManager} prohibits access to {@link
|
||||
* System#getProperties()} or {@link System#getenv()}.
|
||||
* <p>Used by {@link AbstractApplicationContext} when a {@link SecurityManager} prohibits
|
||||
* access to {@link System#getProperties()} or {@link System#getenv()}. It is for this
|
||||
* reason that the implementations of {@link #keySet()}, {@link #entrySet()}, and
|
||||
* {@link #values()} always return empty even though {@link #get(Object)} may in fact
|
||||
* return non-null if the current security manager allows access to individual keys.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Chris Beams
|
||||
@@ -85,7 +89,7 @@ abstract class ReadOnlySystemAttributesMap implements Map<String, String> {
|
||||
}
|
||||
|
||||
public Set<String> keySet() {
|
||||
throw new UnsupportedOperationException();
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
public void putAll(Map<? extends String, ? extends String> m) {
|
||||
@@ -93,11 +97,11 @@ abstract class ReadOnlySystemAttributesMap implements Map<String, String> {
|
||||
}
|
||||
|
||||
public Collection<String> values() {
|
||||
throw new UnsupportedOperationException();
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
public Set<Entry<String, String>> entrySet() {
|
||||
throw new UnsupportedOperationException();
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -83,4 +83,4 @@ class SimpleCommandLineArgsParser {
|
||||
return commandLineArgs;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -76,7 +76,7 @@ public class SystemEnvironmentPropertySource extends MapPropertySource {
|
||||
*/
|
||||
@Override
|
||||
public boolean containsProperty(String name) {
|
||||
return resolvePropertyName(name) != null;
|
||||
return getProperty(name) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,10 +88,6 @@ public class SystemEnvironmentPropertySource extends MapPropertySource {
|
||||
public Object getProperty(String name) {
|
||||
Assert.notNull(name, "property name must not be null");
|
||||
String actualName = resolvePropertyName(name);
|
||||
if (actualName == null) {
|
||||
// at this point we know the property does not exist
|
||||
return null;
|
||||
}
|
||||
if (logger.isDebugEnabled() && !name.equals(actualName)) {
|
||||
logger.debug(String.format(
|
||||
"PropertySource [%s] does not contain '%s', but found equivalent '%s'",
|
||||
@@ -102,8 +98,8 @@ public class SystemEnvironmentPropertySource extends MapPropertySource {
|
||||
|
||||
/**
|
||||
* Check to see if this property source contains a property with the given name, or
|
||||
* any underscore / uppercase variation thereof. Return the resolved name or
|
||||
* {@code null} if none found.
|
||||
* any underscore / uppercase variation thereof. Return the resolved name if one is
|
||||
* found or otherwise the original name. Never returns {@code null}.
|
||||
*/
|
||||
private String resolvePropertyName(String name) {
|
||||
if (super.containsProperty(name)) {
|
||||
@@ -127,6 +123,6 @@ public class SystemEnvironmentPropertySource extends MapPropertySource {
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,18 +205,14 @@ public class ClassPathResource extends AbstractFileResolvingResource {
|
||||
*/
|
||||
public String getDescription() {
|
||||
StringBuilder builder = new StringBuilder("class path resource [");
|
||||
|
||||
String pathToUse = path;
|
||||
|
||||
if (this.clazz != null && !pathToUse.startsWith("/")) {
|
||||
builder.append(ClassUtils.classPackageAsResourcePath(this.clazz));
|
||||
builder.append('/');
|
||||
}
|
||||
|
||||
if (pathToUse.startsWith("/")) {
|
||||
pathToUse = pathToUse.substring(1);
|
||||
}
|
||||
|
||||
builder.append(pathToUse);
|
||||
builder.append(']');
|
||||
return builder.toString();
|
||||
@@ -232,9 +228,9 @@ public class ClassPathResource extends AbstractFileResolvingResource {
|
||||
}
|
||||
if (obj instanceof ClassPathResource) {
|
||||
ClassPathResource otherRes = (ClassPathResource) obj;
|
||||
return (this.path.equals(otherRes.path)
|
||||
&& ObjectUtils.nullSafeEquals(this.classLoader, otherRes.classLoader) && ObjectUtils.nullSafeEquals(
|
||||
this.clazz, otherRes.clazz));
|
||||
return (this.path.equals(otherRes.path) &&
|
||||
ObjectUtils.nullSafeEquals(this.classLoader, otherRes.classLoader) &&
|
||||
ObjectUtils.nullSafeEquals(this.clazz, otherRes.clazz));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -257,4 +257,4 @@ public abstract class VfsUtils {
|
||||
protected static String doGetPath(Object resource) {
|
||||
return (String) ReflectionUtils.invokeMethod(VIRTUAL_FILE_METHOD_GET_PATH_NAME, resource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -17,8 +17,10 @@
|
||||
package org.springframework.core.io.support;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -39,7 +41,9 @@ public class EncodedResource {
|
||||
|
||||
private final Resource resource;
|
||||
|
||||
private final String encoding;
|
||||
private String encoding;
|
||||
|
||||
private Charset charset;
|
||||
|
||||
|
||||
/**
|
||||
@@ -48,7 +52,8 @@ public class EncodedResource {
|
||||
* @param resource the Resource to hold
|
||||
*/
|
||||
public EncodedResource(Resource resource) {
|
||||
this(resource, null);
|
||||
Assert.notNull(resource, "Resource must not be null");
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,6 +68,18 @@ public class EncodedResource {
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new EncodedResource for the given Resource,
|
||||
* using the specified encoding.
|
||||
* @param resource the Resource to hold
|
||||
* @param charset the charset to use for reading from the resource
|
||||
*/
|
||||
public EncodedResource(Resource resource, Charset charset) {
|
||||
Assert.notNull(resource, "Resource must not be null");
|
||||
this.resource = resource;
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the Resource held.
|
||||
@@ -79,13 +96,36 @@ public class EncodedResource {
|
||||
return this.encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the charset to use for reading from the resource,
|
||||
* or {@code null} if none specified.
|
||||
*/
|
||||
public final Charset getCharset() {
|
||||
return this.charset;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine whether a {@link Reader} is required as opposed to an {@link InputStream},
|
||||
* i.e. whether an encoding or a charset has been specified.
|
||||
* @see #getReader()
|
||||
* @see #getInputStream()
|
||||
*/
|
||||
public boolean requiresReader() {
|
||||
return (this.encoding != null || this.charset != null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a {@code java.io.Reader} for the specified resource,
|
||||
* using the specified encoding (if any).
|
||||
* @throws IOException if opening the Reader failed
|
||||
* @see #requiresReader()
|
||||
*/
|
||||
public Reader getReader() throws IOException {
|
||||
if (this.encoding != null) {
|
||||
if (this.charset != null) {
|
||||
return new InputStreamReader(this.resource.getInputStream(), this.charset);
|
||||
}
|
||||
else if (this.encoding != null) {
|
||||
return new InputStreamReader(this.resource.getInputStream(), this.encoding);
|
||||
}
|
||||
else {
|
||||
@@ -93,6 +133,16 @@ public class EncodedResource {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Open an {@code java.io.InputStream} for the specified resource,
|
||||
* typically assuming that there is no specific encoding to use.
|
||||
* @throws IOException if opening the InputStream failed
|
||||
* @see #requiresReader()
|
||||
*/
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return this.resource.getInputStream();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -658,6 +658,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
|
||||
/**
|
||||
* VFS visitor for path matching purposes.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
private static class PatternVirtualFileVisitor implements InvocationHandler {
|
||||
|
||||
private final String subPattern;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -17,8 +17,6 @@
|
||||
package org.springframework.core.io.support;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -39,9 +37,6 @@ import org.springframework.util.PropertiesPersister;
|
||||
*/
|
||||
public abstract class PropertiesLoaderSupport {
|
||||
|
||||
public static final String XML_FILE_EXTENSION = ".xml";
|
||||
|
||||
|
||||
/** Logger available to subclasses */
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
@@ -167,7 +162,7 @@ public abstract class PropertiesLoaderSupport {
|
||||
/**
|
||||
* Load properties into the given instance.
|
||||
* @param props the Properties instance to load into
|
||||
* @throws java.io.IOException in case of I/O errors
|
||||
* @throws IOException in case of I/O errors
|
||||
* @see #setLocations
|
||||
*/
|
||||
protected void loadProperties(Properties props) throws IOException {
|
||||
@@ -176,21 +171,9 @@ public abstract class PropertiesLoaderSupport {
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Loading properties file from " + location);
|
||||
}
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = location.getInputStream();
|
||||
String filename = location.getFilename();
|
||||
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
|
||||
this.propertiesPersister.loadFromXml(props, is);
|
||||
}
|
||||
else {
|
||||
if (this.fileEncoding != null) {
|
||||
this.propertiesPersister.load(props, new InputStreamReader(is, this.fileEncoding));
|
||||
}
|
||||
else {
|
||||
this.propertiesPersister.load(props, is);
|
||||
}
|
||||
}
|
||||
PropertiesLoaderUtils.fillProperties(
|
||||
props, new EncodedResource(location, this.fileEncoding), this.propertiesPersister);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
if (this.ignoreResourceNotFound) {
|
||||
@@ -202,11 +185,6 @@ public abstract class PropertiesLoaderSupport {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
finally {
|
||||
if (is != null) {
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -18,6 +18,7 @@ package org.springframework.core.io.support;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.Enumeration;
|
||||
@@ -26,6 +27,8 @@ import java.util.Properties;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.DefaultPropertiesPersister;
|
||||
import org.springframework.util.PropertiesPersister;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
|
||||
/**
|
||||
@@ -42,11 +45,76 @@ import org.springframework.util.ResourceUtils;
|
||||
*/
|
||||
public abstract class PropertiesLoaderUtils {
|
||||
|
||||
private static final String XML_FILE_EXTENSION = ".xml";
|
||||
|
||||
|
||||
/**
|
||||
* Load properties from the given resource.
|
||||
* Load properties from the given EncodedResource,
|
||||
* potentially defining a specific encoding for the properties file.
|
||||
* @see #fillProperties(java.util.Properties, EncodedResource)
|
||||
*/
|
||||
public static Properties loadProperties(EncodedResource resource) throws IOException {
|
||||
Properties props = new Properties();
|
||||
fillProperties(props, resource);
|
||||
return props;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill the given properties from the given EncodedResource,
|
||||
* potentially defining a specific encoding for the properties file.
|
||||
* @param props the Properties instance to load into
|
||||
* @param resource the resource to load from
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
public static void fillProperties(Properties props, EncodedResource resource)
|
||||
throws IOException {
|
||||
|
||||
fillProperties(props, resource, new DefaultPropertiesPersister());
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually load properties from the given EncodedResource into the given Properties instance.
|
||||
* @param props the Properties instance to load into
|
||||
* @param resource the resource to load from
|
||||
* @param persister the PropertiesPersister to use
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
static void fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister)
|
||||
throws IOException {
|
||||
|
||||
InputStream stream = null;
|
||||
Reader reader = null;
|
||||
try {
|
||||
String filename = resource.getResource().getFilename();
|
||||
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
|
||||
stream = resource.getInputStream();
|
||||
persister.loadFromXml(props, stream);
|
||||
}
|
||||
else if (resource.requiresReader()) {
|
||||
reader = resource.getReader();
|
||||
persister.load(props, reader);
|
||||
}
|
||||
else {
|
||||
stream = resource.getInputStream();
|
||||
persister.load(props, stream);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
if (stream != null) {
|
||||
stream.close();
|
||||
}
|
||||
if (reader != null) {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load properties from the given resource (in ISO-8859-1 encoding).
|
||||
* @param resource the resource to load from
|
||||
* @return the populated Properties instance
|
||||
* @throws IOException if loading failed
|
||||
* @see #fillProperties(java.util.Properties, Resource)
|
||||
*/
|
||||
public static Properties loadProperties(Resource resource) throws IOException {
|
||||
Properties props = new Properties();
|
||||
@@ -55,7 +123,7 @@ public abstract class PropertiesLoaderUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill the given properties from the given resource.
|
||||
* Fill the given properties from the given resource (in ISO-8859-1 encoding).
|
||||
* @param props the Properties instance to fill
|
||||
* @param resource the resource to load from
|
||||
* @throws IOException if loading failed
|
||||
@@ -63,7 +131,13 @@ public abstract class PropertiesLoaderUtils {
|
||||
public static void fillProperties(Properties props, Resource resource) throws IOException {
|
||||
InputStream is = resource.getInputStream();
|
||||
try {
|
||||
props.load(is);
|
||||
String filename = resource.getFilename();
|
||||
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
|
||||
props.loadFromXML(is);
|
||||
}
|
||||
else {
|
||||
props.load(is);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
is.close();
|
||||
@@ -71,8 +145,8 @@ public abstract class PropertiesLoaderUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Load all properties from the given class path resource,
|
||||
* using the default class loader.
|
||||
* Load all properties from the specified class path resource
|
||||
* (in ISO-8859-1 encoding), using the default class loader.
|
||||
* <p>Merges properties if more than one resource of the same name
|
||||
* found in the class path.
|
||||
* @param resourceName the name of the class path resource
|
||||
@@ -84,8 +158,8 @@ public abstract class PropertiesLoaderUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Load all properties from the given class path resource,
|
||||
* using the given class loader.
|
||||
* Load all properties from the specified class path resource
|
||||
* (in ISO-8859-1 encoding), using the given class loader.
|
||||
* <p>Merges properties if more than one resource of the same name
|
||||
* found in the class path.
|
||||
* @param resourceName the name of the class path resource
|
||||
@@ -100,24 +174,26 @@ public abstract class PropertiesLoaderUtils {
|
||||
if (clToUse == null) {
|
||||
clToUse = ClassUtils.getDefaultClassLoader();
|
||||
}
|
||||
Properties properties = new Properties();
|
||||
Properties props = new Properties();
|
||||
Enumeration urls = clToUse.getResources(resourceName);
|
||||
while (urls.hasMoreElements()) {
|
||||
URL url = (URL) urls.nextElement();
|
||||
InputStream is = null;
|
||||
URLConnection con = url.openConnection();
|
||||
ResourceUtils.useCachesIfNecessary(con);
|
||||
InputStream is = con.getInputStream();
|
||||
try {
|
||||
URLConnection con = url.openConnection();
|
||||
ResourceUtils.useCachesIfNecessary(con);
|
||||
is = con.getInputStream();
|
||||
properties.load(is);
|
||||
}
|
||||
finally {
|
||||
if (is != null) {
|
||||
is.close();
|
||||
if (resourceName != null && resourceName.endsWith(XML_FILE_EXTENSION)) {
|
||||
props.loadFromXML(is);
|
||||
}
|
||||
else {
|
||||
props.load(is);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
return properties;
|
||||
return props;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -17,30 +17,51 @@
|
||||
package org.springframework.core.io.support;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.core.env.PropertiesPropertySource;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Subclass of {@link PropertiesPropertySource} that loads a {@link Properties}
|
||||
* object from a given {@link org.springframework.core.io.Resource} or resource location such as
|
||||
* {@code "classpath:/com/myco/foo.properties"} or {@code "file:/path/to/file.properties"}.
|
||||
* {@code "classpath:/com/myco/foo.properties"} or {@code "file:/path/to/file.xml"}.
|
||||
* Both traditional and XML-based properties file formats are supported, however in order
|
||||
* for XML processing to take effect, the underlying {@code Resource}'s
|
||||
* {@link org.springframework.core.io.Resource#getFilename() getFilename()} method must
|
||||
* return non-{@code null} and end in ".xml".
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.1
|
||||
*/
|
||||
public class ResourcePropertySource extends PropertiesPropertySource {
|
||||
|
||||
/**
|
||||
* Create a PropertySource having the given name based on Properties
|
||||
* loaded from the given resource.
|
||||
* loaded from the given encoded resource.
|
||||
*/
|
||||
public ResourcePropertySource(String name, EncodedResource resource) throws IOException {
|
||||
super(name, PropertiesLoaderUtils.loadProperties(resource));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a PropertySource based on Properties loaded from the given resource.
|
||||
* The name of the PropertySource will be generated based on the
|
||||
* {@link Resource#getDescription() description} of the given resource.
|
||||
*/
|
||||
public ResourcePropertySource(EncodedResource resource) throws IOException {
|
||||
this(getNameForResource(resource.getResource()), resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a PropertySource having the given name based on Properties
|
||||
* loaded from the given encoded resource.
|
||||
*/
|
||||
public ResourcePropertySource(String name, Resource resource) throws IOException {
|
||||
super(name, loadPropertiesForResource(resource));
|
||||
super(name, PropertiesLoaderUtils.loadProperties(new EncodedResource(resource)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,17 +79,7 @@ public class ResourcePropertySource extends PropertiesPropertySource {
|
||||
* resource (assuming it is prefixed with {@code classpath:}).
|
||||
*/
|
||||
public ResourcePropertySource(String name, String location, ClassLoader classLoader) throws IOException {
|
||||
this(name, getResourceForLocation(location, classLoader));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a PropertySource having the given name based on Properties loaded from
|
||||
* the given resource location. The default thread context class loader will be
|
||||
* used to load the resource (assuming the location string is prefixed with
|
||||
* {@code classpath:}.
|
||||
*/
|
||||
public ResourcePropertySource(String name, String location) throws IOException {
|
||||
this(name, location, ClassUtils.getDefaultClassLoader());
|
||||
this(name, new DefaultResourceLoader(classLoader).getResource(location));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,7 +90,17 @@ public class ResourcePropertySource extends PropertiesPropertySource {
|
||||
* resource.
|
||||
*/
|
||||
public ResourcePropertySource(String location, ClassLoader classLoader) throws IOException {
|
||||
this(getResourceForLocation(location, classLoader));
|
||||
this(new DefaultResourceLoader(classLoader).getResource(location));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a PropertySource having the given name based on Properties loaded from
|
||||
* the given resource location. The default thread context class loader will be
|
||||
* used to load the resource (assuming the location string is prefixed with
|
||||
* {@code classpath:}.
|
||||
*/
|
||||
public ResourcePropertySource(String name, String location) throws IOException {
|
||||
this(name, new DefaultResourceLoader().getResource(location));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,28 +109,12 @@ public class ResourcePropertySource extends PropertiesPropertySource {
|
||||
* {@link Resource#getDescription() description} of the resource.
|
||||
*/
|
||||
public ResourcePropertySource(String location) throws IOException {
|
||||
this(getResourceForLocation(location, ClassUtils.getDefaultClassLoader()));
|
||||
this(new DefaultResourceLoader().getResource(location));
|
||||
}
|
||||
|
||||
|
||||
private static Resource getResourceForLocation(String location, ClassLoader classLoader) {
|
||||
return new PathMatchingResourcePatternResolver(classLoader).getResource(location);
|
||||
}
|
||||
|
||||
private static Properties loadPropertiesForResource(Resource resource) throws IOException {
|
||||
Properties props = new Properties();
|
||||
InputStream is = resource.getInputStream();
|
||||
props.load(is);
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException ex) {
|
||||
// ignore
|
||||
}
|
||||
return props;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the description string for the resource, and if empty returns
|
||||
* Return the description string for the resource, and if empty returns
|
||||
* the class name of the resource plus its identity hash code.
|
||||
*/
|
||||
private static String getNameForResource(Resource resource) {
|
||||
|
||||
@@ -258,4 +258,4 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib
|
||||
this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,4 +97,4 @@ final class MethodMetadataReadingVisitor extends MethodVisitor implements Method
|
||||
public String getDeclaringClassName() {
|
||||
return this.declaringClassName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -21,6 +21,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.springframework.asm.ClassReader;
|
||||
import org.springframework.core.NestedIOException;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.core.type.ClassMetadata;
|
||||
@@ -47,10 +48,14 @@ final class SimpleMetadataReader implements MetadataReader {
|
||||
|
||||
SimpleMetadataReader(Resource resource, ClassLoader classLoader) throws IOException {
|
||||
InputStream is = new BufferedInputStream(resource.getInputStream());
|
||||
ClassReader classReader = null;
|
||||
ClassReader classReader;
|
||||
try {
|
||||
classReader = new ClassReader(is);
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
throw new NestedIOException("ASM ClassReader failed to parse class file - " +
|
||||
"probably due to a new Java class file version that isn't supported yet: " + resource, ex);
|
||||
}
|
||||
finally {
|
||||
is.close();
|
||||
}
|
||||
@@ -64,6 +69,7 @@ final class SimpleMetadataReader implements MetadataReader {
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
|
||||
public Resource getResource() {
|
||||
return this.resource;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -60,12 +60,18 @@ public class AntPathMatcher implements PathMatcher {
|
||||
private final Map<String, AntPathStringMatcher> stringMatcherCache =
|
||||
new ConcurrentHashMap<String, AntPathStringMatcher>(256);
|
||||
|
||||
private boolean trimTokens = true;
|
||||
|
||||
|
||||
/** Set the path separator to use for pattern parsing. Default is "/", as in Ant. */
|
||||
public void setPathSeparator(String pathSeparator) {
|
||||
this.pathSeparator = (pathSeparator != null ? pathSeparator : DEFAULT_PATH_SEPARATOR);
|
||||
}
|
||||
|
||||
/** Whether to trim tokenized paths and patterns. */
|
||||
public void setTrimTokens(boolean trimTokens) {
|
||||
this.trimTokens = trimTokens;
|
||||
}
|
||||
|
||||
public boolean isPattern(String path) {
|
||||
return (path.indexOf('*') != -1 || path.indexOf('?') != -1);
|
||||
@@ -95,8 +101,8 @@ public class AntPathMatcher implements PathMatcher {
|
||||
return false;
|
||||
}
|
||||
|
||||
String[] pattDirs = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator);
|
||||
String[] pathDirs = StringUtils.tokenizeToStringArray(path, this.pathSeparator);
|
||||
String[] pattDirs = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, this.trimTokens, true);
|
||||
String[] pathDirs = StringUtils.tokenizeToStringArray(path, this.pathSeparator, this.trimTokens, true);
|
||||
|
||||
int pattIdxStart = 0;
|
||||
int pattIdxEnd = pattDirs.length - 1;
|
||||
@@ -246,8 +252,8 @@ public class AntPathMatcher implements PathMatcher {
|
||||
* does <strong>not</strong> enforce this.
|
||||
*/
|
||||
public String extractPathWithinPattern(String pattern, String path) {
|
||||
String[] patternParts = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator);
|
||||
String[] pathParts = StringUtils.tokenizeToStringArray(path, this.pathSeparator);
|
||||
String[] patternParts = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, this.trimTokens, true);
|
||||
String[] pathParts = StringUtils.tokenizeToStringArray(path, this.pathSeparator, this.trimTokens, true);
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
@@ -311,7 +317,9 @@ public class AntPathMatcher implements PathMatcher {
|
||||
else if (!StringUtils.hasText(pattern2)) {
|
||||
return pattern1;
|
||||
}
|
||||
else if (!pattern1.equals(pattern2) && !pattern1.contains("{") && match(pattern1, pattern2)) {
|
||||
|
||||
boolean pattern1ContainsUriVar = pattern1.indexOf('{') != -1;
|
||||
if (!pattern1.equals(pattern2) && !pattern1ContainsUriVar && match(pattern1, pattern2)) {
|
||||
// /* + /hotel -> /hotel ; "/*.*" + "/*.html" -> /*.html
|
||||
// However /user + /user -> /usr/user ; /{foo} + /bar -> /{foo}/bar
|
||||
return pattern2;
|
||||
@@ -338,7 +346,7 @@ public class AntPathMatcher implements PathMatcher {
|
||||
}
|
||||
else {
|
||||
int dotPos1 = pattern1.indexOf('.');
|
||||
if (dotPos1 == -1) {
|
||||
if (dotPos1 == -1 || pattern1ContainsUriVar) {
|
||||
// simply concatenate the two patterns
|
||||
if (pattern1.endsWith("/") || pattern2.startsWith("/")) {
|
||||
return pattern1 + pattern2;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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
|
||||
@@ -334,8 +334,9 @@ public abstract class Assert {
|
||||
public static void isInstanceOf(Class type, Object obj, String message) {
|
||||
notNull(type, "Type to check against must not be null");
|
||||
if (!type.isInstance(obj)) {
|
||||
throw new IllegalArgumentException(message +
|
||||
". Object of class [" + (obj != null ? obj.getClass().getName() : "null") +
|
||||
throw new IllegalArgumentException(
|
||||
(StringUtils.hasLength(message) ? message + " " : "") +
|
||||
"Object of class [" + (obj != null ? obj.getClass().getName() : "null") +
|
||||
"] must be an instance of " + type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,4 +72,4 @@ public class CompositeIterator<E> implements Iterator<E> {
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("Remove is not supported");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -33,8 +33,8 @@ import java.util.Properties;
|
||||
*
|
||||
* <p>Allows for reading from any Reader and writing to any Writer, for example
|
||||
* to specify a charset for a properties file. This is a capability that standard
|
||||
* {@code java.util.Properties} unfortunately lacks up until JDK 1.5:
|
||||
* You can only load files using the ISO-8859-1 charset there.
|
||||
* {@code java.util.Properties} unfortunately lacked up until JDK 1.5:
|
||||
* You were only able to load files using the ISO-8859-1 charset there.
|
||||
*
|
||||
* <p>Loading from and storing to a stream delegates to {@code Properties.load}
|
||||
* and {@code Properties.store}, respectively, to be fully compatible with
|
||||
@@ -49,20 +49,11 @@ import java.util.Properties;
|
||||
* an encoding for a Reader/Writer (like ReloadableResourceBundleMessageSource's
|
||||
* "defaultEncoding" and "fileEncodings" properties).
|
||||
*
|
||||
* <p>As of Spring 1.2.2, this implementation also supports properties XML files,
|
||||
* through the {@code loadFromXml} and {@code storeToXml} methods.
|
||||
* The default implementations delegate to JDK 1.5's corresponding methods,
|
||||
* throwing an exception if running on an older JDK. Those implementations
|
||||
* could be subclassed to apply custom XML handling on JDK 1.4, for example.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 10.03.2004
|
||||
* @see java.util.Properties
|
||||
* @see java.util.Properties#load
|
||||
* @see java.util.Properties#store
|
||||
* @see org.springframework.context.support.ReloadableResourceBundleMessageSource#setPropertiesPersister
|
||||
* @see org.springframework.context.support.ReloadableResourceBundleMessageSource#setDefaultEncoding
|
||||
* @see org.springframework.context.support.ReloadableResourceBundleMessageSource#setFileEncodings
|
||||
*/
|
||||
public class DefaultPropertiesPersister implements PropertiesPersister {
|
||||
|
||||
@@ -228,30 +219,15 @@ public class DefaultPropertiesPersister implements PropertiesPersister {
|
||||
|
||||
|
||||
public void loadFromXml(Properties props, InputStream is) throws IOException {
|
||||
try {
|
||||
props.loadFromXML(is);
|
||||
}
|
||||
catch (NoSuchMethodError err) {
|
||||
throw new IOException("Cannot load properties XML file - not running on JDK 1.5+: " + err.getMessage());
|
||||
}
|
||||
props.loadFromXML(is);
|
||||
}
|
||||
|
||||
public void storeToXml(Properties props, OutputStream os, String header) throws IOException {
|
||||
try {
|
||||
props.storeToXML(os, header);
|
||||
}
|
||||
catch (NoSuchMethodError err) {
|
||||
throw new IOException("Cannot store properties XML file - not running on JDK 1.5+: " + err.getMessage());
|
||||
}
|
||||
props.storeToXML(os, header);
|
||||
}
|
||||
|
||||
public void storeToXml(Properties props, OutputStream os, String header, String encoding) throws IOException {
|
||||
try {
|
||||
props.storeToXML(os, header, encoding);
|
||||
}
|
||||
catch (NoSuchMethodError err) {
|
||||
throw new IOException("Cannot store properties XML file - not running on JDK 1.5+: " + err.getMessage());
|
||||
}
|
||||
props.storeToXML(os, header, encoding);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -31,19 +31,19 @@ import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
/**
|
||||
* Simple utility methods for file and stream copying.
|
||||
* All copy methods use a block size of 4096 bytes,
|
||||
* and close all affected streams when done.
|
||||
* Simple utility methods for file and stream copying. All copy methods use a block size
|
||||
* of 4096 bytes, and close all affected streams when done. A variation of the copy
|
||||
* methods from this class that leave streams open can be found in {@link StreamUtils}.
|
||||
*
|
||||
* <p>Mainly for use within the framework,
|
||||
* but also useful for application code.
|
||||
* <p>Mainly for use within the framework, but also useful for application code.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 06.10.2003
|
||||
* @see StreamUtils
|
||||
*/
|
||||
public abstract class FileCopyUtils {
|
||||
|
||||
public static final int BUFFER_SIZE = 4096;
|
||||
public static final int BUFFER_SIZE = StreamUtils.BUFFER_SIZE;
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
@@ -106,15 +106,7 @@ public abstract class FileCopyUtils {
|
||||
Assert.notNull(in, "No InputStream specified");
|
||||
Assert.notNull(out, "No OutputStream specified");
|
||||
try {
|
||||
int byteCount = 0;
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesRead);
|
||||
byteCount += bytesRead;
|
||||
}
|
||||
out.flush();
|
||||
return byteCount;
|
||||
return StreamUtils.copy(in, out);
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
@@ -208,7 +200,7 @@ public abstract class FileCopyUtils {
|
||||
|
||||
/**
|
||||
* Copy the contents of the given String to the given output Writer.
|
||||
* Closes the write when done.
|
||||
* Closes the writer when done.
|
||||
* @param in the String to copy from
|
||||
* @param out the Writer to copy to
|
||||
* @throws IOException in case of I/O errors
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -481,7 +481,7 @@ public abstract class ObjectUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the same value as {@code {@link Boolean#hashCode()}}.
|
||||
* Return the same value as {@link Boolean#hashCode()}}.
|
||||
* @see Boolean#hashCode()
|
||||
*/
|
||||
public static int hashCode(boolean bool) {
|
||||
@@ -489,7 +489,7 @@ public abstract class ObjectUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the same value as {@code {@link Double#hashCode()}}.
|
||||
* Return the same value as {@link Double#hashCode()}}.
|
||||
* @see Double#hashCode()
|
||||
*/
|
||||
public static int hashCode(double dbl) {
|
||||
@@ -498,7 +498,7 @@ public abstract class ObjectUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the same value as {@code {@link Float#hashCode()}}.
|
||||
* Return the same value as {@link Float#hashCode()}}.
|
||||
* @see Float#hashCode()
|
||||
*/
|
||||
public static int hashCode(float flt) {
|
||||
@@ -506,7 +506,7 @@ public abstract class ObjectUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the same value as {@code {@link Long#hashCode()}}.
|
||||
* Return the same value as {@link Long#hashCode()}}.
|
||||
* @see Long#hashCode()
|
||||
*/
|
||||
public static int hashCode(long lng) {
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.util;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FilterInputStream;
|
||||
import java.io.FilterOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
|
||||
/**
|
||||
* Simple utility methods for dealing with streams. The copy methods of this class are
|
||||
* similar to those defined in {@link FileCopyUtils} except that all affected streams are
|
||||
* left open when done. All copy methods use a block size of 4096 bytes.
|
||||
*
|
||||
* <p>Mainly for use within the framework, but also useful for application code.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Phillip Webb
|
||||
* @since 3.2.2
|
||||
* @see FileCopyUtils
|
||||
*/
|
||||
public abstract class StreamUtils {
|
||||
|
||||
public static final int BUFFER_SIZE = 4096;
|
||||
|
||||
|
||||
/**
|
||||
* Copy the contents of the given InputStream into a new byte array.
|
||||
* Leaves the stream open when done.
|
||||
* @param in the stream to copy from
|
||||
* @return the new byte array that has been copied to
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
public static byte[] copyToByteArray(InputStream in) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
|
||||
copy(in, out);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the contents of the given InputStream into a String.
|
||||
* Leaves the stream open when done.
|
||||
* @param in the InputStream to copy from
|
||||
* @return the String that has been copied to
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
public static String copyToString(InputStream in, Charset charset) throws IOException {
|
||||
Assert.notNull(in, "No InputStream specified");
|
||||
StringBuilder out = new StringBuilder();
|
||||
InputStreamReader reader = new InputStreamReader(in, charset);
|
||||
char[] buffer = new char[BUFFER_SIZE];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = reader.read(buffer)) != -1) {
|
||||
out.append(buffer, 0, bytesRead);
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the contents of the given byte array to the given OutputStream.
|
||||
* Leaves the stream open when done.
|
||||
* @param in the byte array to copy from
|
||||
* @param out the OutputStream to copy to
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
public static void copy(byte[] in, OutputStream out) throws IOException {
|
||||
Assert.notNull(in, "No input byte array specified");
|
||||
Assert.notNull(out, "No OutputStream specified");
|
||||
out.write(in);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the contents of the given String to the given output OutputStream.
|
||||
* Leaves the stream open when done.
|
||||
* @param in the String to copy from
|
||||
* @param charset the Charset
|
||||
* @param out the OutputStream to copy to
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
public static void copy(String in, Charset charset, OutputStream out) throws IOException {
|
||||
Assert.notNull(in, "No input String specified");
|
||||
Assert.notNull(charset, "No charset specified");
|
||||
Assert.notNull(out, "No OutputStream specified");
|
||||
Writer writer = new OutputStreamWriter(out, charset);
|
||||
writer.write(in);
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the contents of the given InputStream to the given OutputStream.
|
||||
* Leaves both streams open when done.
|
||||
* @param in the InputStream to copy from
|
||||
* @param out the OutputStream to copy to
|
||||
* @return the number of bytes copied
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
public static int copy(InputStream in, OutputStream out) throws IOException {
|
||||
Assert.notNull(in, "No InputStream specified");
|
||||
Assert.notNull(out, "No OutputStream specified");
|
||||
int byteCount = 0;
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesRead);
|
||||
byteCount += bytesRead;
|
||||
}
|
||||
out.flush();
|
||||
return byteCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a variant of the given {@link InputStream} where calling
|
||||
* {@link InputStream#close() close()} has no effect.
|
||||
* @param in the InputStream to decorate
|
||||
* @return a version of the InputStream that ignores calls to close
|
||||
*/
|
||||
public static InputStream nonClosing(InputStream in) {
|
||||
Assert.notNull(in, "No InputStream specified");
|
||||
return new NonClosingInputStream(in);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a variant of the given {@link OutputStream} where calling
|
||||
* {@link OutputStream#close() close()} has no effect.
|
||||
* @param out the OutputStream to decorate
|
||||
* @return a version of the OutputStream that ignores calls to close
|
||||
*/
|
||||
public static OutputStream nonClosing(OutputStream out) {
|
||||
Assert.notNull(out, "No OutputStream specified");
|
||||
return new NonClosingOutputStream(out);
|
||||
}
|
||||
|
||||
|
||||
private static class NonClosingInputStream extends FilterInputStream {
|
||||
|
||||
public NonClosingInputStream(InputStream in) {
|
||||
super(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class NonClosingOutputStream extends FilterOutputStream {
|
||||
|
||||
public NonClosingOutputStream(OutputStream out) {
|
||||
super(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int let) throws IOException {
|
||||
// It is critical that we override this method for performance
|
||||
out.write(b, off, let);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -24,24 +24,24 @@ import javax.xml.transform.sax.SAXResult;
|
||||
import org.xml.sax.ContentHandler;
|
||||
|
||||
/**
|
||||
* Implementation of the {@code Result} tagging interface for StAX writers. Can be constructed with a
|
||||
* {@code XMLEventConsumer} or a {@code XMLStreamWriter}.
|
||||
* Implementation of the {@code Result} tagging interface for StAX writers. Can be constructed with
|
||||
* an {@code XMLEventConsumer} or an {@code XMLStreamWriter}.
|
||||
*
|
||||
* <p>This class is necessary because there is no implementation of {@code Source} for StaxReaders in JAXP 1.3.
|
||||
* There is a {@code StAXResult} in JAXP 1.4 (JDK 1.6), but this class is kept around for back-ward compatibility
|
||||
* reasons.
|
||||
* <p>This class is necessary because there is no implementation of {@code Source} for StaxReaders
|
||||
* in JAXP 1.3. There is a {@code StAXResult} in JAXP 1.4 (JDK 1.6), but this class is kept around
|
||||
* for backwards compatibility reasons.
|
||||
*
|
||||
* <p>Even though {@code StaxResult} extends from {@code SAXResult}, calling the methods of
|
||||
* {@code SAXResult} is <strong>not supported</strong>. In general, the only supported operation on this class is
|
||||
* to use the {@code ContentHandler} obtained via {@link #getHandler()} to parse an input source using an
|
||||
* {@code XMLReader}. Calling {@link #setHandler(org.xml.sax.ContentHandler)} will result in
|
||||
* {@code UnsupportedOperationException}s.
|
||||
* {@code SAXResult} is <strong>not supported</strong>. In general, the only supported operation
|
||||
* on this class is to use the {@code ContentHandler} obtained via {@link #getHandler()} to parse an
|
||||
* input source using an {@code XMLReader}. Calling {@link #setHandler(org.xml.sax.ContentHandler)}
|
||||
* will result in {@code UnsupportedOperationException}s.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.0
|
||||
* @see XMLEventWriter
|
||||
* @see XMLStreamWriter
|
||||
* @see javax.xml.transform.Transformer
|
||||
* @since 3.0
|
||||
*/
|
||||
class StaxResult extends SAXResult {
|
||||
|
||||
@@ -49,9 +49,9 @@ class StaxResult extends SAXResult {
|
||||
|
||||
private XMLStreamWriter streamWriter;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the {@code StaxResult} with the specified {@code XMLStreamWriter}.
|
||||
*
|
||||
* Construct a new instance of the {@code StaxResult} with the specified {@code XMLStreamWriter}.
|
||||
* @param streamWriter the {@code XMLStreamWriter} to write to
|
||||
*/
|
||||
StaxResult(XMLStreamWriter streamWriter) {
|
||||
@@ -60,8 +60,7 @@ class StaxResult extends SAXResult {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the {@code StaxResult} with the specified {@code XMLEventWriter}.
|
||||
*
|
||||
* Construct a new instance of the {@code StaxResult} with the specified {@code XMLEventWriter}.
|
||||
* @param eventWriter the {@code XMLEventWriter} to write to
|
||||
*/
|
||||
StaxResult(XMLEventWriter eventWriter) {
|
||||
@@ -70,9 +69,8 @@ class StaxResult extends SAXResult {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the {@code StaxResult} with the specified {@code XMLEventWriter} and
|
||||
* {@code XMLEventFactory}.
|
||||
*
|
||||
* Construct a new instance of the {@code StaxResult} with the specified {@code XMLEventWriter}
|
||||
* and {@code XMLEventFactory}.
|
||||
* @param eventWriter the {@code XMLEventWriter} to write to
|
||||
* @param eventFactory the {@code XMLEventFactory} to use for creating events
|
||||
*/
|
||||
@@ -81,35 +79,35 @@ class StaxResult extends SAXResult {
|
||||
this.eventWriter = eventWriter;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the {@code XMLEventWriter} used by this {@code StaxResult}. If this {@code StaxResult} was
|
||||
* created with an {@code XMLStreamWriter}, the result will be {@code null}.
|
||||
*
|
||||
* Return the {@code XMLEventWriter} used by this {@code StaxResult}. If this {@code StaxResult}
|
||||
* was created with an {@code XMLStreamWriter}, the result will be {@code null}.
|
||||
* @return the StAX event writer used by this result
|
||||
* @see #StaxResult(javax.xml.stream.XMLEventWriter)
|
||||
*/
|
||||
XMLEventWriter getXMLEventWriter() {
|
||||
return eventWriter;
|
||||
return this.eventWriter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@code XMLStreamWriter} used by this {@code StaxResult}. If this {@code StaxResult} was
|
||||
* created with an {@code XMLEventConsumer}, the result will be {@code null}.
|
||||
*
|
||||
* Return the {@code XMLStreamWriter} used by this {@code StaxResult}. If this {@code StaxResult}
|
||||
* was created with an {@code XMLEventConsumer}, the result will be {@code null}.
|
||||
* @return the StAX stream writer used by this result
|
||||
* @see #StaxResult(javax.xml.stream.XMLStreamWriter)
|
||||
*/
|
||||
XMLStreamWriter getXMLStreamWriter() {
|
||||
return streamWriter;
|
||||
return this.streamWriter;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Throws a {@code UnsupportedOperationException}.
|
||||
*
|
||||
* Throws an {@code UnsupportedOperationException}.
|
||||
* @throws UnsupportedOperationException always
|
||||
*/
|
||||
@Override
|
||||
public void setHandler(ContentHandler handler) {
|
||||
throw new UnsupportedOperationException("setHandler is not supported");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -24,24 +24,24 @@ import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
|
||||
/**
|
||||
* Implementation of the {@code Source} tagging interface for StAX readers. Can be constructed with a
|
||||
* {@code XMLEventReader} or a {@code XMLStreamReader}.
|
||||
* Implementation of the {@code Source} tagging interface for StAX readers. Can be constructed with
|
||||
* an {@code XMLEventReader} or an {@code XMLStreamReader}.
|
||||
*
|
||||
* <p>This class is necessary because there is no implementation of {@code Source} for StAX Readers in JAXP 1.3.
|
||||
* There is a {@code StAXSource} in JAXP 1.4 (JDK 1.6), but this class is kept around for back-ward compatibility
|
||||
* reasons.
|
||||
* <p>This class is necessary because there is no implementation of {@code Source} for StAX Readers
|
||||
* in JAXP 1.3. There is a {@code StAXSource} in JAXP 1.4 (JDK 1.6), but this class is kept around
|
||||
* for backwards compatibility reasons.
|
||||
*
|
||||
* <p>Even though {@code StaxSource} extends from {@code SAXSource}, calling the methods of
|
||||
* {@code SAXSource} is <strong>not supported</strong>. In general, the only supported operation on this class is
|
||||
* to use the {@code XMLReader} obtained via {@link #getXMLReader()} to parse the input source obtained via {@link
|
||||
* #getInputSource()}. Calling {@link #setXMLReader(XMLReader)} or {@link #setInputSource(InputSource)} will result in
|
||||
* {@code UnsupportedOperationException}s.
|
||||
* {@code SAXSource} is <strong>not supported</strong>. In general, the only supported operation
|
||||
* on this class is to use the {@code XMLReader} obtained via {@link #getXMLReader()} to parse the
|
||||
* input source obtained via {@link #getInputSource()}. Calling {@link #setXMLReader(XMLReader)}
|
||||
* or {@link #setInputSource(InputSource)} will result in {@code UnsupportedOperationException}s.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.0
|
||||
* @see XMLEventReader
|
||||
* @see XMLStreamReader
|
||||
* @see javax.xml.transform.Transformer
|
||||
* @since 3.0
|
||||
*/
|
||||
class StaxSource extends SAXSource {
|
||||
|
||||
@@ -49,11 +49,11 @@ class StaxSource extends SAXSource {
|
||||
|
||||
private XMLStreamReader streamReader;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the {@code StaxSource} with the specified {@code XMLStreamReader}. The
|
||||
* supplied stream reader must be in {@code XMLStreamConstants.START_DOCUMENT} or
|
||||
* Construct a new instance of the {@code StaxSource} with the specified {@code XMLStreamReader}.
|
||||
* The supplied stream reader must be in {@code XMLStreamConstants.START_DOCUMENT} or
|
||||
* {@code XMLStreamConstants.START_ELEMENT} state.
|
||||
*
|
||||
* @param streamReader the {@code XMLStreamReader} to read from
|
||||
* @throws IllegalStateException if the reader is not at the start of a document or element
|
||||
*/
|
||||
@@ -63,10 +63,9 @@ class StaxSource extends SAXSource {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the {@code StaxSource} with the specified {@code XMLEventReader}. The
|
||||
* supplied event reader must be in {@code XMLStreamConstants.START_DOCUMENT} or
|
||||
* Construct a new instance of the {@code StaxSource} with the specified {@code XMLEventReader}.
|
||||
* The supplied event reader must be in {@code XMLStreamConstants.START_DOCUMENT} or
|
||||
* {@code XMLStreamConstants.START_ELEMENT} state.
|
||||
*
|
||||
* @param eventReader the {@code XMLEventReader} to read from
|
||||
* @throws IllegalStateException if the reader is not at the start of a document or element
|
||||
*/
|
||||
@@ -75,31 +74,30 @@ class StaxSource extends SAXSource {
|
||||
this.eventReader = eventReader;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the {@code XMLEventReader} used by this {@code StaxSource}. If this {@code StaxSource} was
|
||||
* created with an {@code XMLStreamReader}, the result will be {@code null}.
|
||||
*
|
||||
* Return the {@code XMLEventReader} used by this {@code StaxSource}. If this {@code StaxSource}
|
||||
* was created with an {@code XMLStreamReader}, the result will be {@code null}.
|
||||
* @return the StAX event reader used by this source
|
||||
* @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader)
|
||||
*/
|
||||
XMLEventReader getXMLEventReader() {
|
||||
return eventReader;
|
||||
return this.eventReader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@code XMLStreamReader} used by this {@code StaxSource}. If this {@code StaxSource} was
|
||||
* created with an {@code XMLEventReader}, the result will be {@code null}.
|
||||
*
|
||||
* Return the {@code XMLStreamReader} used by this {@code StaxSource}. If this {@code StaxSource}
|
||||
* was created with an {@code XMLEventReader}, the result will be {@code null}.
|
||||
* @return the StAX event reader used by this source
|
||||
* @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader)
|
||||
*/
|
||||
XMLStreamReader getXMLStreamReader() {
|
||||
return streamReader;
|
||||
return this.streamReader;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Throws a {@code UnsupportedOperationException}.
|
||||
*
|
||||
* Throws an {@code UnsupportedOperationException}.
|
||||
* @throws UnsupportedOperationException always
|
||||
*/
|
||||
@Override
|
||||
@@ -108,12 +106,12 @@ class StaxSource extends SAXSource {
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws a {@code UnsupportedOperationException}.
|
||||
*
|
||||
* Throws an {@code UnsupportedOperationException}.
|
||||
* @throws UnsupportedOperationException always
|
||||
*/
|
||||
@Override
|
||||
public void setXMLReader(XMLReader reader) {
|
||||
throw new UnsupportedOperationException("setXMLReader is not supported");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -45,14 +45,15 @@ import org.springframework.util.ClassUtils;
|
||||
*/
|
||||
public abstract class StaxUtils {
|
||||
|
||||
// JAXP 1.4 is only available on JDK 1.6+
|
||||
private static boolean jaxp14Available =
|
||||
ClassUtils.isPresent("javax.xml.transform.stax.StAXSource", StaxUtils.class.getClassLoader());
|
||||
|
||||
|
||||
// Stax Source
|
||||
|
||||
/**
|
||||
* Create a custom, non-JAXP 1.4 StAX {@link Source} for the given {@link XMLStreamReader}.
|
||||
*
|
||||
* @param streamReader the StAX stream reader
|
||||
* @return a source wrapping the {@code streamReader}
|
||||
*/
|
||||
@@ -62,9 +63,8 @@ public abstract class StaxUtils {
|
||||
|
||||
/**
|
||||
* Create a StAX {@link Source} for the given {@link XMLStreamReader}.
|
||||
*
|
||||
* <p>If JAXP 1.4 is available, this method returns a {@link StAXSource}; otherwise it returns a
|
||||
* custom StAX Source.
|
||||
* <p>If JAXP 1.4 is available, this method returns a {@link StAXSource};
|
||||
* otherwise it returns a custom StAX Source.
|
||||
* @param streamReader the StAX stream reader
|
||||
* @return a source wrapping the {@code streamReader}
|
||||
* @see #createCustomStaxSource(XMLStreamReader)
|
||||
@@ -80,7 +80,6 @@ public abstract class StaxUtils {
|
||||
|
||||
/**
|
||||
* Create a custom, non-JAXP 1.4 StAX {@link Source} for the given {@link XMLEventReader}.
|
||||
*
|
||||
* @param eventReader the StAX event reader
|
||||
* @return a source wrapping the {@code eventReader}
|
||||
*/
|
||||
@@ -90,9 +89,8 @@ public abstract class StaxUtils {
|
||||
|
||||
/**
|
||||
* Create a StAX {@link Source} for the given {@link XMLEventReader}.
|
||||
*
|
||||
* <p>If JAXP 1.4 is available, this method returns a {@link StAXSource}; otherwise it returns a
|
||||
* custom StAX Source.
|
||||
* <p>If JAXP 1.4 is available, this method returns a {@link StAXSource};
|
||||
* otherwise it returns a custom StAX Source.
|
||||
* @param eventReader the StAX event reader
|
||||
* @return a source wrapping the {@code eventReader}
|
||||
* @throws XMLStreamException in case of StAX errors
|
||||
@@ -116,11 +114,11 @@ public abstract class StaxUtils {
|
||||
return (source instanceof StaxSource || (jaxp14Available && Jaxp14StaxHandler.isStaxSource(source)));
|
||||
}
|
||||
|
||||
|
||||
// Stax Result
|
||||
|
||||
/**
|
||||
* Create a custom, non-JAXP 1.4 StAX {@link Result} for the given {@link XMLStreamWriter}.
|
||||
*
|
||||
* @param streamWriter the StAX stream writer
|
||||
* @return a source wrapping the {@code streamWriter}
|
||||
*/
|
||||
@@ -130,9 +128,8 @@ public abstract class StaxUtils {
|
||||
|
||||
/**
|
||||
* Create a StAX {@link Result} for the given {@link XMLStreamWriter}.
|
||||
*
|
||||
* <p>If JAXP 1.4 is available, this method returns a {@link StAXResult}; otherwise it returns a
|
||||
* custom StAX Result.
|
||||
* <p>If JAXP 1.4 is available, this method returns a {@link StAXResult};
|
||||
* otherwise it returns a custom StAX Result.
|
||||
* @param streamWriter the StAX stream writer
|
||||
* @return a result wrapping the {@code streamWriter}
|
||||
* @see #createCustomStaxResult(XMLStreamWriter)
|
||||
@@ -148,7 +145,6 @@ public abstract class StaxUtils {
|
||||
|
||||
/**
|
||||
* Create a custom, non-JAXP 1.4 StAX {@link Result} for the given {@link XMLEventWriter}.
|
||||
*
|
||||
* @param eventWriter the StAX event writer
|
||||
* @return a source wrapping the {@code eventWriter}
|
||||
*/
|
||||
@@ -158,7 +154,6 @@ public abstract class StaxUtils {
|
||||
|
||||
/**
|
||||
* Create a StAX {@link Result} for the given {@link XMLEventWriter}.
|
||||
*
|
||||
* <p>If JAXP 1.4 is available, this method returns a {@link StAXResult}; otherwise it returns a
|
||||
* custom StAX Result.
|
||||
* @param eventWriter the StAX event writer
|
||||
@@ -177,8 +172,8 @@ public abstract class StaxUtils {
|
||||
|
||||
/**
|
||||
* Indicate whether the given {@link javax.xml.transform.Result} is a StAX Result.
|
||||
* @return {@code true} if {@code result} is a custom Stax Result or JAXP
|
||||
* 1.4 {@link StAXResult}; {@code false} otherwise.
|
||||
* @return {@code true} if {@code result} is a custom Stax Result or JAXP 1.4
|
||||
* {@link StAXResult}; {@code false} otherwise.
|
||||
*/
|
||||
public static boolean isStaxResult(Result result) {
|
||||
return (result instanceof StaxResult || (jaxp14Available && Jaxp14StaxHandler.isStaxResult(result)));
|
||||
@@ -327,6 +322,7 @@ public abstract class StaxUtils {
|
||||
return new XMLEventStreamWriter(eventWriter, eventFactory);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inner class to avoid a static JAXP 1.4 dependency.
|
||||
*/
|
||||
@@ -349,11 +345,11 @@ public abstract class StaxUtils {
|
||||
}
|
||||
|
||||
private static boolean isStaxSource(Source source) {
|
||||
return source instanceof StAXSource;
|
||||
return (source instanceof StAXSource);
|
||||
}
|
||||
|
||||
private static boolean isStaxResult(Result result) {
|
||||
return result instanceof StAXResult;
|
||||
return (result instanceof StAXResult);
|
||||
}
|
||||
|
||||
private static XMLStreamReader getXMLStreamReader(Source source) {
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 21.08.2003
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class DerivedTestBean extends TestBean implements Serializable {
|
||||
|
||||
private String beanName;
|
||||
|
||||
private boolean initialized;
|
||||
|
||||
private boolean destroyed;
|
||||
|
||||
|
||||
public DerivedTestBean() {
|
||||
}
|
||||
|
||||
public DerivedTestBean(String[] names) {
|
||||
if (names == null || names.length < 2) {
|
||||
throw new IllegalArgumentException("Invalid names array");
|
||||
}
|
||||
setName(names[0]);
|
||||
setBeanName(names[1]);
|
||||
}
|
||||
|
||||
public static DerivedTestBean create(String[] names) {
|
||||
return new DerivedTestBean(names);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setBeanName(String beanName) {
|
||||
if (this.beanName == null || beanName == null) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBeanName() {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
public void setSpouseRef(String name) {
|
||||
setSpouse(new TestBean(name));
|
||||
}
|
||||
|
||||
|
||||
public void initialize() {
|
||||
this.initialized = true;
|
||||
}
|
||||
|
||||
public boolean wasInitialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
this.destroyed = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean wasDestroyed() {
|
||||
return destroyed;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,237 +0,0 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Interface used for {@link org.springframework.beans.TestBean}.
|
||||
*
|
||||
* <p>Two methods are the same as on Person, but if this
|
||||
* extends person it breaks quite a few tests..
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public interface ITestBean {
|
||||
|
||||
int getAge();
|
||||
|
||||
void setAge(int age);
|
||||
|
||||
String getName();
|
||||
|
||||
void setName(String name);
|
||||
|
||||
ITestBean getSpouse();
|
||||
|
||||
void setSpouse(ITestBean spouse);
|
||||
|
||||
ITestBean[] getSpouses();
|
||||
|
||||
String[] getStringArray();
|
||||
|
||||
void setStringArray(String[] stringArray);
|
||||
|
||||
/**
|
||||
* Throws a given (non-null) exception.
|
||||
*/
|
||||
void exceptional(Throwable t) throws Throwable;
|
||||
|
||||
Object returnsThis();
|
||||
|
||||
INestedTestBean getDoctor();
|
||||
|
||||
INestedTestBean getLawyer();
|
||||
|
||||
IndexedTestBean getNestedIndexedBean();
|
||||
|
||||
/**
|
||||
* Increment the age by one.
|
||||
* @return the previous age
|
||||
*/
|
||||
int haveBirthday();
|
||||
|
||||
void unreliableFileOperation() throws IOException;
|
||||
|
||||
}
|
||||
@@ -1,145 +0,0 @@
|
||||
/*
|
||||
* 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.beans;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 11.11.2003
|
||||
*/
|
||||
public class IndexedTestBean {
|
||||
|
||||
private TestBean[] array;
|
||||
|
||||
private Collection collection;
|
||||
|
||||
private List list;
|
||||
|
||||
private Set set;
|
||||
|
||||
private SortedSet sortedSet;
|
||||
|
||||
private Map map;
|
||||
|
||||
private SortedMap sortedMap;
|
||||
|
||||
|
||||
public IndexedTestBean() {
|
||||
this(true);
|
||||
}
|
||||
|
||||
public IndexedTestBean(boolean populate) {
|
||||
if (populate) {
|
||||
populate();
|
||||
}
|
||||
}
|
||||
|
||||
public void populate() {
|
||||
TestBean tb0 = new TestBean("name0", 0);
|
||||
TestBean tb1 = new TestBean("name1", 0);
|
||||
TestBean tb2 = new TestBean("name2", 0);
|
||||
TestBean tb3 = new TestBean("name3", 0);
|
||||
TestBean tb4 = new TestBean("name4", 0);
|
||||
TestBean tb5 = new TestBean("name5", 0);
|
||||
TestBean tb6 = new TestBean("name6", 0);
|
||||
TestBean tb7 = new TestBean("name7", 0);
|
||||
TestBean tbX = new TestBean("nameX", 0);
|
||||
TestBean tbY = new TestBean("nameY", 0);
|
||||
this.array = new TestBean[] {tb0, tb1};
|
||||
this.list = new ArrayList();
|
||||
this.list.add(tb2);
|
||||
this.list.add(tb3);
|
||||
this.set = new TreeSet();
|
||||
this.set.add(tb6);
|
||||
this.set.add(tb7);
|
||||
this.map = new HashMap();
|
||||
this.map.put("key1", tb4);
|
||||
this.map.put("key2", tb5);
|
||||
this.map.put("key.3", tb5);
|
||||
List list = new ArrayList();
|
||||
list.add(tbX);
|
||||
list.add(tbY);
|
||||
this.map.put("key4", list);
|
||||
}
|
||||
|
||||
|
||||
public TestBean[] getArray() {
|
||||
return array;
|
||||
}
|
||||
|
||||
public void setArray(TestBean[] array) {
|
||||
this.array = array;
|
||||
}
|
||||
|
||||
public Collection getCollection() {
|
||||
return collection;
|
||||
}
|
||||
|
||||
public void setCollection(Collection collection) {
|
||||
this.collection = collection;
|
||||
}
|
||||
|
||||
public List getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public Set getSet() {
|
||||
return set;
|
||||
}
|
||||
|
||||
public void setSet(Set set) {
|
||||
this.set = set;
|
||||
}
|
||||
|
||||
public SortedSet getSortedSet() {
|
||||
return sortedSet;
|
||||
}
|
||||
|
||||
public void setSortedSet(SortedSet sortedSet) {
|
||||
this.sortedSet = sortedSet;
|
||||
}
|
||||
|
||||
public Map getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public void setMap(Map map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public SortedMap getSortedMap() {
|
||||
return sortedMap;
|
||||
}
|
||||
|
||||
public void setSortedMap(SortedMap sortedMap) {
|
||||
this.sortedMap = sortedMap;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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;
|
||||
|
||||
/**
|
||||
* Simple nested test bean used for testing bean factories, AOP framework etc.
|
||||
*
|
||||
* @author Trevor D. Cook
|
||||
* @since 30.09.2003
|
||||
*/
|
||||
public class NestedTestBean implements INestedTestBean {
|
||||
|
||||
private String company = "";
|
||||
|
||||
public NestedTestBean() {
|
||||
}
|
||||
|
||||
public NestedTestBean(String company) {
|
||||
setCompany(company);
|
||||
}
|
||||
|
||||
public void setCompany(String company) {
|
||||
this.company = (company != null ? company : "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCompany() {
|
||||
return company;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof NestedTestBean)) {
|
||||
return false;
|
||||
}
|
||||
NestedTestBean ntb = (NestedTestBean) obj;
|
||||
return this.company.equals(ntb.company);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return this.company.hashCode();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "NestedTestBean: " + this.company;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,442 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Simple test bean used for testing bean factories, the AOP framework etc.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @since 15 April 2001
|
||||
*/
|
||||
public class TestBean implements ITestBean, IOther, Comparable {
|
||||
|
||||
private String beanName;
|
||||
|
||||
private String country;
|
||||
|
||||
private boolean postProcessed;
|
||||
|
||||
private String name;
|
||||
|
||||
private String sex;
|
||||
|
||||
private int age;
|
||||
|
||||
private boolean jedi;
|
||||
|
||||
private ITestBean[] spouses;
|
||||
|
||||
private String touchy;
|
||||
|
||||
private String[] stringArray;
|
||||
|
||||
private Integer[] someIntegerArray;
|
||||
|
||||
private Date date = new Date();
|
||||
|
||||
private Float myFloat = new Float(0.0);
|
||||
|
||||
private Collection friends = new LinkedList();
|
||||
|
||||
private Set someSet = new HashSet();
|
||||
|
||||
private Map someMap = new HashMap();
|
||||
|
||||
private List someList = new ArrayList();
|
||||
|
||||
private Properties someProperties = new Properties();
|
||||
|
||||
private INestedTestBean doctor = new NestedTestBean();
|
||||
|
||||
private INestedTestBean lawyer = new NestedTestBean();
|
||||
|
||||
private IndexedTestBean nestedIndexedBean;
|
||||
|
||||
private boolean destroyed;
|
||||
|
||||
private Number someNumber;
|
||||
|
||||
private Colour favouriteColour;
|
||||
|
||||
private Boolean someBoolean;
|
||||
|
||||
private List otherColours;
|
||||
|
||||
private List pets;
|
||||
|
||||
|
||||
public TestBean() {
|
||||
}
|
||||
|
||||
public TestBean(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public TestBean(ITestBean spouse) {
|
||||
this.spouses = new ITestBean[] {spouse};
|
||||
}
|
||||
|
||||
public TestBean(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public TestBean(ITestBean spouse, Properties someProperties) {
|
||||
this.spouses = new ITestBean[] {spouse};
|
||||
this.someProperties = someProperties;
|
||||
}
|
||||
|
||||
public TestBean(List someList) {
|
||||
this.someList = someList;
|
||||
}
|
||||
|
||||
public TestBean(Set someSet) {
|
||||
this.someSet = someSet;
|
||||
}
|
||||
|
||||
public TestBean(Map someMap) {
|
||||
this.someMap = someMap;
|
||||
}
|
||||
|
||||
public TestBean(Properties someProperties) {
|
||||
this.someProperties = someProperties;
|
||||
}
|
||||
|
||||
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
public void setPostProcessed(boolean postProcessed) {
|
||||
this.postProcessed = postProcessed;
|
||||
}
|
||||
|
||||
public boolean isPostProcessed() {
|
||||
return postProcessed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getSex() {
|
||||
return sex;
|
||||
}
|
||||
|
||||
public void setSex(String sex) {
|
||||
this.sex = sex;
|
||||
if (this.name == null) {
|
||||
this.name = sex;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public boolean isJedi() {
|
||||
return jedi;
|
||||
}
|
||||
|
||||
public void setJedi(boolean jedi) {
|
||||
this.jedi = jedi;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITestBean getSpouse() {
|
||||
return (spouses != null ? spouses[0] : null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpouse(ITestBean spouse) {
|
||||
this.spouses = new ITestBean[] {spouse};
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITestBean[] getSpouses() {
|
||||
return spouses;
|
||||
}
|
||||
|
||||
public String getTouchy() {
|
||||
return touchy;
|
||||
}
|
||||
|
||||
public void setTouchy(String touchy) throws Exception {
|
||||
if (touchy.indexOf('.') != -1) {
|
||||
throw new Exception("Can't contain a .");
|
||||
}
|
||||
if (touchy.indexOf(',') != -1) {
|
||||
throw new NumberFormatException("Number format exception: contains a ,");
|
||||
}
|
||||
this.touchy = touchy;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getStringArray() {
|
||||
return stringArray;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStringArray(String[] stringArray) {
|
||||
this.stringArray = stringArray;
|
||||
}
|
||||
|
||||
public Integer[] getSomeIntegerArray() {
|
||||
return someIntegerArray;
|
||||
}
|
||||
|
||||
public void setSomeIntegerArray(Integer[] someIntegerArray) {
|
||||
this.someIntegerArray = someIntegerArray;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public Float getMyFloat() {
|
||||
return myFloat;
|
||||
}
|
||||
|
||||
public void setMyFloat(Float myFloat) {
|
||||
this.myFloat = myFloat;
|
||||
}
|
||||
|
||||
public Collection getFriends() {
|
||||
return friends;
|
||||
}
|
||||
|
||||
public void setFriends(Collection friends) {
|
||||
this.friends = friends;
|
||||
}
|
||||
|
||||
public Set getSomeSet() {
|
||||
return someSet;
|
||||
}
|
||||
|
||||
public void setSomeSet(Set someSet) {
|
||||
this.someSet = someSet;
|
||||
}
|
||||
|
||||
public Map getSomeMap() {
|
||||
return someMap;
|
||||
}
|
||||
|
||||
public void setSomeMap(Map someMap) {
|
||||
this.someMap = someMap;
|
||||
}
|
||||
|
||||
public List getSomeList() {
|
||||
return someList;
|
||||
}
|
||||
|
||||
public void setSomeList(List someList) {
|
||||
this.someList = someList;
|
||||
}
|
||||
|
||||
public Properties getSomeProperties() {
|
||||
return someProperties;
|
||||
}
|
||||
|
||||
public void setSomeProperties(Properties someProperties) {
|
||||
this.someProperties = someProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public INestedTestBean getDoctor() {
|
||||
return doctor;
|
||||
}
|
||||
|
||||
public void setDoctor(INestedTestBean doctor) {
|
||||
this.doctor = doctor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public INestedTestBean getLawyer() {
|
||||
return lawyer;
|
||||
}
|
||||
|
||||
public void setLawyer(INestedTestBean lawyer) {
|
||||
this.lawyer = lawyer;
|
||||
}
|
||||
|
||||
public Number getSomeNumber() {
|
||||
return someNumber;
|
||||
}
|
||||
|
||||
public void setSomeNumber(Number someNumber) {
|
||||
this.someNumber = someNumber;
|
||||
}
|
||||
|
||||
public Colour getFavouriteColour() {
|
||||
return favouriteColour;
|
||||
}
|
||||
|
||||
public void setFavouriteColour(Colour favouriteColour) {
|
||||
this.favouriteColour = favouriteColour;
|
||||
}
|
||||
|
||||
public Boolean getSomeBoolean() {
|
||||
return someBoolean;
|
||||
}
|
||||
|
||||
public void setSomeBoolean(Boolean someBoolean) {
|
||||
this.someBoolean = someBoolean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IndexedTestBean getNestedIndexedBean() {
|
||||
return nestedIndexedBean;
|
||||
}
|
||||
|
||||
public void setNestedIndexedBean(IndexedTestBean nestedIndexedBean) {
|
||||
this.nestedIndexedBean = nestedIndexedBean;
|
||||
}
|
||||
|
||||
public List getOtherColours() {
|
||||
return otherColours;
|
||||
}
|
||||
|
||||
public void setOtherColours(List otherColours) {
|
||||
this.otherColours = otherColours;
|
||||
}
|
||||
|
||||
public List getPets() {
|
||||
return pets;
|
||||
}
|
||||
|
||||
public void setPets(List pets) {
|
||||
this.pets = pets;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @see ITestBean#exceptional(Throwable)
|
||||
*/
|
||||
@Override
|
||||
public void exceptional(Throwable t) throws Throwable {
|
||||
if (t != null) {
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unreliableFileOperation() throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
/**
|
||||
* @see ITestBean#returnsThis()
|
||||
*/
|
||||
@Override
|
||||
public Object returnsThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see IOther#absquatulate()
|
||||
*/
|
||||
@Override
|
||||
public void absquatulate() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int haveBirthday() {
|
||||
return age++;
|
||||
}
|
||||
|
||||
|
||||
public void destroy() {
|
||||
this.destroyed = true;
|
||||
}
|
||||
|
||||
public boolean wasDestroyed() {
|
||||
return destroyed;
|
||||
}
|
||||
|
||||
|
||||
public boolean equals(Object other) {
|
||||
if (this == other) {
|
||||
return true;
|
||||
}
|
||||
if (other == null || !(other instanceof TestBean)) {
|
||||
return false;
|
||||
}
|
||||
TestBean tb2 = (TestBean) other;
|
||||
return (ObjectUtils.nullSafeEquals(this.name, tb2.name) && this.age == tb2.age);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return this.age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Object other) {
|
||||
if (this.name != null && other instanceof TestBean) {
|
||||
return this.name.compareTo(((TestBean) other).getName());
|
||||
}
|
||||
else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -560,6 +560,7 @@ public class BridgeMethodResolverTests {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class StringGenericParameter implements GenericParameter<String> {
|
||||
|
||||
@Override
|
||||
@@ -1048,7 +1049,7 @@ public class BridgeMethodResolverTests {
|
||||
}
|
||||
|
||||
|
||||
public class GenericSqlMapIntegerDao<T extends Integer> extends GenericSqlMapDao<T> {
|
||||
public class GenericSqlMapIntegerDao<T extends Number> extends GenericSqlMapDao<T> {
|
||||
|
||||
@Override
|
||||
public void saveOrUpdate(T t) {
|
||||
@@ -1163,6 +1164,7 @@ public class BridgeMethodResolverTests {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class MegaMessageProducerImpl extends Other<Long, String> implements MegaMessageProducer {
|
||||
|
||||
public void receive(NewMegaMessageEvent event) {
|
||||
@@ -1198,6 +1200,7 @@ public class BridgeMethodResolverTests {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static abstract class AbstractImplementsInterface<D extends DomainObjectSuper> implements IGenericInterface<D> {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -23,7 +23,7 @@ import java.util.Set;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.tests.sample.objects.TestObject;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
@@ -31,23 +31,23 @@ import org.springframework.beans.TestBean;
|
||||
public class ConventionsTests extends TestCase {
|
||||
|
||||
public void testSimpleObject() {
|
||||
TestBean testBean = new TestBean();
|
||||
assertEquals("Incorrect singular variable name", "testBean", Conventions.getVariableName(testBean));
|
||||
TestObject testObject = new TestObject();
|
||||
assertEquals("Incorrect singular variable name", "testObject", Conventions.getVariableName(testObject));
|
||||
}
|
||||
|
||||
public void testArray() {
|
||||
TestBean[] testBeans = new TestBean[0];
|
||||
assertEquals("Incorrect plural array form", "testBeanList", Conventions.getVariableName(testBeans));
|
||||
TestObject[] testObjects = new TestObject[0];
|
||||
assertEquals("Incorrect plural array form", "testObjectList", Conventions.getVariableName(testObjects));
|
||||
}
|
||||
|
||||
public void testCollections() {
|
||||
List<TestBean> list = new ArrayList<TestBean>();
|
||||
list.add(new TestBean());
|
||||
assertEquals("Incorrect plural List form", "testBeanList", Conventions.getVariableName(list));
|
||||
List<TestObject> list = new ArrayList<TestObject>();
|
||||
list.add(new TestObject());
|
||||
assertEquals("Incorrect plural List form", "testObjectList", Conventions.getVariableName(list));
|
||||
|
||||
Set<TestBean> set = new HashSet<TestBean>();
|
||||
set.add(new TestBean());
|
||||
assertEquals("Incorrect plural Set form", "testBeanList", Conventions.getVariableName(set));
|
||||
Set<TestObject> set = new HashSet<TestObject>();
|
||||
set.add(new TestObject());
|
||||
assertEquals("Incorrect plural Set form", "testObjectList", Conventions.getVariableName(set));
|
||||
|
||||
List<?> emptyList = new ArrayList<Object>();
|
||||
try {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -25,8 +25,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.GenericBean;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.tests.sample.objects.GenericObject;
|
||||
|
||||
/**
|
||||
* @author Serge Bogatyrjov
|
||||
@@ -93,11 +93,11 @@ public class GenericCollectionTypeResolverTests extends AbstractGenericsTests {
|
||||
}
|
||||
|
||||
public void testProgrammaticListIntrospection() throws Exception {
|
||||
Method setter = GenericBean.class.getMethod("setResourceList", List.class);
|
||||
Method setter = GenericObject.class.getMethod("setResourceList", List.class);
|
||||
assertEquals(Resource.class,
|
||||
GenericCollectionTypeResolver.getCollectionParameterType(new MethodParameter(setter, 0)));
|
||||
|
||||
Method getter = GenericBean.class.getMethod("getResourceList");
|
||||
Method getter = GenericObject.class.getMethod("getResourceList");
|
||||
assertEquals(Resource.class,
|
||||
GenericCollectionTypeResolver.getCollectionReturnType(getter));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -25,7 +25,7 @@ import java.util.Date;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.tests.sample.objects.TestObject;
|
||||
|
||||
/**
|
||||
* @author Adrian Colyer
|
||||
@@ -35,14 +35,14 @@ public class LocalVariableTableParameterNameDiscovererTests extends TestCase {
|
||||
private LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
|
||||
|
||||
public void testMethodParameterNameDiscoveryNoArgs() throws NoSuchMethodException {
|
||||
Method getName = TestBean.class.getMethod("getName", new Class[0]);
|
||||
Method getName = TestObject.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 });
|
||||
Method setName = TestObject.class.getMethod("setName", new Class[] { String.class });
|
||||
String[] names = discoverer.getParameterNames(setName);
|
||||
assertNotNull("should find method info", names);
|
||||
assertEquals("one argument", 1, names.length);
|
||||
@@ -50,14 +50,14 @@ public class LocalVariableTableParameterNameDiscovererTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testConsParameterNameDiscoveryNoArgs() throws NoSuchMethodException {
|
||||
Constructor<TestBean> noArgsCons = TestBean.class.getConstructor(new Class[0]);
|
||||
Constructor<TestObject> noArgsCons = TestObject.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<TestBean> twoArgCons = TestBean.class.getConstructor(new Class[] { String.class, int.class });
|
||||
Constructor<TestObject> twoArgCons = TestObject.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);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -22,7 +22,7 @@ import java.util.Arrays;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.tests.sample.objects.TestObject;
|
||||
|
||||
public class PrioritizedParameterNameDiscovererTests extends TestCase {
|
||||
|
||||
@@ -56,7 +56,7 @@ public class PrioritizedParameterNameDiscovererTests extends TestCase {
|
||||
private final Class anyClass = Object.class;
|
||||
|
||||
public PrioritizedParameterNameDiscovererTests() throws SecurityException, NoSuchMethodException {
|
||||
anyMethod = TestBean.class.getMethod("getAge", (Class[]) null);
|
||||
anyMethod = TestObject.class.getMethod("getAge", (Class[]) null);
|
||||
}
|
||||
|
||||
public void testNoParametersDiscoverers() {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -13,16 +13,19 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.annotation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AnnotationAwareOrderComparator}.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class AnnotationAwareOrderComparatorTests {
|
||||
@@ -31,4 +34,34 @@ public class AnnotationAwareOrderComparatorTests {
|
||||
public void instanceVariableIsAnAnnotationAwareOrderComparator() {
|
||||
assertThat(AnnotationAwareOrderComparator.INSTANCE, is(instanceOf(AnnotationAwareOrderComparator.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortInstances() {
|
||||
List<Object> list = new ArrayList<>();
|
||||
list.add(new B());
|
||||
list.add(new A());
|
||||
AnnotationAwareOrderComparator.sort(list);
|
||||
assertTrue(list.get(0) instanceof A);
|
||||
assertTrue(list.get(1) instanceof B);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortClasses() {
|
||||
List<Object> list = new ArrayList<>();
|
||||
list.add(B.class);
|
||||
list.add(A.class);
|
||||
AnnotationAwareOrderComparator.sort(list);
|
||||
assertEquals(A.class, list.get(0));
|
||||
assertEquals(B.class, list.get(1));
|
||||
}
|
||||
|
||||
|
||||
@Order(1)
|
||||
private static class A {
|
||||
}
|
||||
|
||||
@Order(2)
|
||||
private static class B {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -25,6 +25,7 @@ import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -36,6 +37,7 @@ import static org.junit.Assert.*;
|
||||
/**
|
||||
* @author Keith Donald
|
||||
* @author Andy Clement
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class TypeDescriptorTests {
|
||||
@@ -849,4 +851,23 @@ public class TypeDescriptorTests {
|
||||
assertEquals(TypeDescriptor.forObject(new CustomMap()).getMapValueTypeDescriptor(), TypeDescriptor.valueOf(Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createMapArray() throws Exception {
|
||||
TypeDescriptor mapType = TypeDescriptor.map(LinkedHashMap.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
|
||||
TypeDescriptor arrayType = TypeDescriptor.array(mapType);
|
||||
assertEquals(arrayType.getType(), LinkedHashMap[].class);
|
||||
assertEquals(arrayType.getElementTypeDescriptor(), mapType);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void createStringArray() throws Exception {
|
||||
TypeDescriptor arrayType = TypeDescriptor.array(TypeDescriptor.valueOf(String.class));
|
||||
assertEquals(arrayType, TypeDescriptor.valueOf(String[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createNullArray() throws Exception {
|
||||
assertNull(TypeDescriptor.array(null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -16,7 +16,15 @@
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.math.BigDecimal;
|
||||
@@ -205,6 +213,11 @@ public class DefaultConversionTests {
|
||||
assertEquals(Foo.BAR, conversionService.convert("BAR", Foo.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToEnumWithSubclss() throws Exception {
|
||||
assertEquals(SubFoo.BAZ, conversionService.convert("BAZ", SubFoo.BAR.getClass()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToEnumEmptyString() {
|
||||
assertEquals(null, conversionService.convert("", Foo.class));
|
||||
@@ -219,6 +232,24 @@ public class DefaultConversionTests {
|
||||
BAR, BAZ;
|
||||
}
|
||||
|
||||
public static enum SubFoo {
|
||||
|
||||
BAR {
|
||||
@Override
|
||||
String s() {
|
||||
return "x";
|
||||
}
|
||||
},
|
||||
BAZ {
|
||||
@Override
|
||||
String s() {
|
||||
return "y";
|
||||
}
|
||||
};
|
||||
|
||||
abstract String s();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToLocale() {
|
||||
assertEquals(Locale.ENGLISH, conversionService.convert("en", Locale.class));
|
||||
@@ -773,6 +804,30 @@ public class DefaultConversionTests {
|
||||
assertEquals(new Long(1), e.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertCharArrayToString() throws Exception {
|
||||
String converted = conversionService.convert(new char[] { 'a', 'b', 'c' }, String.class);
|
||||
assertThat(converted, equalTo("a,b,c"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertStringToCharArray() throws Exception {
|
||||
char[] converted = conversionService.convert("a,b,c", char[].class);
|
||||
assertThat(converted, equalTo(new char[] { 'a', 'b', 'c' }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertStringToCustomCharArray() throws Exception {
|
||||
conversionService.addConverter(new Converter<String, char[]>() {
|
||||
@Override
|
||||
public char[] convert(String source) {
|
||||
return source.toCharArray();
|
||||
}
|
||||
});
|
||||
char[] converted = conversionService.convert("abc", char[].class);
|
||||
assertThat(converted, equalTo(new char[] { 'a', 'b', 'c' }));
|
||||
}
|
||||
|
||||
public static class TestEntity {
|
||||
|
||||
private Long id;
|
||||
|
||||
@@ -358,7 +358,6 @@ public class GenericConversionServiceTests {
|
||||
GenericConversionService service = new DefaultConversionService();
|
||||
List<String> list1 = Arrays.asList("Foo", "Bar");
|
||||
List<String> list2 = Arrays.asList("Baz", "Boop");
|
||||
@SuppressWarnings("unchecked")
|
||||
List<List<String>> list = Arrays.asList(list1, list2);
|
||||
String result = service.convert(list, String.class);
|
||||
assertNotNull(result);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -104,7 +104,7 @@ public class LabeledEnumTests extends TestCase {
|
||||
|
||||
public void testDoesNotMatchWrongClass() {
|
||||
try {
|
||||
LabeledEnum none = StaticLabeledEnumResolver.instance().getLabeledEnumByCode(Dog.class,
|
||||
StaticLabeledEnumResolver.instance().getLabeledEnumByCode(Dog.class,
|
||||
new Short((short) 1));
|
||||
fail("Should have failed");
|
||||
}
|
||||
@@ -119,10 +119,11 @@ public class LabeledEnumTests extends TestCase {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@SuppressWarnings({ "serial", "unused" })
|
||||
private static class Other extends StaticLabeledEnum {
|
||||
|
||||
public static final Other THING1 = new Other(1, "Thing1");
|
||||
|
||||
public static final Other THING2 = new Other(2, "Thing2");
|
||||
|
||||
|
||||
|
||||
75
spring-core/src/test/java/org/springframework/core/env/DummyEnvironment.java
vendored
Normal file
75
spring-core/src/test/java/org/springframework/core/env/DummyEnvironment.java
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.env;
|
||||
|
||||
public class DummyEnvironment implements Environment {
|
||||
|
||||
public boolean containsProperty(String key) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getProperty(String key) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getProperty(String key, String defaultValue) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public <T> T getProperty(String key, Class<T> targetType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getRequiredProperty(String key) throws IllegalStateException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public <T> T getRequiredProperty(String key, Class<T> targetType)
|
||||
throws IllegalStateException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String resolvePlaceholders(String text) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String resolveRequiredPlaceholders(String text)
|
||||
throws IllegalArgumentException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String[] getActiveProfiles() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String[] getDefaultProfiles() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean acceptsProfiles(String... profiles) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -456,7 +456,7 @@ public class StandardEnvironmentTests {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Map<String, String> getModifiableSystemEnvironment() {
|
||||
public static Map<String, String> getModifiableSystemEnvironment() {
|
||||
// for os x / linux
|
||||
Class<?>[] classes = Collections.class.getDeclaredClasses();
|
||||
Map<String, String> env = System.getenv();
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
|
||||
<properties version="1.0">
|
||||
<entry key="foo">bar</entry>
|
||||
</properties>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -39,6 +39,10 @@ public class ResourcePropertySourceTests {
|
||||
private static final String PROPERTIES_LOCATION = "classpath:" + PROPERTIES_PATH;
|
||||
private static final String PROPERTIES_RESOURCE_DESCRIPTION = "class path resource [" + PROPERTIES_PATH + "]";
|
||||
|
||||
private static final String XML_PROPERTIES_PATH = "org/springframework/core/io/example.xml";
|
||||
private static final String XML_PROPERTIES_LOCATION = "classpath:" + XML_PROPERTIES_PATH;
|
||||
private static final String XML_PROPERTIES_RESOURCE_DESCRIPTION = "class path resource [" + XML_PROPERTIES_PATH + "]";
|
||||
|
||||
@Test
|
||||
public void withLocationAndGeneratedName() throws IOException {
|
||||
PropertySource<?> ps = new ResourcePropertySource(PROPERTIES_LOCATION);
|
||||
@@ -46,6 +50,13 @@ public class ResourcePropertySourceTests {
|
||||
assertThat(ps.getName(), is(PROPERTIES_RESOURCE_DESCRIPTION));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void xmlWithLocationAndGeneratedName() throws IOException {
|
||||
PropertySource<?> ps = new ResourcePropertySource(XML_PROPERTIES_LOCATION);
|
||||
assertEquals(ps.getProperty("foo"), "bar");
|
||||
assertThat(ps.getName(), is(XML_PROPERTIES_RESOURCE_DESCRIPTION));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withLocationAndExplicitName() throws IOException {
|
||||
PropertySource<?> ps = new ResourcePropertySource("ps1", PROPERTIES_LOCATION);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -33,8 +33,6 @@ import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.TestAutowired;
|
||||
import org.springframework.beans.factory.annotation.TestQualifier;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.type.classreading.MetadataReader;
|
||||
import org.springframework.core.type.classreading.MetadataReaderFactory;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -122,10 +122,12 @@ public class AnnotationTypeFilterTests {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class SomeSubClassOfSomeComponentInterface implements SomeComponentInterface {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class SomeSubClassOfSomeComponent extends SomeComponent {
|
||||
}
|
||||
|
||||
@@ -139,10 +141,12 @@ public class AnnotationTypeFilterTests {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class SomeSubclassOfSomeClassMarkedWithNonInheritedAnnotation extends SomeClassMarkedWithNonInheritedAnnotation {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class SomeNonCandidateClass {
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -81,6 +81,7 @@ public class AssignableTypeFilterTests extends TestCase {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class TestInterfaceImpl implements TestInterface {
|
||||
}
|
||||
|
||||
@@ -89,6 +90,7 @@ public class AssignableTypeFilterTests extends TestCase {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class SomeDaoLikeImpl extends SimpleJdbcDaoSupport implements SomeDaoLikeInterface {
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ import org.springframework.tests.Assume;
|
||||
import org.springframework.tests.TestGroup;
|
||||
|
||||
/**
|
||||
* Unit test checking the behaviour of {@link CachingMetadataReaderFactory under load.
|
||||
* Unit test checking the behaviour of {@link CachingMetadataReaderFactory} under load.
|
||||
* If the cache is not controller, this test should fail with an out of memory exception around entry
|
||||
* 5k.
|
||||
*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans.factory.annotation;
|
||||
package org.springframework.core.type;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans.factory.annotation;
|
||||
package org.springframework.core.type;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
@@ -21,6 +21,10 @@ import java.util.EnumSet;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static java.lang.String.*;
|
||||
|
||||
/**
|
||||
* A test group used to limit when certain tests are run.
|
||||
*
|
||||
@@ -44,7 +48,13 @@ public enum TestGroup {
|
||||
* {@code StopWatch}, etc. should be considered a candidate as their successful
|
||||
* execution is likely to be based on events occurring within a given time window.
|
||||
*/
|
||||
PERFORMANCE;
|
||||
PERFORMANCE,
|
||||
|
||||
/**
|
||||
* Tests requiring the presence of jmxremote_optional.jar in jre/lib/ext in order to
|
||||
* avoid "Unsupported protocol: jmxmp" errors.
|
||||
*/
|
||||
JMXMP;
|
||||
|
||||
|
||||
/**
|
||||
@@ -64,8 +74,10 @@ public enum TestGroup {
|
||||
try {
|
||||
groups.add(valueOf(group.trim().toUpperCase()));
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new IllegalArgumentException("Unable to find test group '" + group.trim()
|
||||
+ "' when parsing '" + value + "'");
|
||||
throw new IllegalArgumentException(format(
|
||||
"Unable to find test group '%s' when parsing testGroups value: '%s'. " +
|
||||
"Available groups include: [%s]", group.trim(), value,
|
||||
StringUtils.arrayToCommaDelimitedString(TestGroup.values())));
|
||||
}
|
||||
}
|
||||
return groups;
|
||||
|
||||
@@ -62,7 +62,9 @@ public class TestGroupTests {
|
||||
@Test
|
||||
public void parseMissing() throws Exception {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("Unable to find test group 'missing' when parsing 'performance, missing'");
|
||||
thrown.expectMessage("Unable to find test group 'missing' when parsing " +
|
||||
"testGroups value: 'performance, missing'. Available groups include: " +
|
||||
"[LONG_RUNNING,PERFORMANCE,JMXMP]");
|
||||
TestGroup.parse("performance, missing");
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.tests;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* Convenience utilities for common operations with test resources.
|
||||
*
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class TestResourceUtils {
|
||||
|
||||
/**
|
||||
* Loads a {@link ClassPathResource} qualified by the simple name of clazz,
|
||||
* and relative to the package for clazz.
|
||||
*
|
||||
* <p>Example: given a clazz 'com.foo.BarTests' and a resourceSuffix of 'context.xml',
|
||||
* this method will return a ClassPathResource representing com/foo/BarTests-context.xml
|
||||
*
|
||||
* <p>Intended for use loading context configuration XML files within JUnit tests.
|
||||
*
|
||||
* @param clazz
|
||||
* @param resourceSuffix
|
||||
*/
|
||||
public static ClassPathResource qualifiedResource(Class<?> clazz, String resourceSuffix) {
|
||||
return new ClassPathResource(format("%s-%s", clazz.getSimpleName(), resourceSuffix), clazz);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -14,23 +14,21 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans;
|
||||
|
||||
import org.springframework.core.enums.ShortCodedLabeledEnum;
|
||||
package org.springframework.tests;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* This interface can be implemented by cacheable objects or cache entries,
|
||||
* to enable the freshness of objects to be checked.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class Colour extends ShortCodedLabeledEnum {
|
||||
public interface TimeStamped {
|
||||
|
||||
public static final Colour RED = new Colour(0, "RED");
|
||||
public static final Colour BLUE = new Colour(1, "BLUE");
|
||||
public static final Colour GREEN = new Colour(2, "GREEN");
|
||||
public static final Colour PURPLE = new Colour(3, "PURPLE");
|
||||
|
||||
private Colour(int code, String label) {
|
||||
super(code, label);
|
||||
}
|
||||
/**
|
||||
* Return the timestamp for this object.
|
||||
* @return long the timestamp for this object,
|
||||
* as returned by System.currentTimeMillis()
|
||||
*/
|
||||
long getTimeStamp();
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -14,17 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans;
|
||||
package org.springframework.tests.sample.objects;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public enum CustomEnum {
|
||||
import java.io.Serializable;
|
||||
|
||||
VALUE_1, VALUE_2;
|
||||
@SuppressWarnings("serial")
|
||||
public class DerivedTestObject extends TestObject implements Serializable {
|
||||
|
||||
public String toString() {
|
||||
return "CustomEnum: " + name();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.tests.sample.objects;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
|
||||
public class GenericObject<T> {
|
||||
|
||||
private List<Resource> resourceList;
|
||||
|
||||
public List<Resource> getResourceList() {
|
||||
return this.resourceList;
|
||||
}
|
||||
|
||||
public void setResourceList(List<Resource> resourceList) {
|
||||
this.resourceList = resourceList;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -15,10 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans;
|
||||
package org.springframework.tests.sample.objects;
|
||||
|
||||
public interface IOther {
|
||||
|
||||
public interface ITestInterface {
|
||||
|
||||
void absquatulate();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -14,10 +14,20 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans;
|
||||
package org.springframework.tests.sample.objects;
|
||||
|
||||
public interface INestedTestBean {
|
||||
public interface ITestObject {
|
||||
|
||||
public String getCompany();
|
||||
String getName();
|
||||
|
||||
}
|
||||
void setName(String name);
|
||||
|
||||
int getAge();
|
||||
|
||||
void setAge(int age);
|
||||
|
||||
TestObject getSpouse();
|
||||
|
||||
void setSpouse(TestObject spouse);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.tests.sample.objects;
|
||||
|
||||
public class TestObject implements ITestObject, ITestInterface, Comparable<Object> {
|
||||
|
||||
private String name;
|
||||
|
||||
private int age;
|
||||
|
||||
private TestObject spouse;
|
||||
|
||||
public TestObject() {
|
||||
}
|
||||
|
||||
public TestObject(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return this.age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public TestObject getSpouse() {
|
||||
return this.spouse;
|
||||
}
|
||||
|
||||
public void setSpouse(TestObject spouse) {
|
||||
this.spouse = spouse;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void absquatulate() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Object o) {
|
||||
if (this.name != null && o instanceof TestObject) {
|
||||
return this.name.compareTo(((TestObject) o).getName());
|
||||
}
|
||||
else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* General purpose sample objects that can be used with tests.
|
||||
*/
|
||||
package org.springframework.tests.sample.objects;
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -412,6 +412,7 @@ public class AntPathMatcherTests {
|
||||
assertEquals("/*.html", pathMatcher.combine("/*.*", "/*.html"));
|
||||
assertEquals("/{foo}/bar", pathMatcher.combine("/{foo}", "/bar")); // SPR-8858
|
||||
assertEquals("/user/user", pathMatcher.combine("/user", "/user")); // SPR-7970
|
||||
assertEquals("/{foo:.*[^0-9].*}/edit/", pathMatcher.combine("/{foo:.*[^0-9].*}", "/edit/")); // SPR-10062
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -542,6 +543,14 @@ public class AntPathMatcherTests {
|
||||
paths.clear();
|
||||
}
|
||||
|
||||
// SPR-8687
|
||||
|
||||
@Test
|
||||
public void trimTokensOff() {
|
||||
pathMatcher.setTrimTokens(false);
|
||||
|
||||
assertTrue(pathMatcher.match("/group/{groupName}/members", "/group/sales/members"));
|
||||
assertTrue(pathMatcher.match("/group/{groupName}/members", "/group/ sales/members"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -24,7 +24,9 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link Assert} class.
|
||||
@@ -36,6 +38,9 @@ import org.junit.Test;
|
||||
*/
|
||||
public class AssertTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void instanceOf() {
|
||||
final Set<?> set = new HashSet<Object>();
|
||||
@@ -43,6 +48,22 @@ public class AssertTests {
|
||||
Assert.isInstanceOf(HashMap.class, set);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void instanceOfNoMessage() throws Exception {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("Object of class [java.lang.Object] must be an instance " +
|
||||
"of interface java.util.Set");
|
||||
Assert.isInstanceOf(Set.class, new Object(), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void instanceOfMessage() throws Exception {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("Custom message. Object of class [java.lang.Object] must " +
|
||||
"be an instance of interface java.util.Set");
|
||||
Assert.isInstanceOf(Set.class, new Object(), "Custom message.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNullDoesNotThrowExceptionIfArgumentIsNullWithMessage() {
|
||||
Assert.isNull(null, "Bla");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -20,7 +20,7 @@ import java.util.LinkedList;
|
||||
|
||||
import junit.framework.*;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.tests.sample.objects.TestObject;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
@@ -29,11 +29,11 @@ import org.springframework.beans.TestBean;
|
||||
public class AutoPopulatingListTests extends TestCase {
|
||||
|
||||
public void testWithClass() throws Exception {
|
||||
doTestWithClass(new AutoPopulatingList<Object>(TestBean.class));
|
||||
doTestWithClass(new AutoPopulatingList<Object>(TestObject.class));
|
||||
}
|
||||
|
||||
public void testWithClassAndUserSuppliedBackingList() throws Exception {
|
||||
doTestWithClass(new AutoPopulatingList<Object>(new LinkedList<Object>(), TestBean.class));
|
||||
doTestWithClass(new AutoPopulatingList<Object>(new LinkedList<Object>(), TestObject.class));
|
||||
}
|
||||
|
||||
public void testWithElementFactory() throws Exception {
|
||||
@@ -49,7 +49,7 @@ public class AutoPopulatingListTests extends TestCase {
|
||||
for (int x = 0; x < 10; x++) {
|
||||
Object element = list.get(x);
|
||||
assertNotNull("Element is null", list.get(x));
|
||||
assertTrue("Element is incorrect type", element instanceof TestBean);
|
||||
assertTrue("Element is incorrect type", element instanceof TestObject);
|
||||
assertNotSame(lastElement, element);
|
||||
lastElement = element;
|
||||
}
|
||||
@@ -59,10 +59,10 @@ public class AutoPopulatingListTests extends TestCase {
|
||||
list.add(11, helloWorld);
|
||||
assertEquals(helloWorld, list.get(11));
|
||||
|
||||
assertTrue(list.get(10) instanceof TestBean);
|
||||
assertTrue(list.get(12) instanceof TestBean);
|
||||
assertTrue(list.get(13) instanceof TestBean);
|
||||
assertTrue(list.get(20) instanceof TestBean);
|
||||
assertTrue(list.get(10) instanceof TestObject);
|
||||
assertTrue(list.get(12) instanceof TestObject);
|
||||
assertTrue(list.get(13) instanceof TestObject);
|
||||
assertTrue(list.get(20) instanceof TestObject);
|
||||
}
|
||||
|
||||
private void doTestWithElementFactory(AutoPopulatingList<Object> list) {
|
||||
@@ -70,14 +70,14 @@ public class AutoPopulatingListTests extends TestCase {
|
||||
|
||||
for(int x = 0; x < list.size(); x++) {
|
||||
Object element = list.get(x);
|
||||
if(element instanceof TestBean) {
|
||||
assertEquals(x, ((TestBean) element).getAge());
|
||||
if(element instanceof TestObject) {
|
||||
assertEquals(x, ((TestObject) element).getAge());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testSerialization() throws Exception {
|
||||
AutoPopulatingList<?> list = new AutoPopulatingList<Object>(TestBean.class);
|
||||
AutoPopulatingList<?> list = new AutoPopulatingList<Object>(TestObject.class);
|
||||
assertEquals(list, SerializationTestUtils.serializeAndDeserialize(list));
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ public class AutoPopulatingListTests extends TestCase {
|
||||
|
||||
@Override
|
||||
public Object createElement(int index) {
|
||||
TestBean bean = new TestBean();
|
||||
TestObject bean = new TestObject();
|
||||
bean.setAge(index);
|
||||
return bean;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -28,10 +28,10 @@ import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.DerivedTestBean;
|
||||
import org.springframework.beans.IOther;
|
||||
import org.springframework.beans.ITestBean;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.tests.sample.objects.DerivedTestObject;
|
||||
import org.springframework.tests.sample.objects.ITestInterface;
|
||||
import org.springframework.tests.sample.objects.ITestObject;
|
||||
import org.springframework.tests.sample.objects.TestObject;
|
||||
|
||||
/**
|
||||
* @author Colin Sampaleanu
|
||||
@@ -61,11 +61,11 @@ public class ClassUtilsTests extends TestCase {
|
||||
assertEquals(String[].class, ClassUtils.forName(String[].class.getName(), classLoader));
|
||||
assertEquals(String[][].class, ClassUtils.forName(String[][].class.getName(), classLoader));
|
||||
assertEquals(String[][][].class, ClassUtils.forName(String[][][].class.getName(), classLoader));
|
||||
assertEquals(TestBean.class, ClassUtils.forName("org.springframework.beans.TestBean", classLoader));
|
||||
assertEquals(TestBean[].class, ClassUtils.forName("org.springframework.beans.TestBean[]", classLoader));
|
||||
assertEquals(TestBean[].class, ClassUtils.forName(TestBean[].class.getName(), classLoader));
|
||||
assertEquals(TestBean[][].class, ClassUtils.forName("org.springframework.beans.TestBean[][]", classLoader));
|
||||
assertEquals(TestBean[][].class, ClassUtils.forName(TestBean[][].class.getName(), classLoader));
|
||||
assertEquals(TestObject.class, ClassUtils.forName("org.springframework.tests.sample.objects.TestObject", classLoader));
|
||||
assertEquals(TestObject[].class, ClassUtils.forName("org.springframework.tests.sample.objects.TestObject[]", classLoader));
|
||||
assertEquals(TestObject[].class, ClassUtils.forName(TestObject[].class.getName(), classLoader));
|
||||
assertEquals(TestObject[][].class, ClassUtils.forName("org.springframework.tests.sample.objects.TestObject[][]", classLoader));
|
||||
assertEquals(TestObject[][].class, ClassUtils.forName(TestObject[][].class.getName(), classLoader));
|
||||
assertEquals(short[][][].class, ClassUtils.forName("[[[S", classLoader));
|
||||
}
|
||||
|
||||
@@ -201,11 +201,11 @@ public class ClassUtilsTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testCountOverloadedMethods() {
|
||||
assertFalse(ClassUtils.hasAtLeastOneMethodWithName(TestBean.class, "foobar"));
|
||||
assertFalse(ClassUtils.hasAtLeastOneMethodWithName(TestObject.class, "foobar"));
|
||||
// no args
|
||||
assertTrue(ClassUtils.hasAtLeastOneMethodWithName(TestBean.class, "hashCode"));
|
||||
assertTrue(ClassUtils.hasAtLeastOneMethodWithName(TestObject.class, "hashCode"));
|
||||
// matches although it takes an arg
|
||||
assertTrue(ClassUtils.hasAtLeastOneMethodWithName(TestBean.class, "setAge"));
|
||||
assertTrue(ClassUtils.hasAtLeastOneMethodWithName(TestObject.class, "setAge"));
|
||||
}
|
||||
|
||||
public void testNoArgsStaticMethod() throws IllegalAccessException, InvocationTargetException {
|
||||
@@ -260,12 +260,12 @@ public class ClassUtilsTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testGetAllInterfaces() {
|
||||
DerivedTestBean testBean = new DerivedTestBean();
|
||||
DerivedTestObject testBean = new DerivedTestObject();
|
||||
List ifcs = Arrays.asList(ClassUtils.getAllInterfaces(testBean));
|
||||
assertEquals("Correct number of interfaces", 4, ifcs.size());
|
||||
assertTrue("Contains Serializable", ifcs.contains(Serializable.class));
|
||||
assertTrue("Contains ITestBean", ifcs.contains(ITestBean.class));
|
||||
assertTrue("Contains IOther", ifcs.contains(IOther.class));
|
||||
assertTrue("Contains ITestBean", ifcs.contains(ITestObject.class));
|
||||
assertTrue("Contains IOther", ifcs.contains(ITestInterface.class));
|
||||
}
|
||||
|
||||
public void testClassNamesToString() {
|
||||
@@ -308,19 +308,25 @@ public class ClassUtilsTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class OverloadedMethodsClass {
|
||||
|
||||
public void print(String messages) {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
public void print(String[] messages) {
|
||||
/* no-op */
|
||||
}
|
||||
}
|
||||
|
||||
private static class SubOverloadedMethodsClass extends OverloadedMethodsClass{
|
||||
@SuppressWarnings("unused")
|
||||
private static class SubOverloadedMethodsClass extends OverloadedMethodsClass {
|
||||
|
||||
public void print(String header, String[] messages) {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
void print(String header, String[] messages, String footer) {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -191,6 +191,7 @@ public class MethodInvokerTests extends TestCase {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static class Greeter {
|
||||
|
||||
// should handle Salesman (only interface)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -16,15 +16,6 @@
|
||||
|
||||
package org.springframework.util;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
@@ -33,9 +24,17 @@ import java.rmi.RemoteException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.TestBean;
|
||||
|
||||
import org.springframework.tests.Assume;
|
||||
import org.springframework.tests.TestGroup;
|
||||
import org.springframework.tests.sample.objects.TestObject;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
@@ -47,19 +46,19 @@ public class ReflectionUtilsTests {
|
||||
|
||||
@Test
|
||||
public void findField() {
|
||||
Field field = ReflectionUtils.findField(TestBeanSubclassWithPublicField.class, "publicField", String.class);
|
||||
Field field = ReflectionUtils.findField(TestObjectSubclassWithPublicField.class, "publicField", String.class);
|
||||
assertNotNull(field);
|
||||
assertEquals("publicField", field.getName());
|
||||
assertEquals(String.class, field.getType());
|
||||
assertTrue("Field should be public.", Modifier.isPublic(field.getModifiers()));
|
||||
|
||||
field = ReflectionUtils.findField(TestBeanSubclassWithNewField.class, "prot", String.class);
|
||||
field = ReflectionUtils.findField(TestObjectSubclassWithNewField.class, "prot", String.class);
|
||||
assertNotNull(field);
|
||||
assertEquals("prot", field.getName());
|
||||
assertEquals(String.class, field.getType());
|
||||
assertTrue("Field should be protected.", Modifier.isProtected(field.getModifiers()));
|
||||
|
||||
field = ReflectionUtils.findField(TestBeanSubclassWithNewField.class, "name", String.class);
|
||||
field = ReflectionUtils.findField(TestObjectSubclassWithNewField.class, "name", String.class);
|
||||
assertNotNull(field);
|
||||
assertEquals("name", field.getName());
|
||||
assertEquals(String.class, field.getType());
|
||||
@@ -68,8 +67,8 @@ public class ReflectionUtilsTests {
|
||||
|
||||
@Test
|
||||
public void setField() {
|
||||
final TestBeanSubclassWithNewField testBean = new TestBeanSubclassWithNewField();
|
||||
final Field field = ReflectionUtils.findField(TestBeanSubclassWithNewField.class, "name", String.class);
|
||||
final TestObjectSubclassWithNewField testBean = new TestObjectSubclassWithNewField();
|
||||
final Field field = ReflectionUtils.findField(TestObjectSubclassWithNewField.class, "name", String.class);
|
||||
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
|
||||
@@ -83,8 +82,8 @@ public class ReflectionUtilsTests {
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void setFieldIllegal() {
|
||||
final TestBeanSubclassWithNewField testBean = new TestBeanSubclassWithNewField();
|
||||
final Field field = ReflectionUtils.findField(TestBeanSubclassWithNewField.class, "name", String.class);
|
||||
final TestObjectSubclassWithNewField testBean = new TestObjectSubclassWithNewField();
|
||||
final Field field = ReflectionUtils.findField(TestObjectSubclassWithNewField.class, "name", String.class);
|
||||
ReflectionUtils.setField(field, testBean, "FooBar");
|
||||
}
|
||||
|
||||
@@ -92,11 +91,11 @@ public class ReflectionUtilsTests {
|
||||
public void invokeMethod() throws Exception {
|
||||
String rob = "Rob Harrop";
|
||||
|
||||
TestBean bean = new TestBean();
|
||||
TestObject bean = new TestObject();
|
||||
bean.setName(rob);
|
||||
|
||||
Method getName = TestBean.class.getMethod("getName", (Class[]) null);
|
||||
Method setName = TestBean.class.getMethod("setName", new Class[] { String.class });
|
||||
Method getName = TestObject.class.getMethod("getName", (Class[]) null);
|
||||
Method setName = TestObject.class.getMethod("setName", new Class[] { String.class });
|
||||
|
||||
Object name = ReflectionUtils.invokeMethod(getName, bean);
|
||||
assertEquals("Incorrect name returned", rob, name);
|
||||
@@ -123,7 +122,7 @@ public class ReflectionUtilsTests {
|
||||
|
||||
@Test
|
||||
public void copySrcToDestinationOfIncorrectClass() {
|
||||
TestBean src = new TestBean();
|
||||
TestObject src = new TestObject();
|
||||
String dest = new String();
|
||||
try {
|
||||
ReflectionUtils.shallowCopyFieldState(src, dest);
|
||||
@@ -135,7 +134,7 @@ public class ReflectionUtilsTests {
|
||||
|
||||
@Test
|
||||
public void rejectsNullSrc() {
|
||||
TestBean src = null;
|
||||
TestObject src = null;
|
||||
String dest = new String();
|
||||
try {
|
||||
ReflectionUtils.shallowCopyFieldState(src, dest);
|
||||
@@ -147,7 +146,7 @@ public class ReflectionUtilsTests {
|
||||
|
||||
@Test
|
||||
public void rejectsNullDest() {
|
||||
TestBean src = new TestBean();
|
||||
TestObject src = new TestObject();
|
||||
String dest = null;
|
||||
try {
|
||||
ReflectionUtils.shallowCopyFieldState(src, dest);
|
||||
@@ -159,15 +158,15 @@ public class ReflectionUtilsTests {
|
||||
|
||||
@Test
|
||||
public void validCopy() {
|
||||
TestBean src = new TestBean();
|
||||
TestBean dest = new TestBean();
|
||||
TestObject src = new TestObject();
|
||||
TestObject dest = new TestObject();
|
||||
testValidCopy(src, dest);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validCopyOnSubTypeWithNewField() {
|
||||
TestBeanSubclassWithNewField src = new TestBeanSubclassWithNewField();
|
||||
TestBeanSubclassWithNewField dest = new TestBeanSubclassWithNewField();
|
||||
TestObjectSubclassWithNewField src = new TestObjectSubclassWithNewField();
|
||||
TestObjectSubclassWithNewField dest = new TestObjectSubclassWithNewField();
|
||||
src.magic = 11;
|
||||
|
||||
// Will check inherited fields are copied
|
||||
@@ -180,8 +179,8 @@ public class ReflectionUtilsTests {
|
||||
|
||||
@Test
|
||||
public void validCopyToSubType() {
|
||||
TestBean src = new TestBean();
|
||||
TestBeanSubclassWithNewField dest = new TestBeanSubclassWithNewField();
|
||||
TestObject src = new TestObject();
|
||||
TestObjectSubclassWithNewField dest = new TestObjectSubclassWithNewField();
|
||||
dest.magic = 11;
|
||||
testValidCopy(src, dest);
|
||||
// Should have left this one alone
|
||||
@@ -190,28 +189,27 @@ public class ReflectionUtilsTests {
|
||||
|
||||
@Test
|
||||
public void validCopyToSubTypeWithFinalField() {
|
||||
TestBeanSubclassWithFinalField src = new TestBeanSubclassWithFinalField();
|
||||
TestBeanSubclassWithFinalField dest = new TestBeanSubclassWithFinalField();
|
||||
TestObjectSubclassWithFinalField src = new TestObjectSubclassWithFinalField();
|
||||
TestObjectSubclassWithFinalField dest = new TestObjectSubclassWithFinalField();
|
||||
// Check that this doesn't fail due to attempt to assign final
|
||||
testValidCopy(src, dest);
|
||||
}
|
||||
|
||||
private void testValidCopy(TestBean src, TestBean dest) {
|
||||
private void testValidCopy(TestObject src, TestObject dest) {
|
||||
src.setName("freddie");
|
||||
src.setAge(15);
|
||||
src.setSpouse(new TestBean());
|
||||
src.setSpouse(new TestObject());
|
||||
assertFalse(src.getAge() == dest.getAge());
|
||||
|
||||
ReflectionUtils.shallowCopyFieldState(src, dest);
|
||||
assertEquals(src.getAge(), dest.getAge());
|
||||
assertEquals(src.getSpouse(), dest.getSpouse());
|
||||
assertEquals(src.getDoctor(), dest.getDoctor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doWithProtectedMethods() {
|
||||
ListSavingMethodCallback mc = new ListSavingMethodCallback();
|
||||
ReflectionUtils.doWithMethods(TestBean.class, mc, new ReflectionUtils.MethodFilter() {
|
||||
ReflectionUtils.doWithMethods(TestObject.class, mc, new ReflectionUtils.MethodFilter() {
|
||||
@Override
|
||||
public boolean matches(Method m) {
|
||||
return Modifier.isProtected(m.getModifiers());
|
||||
@@ -227,7 +225,7 @@ public class ReflectionUtilsTests {
|
||||
@Test
|
||||
public void duplicatesFound() {
|
||||
ListSavingMethodCallback mc = new ListSavingMethodCallback();
|
||||
ReflectionUtils.doWithMethods(TestBeanSubclass.class, mc);
|
||||
ReflectionUtils.doWithMethods(TestObjectSubclass.class, mc);
|
||||
int absquatulateCount = 0;
|
||||
for (String name : mc.getMethodNames()) {
|
||||
if (name.equals("absquatulate")) {
|
||||
@@ -348,6 +346,43 @@ public class ReflectionUtilsTests {
|
||||
assertFalse(ObjectUtils.containsElement(methods, Parent.class.getMethod("m1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUniqueDeclaredMethods_isFastEnough() {
|
||||
Assume.group(TestGroup.PERFORMANCE);
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
class C {
|
||||
void m00() { } void m01() { } void m02() { } void m03() { } void m04() { }
|
||||
void m05() { } void m06() { } void m07() { } void m08() { } void m09() { }
|
||||
void m10() { } void m11() { } void m12() { } void m13() { } void m14() { }
|
||||
void m15() { } void m16() { } void m17() { } void m18() { } void m19() { }
|
||||
void m20() { } void m21() { } void m22() { } void m23() { } void m24() { }
|
||||
void m25() { } void m26() { } void m27() { } void m28() { } void m29() { }
|
||||
void m30() { } void m31() { } void m32() { } void m33() { } void m34() { }
|
||||
void m35() { } void m36() { } void m37() { } void m38() { } void m39() { }
|
||||
void m40() { } void m41() { } void m42() { } void m43() { } void m44() { }
|
||||
void m45() { } void m46() { } void m47() { } void m48() { } void m49() { }
|
||||
void m50() { } void m51() { } void m52() { } void m53() { } void m54() { }
|
||||
void m55() { } void m56() { } void m57() { } void m58() { } void m59() { }
|
||||
void m60() { } void m61() { } void m62() { } void m63() { } void m64() { }
|
||||
void m65() { } void m66() { } void m67() { } void m68() { } void m69() { }
|
||||
void m70() { } void m71() { } void m72() { } void m73() { } void m74() { }
|
||||
void m75() { } void m76() { } void m77() { } void m78() { } void m79() { }
|
||||
void m80() { } void m81() { } void m82() { } void m83() { } void m84() { }
|
||||
void m85() { } void m86() { } void m87() { } void m88() { } void m89() { }
|
||||
void m90() { } void m91() { } void m92() { } void m93() { } void m94() { }
|
||||
void m95() { } void m96() { } void m97() { } void m98() { } void m99() { }
|
||||
}
|
||||
|
||||
StopWatch sw = new StopWatch();
|
||||
sw.start();
|
||||
Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(C.class);
|
||||
sw.stop();
|
||||
long totalMs = sw.getTotalTimeMillis();
|
||||
assertThat(methods.length, Matchers.greaterThan(100));
|
||||
assertThat(totalMs, Matchers.lessThan(10L));
|
||||
}
|
||||
|
||||
private static class ListSavingMethodCallback implements ReflectionUtils.MethodCallback {
|
||||
|
||||
private List<String> methodNames = new LinkedList<String>();
|
||||
@@ -370,7 +405,7 @@ public class ReflectionUtilsTests {
|
||||
}
|
||||
}
|
||||
|
||||
private static class TestBeanSubclass extends TestBean {
|
||||
private static class TestObjectSubclass extends TestObject {
|
||||
|
||||
@Override
|
||||
public void absquatulate() {
|
||||
@@ -378,20 +413,20 @@ public class ReflectionUtilsTests {
|
||||
}
|
||||
}
|
||||
|
||||
private static class TestBeanSubclassWithPublicField extends TestBean {
|
||||
private static class TestObjectSubclassWithPublicField extends TestObject {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public String publicField = "foo";
|
||||
}
|
||||
|
||||
private static class TestBeanSubclassWithNewField extends TestBean {
|
||||
private static class TestObjectSubclassWithNewField extends TestObject {
|
||||
|
||||
private int magic;
|
||||
|
||||
protected String prot = "foo";
|
||||
}
|
||||
|
||||
private static class TestBeanSubclassWithFinalField extends TestBean {
|
||||
private static class TestObjectSubclassWithFinalField extends TestObject {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private final String foo = "will break naive copy that doesn't exclude statics";
|
||||
|
||||
@@ -1,11 +1,21 @@
|
||||
/*
|
||||
* The Spring Framework is published under the terms
|
||||
* of the Apache Software License.
|
||||
* Copyright 2002-2013 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.util;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
@@ -13,20 +23,14 @@ import java.io.NotSerializableException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
|
||||
/**
|
||||
* Utilities for testing serializability of objects.
|
||||
* Exposes static methods for use in other test cases.
|
||||
* Extends TestCase only to test itself.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
public class SerializationTestUtils extends TestCase {
|
||||
public class SerializationTestUtils {
|
||||
|
||||
public static void testSerialization(Object o) throws IOException {
|
||||
OutputStream baos = new ByteArrayOutputStream();
|
||||
@@ -55,43 +59,7 @@ public class SerializationTestUtils extends TestCase {
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
Object o2 = ois.readObject();
|
||||
|
||||
return o2;
|
||||
}
|
||||
|
||||
public SerializationTestUtils(String s) {
|
||||
super(s);
|
||||
}
|
||||
|
||||
public void testWithNonSerializableObject() throws IOException {
|
||||
TestBean o = new TestBean();
|
||||
assertFalse(o instanceof Serializable);
|
||||
|
||||
assertFalse(isSerializable(o));
|
||||
|
||||
try {
|
||||
testSerialization(o);
|
||||
fail();
|
||||
}
|
||||
catch (NotSerializableException ex) {
|
||||
// Ok
|
||||
}
|
||||
}
|
||||
|
||||
public void testWithSerializableObject() throws Exception {
|
||||
int x = 5;
|
||||
int y = 10;
|
||||
Point p = new Point(x, y);
|
||||
assertTrue(p instanceof Serializable);
|
||||
|
||||
testSerialization(p);
|
||||
|
||||
assertTrue(isSerializable(p));
|
||||
|
||||
Point p2 = (Point) serializeAndDeserialize(p);
|
||||
assertNotSame(p, p2);
|
||||
assertEquals(x, (int) p2.getX());
|
||||
assertEquals(y, (int) p2.getY());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -34,7 +34,6 @@ public class StopWatchTests extends TestCase {
|
||||
String name1 = "Task 1";
|
||||
String name2 = "Task 2";
|
||||
|
||||
long fudgeFactor = 5L;
|
||||
assertFalse(sw.isRunning());
|
||||
sw.start(name1);
|
||||
Thread.sleep(int1);
|
||||
@@ -44,6 +43,7 @@ public class StopWatchTests extends TestCase {
|
||||
// TODO are timings off in JUnit? Why do these assertions sometimes fail
|
||||
// under both Ant and Eclipse?
|
||||
|
||||
//long fudgeFactor = 5L;
|
||||
//assertTrue("Unexpected timing " + sw.getTotalTime(), sw.getTotalTime() >= int1);
|
||||
//assertTrue("Unexpected timing " + sw.getTotalTime(), sw.getTotalTime() <= int1 + fudgeFactor);
|
||||
sw.start(name2);
|
||||
@@ -72,7 +72,6 @@ public class StopWatchTests extends TestCase {
|
||||
String name1 = "Task 1";
|
||||
String name2 = "Task 2";
|
||||
|
||||
long fudgeFactor = 5L;
|
||||
assertFalse(sw.isRunning());
|
||||
sw.start(name1);
|
||||
Thread.sleep(int1);
|
||||
@@ -82,6 +81,7 @@ public class StopWatchTests extends TestCase {
|
||||
// TODO are timings off in JUnit? Why do these assertions sometimes fail
|
||||
// under both Ant and Eclipse?
|
||||
|
||||
//long fudgeFactor = 5L;
|
||||
//assertTrue("Unexpected timing " + sw.getTotalTime(), sw.getTotalTime() >= int1);
|
||||
//assertTrue("Unexpected timing " + sw.getTotalTime(), sw.getTotalTime() <= int1 + fudgeFactor);
|
||||
sw.start(name2);
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.util;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
/**
|
||||
* Tests for {@link StreamUtils}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class StreamUtilsTests {
|
||||
|
||||
private byte[] bytes = new byte[StreamUtils.BUFFER_SIZE + 10];
|
||||
|
||||
private String string = "";
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
new Random().nextBytes(bytes);
|
||||
while (string.length() < StreamUtils.BUFFER_SIZE + 10) {
|
||||
string += UUID.randomUUID().toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyToByteArray() throws Exception {
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(bytes));
|
||||
byte[] actual = StreamUtils.copyToByteArray(inputStream);
|
||||
assertThat(actual, equalTo(bytes));
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyToString() throws Exception {
|
||||
Charset charset = Charset.defaultCharset();
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(string.getBytes(charset)));
|
||||
String actual = StreamUtils.copyToString(inputStream, charset);
|
||||
assertThat(actual, equalTo(string));
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyBytes() throws Exception {
|
||||
ByteArrayOutputStream out = spy(new ByteArrayOutputStream());
|
||||
StreamUtils.copy(bytes, out);
|
||||
assertThat(out.toByteArray(), equalTo(bytes));
|
||||
verify(out, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyString() throws Exception {
|
||||
Charset charset = Charset.defaultCharset();
|
||||
ByteArrayOutputStream out = spy(new ByteArrayOutputStream());
|
||||
StreamUtils.copy(string, charset, out);
|
||||
assertThat(out.toByteArray(), equalTo(string.getBytes(charset)));
|
||||
verify(out, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyStream() throws Exception {
|
||||
ByteArrayOutputStream out = spy(new ByteArrayOutputStream());
|
||||
StreamUtils.copy(new ByteArrayInputStream(bytes), out);
|
||||
assertThat(out.toByteArray(), equalTo(bytes));
|
||||
verify(out, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonClosingInputStream() throws Exception {
|
||||
InputStream source = mock(InputStream.class);
|
||||
InputStream nonClosing = StreamUtils.nonClosing(source);
|
||||
nonClosing.read();
|
||||
nonClosing.read(bytes);
|
||||
nonClosing.read(bytes, 1, 2);
|
||||
nonClosing.close();
|
||||
InOrder ordered = inOrder(source);
|
||||
ordered.verify(source).read();
|
||||
ordered.verify(source).read(bytes, 0, bytes.length);
|
||||
ordered.verify(source).read(bytes, 1, 2);
|
||||
ordered.verify(source, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonClosingOutputStream() throws Exception {
|
||||
OutputStream source = mock(OutputStream.class);
|
||||
OutputStream nonClosing = StreamUtils.nonClosing(source);
|
||||
nonClosing.write(1);
|
||||
nonClosing.write(bytes);
|
||||
nonClosing.write(bytes, 1, 2);
|
||||
nonClosing.close();
|
||||
InOrder ordered = inOrder(source);
|
||||
ordered.verify(source).write(1);
|
||||
ordered.verify(source).write(bytes, 0, bytes.length);
|
||||
ordered.verify(source).write(bytes, 1, 2);
|
||||
ordered.verify(source, never()).close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user