felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1739745 - in /felix/trunk/converter/src/test/java/org/apache/felix/converter/impl: ConverterServiceTest.java ConverterTest.java
Date Mon, 18 Apr 2016 11:34:34 GMT
Author: davidb
Date: Mon Apr 18 11:34:34 2016
New Revision: 1739745

URL: http://svn.apache.org/viewvc?rev=1739745&view=rev
Log:
Felix Converter Service - Move tests together

Removed:
    felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
Modified:
    felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterServiceTest.java

Modified: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterServiceTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterServiceTest.java?rev=1739745&r1=1739744&r2=1739745&view=diff
==============================================================================
--- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterServiceTest.java
(original)
+++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterServiceTest.java
Mon Apr 18 11:34:34 2016
@@ -16,34 +16,246 @@
  */
 package org.apache.felix.converter.impl;
 
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.net.URL;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
 import java.time.OffsetDateTime;
 import java.time.OffsetTime;
 import java.time.ZonedDateTime;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
 import java.util.UUID;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+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.TypeReference;
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
 public class ConverterServiceTest {
-    private ConverterService cs;
+    private ConverterService converter;
 
     @Before
     public void setUp() {
-        cs = new ConverterService();
+        converter = new ConverterService();
     }
 
     @After
     public void tearDown() {
-        cs = null;
+        converter = null;
+    }
+
+    @Test
+    public void testSimpleConversions() {
+        // Conversions to String
+        assertEquals("abc", converter.convert("abc").to(String.class));
+        assertEquals("true", converter.convert(Boolean.TRUE).to(String.class));
+        assertEquals("c", converter.convert('c').to(String.class));
+        assertEquals("123", converter.convert(123).to(String.class));
+        assertEquals("" + Long.MAX_VALUE, converter.convert(Long.MAX_VALUE).to(String.class));
+        assertEquals("12.3", converter.convert(12.3f).to(String.class));
+        assertEquals("12.345", converter.convert(12.345d).to(String.class));
+        assertEquals(null, converter.convert(null).to(String.class));
+        assertEquals(null, converter.convert(Collections.emptyList()).to(String.class));
+
+        String bistr = "999999999999999999999"; // more than Long.MAX_VALUE
+        assertEquals(bistr, converter.convert(new BigInteger(bistr)).to(String.class));
+
+        // Conversions to boolean
+        assertTrue(converter.convert("true").to(boolean.class));
+        assertTrue(converter.convert("TRUE").to(boolean.class));
+        assertTrue(converter.convert('x').to(boolean.class));
+        assertTrue(converter.convert(Long.MIN_VALUE).to(boolean.class));
+        assertTrue(converter.convert(72).to(boolean.class));
+        assertFalse(converter.convert("false").to(boolean.class));
+        assertFalse(converter.convert("bleh").to(boolean.class));
+        assertFalse(converter.convert((char) 0).to(boolean.class));
+        assertFalse(converter.convert(null).to(boolean.class));
+        assertFalse(converter.convert(Collections.emptyList()).to(boolean.class));
+
+        // Converstions to integer
+        assertEquals(Integer.valueOf(123), converter.convert("123").to(int.class));
+        assertEquals(1, (int) converter.convert(true).to(int.class));
+        assertEquals(0, (int) converter.convert(false).to(int.class));
+
+        // Conversions to Class
+        assertEquals(BigDecimal.class, converter.convert("java.math.BigDecimal").to(Class.class));
+        assertNull(converter.convert(null).to(Class.class));
+        assertNull(converter.convert(Collections.emptyList()).to(Class.class));
+
+        assertEquals(Integer.valueOf(123), converter.convert("123").to(Integer.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));
+    }
+
+    enum TestEnum { FOO, BAR, BLAH, FALSE, X};
+    @Test
+    public void testEnums() {
+        assertEquals(TestEnum.BLAH, converter.convert("BLAH").to(TestEnum.class));
+        assertEquals(TestEnum.X, converter.convert('X').to(TestEnum.class));
+        assertEquals(TestEnum.FALSE, converter.convert(false).to(TestEnum.class));
+        assertEquals(TestEnum.BAR, converter.convert(1).to(TestEnum.class));
+        assertNull(converter.convert(null).to(TestEnum.class));
+        assertNull(converter.convert(Collections.emptySet()).to(TestEnum.class));
+    }
+
+    @Test
+    public void testIdentialTarget() {
+        Object o = new Object();
+        assertSame(o, converter.convert(o).to(Object.class));
+
+        Thread t = new Thread(); // No converter available
+        assertSame(t, converter.convert(t).to(Thread.class));
+        assertSame(t, converter.convert(t).to(Runnable.class));
+        assertSame(t, converter.convert(t).to(Object.class));
+
+        Thread st = new Thread() {}; // Subclass of Thread
+        assertSame(st, converter.convert(st).to(Thread.class));
+    }
+
+    @Test
+    public void testFromUnknownDataTypeViaString() {
+        class MyClass {
+            @Override
+            public String toString() {
+                return "1234";
+            }
+        };
+        MyClass o = new MyClass();
+
+        assertEquals(1234, (int) converter.convert(o).to(int.class));
+        assertEquals("1234", converter.convert(o).to(String.class));
+    }
+
+    @Test
+    public void testToUnknownViaStringCtor() {
+        class MyClass {
+            @Override
+            public String toString() {
+                return "http://127.0.0.1:1234/blah";
+            }
+        };
+        MyClass o = new MyClass();
+
+        URL url = converter.convert(o).to(URL.class);
+        assertEquals("http://127.0.0.1:1234/blah", url.toString());
+        assertEquals("http", url.getProtocol());
+        assertEquals("127.0.0.1", url.getHost());
+        assertEquals(1234, url.getPort());
+        assertEquals("/blah", url.getPath());
+
+        assertNull(converter.convert(null).to(URL.class));
+        assertNull(converter.convert(Collections.emptyList()).to(URL.class));
+    }
+
+    @Test
+    public void testFromMultiToSingle() {
+        assertEquals("abc", converter.convert(Collections.singleton("abc")).to(String.class));
+        assertEquals("abc", converter.convert(Arrays.asList("abc", "def", "ghi")).to(String.class));
+        assertEquals(42, (int) converter.convert(Arrays.asList("42", "17")).to(Integer.class));
+        MyClass2 mc = converter.convert(new String[] {"xxx", "yyy", "zzz"}).to(MyClass2.class);
+        assertEquals("xxx", mc.toString());
+        MyClass2[] arr = new MyClass2[] {new MyClass2("3.1412"), new MyClass2("6.2824")};
+        assertEquals(Float.valueOf(3.1412f), Float.valueOf(converter.convert(arr).to(float.class)));
+    }
+
+    @Test
+    public void testFromListToSet() {
+        List<Object> l = new ArrayList<>(Arrays.asList("A", 'B', 333));
+
+        Set<?> s = converter.convert(l).to(Set.class);
+        assertEquals(3, s.size());
+
+        for (Object o : s) {
+            Object expected = l.remove(0);
+            assertEquals(expected, o);
+        }
+    }
+
+    @Test
+    public void testFromGenericSetToLinkedList() {
+        Set<Integer> s = new LinkedHashSet<>();
+        s.add(123);
+        s.add(456);
+
+        LinkedList<String> ll = converter.convert(s).to(new TypeReference<LinkedList<String>>()
{});
+        assertEquals(Arrays.asList("123", "456"), ll);
+    }
+
+    @Test
+    public void testFromArrayToGenericOrderPreservingSet() {
+        String[] sa = {"567", "-765", "0", "-900"};
+
+        // Returned set should be order preserving
+        Set<Long> s = converter.convert(sa).to(new TypeReference<Set<Long>>()
{});
+
+        List<String> sl = new ArrayList<>(Arrays.asList(sa));
+        for (long l : s) {
+            long expected = Long.parseLong(sl.remove(0));
+            assertEquals(expected, l);
+        }
+    }
+
+    @Test
+    public void testFromSetToArray() {
+        Set<Integer> s = new LinkedHashSet<>();
+        s.add(Integer.MIN_VALUE);
+
+        long[] la = converter.convert(s).to(long[].class);
+        assertEquals(1, la.length);
+        assertEquals(Integer.MIN_VALUE, la[0]);
+    }
+
+    @Test
+    public void testStringArrayToIntegerArray() {
+        String[] sa = {"999", "111", "-909"};
+        Integer[] ia = converter.convert(sa).to(Integer[].class);
+        assertEquals(3, ia.length);
+        assertArrayEquals(new Integer[] {999, 111, -909}, ia);
+    }
+
+    @Test
+    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));
     }
 
     @Test
@@ -59,64 +271,76 @@ public class ConverterServiceTest {
     @Test
     public void testPatternConversion() {
         String p = "\\S*";
-        Pattern pattern = cs.convert(p).to(Pattern.class);
+        Pattern pattern = converter.convert(p).to(Pattern.class);
         Matcher matcher = pattern.matcher("hi");
         assertTrue(matcher.matches());
-        String p2 = cs.convert(pattern).to(String.class);
+        String p2 = converter.convert(pattern).to(String.class);
         assertEquals(p, p2);
     }
 
     @Test
     public void testLocalDateTime() {
         LocalDateTime ldt = LocalDateTime.now();
-        String s = cs.convert(ldt).to(String.class);
+        String s = converter.convert(ldt).to(String.class);
         assertTrue(s.length() > 0);
-        LocalDateTime ldt2 = cs.convert(s).to(LocalDateTime.class);
+        LocalDateTime ldt2 = converter.convert(s).to(LocalDateTime.class);
         assertEquals(ldt, ldt2);
     }
 
     @Test
     public void testLocalDate() {
         LocalDate ld = LocalDate.now();
-        String s = cs.convert(ld).to(String.class);
+        String s = converter.convert(ld).to(String.class);
         assertTrue(s.length() > 0);
-        LocalDate ld2 = cs.convert(s).to(LocalDate.class);
+        LocalDate ld2 = converter.convert(s).to(LocalDate.class);
         assertEquals(ld, ld2);
     }
 
     @Test
     public void testLocalTime() {
         LocalTime lt = LocalTime.now();
-        String s = cs.convert(lt).to(String.class);
+        String s = converter.convert(lt).to(String.class);
         assertTrue(s.length() > 0);
-        LocalTime lt2 = cs.convert(s).to(LocalTime.class);
+        LocalTime lt2 = converter.convert(s).to(LocalTime.class);
         assertEquals(lt, lt2);
     }
 
     @Test
     public void testOffsetDateTime() {
         OffsetDateTime ot = OffsetDateTime.now();
-        String s = cs.convert(ot).to(String.class);
+        String s = converter.convert(ot).to(String.class);
         assertTrue(s.length() > 0);
-        OffsetDateTime ot2 = cs.convert(s).to(OffsetDateTime.class);
+        OffsetDateTime ot2 = converter.convert(s).to(OffsetDateTime.class);
         assertEquals(ot, ot2);
     }
 
     @Test
     public void testOffsetTime() {
         OffsetTime ot = OffsetTime.now();
-        String s = cs.convert(ot).to(String.class);
+        String s = converter.convert(ot).to(String.class);
         assertTrue(s.length() > 0);
-        OffsetTime ot2 = cs.convert(s).to(OffsetTime.class);
+        OffsetTime ot2 = converter.convert(s).to(OffsetTime.class);
         assertEquals(ot, ot2);
     }
 
     @Test
     public void testZonedDateTime() {
         ZonedDateTime zdt = ZonedDateTime.now();
-        String s = cs.convert(zdt).to(String.class);
+        String s = converter.convert(zdt).to(String.class);
         assertTrue(s.length() > 0);
-        ZonedDateTime zdt2 = cs.convert(s).to(ZonedDateTime.class);
+        ZonedDateTime zdt2 = converter.convert(s).to(ZonedDateTime.class);
         assertEquals(zdt, zdt2);
     }
+
+    static class MyClass2 {
+        private final String value;
+        public MyClass2(String v) {
+            value = v;
+        }
+
+        @Override
+        public String toString() {
+            return value;
+        }
+    }
 }



Mime
View raw message