svn commit: r965843 - in /ofbiz/trunk/framework/base: build.xml src/org/ofbiz/base/conversion/test/TestBooleanConverters.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r965843 - in /ofbiz/trunk/framework/base: build.xml src/org/ofbiz/base/conversion/test/TestBooleanConverters.java

adrianc
Author: adrianc
Date: Tue Jul 20 13:39:05 2010
New Revision: 965843

URL: http://svn.apache.org/viewvc?rev=965843&view=rev
Log:
Added Boolean converter tests - copied from Apache Commons Convert.

Added:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/TestBooleanConverters.java   (with props)
Modified:
    ofbiz/trunk/framework/base/build.xml

Modified: ofbiz/trunk/framework/base/build.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/build.xml?rev=965843&r1=965842&r2=965843&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/build.xml (original)
+++ ofbiz/trunk/framework/base/build.xml Tue Jul 20 13:39:05 2010
@@ -50,6 +50,7 @@ under the License.
         <file name="org/ofbiz/base/util/test/TimeDurationTests.java"/>
         <file name="org/ofbiz/base/util/cache/test/UtilCacheTests.java"/>
         <file name="org/ofbiz/base/json/test/JSONTests.java"/>
+        <file name="org/ofbiz/base/conversion/test/TestBooleanConverters.java"/>
         <file name="org/ofbiz/base/conversion/test/DateTimeTests.java"/>
         <file name="org/ofbiz/base/conversion/test/MiscTests.java"/>
         <file name="org/ofbiz/base/util/test/UtilIOTests.java"/>

Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/TestBooleanConverters.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/TestBooleanConverters.java?rev=965843&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/TestBooleanConverters.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/TestBooleanConverters.java Tue Jul 20 13:39:05 2010
@@ -0,0 +1,71 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.ofbiz.base.conversion.test;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+import org.ofbiz.base.conversion.BooleanConverters;
+import org.ofbiz.base.conversion.Converter;
+import org.ofbiz.base.conversion.ConverterLoader;
+import org.ofbiz.base.conversion.Converters;
+
+public class TestBooleanConverters extends TestCase {
+
+    public TestBooleanConverters(String name) {
+        super(name);
+    }
+
+    public static <T> void assertFromBoolean(String label, Converter<Boolean, T> converter, T trueResult, T falseResult) throws Exception {
+        assertTrue(label + " can convert", converter.canConvert(Boolean.class, trueResult.getClass()));
+        assertEquals(label + " registered", converter.getClass(), Converters.getConverter(Boolean.class, trueResult.getClass()).getClass());
+        assertEquals(label + " converted", trueResult, converter.convert(true));
+        assertEquals(label + " converted", falseResult, converter.convert(false));
+    }
+
+    public static <S> void assertToBoolean(String label, Converter<S, Boolean> converter, S trueSource, S falseSource) throws Exception {
+        assertTrue(label + " can convert", converter.canConvert(trueSource.getClass(), Boolean.class));
+        assertEquals(label + " registered", converter.getClass(), Converters.getConverter(trueSource.getClass(), Boolean.class).getClass());
+        assertEquals(label + " converted", Boolean.TRUE, converter.convert(trueSource));
+        assertEquals(label + " converted", Boolean.FALSE, converter.convert(falseSource));
+    }
+
+    @SuppressWarnings("unchecked")
+    public static <S> void assertToCollection(String label, S source) throws Exception {
+        Converter<S, ? extends Collection> toList = (Converter<S, ? extends Collection>) Converters.getConverter(source.getClass(), List.class);
+        Collection<S> listResult = toList.convert(source);
+        assertEquals(label + " converted to List", source, listResult.toArray()[0]);
+        Converter<S, ? extends Collection> toSet = (Converter<S, ? extends Collection>) Converters.getConverter(source.getClass(), Set.class);
+        Collection<S> setResult = toSet.convert(source);
+        assertEquals(label + " converted to Set", source, setResult.toArray()[0]);
+    }
+
+    public void testBooleanConverters() throws Exception {
+        ConverterLoader loader = new BooleanConverters();
+        loader.loadConverters();
+        assertFromBoolean("BooleanToInteger", new BooleanConverters.BooleanToInteger(), 1, 0);
+        assertFromBoolean("BooleanToString", new BooleanConverters.BooleanToString(), "true", "false");
+        assertToBoolean("IntegerToBoolean", new BooleanConverters.IntegerToBoolean(), 1, 0);
+        assertToBoolean("StringToBoolean", new BooleanConverters.StringToBoolean(), "true", "false");
+        assertToCollection("BooleanToCollection", Boolean.TRUE);
+    }
+}

Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/TestBooleanConverters.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/TestBooleanConverters.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/TestBooleanConverters.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain