mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-02 19:15:29 -04:00
TMC-LANG: Code clean-up, added tests and fixed issues.
This commit is contained in:
parent
f5b5e818c5
commit
cdce0aebff
@ -41,8 +41,7 @@ import java.util.Arrays;
|
|||||||
/**
|
/**
|
||||||
* A utility class with some useful bean-related functions.
|
* A utility class with some useful bean-related functions.
|
||||||
* <p/>
|
* <p/>
|
||||||
* <em>NOTE: This class is not considered part of the public API and may be
|
* <em>NOTE: This class is not considered part of the public API and may be changed without notice</em>
|
||||||
* changed without notice</em>
|
|
||||||
*
|
*
|
||||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||||
* @author last modified by $Author: haku $
|
* @author last modified by $Author: haku $
|
||||||
@ -60,10 +59,10 @@ public final class BeanUtil {
|
|||||||
* Now supports getting values from properties of properties
|
* Now supports getting values from properties of properties
|
||||||
* (recursive).
|
* (recursive).
|
||||||
*
|
*
|
||||||
* @param pObject The object to get the property from
|
* @param pObject The object to get the property from
|
||||||
* @param pProperty The name of the property
|
* @param pProperty The name of the property
|
||||||
*
|
*
|
||||||
* @return A string containing the value of the given property, or null
|
* @return A string containing the value of the given property, or {@code null}
|
||||||
* if it can not be found.
|
* if it can not be found.
|
||||||
* @todo Remove System.err's... Create new Exception? Hmm..
|
* @todo Remove System.err's... Create new Exception? Hmm..
|
||||||
*/
|
*/
|
||||||
@ -77,7 +76,7 @@ public final class BeanUtil {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Class objClass = pObject.getClass();
|
Class<?> objClass = pObject.getClass();
|
||||||
|
|
||||||
Object result = pObject;
|
Object result = pObject;
|
||||||
|
|
||||||
@ -154,9 +153,8 @@ public final class BeanUtil {
|
|||||||
catch (NoSuchMethodException e) {
|
catch (NoSuchMethodException e) {
|
||||||
System.err.print("No method named \"" + methodName + "()\"");
|
System.err.print("No method named \"" + methodName + "()\"");
|
||||||
// The array might be of size 0...
|
// The array might be of size 0...
|
||||||
if (paramClass != null && paramClass.length > 0) {
|
if (paramClass.length > 0 && paramClass[0] != null) {
|
||||||
System.err.print(" with the parameter "
|
System.err.print(" with the parameter " + paramClass[0].getName());
|
||||||
+ paramClass[0].getName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
System.err.println(" in class " + objClass.getName() + "!");
|
System.err.println(" in class " + objClass.getName() + "!");
|
||||||
@ -177,8 +175,7 @@ public final class BeanUtil {
|
|||||||
result = method.invoke(result, param);
|
result = method.invoke(result, param);
|
||||||
}
|
}
|
||||||
catch (InvocationTargetException e) {
|
catch (InvocationTargetException e) {
|
||||||
System.err.println("property=" + pProperty + " & result="
|
System.err.println("property=" + pProperty + " & result=" + result + " & param=" + Arrays.toString(param));
|
||||||
+ result + " & param=" + Arrays.toString(param));
|
|
||||||
e.getTargetException().printStackTrace();
|
e.getTargetException().printStackTrace();
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
@ -188,8 +185,7 @@ public final class BeanUtil {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
catch (NullPointerException e) {
|
catch (NullPointerException e) {
|
||||||
System.err.println(objClass.getName() + "." + method.getName()
|
System.err.println(objClass.getName() + "." + method.getName() + "(" + ((paramClass.length > 0 && paramClass[0] != null) ? paramClass[0].getName() : "") + ")");
|
||||||
+ "(" + ((paramClass != null && paramClass.length > 0) ? paramClass[0].getName() : "") + ")");
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -221,10 +217,8 @@ public final class BeanUtil {
|
|||||||
* @throws IllegalAccessException if the caller class has no access to the
|
* @throws IllegalAccessException if the caller class has no access to the
|
||||||
* write method
|
* write method
|
||||||
*/
|
*/
|
||||||
public static void setPropertyValue(Object pObject, String pProperty,
|
public static void setPropertyValue(Object pObject, String pProperty, Object pValue)
|
||||||
Object pValue)
|
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||||
throws NoSuchMethodException, InvocationTargetException,
|
|
||||||
IllegalAccessException {
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// TODO: Support set(Object, Object)/put(Object, Object) methods
|
// TODO: Support set(Object, Object)/put(Object, Object) methods
|
||||||
@ -255,7 +249,8 @@ public final class BeanUtil {
|
|||||||
method.invoke(obj, params);
|
method.invoke(obj, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Method getMethodMayModifyParams(Object pObject, String pName, Class[] pParams, Object[] pValues) throws NoSuchMethodException {
|
private static Method getMethodMayModifyParams(Object pObject, String pName, Class[] pParams, Object[] pValues)
|
||||||
|
throws NoSuchMethodException {
|
||||||
// NOTE: This method assumes pParams.length == 1 && pValues.length == 1
|
// NOTE: This method assumes pParams.length == 1 && pValues.length == 1
|
||||||
|
|
||||||
Method method = null;
|
Method method = null;
|
||||||
@ -307,10 +302,8 @@ public final class BeanUtil {
|
|||||||
if (method == null) {
|
if (method == null) {
|
||||||
Method[] methods = pObject.getClass().getMethods();
|
Method[] methods = pObject.getClass().getMethods();
|
||||||
for (Method candidate : methods) {
|
for (Method candidate : methods) {
|
||||||
if (Modifier.isPublic(candidate.getModifiers())
|
if (Modifier.isPublic(candidate.getModifiers()) && candidate.getName().equals(pName)
|
||||||
&& candidate.getName().equals(pName)
|
&& candidate.getReturnType() == Void.TYPE && candidate.getParameterTypes().length == 1) {
|
||||||
&& candidate.getReturnType() == Void.TYPE
|
|
||||||
&& candidate.getParameterTypes().length == 1) {
|
|
||||||
// NOTE: Assumes paramTypes.length == 1
|
// NOTE: Assumes paramTypes.length == 1
|
||||||
|
|
||||||
Class type = candidate.getParameterTypes()[0];
|
Class type = candidate.getParameterTypes()[0];
|
||||||
@ -337,7 +330,7 @@ public final class BeanUtil {
|
|||||||
return method;
|
return method;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Object convertValueToType(Object pValue, Class pType) throws ConversionException {
|
private static Object convertValueToType(Object pValue, Class<?> pType) throws ConversionException {
|
||||||
if (pType.isPrimitive()) {
|
if (pType.isPrimitive()) {
|
||||||
if (pType == Boolean.TYPE && pValue instanceof Boolean) {
|
if (pType == Boolean.TYPE && pValue instanceof Boolean) {
|
||||||
return pValue;
|
return pValue;
|
||||||
@ -395,7 +388,7 @@ public final class BeanUtil {
|
|||||||
* @throws InvocationTargetException if the constructor failed
|
* @throws InvocationTargetException if the constructor failed
|
||||||
*/
|
*/
|
||||||
// TODO: Move to ReflectUtil
|
// TODO: Move to ReflectUtil
|
||||||
public static Object createInstance(Class pClass, Object pParam)
|
public static <T> T createInstance(Class<T> pClass, Object pParam)
|
||||||
throws InvocationTargetException {
|
throws InvocationTargetException {
|
||||||
return createInstance(pClass, new Object[] {pParam});
|
return createInstance(pClass, new Object[] {pParam});
|
||||||
}
|
}
|
||||||
@ -414,9 +407,9 @@ public final class BeanUtil {
|
|||||||
* @throws InvocationTargetException if the constructor failed
|
* @throws InvocationTargetException if the constructor failed
|
||||||
*/
|
*/
|
||||||
// TODO: Move to ReflectUtil
|
// TODO: Move to ReflectUtil
|
||||||
public static Object createInstance(Class pClass, Object... pParams)
|
public static <T> T createInstance(Class<T> pClass, Object... pParams)
|
||||||
throws InvocationTargetException {
|
throws InvocationTargetException {
|
||||||
Object value;
|
T value;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Create param and argument arrays
|
// Create param and argument arrays
|
||||||
@ -429,8 +422,7 @@ public final class BeanUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get constructor
|
// Get constructor
|
||||||
//Constructor constructor = pClass.getDeclaredConstructor(paramTypes);
|
Constructor<T> constructor = pClass.getConstructor(paramTypes);
|
||||||
Constructor constructor = pClass.getConstructor(paramTypes);
|
|
||||||
|
|
||||||
// Invoke and create instance
|
// Invoke and create instance
|
||||||
value = constructor.newInstance(pParams);
|
value = constructor.newInstance(pParams);
|
||||||
@ -468,12 +460,11 @@ public final class BeanUtil {
|
|||||||
* If the return type of the method is void, null is returned.
|
* If the return type of the method is void, null is returned.
|
||||||
* If the method could not be invoked for any reason, null is returned.
|
* If the method could not be invoked for any reason, null is returned.
|
||||||
*
|
*
|
||||||
* @throws InvocationTargetException if the invocaton failed
|
* @throws InvocationTargetException if the invocation failed
|
||||||
*/
|
*/
|
||||||
// TODO: Move to ReflectUtil
|
// TODO: Move to ReflectUtil
|
||||||
// TODO: Rename to invokeStatic?
|
// TODO: Rename to invokeStatic?
|
||||||
public static Object invokeStaticMethod(Class pClass, String pMethod,
|
public static Object invokeStaticMethod(Class<?> pClass, String pMethod, Object pParam)
|
||||||
Object pParam)
|
|
||||||
throws InvocationTargetException {
|
throws InvocationTargetException {
|
||||||
|
|
||||||
return invokeStaticMethod(pClass, pMethod, new Object[] {pParam});
|
return invokeStaticMethod(pClass, pMethod, new Object[] {pParam});
|
||||||
@ -492,12 +483,11 @@ public final class BeanUtil {
|
|||||||
* If the return type of the method is void, null is returned.
|
* If the return type of the method is void, null is returned.
|
||||||
* If the method could not be invoked for any reason, null is returned.
|
* If the method could not be invoked for any reason, null is returned.
|
||||||
*
|
*
|
||||||
* @throws InvocationTargetException if the invocaton failed
|
* @throws InvocationTargetException if the invocation failed
|
||||||
*/
|
*/
|
||||||
// TODO: Move to ReflectUtil
|
// TODO: Move to ReflectUtil
|
||||||
// TODO: Rename to invokeStatic?
|
// TODO: Rename to invokeStatic?
|
||||||
public static Object invokeStaticMethod(Class pClass, String pMethod,
|
public static Object invokeStaticMethod(Class<?> pClass, String pMethod, Object... pParams)
|
||||||
Object[] pParams)
|
|
||||||
throws InvocationTargetException {
|
throws InvocationTargetException {
|
||||||
|
|
||||||
Object value = null;
|
Object value = null;
|
||||||
@ -518,8 +508,7 @@ public final class BeanUtil {
|
|||||||
Method method = pClass.getMethod(pMethod, paramTypes);
|
Method method = pClass.getMethod(pMethod, paramTypes);
|
||||||
|
|
||||||
// Invoke public static method
|
// Invoke public static method
|
||||||
if (Modifier.isPublic(method.getModifiers())
|
if (Modifier.isPublic(method.getModifiers()) && Modifier.isStatic(method.getModifiers())) {
|
||||||
&& Modifier.isStatic(method.getModifiers())) {
|
|
||||||
value = method.invoke(null, pParams);
|
value = method.invoke(null, pParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ import java.util.Map;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The converter (singleton). Converts strings to objects and back.
|
* The converter (singleton). Converts strings to objects and back.
|
||||||
* This is the entrypoint to the converter framework.
|
* This is the entry point to the converter framework.
|
||||||
* <p/>
|
* <p/>
|
||||||
* By default, converters for {@link com.twelvemonkeys.util.Time}, {@link Date}
|
* By default, converters for {@link com.twelvemonkeys.util.Time}, {@link Date}
|
||||||
* and {@link Object}
|
* and {@link Object}
|
||||||
@ -53,17 +53,17 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
// TODO: Get rid of singleton stuff
|
// TODO: Get rid of singleton stuff
|
||||||
// Can probably be a pure static class, but is that a good idea?
|
// Can probably be a pure static class, but is that a good idea?
|
||||||
// Maybe have BeanUtil act as a "proxy", and hide this class alltogheter?
|
// Maybe have BeanUtil act as a "proxy", and hide this class all together?
|
||||||
// TODO: ServiceRegistry for registering 3rd party converters
|
// TODO: ServiceRegistry for registering 3rd party converters
|
||||||
// TODO: URI scheme, for implicit typing? Is that a good idea?
|
// TODO: URI scheme, for implicit typing? Is that a good idea?
|
||||||
// TODO: Array converters?
|
// TODO: Array converters?
|
||||||
public abstract class Converter implements PropertyConverter {
|
public abstract class Converter implements PropertyConverter {
|
||||||
|
|
||||||
/** Our singleton instance */
|
/** Our singleton instance */
|
||||||
protected static Converter sInstance = new ConverterImpl(); // Thread safe & EASY
|
protected static final Converter sInstance = new ConverterImpl(); // Thread safe & EASY
|
||||||
|
|
||||||
/** The conveters Map */
|
/** The converters Map */
|
||||||
protected Map converters = new Hashtable();
|
protected final Map<Class, PropertyConverter> converters = new Hashtable<Class, PropertyConverter>();
|
||||||
|
|
||||||
// Register our predefined converters
|
// Register our predefined converters
|
||||||
static {
|
static {
|
||||||
@ -115,20 +115,21 @@ public abstract class Converter implements PropertyConverter {
|
|||||||
*
|
*
|
||||||
* @see #unregisterConverter(Class)
|
* @see #unregisterConverter(Class)
|
||||||
*/
|
*/
|
||||||
public static void registerConverter(Class pType, PropertyConverter pConverter) {
|
public static void registerConverter(final Class<?> pType, final PropertyConverter pConverter) {
|
||||||
getInstance().converters.put(pType, pConverter);
|
getInstance().converters.put(pType, pConverter);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unregisters a converter for a given type. That is, making it unavailable
|
* Un-registers a converter for a given type. That is, making it unavailable
|
||||||
* for the converter framework, and making it (potentially) available for
|
* for the converter framework, and making it (potentially) available for
|
||||||
* garbabe collection.
|
* garbage collection.
|
||||||
*
|
*
|
||||||
* @param pType the (super) type to remove converter for
|
* @param pType the (super) type to remove converter for
|
||||||
*
|
*
|
||||||
* @see #registerConverter(Class,PropertyConverter)
|
* @see #registerConverter(Class,PropertyConverter)
|
||||||
*/
|
*/
|
||||||
public static void unregisterConverter(Class pType) {
|
@SuppressWarnings("UnusedDeclaration")
|
||||||
|
public static void unregisterConverter(final Class<?> pType) {
|
||||||
getInstance().converters.remove(pType);
|
getInstance().converters.remove(pType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,8 +144,7 @@ public abstract class Converter implements PropertyConverter {
|
|||||||
* @throws ConversionException if the string cannot be converted for any
|
* @throws ConversionException if the string cannot be converted for any
|
||||||
* reason.
|
* reason.
|
||||||
*/
|
*/
|
||||||
public Object toObject(String pString, Class pType)
|
public Object toObject(final String pString, final Class pType) throws ConversionException {
|
||||||
throws ConversionException {
|
|
||||||
return toObject(pString, pType, null);
|
return toObject(pString, pType, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,7 +174,7 @@ public abstract class Converter implements PropertyConverter {
|
|||||||
* @throws ConversionException if the object cannot be converted to a
|
* @throws ConversionException if the object cannot be converted to a
|
||||||
* string for any reason.
|
* string for any reason.
|
||||||
*/
|
*/
|
||||||
public String toString(Object pObject) throws ConversionException {
|
public String toString(final Object pObject) throws ConversionException {
|
||||||
return toString(pObject, null);
|
return toString(pObject, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,9 +67,9 @@ public final class DefaultConverter implements PropertyConverter {
|
|||||||
*
|
*
|
||||||
* @throws ConversionException if the type is null, or if the string cannot
|
* @throws ConversionException if the type is null, or if the string cannot
|
||||||
* be converted into the given type, using a string constructor or static
|
* be converted into the given type, using a string constructor or static
|
||||||
* {@code valueof} method.
|
* {@code valueOf} method.
|
||||||
*/
|
*/
|
||||||
public Object toObject(String pString, final Class pType, String pFormat) throws ConversionException {
|
public Object toObject(final String pString, final Class pType, final String pFormat) throws ConversionException {
|
||||||
if (pString == null) {
|
if (pString == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -87,13 +87,7 @@ public final class DefaultConverter implements PropertyConverter {
|
|||||||
// But what about generic type?! It's erased...
|
// But what about generic type?! It's erased...
|
||||||
|
|
||||||
// Primitive -> wrapper
|
// Primitive -> wrapper
|
||||||
Class type;
|
Class type = unBoxType(pType);
|
||||||
if (pType == Boolean.TYPE) {
|
|
||||||
type = Boolean.class;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
type = pType;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Try to create instance from <Constructor>(String)
|
// Try to create instance from <Constructor>(String)
|
||||||
@ -101,13 +95,15 @@ public final class DefaultConverter implements PropertyConverter {
|
|||||||
|
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
// createInstance failed for some reason
|
// createInstance failed for some reason
|
||||||
|
// Try to invoke the static method valueOf(String)
|
||||||
// Try to invoke the static method valueof(String)
|
|
||||||
value = BeanUtil.invokeStaticMethod(type, "valueOf", pString);
|
value = BeanUtil.invokeStaticMethod(type, "valueOf", pString);
|
||||||
|
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
// If the value is still null, well, then I cannot help...
|
// If the value is still null, well, then I cannot help...
|
||||||
throw new ConversionException("Could not convert String to " + pType.getName() + ": No constructor " + type.getName() + "(String) or static " + type.getName() + ".valueof(String) method found!");
|
throw new ConversionException(String.format(
|
||||||
|
"Could not convert String to %1$s: No constructor %1$s(String) or static %1$s.valueOf(String) method found!",
|
||||||
|
type.getName()
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,12 +112,15 @@ public final class DefaultConverter implements PropertyConverter {
|
|||||||
catch (InvocationTargetException ite) {
|
catch (InvocationTargetException ite) {
|
||||||
throw new ConversionException(ite.getTargetException());
|
throw new ConversionException(ite.getTargetException());
|
||||||
}
|
}
|
||||||
|
catch (ConversionException ce) {
|
||||||
|
throw ce;
|
||||||
|
}
|
||||||
catch (RuntimeException rte) {
|
catch (RuntimeException rte) {
|
||||||
throw new ConversionException(rte);
|
throw new ConversionException(rte);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object toArray(String pString, Class pType, String pFormat) {
|
private Object toArray(final String pString, final Class pType, final String pFormat) {
|
||||||
String[] strings = StringUtil.toStringArray(pString, pFormat != null ? pFormat : StringUtil.DELIMITER_STRING);
|
String[] strings = StringUtil.toStringArray(pString, pFormat != null ? pFormat : StringUtil.DELIMITER_STRING);
|
||||||
Class type = pType.getComponentType();
|
Class type = pType.getComponentType();
|
||||||
if (type == String.class) {
|
if (type == String.class) {
|
||||||
@ -152,10 +151,9 @@ public final class DefaultConverter implements PropertyConverter {
|
|||||||
* @param pObject the object to convert.
|
* @param pObject the object to convert.
|
||||||
* @param pFormat ignored.
|
* @param pFormat ignored.
|
||||||
*
|
*
|
||||||
* @return the string representation of the object, or {@code null} if
|
* @return the string representation of the object, or {@code null} if {@code pObject == null}
|
||||||
* {@code pObject == null}
|
|
||||||
*/
|
*/
|
||||||
public String toString(Object pObject, String pFormat)
|
public String toString(final Object pObject, final String pFormat)
|
||||||
throws ConversionException {
|
throws ConversionException {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -170,7 +168,7 @@ public final class DefaultConverter implements PropertyConverter {
|
|||||||
return pFormat == null ? StringUtil.toCSVString(pArray) : StringUtil.toCSVString(pArray, pFormat);
|
return pFormat == null ? StringUtil.toCSVString(pArray) : StringUtil.toCSVString(pArray, pFormat);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object[] toObjectArray(Object pObject) {
|
private Object[] toObjectArray(final Object pObject) {
|
||||||
// TODO: Extract util method for wrapping/unwrapping native arrays?
|
// TODO: Extract util method for wrapping/unwrapping native arrays?
|
||||||
Object[] array;
|
Object[] array;
|
||||||
Class<?> componentType = pObject.getClass().getComponentType();
|
Class<?> componentType = pObject.getClass().getComponentType();
|
||||||
@ -232,4 +230,37 @@ public final class DefaultConverter implements PropertyConverter {
|
|||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Class<?> unBoxType(final Class<?> pType) {
|
||||||
|
if (pType.isPrimitive()) {
|
||||||
|
if (pType == boolean.class) {
|
||||||
|
return Boolean.class;
|
||||||
|
}
|
||||||
|
if (pType == byte.class) {
|
||||||
|
return Byte.class;
|
||||||
|
}
|
||||||
|
if (pType == char.class) {
|
||||||
|
return Character.class;
|
||||||
|
}
|
||||||
|
if (pType == short.class) {
|
||||||
|
return Short.class;
|
||||||
|
}
|
||||||
|
if (pType == int.class) {
|
||||||
|
return Integer.class;
|
||||||
|
}
|
||||||
|
if (pType == float.class) {
|
||||||
|
return Float.class;
|
||||||
|
}
|
||||||
|
if (pType == long.class) {
|
||||||
|
return Long.class;
|
||||||
|
}
|
||||||
|
if (pType == double.class) {
|
||||||
|
return Double.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException("Unknown type: " + pType);
|
||||||
|
}
|
||||||
|
|
||||||
|
return pType;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -108,13 +108,13 @@ public class BeanUtilTestCase extends TestCase {
|
|||||||
assertEquals(0.3, bean.getDoubleValue());
|
assertEquals(0.3, bean.getDoubleValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testConfigureAmbigious1() {
|
public void testConfigureAmbiguous1() {
|
||||||
TestBean bean = new TestBean();
|
TestBean bean = new TestBean();
|
||||||
|
|
||||||
Map<String, String> map = new HashMap<String, String>();
|
Map<String, String> map = new HashMap<String, String>();
|
||||||
|
|
||||||
String value = "one";
|
String value = "one";
|
||||||
map.put("ambigious", value);
|
map.put("ambiguous", value);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
BeanUtil.configure(bean, map);
|
BeanUtil.configure(bean, map);
|
||||||
@ -123,20 +123,20 @@ public class BeanUtilTestCase extends TestCase {
|
|||||||
fail(e.getMessage());
|
fail(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
assertNotNull(bean.getAmbigious());
|
assertNotNull(bean.getAmbiguous());
|
||||||
assertEquals("String converted rather than invoking setAmbigiouos(String), ordering not predictable",
|
assertEquals("String converted rather than invoking setAmbiguous(String), ordering not predictable",
|
||||||
"one", bean.getAmbigious());
|
"one", bean.getAmbiguous());
|
||||||
assertSame("String converted rather than invoking setAmbigiouos(String), ordering not predictable",
|
assertSame("String converted rather than invoking setAmbiguous(String), ordering not predictable",
|
||||||
value, bean.getAmbigious());
|
value, bean.getAmbiguous());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testConfigureAmbigious2() {
|
public void testConfigureAmbiguous2() {
|
||||||
TestBean bean = new TestBean();
|
TestBean bean = new TestBean();
|
||||||
|
|
||||||
Map<String, Integer> map = new HashMap<String, Integer>();
|
Map<String, Integer> map = new HashMap<String, Integer>();
|
||||||
|
|
||||||
Integer value = 2;
|
Integer value = 2;
|
||||||
map.put("ambigious", value);
|
map.put("ambiguous", value);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
BeanUtil.configure(bean, map);
|
BeanUtil.configure(bean, map);
|
||||||
@ -145,20 +145,20 @@ public class BeanUtilTestCase extends TestCase {
|
|||||||
fail(e.getMessage());
|
fail(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
assertNotNull(bean.getAmbigious());
|
assertNotNull(bean.getAmbiguous());
|
||||||
assertEquals("Integer converted rather than invoking setAmbigiouos(Integer), ordering not predictable",
|
assertEquals("Integer converted rather than invoking setAmbiguous(Integer), ordering not predictable",
|
||||||
2, bean.getAmbigious());
|
2, bean.getAmbiguous());
|
||||||
assertSame("Integer converted rather than invoking setAmbigiouos(Integer), ordering not predictable",
|
assertSame("Integer converted rather than invoking setAmbiguous(Integer), ordering not predictable",
|
||||||
value, bean.getAmbigious());
|
value, bean.getAmbiguous());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testConfigureAmbigious3() {
|
public void testConfigureAmbiguous3() {
|
||||||
TestBean bean = new TestBean();
|
TestBean bean = new TestBean();
|
||||||
|
|
||||||
Map<String, Double> map = new HashMap<String, Double>();
|
Map<String, Double> map = new HashMap<String, Double>();
|
||||||
|
|
||||||
Double value = .3;
|
Double value = .3;
|
||||||
map.put("ambigious", value);
|
map.put("ambiguous", value);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
BeanUtil.configure(bean, map);
|
BeanUtil.configure(bean, map);
|
||||||
@ -167,11 +167,11 @@ public class BeanUtilTestCase extends TestCase {
|
|||||||
fail(e.getMessage());
|
fail(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
assertNotNull(bean.getAmbigious());
|
assertNotNull(bean.getAmbiguous());
|
||||||
assertEquals("Object converted rather than invoking setAmbigious(Object), ordering not predictable",
|
assertEquals("Object converted rather than invoking setAmbiguous(Object), ordering not predictable",
|
||||||
value.getClass(), bean.getAmbigious().getClass());
|
value.getClass(), bean.getAmbiguous().getClass());
|
||||||
assertSame("Object converted rather than invoking setAmbigious(Object), ordering not predictable",
|
assertSame("Object converted rather than invoking setAmbiguous(Object), ordering not predictable",
|
||||||
value, bean.getAmbigious());
|
value, bean.getAmbiguous());
|
||||||
}
|
}
|
||||||
|
|
||||||
static class TestBean {
|
static class TestBean {
|
||||||
@ -179,7 +179,7 @@ public class BeanUtilTestCase extends TestCase {
|
|||||||
private int intVal;
|
private int intVal;
|
||||||
private Double doubleVal;
|
private Double doubleVal;
|
||||||
|
|
||||||
private Object ambigious;
|
private Object ambiguous;
|
||||||
|
|
||||||
public Double getDoubleValue() {
|
public Double getDoubleValue() {
|
||||||
return doubleVal;
|
return doubleVal;
|
||||||
@ -193,36 +193,43 @@ public class BeanUtilTestCase extends TestCase {
|
|||||||
return stringVal;
|
return stringVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("UnusedDeclaration")
|
||||||
public void setStringValue(String pString) {
|
public void setStringValue(String pString) {
|
||||||
stringVal = pString;
|
stringVal = pString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("UnusedDeclaration")
|
||||||
public void setIntValue(int pInt) {
|
public void setIntValue(int pInt) {
|
||||||
intVal = pInt;
|
intVal = pInt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("UnusedDeclaration")
|
||||||
public void setDoubleValue(Double pDouble) {
|
public void setDoubleValue(Double pDouble) {
|
||||||
doubleVal = pDouble;
|
doubleVal = pDouble;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAmbigious(String pString) {
|
@SuppressWarnings("UnusedDeclaration")
|
||||||
ambigious = pString;
|
public void setAmbiguous(String pString) {
|
||||||
|
ambiguous = pString;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAmbigious(Object pObject) {
|
@SuppressWarnings("UnusedDeclaration")
|
||||||
ambigious = pObject;
|
public void setAmbiguous(Object pObject) {
|
||||||
|
ambiguous = pObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAmbigious(Integer pInteger) {
|
@SuppressWarnings("UnusedDeclaration")
|
||||||
ambigious = pInteger;
|
public void setAmbiguous(Integer pInteger) {
|
||||||
|
ambiguous = pInteger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAmbigious(int pInt) {
|
@SuppressWarnings("UnusedDeclaration")
|
||||||
ambigious = (long) pInt; // Just to differentiate...
|
public void setAmbiguous(int pInt) {
|
||||||
|
ambiguous = (long) pInt; // Just to differentiate...
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getAmbigious() {
|
public Object getAmbiguous() {
|
||||||
return ambigious;
|
return ambiguous;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
package com.twelvemonkeys.util.convert;
|
package com.twelvemonkeys.util.convert;
|
||||||
|
|
||||||
import com.twelvemonkeys.lang.Validate;
|
import com.twelvemonkeys.lang.Validate;
|
||||||
|
import org.junit.Ignore;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DefaultConverterTestCase
|
* DefaultConverterTestCase
|
||||||
* <p/>
|
* <p/>
|
||||||
@ -56,7 +60,61 @@ public class DefaultConverterTestCase extends PropertyConverterAbstractTestCase
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Test boolean -> Boolean conversion
|
@Test
|
||||||
|
public void testConvertBooleanPrimitive() {
|
||||||
|
PropertyConverter converter = makePropertyConverter();
|
||||||
|
assertTrue((Boolean) converter.toObject("true", boolean.class, null));
|
||||||
|
assertFalse((Boolean) converter.toObject("FalsE", Boolean.TYPE, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConvertShortPrimitive() {
|
||||||
|
PropertyConverter converter = makePropertyConverter();
|
||||||
|
assertEquals(1, (short) (Short) converter.toObject("1", short.class, null));
|
||||||
|
assertEquals(-2, (short) (Short) converter.toObject("-2", Short.TYPE, null));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testConvertIntPrimitive() {
|
||||||
|
PropertyConverter converter = makePropertyConverter();
|
||||||
|
assertEquals(1, (int) (Integer) converter.toObject("1", int.class, null));
|
||||||
|
assertEquals(-2, (int) (Integer) converter.toObject("-2", Integer.TYPE, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConvertLongPrimitive() {
|
||||||
|
PropertyConverter converter = makePropertyConverter();
|
||||||
|
assertEquals(Long.MAX_VALUE, (long) (Long) converter.toObject("9223372036854775807", long.class, null));
|
||||||
|
assertEquals(-2, (long) (Long) converter.toObject("-2", Long.TYPE, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConvertBytePrimitive() {
|
||||||
|
PropertyConverter converter = makePropertyConverter();
|
||||||
|
assertEquals(1, (byte) (Byte) converter.toObject("1", byte.class, null));
|
||||||
|
assertEquals(-2, (byte) (Byte) converter.toObject("-2", Byte.TYPE, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConvertFloatPrimitive() {
|
||||||
|
PropertyConverter converter = makePropertyConverter();
|
||||||
|
assertEquals(1f, (Float) converter.toObject("1.0", float.class, null), 0);
|
||||||
|
assertEquals(-2.3456f, (Float) converter.toObject("-2.3456", Float.TYPE, null), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConvertDoublePrimitive() {
|
||||||
|
PropertyConverter converter = makePropertyConverter();
|
||||||
|
assertEquals(1d, (Double) converter.toObject("1.0", double.class, null), 0);
|
||||||
|
assertEquals(-2.3456, (Double) converter.toObject("-2.3456", Double.TYPE, null), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Ignore("Known issue. Why would anyone do something like this?")
|
||||||
|
@Test
|
||||||
|
public void testConvertCharPrimitive() {
|
||||||
|
PropertyConverter converter = makePropertyConverter();
|
||||||
|
assertEquals('A', (char) (Character) converter.toObject("A", char.class, null));
|
||||||
|
assertEquals('Z', (char) (Character) converter.toObject("Z", Character.TYPE, null));
|
||||||
|
}
|
||||||
|
|
||||||
public static class FooBar {
|
public static class FooBar {
|
||||||
private final String bar;
|
private final String bar;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user