felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1737872 - in /felix/trunk/converter/src: main/java/org/apache/felix/converter/impl/ConvertingImpl.java main/java/org/osgi/service/converter/Converting.java test/java/org/apache/felix/converter/impl/ConverterTest.java
Date Tue, 05 Apr 2016 19:33:39 GMT
Author: davidb
Date: Tue Apr  5 19:33:38 2016
New Revision: 1737872

URL: http://svn.apache.org/viewvc?rev=1737872&view=rev
Log:
Additional simple tests for the converter and some tiny tweaks to the impl...

Modified:
    felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
    felix/trunk/converter/src/main/java/org/osgi/service/converter/Converting.java
    felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java

Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java?rev=1737872&r1=1737871&r2=1737872&view=diff
==============================================================================
--- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
(original)
+++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
Tue Apr  5 19:33:38 2016
@@ -78,4 +78,9 @@ public class ConvertingImpl implements C
         }
         return null;
     }
+
+    @Override
+    public String toString() {
+        return to(String.class);
+    }
 }

Modified: felix/trunk/converter/src/main/java/org/osgi/service/converter/Converting.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/osgi/service/converter/Converting.java?rev=1737872&r1=1737871&r2=1737872&view=diff
==============================================================================
--- felix/trunk/converter/src/main/java/org/osgi/service/converter/Converting.java (original)
+++ felix/trunk/converter/src/main/java/org/osgi/service/converter/Converting.java Tue Apr
 5 19:33:38 2016
@@ -21,13 +21,13 @@ import java.lang.reflect.Type;
  * This interface is used to specify the target that an object should be
  * converted to. A {@link Converting} instance can be obtained via the
  * {@link Converter} service by starting a conversion for a specific object.
- * 
+ *
  * @author $Id:$
  */
 public interface Converting {
 	/**
 	 * Specify the target object type for the conversion as a class object.
-	 * 
+	 *
 	 * @param cls The class to convert to.
 	 * @return The converted object.
 	 */
@@ -38,13 +38,13 @@ public interface Converting {
 	 * class carries generics information a TypeReference should be used as this
 	 * preserves the generic information whereas a Class object has this
 	 * information erased. Example use:
-	 * 
+	 *
 	 * <pre>
 	 *  List<String> result =
 	 *      converter.convert(Arrays.asList(1,2,3)).
 	 *          to(new TypeReference&lt;List&lt;String&gt;&gt;() {});
 	 * </pre>
-	 * 
+	 *
 	 * @param ref A type reference to the object being converted to.
 	 * @return The converted object.
 	 */
@@ -52,10 +52,17 @@ public interface Converting {
 
 	/**
 	 * Specify the target object type as a Java Refelection Type object.
-	 * 
+	 *
 	 * @param type A Type object to represent the target type to be converted
 	 *        to.
 	 * @return The converted object.
 	 */
 	Object to(Type type);
+
+	/**
+	 * Same as to(String.class)
+	 * @return The converted object.
+	 */
+    @Override
+    String toString();
 }

Modified: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java?rev=1737872&r1=1737871&r2=1737872&view=diff
==============================================================================
--- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
(original)
+++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
Tue Apr  5 19:33:38 2016
@@ -16,16 +16,62 @@
  */
 package org.apache.felix.converter.impl;
 
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
+import org.osgi.service.converter.Adapter;
 import org.osgi.service.converter.Converter;
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 
 public class ConverterTest {
+    private Converter converter;
+
+    @Before
+    public void setUp() {
+        converter = new ConverterImpl();
+    }
+
+    @After
+    public void tearDown() {
+        converter = null;
+    }
+
+    @Test
+    public void testSimpleConversions() {
+        assertEquals(Integer.valueOf(123), converter.convert("123").to(Integer.class));
+        //assertEquals(Integer.valueOf(123), c.convert("123").to(int.class));
+        assertEquals(Long.valueOf(123), converter.convert("123").to(Long.class));
+//        assertEquals(Character.valueOf(123), c.convert("123").to(Character.class));
+        assertEquals(Byte.valueOf((byte) 123), converter.convert("123").to(Byte.class));
+        assertEquals(Float.valueOf("12.3"), converter.convert("12.3").to(Float.class));
+        assertEquals(Double.valueOf("12.3"), converter.convert("12.3").to(Double.class));
+        assertEquals("123", converter.convert(123).to(String.class));
+    }
+
     @Test
-    public void testConverter() {
-        Converter c = new ConverterImpl();
-        int i = c.convert("123").to(Integer.class);
-        assertEquals(123, i);
+    public void testStandardStringArrayConversion() {
+        String[] sa = {"A", "B"};
+        assertEquals("A", converter.convert(sa).toString());
+        assertEquals("A", converter.convert(sa).to(String.class));
+
+        String[] sa2 = {"A"};
+        assertArrayEquals(sa2, converter.convert("A").to(String[].class));
+    }
+
+    @Test
+    public void testCustomStringArrayConverstion() {
+        Adapter adapter = converter.getAdapter();
+        adapter.rule(String[].class, String.class,
+                v -> Stream.of(v).collect(Collectors.joining(",")),
+                v -> v.split(","));
+
+        String[] sa = {"A", "B"};
+        assertEquals("A,B", adapter.convert(sa).to(String.class));
+        assertArrayEquals(sa, adapter.convert("A,B").to(String[].class));
     }
 }



Mime
View raw message