scolebourne 2004/03/06 17:16:29
Modified: convert/src/java/org/apache/commons/convert2
ConvertUtils.java ConversionRegistry.java
Converter.java
Added: convert/src/java/org/apache/commons/convert2 Conversion.java
ConversionFactory.java
convert/src/java/org/apache/commons/convert2/conversion
ObjectToStringConversionFactory.java
ClassToStringConversion.java
TimeZoneToStringConversionFactory.java
convert/src/test/org/apache/commons/convert2
PackageTestSuite.java TestConvertUtils.java
ProjectConvertTestSuite.java
TestDefaultConversions.java
convert/src/test/org/apache/commons/convert2/conversion
TestObjectToStringConversionFactory.java
TestTimeZoneToStringConversionFactory.java
PackageTestSuite.java
Removed: convert/src/java/org/apache/commons/convert2/conversion
ToStringConversion.java ToIntConversion.java
ToBooleanConversion.java ConversionFactory.java
Conversion.java
convert/src/java/org/apache/commons/convert2/number
IntegerToStringConversion.java
convert/src/test/org/apache/commons/convert2
ConvertUtilsTestCase.java
Log:
Update conversion code to add basic implementations
Revision Changes Path
1.3 +2 -8
jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/ConvertUtils.java
Index: ConvertUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/ConvertUtils.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ConvertUtils.java 6 Mar 2004 01:20:41 -0000 1.2
+++ ConvertUtils.java 7 Mar 2004 01:16:28 -0000 1.3
@@ -15,8 +15,6 @@
*/
package org.apache.commons.convert2;
-import org.apache.commons.convert2.number.IntegerToStringConversion;
-
/**
* Simple public API for the conversion system consisting of static methods.
* <p>
@@ -32,12 +30,8 @@
public class ConvertUtils {
/** The default Converter, private to prevent subclasses altering the default */
- private static final Converter DEFAULT = new Converter();
+ private static final Converter DEFAULT = new Converter(true);
- static {
- DEFAULT.getRegistry().addConversion(new IntegerToStringConversion());
- }
-
/**
* Restricted constructor.
* The main access to this class is via static methods, and it is not intended
1.2 +105 -20
jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/ConversionRegistry.java
Index: ConversionRegistry.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/ConversionRegistry.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ConversionRegistry.java 6 Mar 2004 01:20:41 -0000 1.1
+++ ConversionRegistry.java 7 Mar 2004 01:16:28 -0000 1.2
@@ -18,20 +18,35 @@
import java.util.HashMap;
import java.util.Map;
-import org.apache.commons.convert2.conversion.Conversion;
+import org.apache.commons.convert2.conversion.ClassToStringConversion;
+import org.apache.commons.convert2.conversion.ObjectToStringConversionFactory;
+import org.apache.commons.convert2.conversion.TimeZoneToStringConversionFactory;
/**
- * ConversionRegistry allows the addition and removal of converters.
+ * ConversionRegistry manages the <code>Conversion</code> and
+ * <code>ConversionFactory</code> objects.
+ * <p>
+ * This class allows conversions to be added, removed and looked up.
+ * This implementation is fully synchronized.
+ * <pre>
+ * Converter converter = new Converter();
+ * converter.getRegistry().addDefaultConversions();
+ * converter.getRegistry().addConversion(new MyNewConversion());
+ * </pre>
*
* @author Stephen Colebourne
* @version $Id$
* @since 1.0
*/
public class ConversionRegistry {
-
+
/** Map of from class to conversion */
- private Map iConversions = new HashMap();
-
+ protected Map iConversions = new HashMap();
+ /** Array of conversion factories */
+ protected ConversionFactory[] iFactories = new ConversionFactory[0];
+ /** Object to synchronize on for factories */
+ protected final Object iFactoryLock = new Object();
+
/**
* Restricted constructor, use Converter.
*/
@@ -41,39 +56,109 @@
//-----------------------------------------------------------------------
/**
+ * Add the default set of conversions to the registry.
+ */
+ public void addDefaultConversions() {
+ synchronized (iConversions) {
+ addConversion(ClassToStringConversion.INSTANCE);
+ }
+ synchronized (iFactoryLock) {
+ addConversionFactory(ObjectToStringConversionFactory.INSTANCE);
+ addConversionFactory(TimeZoneToStringConversionFactory.INSTANCE);
+ }
+ }
+
+ //-----------------------------------------------------------------------
+ /**
* Adds a Conversion to the map of known conversions.
* Any previous conversion for this from-to pair is replaced.
*
* @param conv the conversion to add
*/
public void addConversion(Conversion conv) {
- Map map = (Map) iConversions.get(conv.getFromType());
- if (map == null) {
- map = new HashMap();
- iConversions.put(conv.getFromType(), map);
+ if (conv != null) {
+ synchronized (iConversions) {
+ Map map = (Map) iConversions.get(conv.getFromType());
+ if (map == null) {
+ map = new HashMap();
+ iConversions.put(conv.getFromType(), map);
+ }
+ map.put(conv.getToType(), conv);
+ }
}
- map.put(conv.getToType(), conv);
}
-
+
+ /**
+ * Adds a ConversionFactory to the set of known factories.
+ * Any previous factory that matches by <code>equals()</code> is replaced.
+ *
+ * @param factory the factory to add
+ */
+ public void addConversionFactory(ConversionFactory factory) {
+ if (factory != null) {
+ synchronized (iFactoryLock) {
+ ConversionFactory[] oldFactories = iFactories;
+ for (int i = 0; i < oldFactories.length; i++) {
+ if (oldFactories[i].equals(factory)) {
+ iFactories[i] = factory;
+ return;
+ }
+ }
+ ConversionFactory[] newFactories = new
ConversionFactory[oldFactories.length + 1];
+ System.arraycopy(oldFactories, 0, newFactories, 0,
oldFactories.length);
+ newFactories[oldFactories.length] = factory;
+ iFactories = newFactories;
+ }
+ }
+ }
+
+ //-----------------------------------------------------------------------
/**
* Gets the conversion object that best matches the from and to types.
+ * <p>
+ * The lookup first examines the known conversions. If none is found, the
+ * factories are used to search for and create a conversion. If no suitable
+ * factory is found then <code>null</code> is returned.
*
* @param fromType the type to convert from
* @param toType the type to convert to
* @return the best matching conversion, null if no match
*/
- public Conversion getConversion(Class fromType, Class toType) {
- Map map = (Map) iConversions.get(fromType);
- if (map != null) {
- Conversion conv = (Conversion) map.get(toType);
- if (conv != null) {
- return conv;
+ public Conversion getConversion(Object value, Class fromType, Class toType) {
+ // try known conversions
+ synchronized (iConversions) {
+ Map map = (Map) iConversions.get(fromType);
+ if (map != null) {
+ Conversion conv = (Conversion) map.get(toType);
+ if (conv != null) {
+ return conv;
+ }
}
}
- // TODO: Add extended searching here
+
+ // try factories
+ int max = 0;
+ ConversionFactory maxFactory = null;
+ synchronized (iFactoryLock) {
+ ConversionFactory[] factories = iFactories;
+ for (int i = 0; i < factories.length; i++) {
+ int match = factories[i].getMatchPercent(value, fromType, toType);
+ if (match > max) {
+ max = match;
+ maxFactory = factories[i];
+ }
+ }
+ }
+ if (maxFactory != null) {
+ Conversion conv = maxFactory.getInstance(value, fromType, toType);
+ addConversion(conv);
+ return conv;
+ }
+
+ // no match
return null;
}
-
+
//-----------------------------------------------------------------------
/**
* Returns a string describing this object.
1.2 +18 -5
jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/Converter.java
Index: Converter.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/Converter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Converter.java 6 Mar 2004 01:20:41 -0000 1.1
+++ Converter.java 7 Mar 2004 01:16:28 -0000 1.2
@@ -15,8 +15,6 @@
*/
package org.apache.commons.convert2;
-import org.apache.commons.convert2.conversion.Conversion;
-
/**
* Converter is the central class that holds a set of registered converters
* together and allows conversion to occur.
@@ -40,10 +38,25 @@
/**
* Constructs a new instance of the Converter useful to create a set
* of conversions separate from the default set.
+ * <p>
+ * The created converter has no conversions registered.
*/
public Converter() {
+ this(false);
+ }
+
+ /**
+ * Constructs a new instance of the Converter useful to create a set
+ * of conversions separate from the default set.
+ *
+ * @param addDefaults whether to add the default conversions
+ */
+ public Converter(boolean addDefaults) {
super();
registry = createRegistry();
+ if (addDefaults) {
+ registry.addDefaultConversions();
+ }
}
/**
@@ -67,11 +80,11 @@
*/
public Object convert(Object value, Class fromClass, Class toClass) {
Class valueClass = (value == null ? fromClass : value.getClass());
- Conversion conv = getRegistry().getConversion(valueClass, toClass);
+ Conversion conv = getRegistry().getConversion(value, valueClass, toClass);
if (conv == null) {
throw new ConversionException("No Converter found to convert " +
valueClass + " to " +toClass);
}
- return conv.convert(value);
+ return conv.convert(value, this);
}
/**
1.1
jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/Conversion.java
Index: Conversion.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.commons.convert2;
/**
* Defines a Converter that will convert an object to an object of another type.
* <p>
* All configuration data for the conversion must be set on the converter
* separately.
*
* @author Stephen Colebourne
* @version $Id: Conversion.java,v 1.1 2004/03/07 01:16:28 scolebourne Exp $
* @since 1.0
*/
public interface Conversion {
/**
* Convert the specified input object into an output object
* of the another type.
*
* @param value the value to be converted, read only, may be null
* @param converter the converter being used, not null
* @throws ConversionException (runtime) if conversion fails
*/
public Object convert(Object value, Converter converter);
/**
* The type to convert from.
*
* @return the Class object representing the class to convert to
*/
public Class getFromType();
/**
* The type to convert to.
*
* @return the Class object representing the class to convert from
*/
public Class getToType();
}
1.1
jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/ConversionFactory.java
Index: ConversionFactory.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.commons.convert2;
/**
* Defines a factory for Conversion objects that will create a conversion on demand
* or return a singleton.
*
* @author Stephen Colebourne
* @version $Id: ConversionFactory.java,v 1.1 2004/03/07 01:16:28 scolebourne Exp $
* @since 1.0
*/
public interface ConversionFactory {
/**
* Checks if this factory supports the required conversion returning a
* percentage match.
* <p>
* The returned <code>int</code> represents the percentage by which this
* factory matches the required conversion. The percentage (ie 0-100) is used
* to determine which conversion factory to use when a conflict arises.
*
* @param value the value to be converted, read only, may be null
* @param fromType the type to convert from, may be null
* @param toType th type to convert to, may be null
* @return a value between 0 and 100 inclusive, 0 means no match, 100 perfact
match
*/
public int getMatchPercent(Object value, Class fromType, Class toType);
/**
* Create a conversion object for the conversion.
* <p>
* The returned conversion must not store the value. The conversion object
* will be used repeatedly for all future conversions between these two types
* without further reference to the factory.
*
* @param value the value to be converted, read only, may be null
* @param fromType the type to convert from, may be null
* @param toType th type to convert to, may be null
* @return a Conversion object for repeatedly performing conversions
*/
public Conversion getInstance(Object value, Class fromType, Class toType);
}
1.1
jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/ObjectToStringConversionFactory.java
Index: ObjectToStringConversionFactory.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.commons.convert2.conversion;
import org.apache.commons.convert2.Conversion;
import org.apache.commons.convert2.ConversionFactory;
import org.apache.commons.convert2.Converter;
/**
* Conversion from Object to String using <code>toString()</code>.
*
* @author Stephen Colebourne
* @version $Id: ObjectToStringConversionFactory.java,v 1.1 2004/03/07 01:16:28
scolebourne Exp $
* @since 1.0
*/
public class ObjectToStringConversionFactory implements ConversionFactory {
/** Singleton instance of this factory */
public static final ConversionFactory INSTANCE = new
ObjectToStringConversionFactory();
/**
* Restricted constructor.
*/
private ObjectToStringConversionFactory() {
super();
}
//-----------------------------------------------------------------------
/**
* Checks if this factory supports the required conversion returning a
* percentage match.
* <p>
* This implementation returns <code>20</code> if the <code>toType</code> is
* <code>String</code>.
*
* @param value the value to be converted, read only, may be null
* @param fromType the type to convert from, may be null
* @param toType th type to convert to, may be null
* @return 20 if toType is String
*/
public int getMatchPercent(Object value, Class fromType, Class toType) {
if (toType == String.class) {
return 20;
}
return 0;
}
/**
* Create a conversion object for the conversion.
* <p>
* The returned conversion must not store the value. The conversion object
* will be used repeatedly for all future conversions between these two types
* without further reference to the factory.
*
* @param value the value to be converted, read only, may be null
* @param fromType the type to convert from, may be null
* @param toType th type to convert to, may be null
* @return a Conversion object for repeatedly performing conversions
*/
public Conversion getInstance(Object value, Class fromType, Class toType) {
return new ObjectToStringConversion(fromType);
}
//-----------------------------------------------------------------------
/**
* Conversion implementation for Number to String converions.
*/
class ObjectToStringConversion implements Conversion {
/** The type to convert from */
private final Class fromType;
/**
* Constructs a Conversion for a specific Number subclass.
*
* @param fromType the type to convert from
*/
ObjectToStringConversion(Class fromType) {
super();
this.fromType = fromType;
}
/**
* Convert the specified input object into an output object
* of the another type.
*
* @param value the input value to be converted, may be null
* @param converter the converter being used, not null
* @throws ConversionException (runtime) if conversion fails
*/
public Object convert(Object value, Converter converter) {
if (value == null) {
return null;
}
return value.toString();
}
/**
* The type to convert from.
*
* @return the Class object representing the class to convert to
*/
public Class getFromType() {
return fromType;
}
/**
* The type to convert to.
*
* @return the Class object representing the class to convert from
*/
public Class getToType() {
return String.class;
}
}
}
1.1
jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/ClassToStringConversion.java
Index: ClassToStringConversion.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.commons.convert2.conversion;
import org.apache.commons.convert2.Conversion;
import org.apache.commons.convert2.Converter;
/**
* Conversion from Class to String using <code>getName()</code>.
*
* @author Stephen Colebourne
* @version $Id: ClassToStringConversion.java,v 1.1 2004/03/07 01:16:28 scolebourne
Exp $
* @since 1.0
*/
public class ClassToStringConversion implements Conversion {
/** Singleton instance of this factory */
public static final Conversion INSTANCE = new ClassToStringConversion();
/**
* Constructor, setting <code>null</code> as the default value.
*/
public ClassToStringConversion() {
super();
}
//-----------------------------------------------------------------------
/**
* Convert from <code>Class</code> to <code>String</code> using
<code>getName()</code>.
*
* @param value the input value to be converted, may be null
* @param converter the converter being used, not null
* @throws ConversionException (runtime) if conversion fails
*/
public Object convert(Object value, Converter converter) {
if (value == null) {
return null;
}
Class cls = (Class) value;
return cls.getName();
}
/**
* The type to convert from.
*
* @return the Class object representing the class to convert to
*/
public Class getFromType() {
return Class.class;
}
/**
* The type to convert to.
*
* @return the Class object representing the class to convert from
*/
public Class getToType() {
return String.class;
}
}
1.1
jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/TimeZoneToStringConversionFactory.java
Index: TimeZoneToStringConversionFactory.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.commons.convert2.conversion;
import java.util.TimeZone;
import org.apache.commons.convert2.Conversion;
import org.apache.commons.convert2.ConversionFactory;
import org.apache.commons.convert2.Converter;
/**
* Conversion from TimeZone to String using <code>getID()</code>.
*
* @author Stephen Colebourne
* @version $Id: TimeZoneToStringConversionFactory.java,v 1.1 2004/03/07 01:16:28
scolebourne Exp $
* @since 1.0
*/
public class TimeZoneToStringConversionFactory implements ConversionFactory {
/** Singleton instance of this factory */
public static final ConversionFactory INSTANCE = new
TimeZoneToStringConversionFactory();
/**
* Restricted constructor.
*/
private TimeZoneToStringConversionFactory() {
super();
}
//-----------------------------------------------------------------------
/**
* Checks if this factory supports the required conversion returning a
* percentage match.
* <p>
* This implementation returns <code>60</code> if the
* <code>fromType</code> is TimeZone or subclass and the
* <code>toType</code> is <code>String</code>.
*
* @param value the value to be converted, read only, may be null
* @param fromType the type to convert from, may be null
* @param toType th type to convert to, may be null
* @return 60 if fromType is TimeZone or subclass and toType is String
*/
public int getMatchPercent(Object value, Class fromType, Class toType) {
if (toType == String.class) {
if (value != null && value instanceof TimeZone) {
return 60;
} else if (TimeZone.class.isAssignableFrom(fromType)) {
return 60;
}
}
return 0;
}
/**
* Create a conversion object for the conversion.
* <p>
* The returned conversion must not store the value. The conversion object
* will be used repeatedly for all future conversions between these two types
* without further reference to the factory.
*
* @param value the value to be converted, read only, may be null
* @param fromType the type to convert from, may be null
* @param toType th type to convert to, may be null
* @return a Conversion object for repeatedly performing conversions
*/
public Conversion getInstance(Object value, Class fromType, Class toType) {
return new TimeZoneToStringConversion(fromType);
}
//-----------------------------------------------------------------------
/**
* Conversion implementation for Number to String converions.
*/
class TimeZoneToStringConversion implements Conversion {
/** The type to convert from */
private final Class fromType;
/**
* Constructs a Conversion for a specific Number subclass.
*
* @param fromType the type to convert from
*/
TimeZoneToStringConversion(Class fromType) {
super();
this.fromType = fromType;
}
/**
* Convert the specified input object into an output object
* of the another type.
*
* @param value the input value to be converted, may be null
* @param converter the converter being used, not null
* @throws ConversionException (runtime) if conversion fails
*/
public Object convert(Object value, Converter converter) {
if (value == null) {
return null;
}
TimeZone zone = (TimeZone) value;
return zone.getID();
}
/**
* The type to convert from.
*
* @return the Class object representing the class to convert to
*/
public Class getFromType() {
return fromType;
}
/**
* The type to convert to.
*
* @return the Class object representing the class to convert from
*/
public Class getToType() {
return String.class;
}
}
}
1.1
jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/PackageTestSuite.java
Index: PackageTestSuite.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.commons.convert2;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* Test suite.
*
* @author Stephen Colebourne
*/
public class PackageTestSuite extends TestCase {
public PackageTestSuite(String name) {
super(name);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
TestSuite suite = new TestSuite("Main package");
suite.addTest(TestConvertUtils.suite());
suite.addTest(TestDefaultConversions.suite());
return suite;
}
}
1.1
jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/TestConvertUtils.java
Index: TestConvertUtils.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.commons.convert2;
import java.util.TimeZone;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* Test case.
*
* @author Stephen Colebourne
*/
public class TestConvertUtils extends TestCase {
public TestConvertUtils(String name) {
super(name);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return (new TestSuite(TestConvertUtils.class, "ConvertUtils"));
}
//-----------------------------------------------------------------------
public void testConvertToString() {
assertEquals("true", ConvertUtils.convertToString(Boolean.TRUE));
assertEquals("A", ConvertUtils.convertToString(new Character('A')));
assertEquals("6", ConvertUtils.convertToString(new Byte((byte)6)));
assertEquals("6", ConvertUtils.convertToString(new Short((short)6)));
assertEquals("6", ConvertUtils.convertToString(new Integer(6)));
assertEquals("6", ConvertUtils.convertToString(new Long(6)));
assertEquals("6.0", ConvertUtils.convertToString(new Double(6)));
assertEquals("6.0", ConvertUtils.convertToString(new Float(6)));
assertEquals("Europe/Paris",
ConvertUtils.convertToString(TimeZone.getTimeZone("Europe/Paris")));
}
}
1.1
jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/ProjectConvertTestSuite.java
Index: ProjectConvertTestSuite.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.commons.convert2;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* Test suite.
*
* @author Stephen Colebourne
*/
public class ProjectConvertTestSuite extends TestCase {
public ProjectConvertTestSuite(String name) {
super(name);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
TestSuite suite = new TestSuite("Convert");
suite.addTest(org.apache.commons.convert2.PackageTestSuite.suite());
suite.addTest(org.apache.commons.convert2.conversion.PackageTestSuite.suite());
return suite;
}
}
1.1
jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/TestDefaultConversions.java
Index: TestDefaultConversions.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.commons.convert2;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* Test case.
*
* @author Stephen Colebourne
*/
public class TestDefaultConversions extends TestCase {
public TestDefaultConversions(String name) {
super(name);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return (new TestSuite(TestDefaultConversions.class, "DefaultConversions"));
}
//-----------------------------------------------------------------------
public void testCoversionBooleanToString() {
assertEquals("true", ConvertUtils.convertToString(Boolean.TRUE));
assertEquals("false", ConvertUtils.convertToString(Boolean.FALSE));
assertEquals(null, ConvertUtils.convertToString(null, Boolean.class));
}
public void testCoversionCharacterToString() {
assertEquals("A", ConvertUtils.convertToString(new Character('A')));
assertEquals("a", ConvertUtils.convertToString(new Character('a')));
assertEquals(null, ConvertUtils.convertToString(null, Character.class));
}
public void testCoversionByteToString() {
assertEquals("0", ConvertUtils.convertToString(new Byte((byte) 0)));
assertEquals("20", ConvertUtils.convertToString(new Byte((byte) 20)));
assertEquals(null, ConvertUtils.convertToString(null, Byte.class));
}
public void testCoversionShortToString() {
assertEquals("0", ConvertUtils.convertToString(new Short((short) 0)));
assertEquals("20", ConvertUtils.convertToString(new Short((short) 20)));
assertEquals(null, ConvertUtils.convertToString(null, Short.class));
}
public void testCoversionIntegerToString() {
assertEquals("0", ConvertUtils.convertToString(new Integer(0)));
assertEquals("20", ConvertUtils.convertToString(new Integer(20)));
assertEquals(null, ConvertUtils.convertToString(null, Integer.class));
}
public void testCoversionLongToString() {
assertEquals("0", ConvertUtils.convertToString(new Long(0)));
assertEquals("20", ConvertUtils.convertToString(new Long(20)));
assertEquals(null, ConvertUtils.convertToString(null, Long.class));
}
public void testCoversionFloatToString() {
assertEquals("0.0", ConvertUtils.convertToString(new Float(0)));
assertEquals("20.0", ConvertUtils.convertToString(new Float(20)));
assertEquals(null, ConvertUtils.convertToString(null, Float.class));
}
public void testCoversionDoubleToString() {
assertEquals("0.0", ConvertUtils.convertToString(new Double(0)));
assertEquals("20.0", ConvertUtils.convertToString(new Double(20)));
assertEquals(null, ConvertUtils.convertToString(null, Double.class));
}
public void testCoversionListToString() {
assertEquals("[]", ConvertUtils.convertToString(new ArrayList()));
assertEquals("[a, b, c]", ConvertUtils.convertToString(new
ArrayList(Arrays.asList(new Object[] {"a", "b", "c"}))));
assertEquals(null, ConvertUtils.convertToString(null, List.class));
}
public void testCoversionMapToString() {
assertEquals("{}", ConvertUtils.convertToString(new HashMap()));
Map map = new HashMap();
map.put("a", "A");
map.put("b", "B");
map.put("c", "C");
assertEquals(map.toString(), ConvertUtils.convertToString(map));
assertEquals(null, ConvertUtils.convertToString(null, List.class));
}
public void testCoversionClassToString() {
assertEquals("java.lang.String", ConvertUtils.convertToString(String.class));
assertEquals("java.util.Map", ConvertUtils.convertToString(Map.class));
assertEquals(null, ConvertUtils.convertToString(null, Class.class));
}
public void testCoversionStringToString() {
assertEquals("0", ConvertUtils.convertToString("0"));
assertEquals("20", ConvertUtils.convertToString("20"));
assertEquals(null, ConvertUtils.convertToString(null, String.class));
}
}
1.1
jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/conversion/TestObjectToStringConversionFactory.java
Index: TestObjectToStringConversionFactory.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.commons.convert2.conversion;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.apache.commons.convert2.Conversion;
import org.apache.commons.convert2.ConversionFactory;
import org.apache.commons.convert2.Converter;
/**
* Test class.
*
* @author Stephen Colebourne
*/
public class TestObjectToStringConversionFactory extends TestCase {
public TestObjectToStringConversionFactory(String name) {
super(name);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return (new TestSuite(TestObjectToStringConversionFactory.class,
"ObjectToString"));
}
//-----------------------------------------------------------------------
public void testClass() {
Constructor[] con =
ObjectToStringConversionFactory.class.getDeclaredConstructors();
assertEquals(1, con.length);
assertEquals(true, Modifier.isPrivate(con[0].getModifiers()));
assertTrue(ConversionFactory.class.isAssignableFrom(ObjectToStringConversionFactory.class));
}
public void testGetMatch() {
assertEquals(0,
ObjectToStringConversionFactory.INSTANCE.getMatchPercent(null, null, null));
assertEquals(0,
ObjectToStringConversionFactory.INSTANCE.getMatchPercent(null, Object.class, null));
assertEquals(0,
ObjectToStringConversionFactory.INSTANCE.getMatchPercent(null, Object.class,
Object.class));
assertEquals(0,
ObjectToStringConversionFactory.INSTANCE.getMatchPercent(null, Object.class,
Integer.class));
assertEquals(20,
ObjectToStringConversionFactory.INSTANCE.getMatchPercent(null, Object.class,
String.class));
assertEquals(20,
ObjectToStringConversionFactory.INSTANCE.getMatchPercent(null, String.class,
String.class));
assertEquals(20,
ObjectToStringConversionFactory.INSTANCE.getMatchPercent(null, Integer.class,
String.class));
}
//-----------------------------------------------------------------------
public void testCoversionFromBoolean() {
Converter converter = new Converter();
Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null,
Boolean.class, String.class);
assertEquals(Boolean.class, conv.getFromType());
assertEquals(String.class, conv.getToType());
assertEquals("true", conv.convert(Boolean.TRUE, converter));
assertEquals("false", conv.convert(Boolean.FALSE, converter));
assertEquals(null, conv.convert(null, converter));
}
public void testCoversionFromCharacter() {
Converter converter = new Converter();
Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null,
Character.class, String.class);
assertEquals(Character.class, conv.getFromType());
assertEquals(String.class, conv.getToType());
assertEquals("A", conv.convert(new Character('A'), converter));
assertEquals("a", conv.convert(new Character('a'), converter));
assertEquals(null, conv.convert(null, converter));
}
public void testCoversionFromByte() {
Converter converter = new Converter();
Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null,
Byte.class, String.class);
assertEquals(Byte.class, conv.getFromType());
assertEquals(String.class, conv.getToType());
assertEquals("0", conv.convert(new Byte((byte) 0), converter));
assertEquals("20", conv.convert(new Byte((byte) 20), converter));
assertEquals(null, conv.convert(null, converter));
}
public void testCoversionFromShort() {
Converter converter = new Converter();
Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null,
Short.class, String.class);
assertEquals(Short.class, conv.getFromType());
assertEquals(String.class, conv.getToType());
assertEquals("0", conv.convert(new Short((short) 0), converter));
assertEquals("20", conv.convert(new Short((short) 20), converter));
assertEquals(null, conv.convert(null, converter));
}
public void testCoversionFromInteger() {
Converter converter = new Converter();
Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null,
Integer.class, String.class);
assertEquals(Integer.class, conv.getFromType());
assertEquals(String.class, conv.getToType());
assertEquals("0", conv.convert(new Integer(0), converter));
assertEquals("20", conv.convert(new Integer(20), converter));
assertEquals(null, conv.convert(null, converter));
}
public void testCoversionFromLong() {
Converter converter = new Converter();
Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null,
Long.class, String.class);
assertEquals(Long.class, conv.getFromType());
assertEquals(String.class, conv.getToType());
assertEquals("0", conv.convert(new Long(0), converter));
assertEquals("20", conv.convert(new Long(20), converter));
assertEquals(null, conv.convert(null, converter));
}
public void testCoversionFromList() {
Converter converter = new Converter();
Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null,
List.class, String.class);
assertEquals(List.class, conv.getFromType());
assertEquals(String.class, conv.getToType());
assertEquals("[]", conv.convert(new ArrayList(), converter));
assertEquals("[a, b, c]", conv.convert(new ArrayList(Arrays.asList(new
Object[] {"a", "b", "c"})), converter));
assertEquals(null, conv.convert(null, converter));
}
public void testCoversionFromString() {
Converter converter = new Converter();
Conversion conv = ObjectToStringConversionFactory.INSTANCE.getInstance(null,
String.class, String.class);
assertEquals(String.class, conv.getFromType());
assertEquals(String.class, conv.getToType());
assertEquals("0", conv.convert("0", converter));
assertEquals("20", conv.convert("20", converter));
assertEquals(null, conv.convert(null, converter));
}
}
1.1
jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/conversion/TestTimeZoneToStringConversionFactory.java
Index: TestTimeZoneToStringConversionFactory.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.commons.convert2.conversion;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.apache.commons.convert2.Conversion;
import org.apache.commons.convert2.ConversionFactory;
import org.apache.commons.convert2.Converter;
/**
* Test class.
*
* @author Stephen Colebourne
*/
public class TestTimeZoneToStringConversionFactory extends TestCase {
public TestTimeZoneToStringConversionFactory(String name) {
super(name);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return (new TestSuite(TestTimeZoneToStringConversionFactory.class,
"TimeZoneToString"));
}
//-----------------------------------------------------------------------
public void testClass() {
Constructor[] con =
TimeZoneToStringConversionFactory.class.getDeclaredConstructors();
assertEquals(1, con.length);
assertEquals(true, Modifier.isPrivate(con[0].getModifiers()));
assertTrue(ConversionFactory.class.isAssignableFrom(TimeZoneToStringConversionFactory.class));
}
public void testGetMatch() {
assertEquals(0,
TimeZoneToStringConversionFactory.INSTANCE.getMatchPercent(null, null, null));
assertEquals(0,
TimeZoneToStringConversionFactory.INSTANCE.getMatchPercent(null, TimeZone.class,
null));
assertEquals(0,
TimeZoneToStringConversionFactory.INSTANCE.getMatchPercent(null, TimeZone.class,
Object.class));
assertEquals(0,
TimeZoneToStringConversionFactory.INSTANCE.getMatchPercent(null, TimeZone.class,
Integer.class));
assertEquals(0,
TimeZoneToStringConversionFactory.INSTANCE.getMatchPercent(null, Object.class,
String.class));
assertEquals(0,
TimeZoneToStringConversionFactory.INSTANCE.getMatchPercent(null, Integer.class,
String.class));
assertEquals(60,
TimeZoneToStringConversionFactory.INSTANCE.getMatchPercent(null, TimeZone.class,
String.class));
assertEquals(60,
TimeZoneToStringConversionFactory.INSTANCE.getMatchPercent(null, SimpleTimeZone.class,
String.class));
}
public void testCoversionFromSimpleTimeZone() {
Converter converter = new Converter();
Conversion conv =
TimeZoneToStringConversionFactory.INSTANCE.getInstance(null, SimpleTimeZone.class,
String.class);
assertEquals(SimpleTimeZone.class, conv.getFromType());
assertEquals(String.class, conv.getToType());
assertEquals("GMT", conv.convert(new SimpleTimeZone(0, "GMT"), converter));
assertEquals("GMT+1", conv.convert(new SimpleTimeZone(0, "GMT+1"),
converter));
assertEquals(null, conv.convert(null, converter));
}
public void testCoversionFromTimeZone() {
Converter converter = new Converter();
Conversion conv =
TimeZoneToStringConversionFactory.INSTANCE.getInstance(null, TimeZone.class,
String.class);
assertEquals(TimeZone.class, conv.getFromType());
assertEquals(String.class, conv.getToType());
assertEquals("GMT", conv.convert(TimeZone.getTimeZone("GMT"), converter));
assertEquals("Europe/Paris",
conv.convert(TimeZone.getTimeZone("Europe/Paris"), converter));
assertEquals(null, conv.convert(null, converter));
}
}
1.1
jakarta-commons-sandbox/convert/src/test/org/apache/commons/convert2/conversion/PackageTestSuite.java
Index: PackageTestSuite.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.commons.convert2.conversion;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* Test suite.
*
* @author Stephen Colebourne
*/
public class PackageTestSuite extends TestCase {
public PackageTestSuite(String name) {
super(name);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
TestSuite suite = new TestSuite("Conversions");
suite.addTest(TestObjectToStringConversionFactory.suite());
suite.addTest(TestTimeZoneToStringConversionFactory.suite());
return suite;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]