Author: davidb Date: Tue Apr 12 11:24:29 2016 New Revision: 1738769 URL: http://svn.apache.org/viewvc?rev=1738769&view=rev Log: Felix Converter Service - add tests for to-string conversions
Also added a small amount of additional logic to support the tested conversions. Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JsonCodecTest.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=1738769&r1=1738768&r2=1738769&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 12 11:24:29 2016 @@ -18,6 +18,7 @@ package org.apache.felix.converter.impl; import java.lang.reflect.Method; import java.lang.reflect.Type; +import java.util.Collection; import org.osgi.service.converter.Converter; import org.osgi.service.converter.Converting; @@ -35,9 +36,20 @@ public class ConvertingImpl implements C @SuppressWarnings("unchecked") @Override public <T> T to(Class<T> cls) { + if (object == null) + return null; + if (cls.isAssignableFrom(object.getClass())) + return (T) object; + if (String.class.equals(cls)) { - if (object instanceof Object[]) + if (object instanceof Object[]) { return (T) ((Object[])object)[0]; + } else if (object instanceof Collection) { + Collection<?> c = (Collection<?>) object; + if (c.size() == 0) { + return null; + } + } return (T) object.toString(); } else if (String[].class.equals(cls)) { String[] res = new String[1]; 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=1738769&r1=1738768&r2=1738769&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 12 11:24:29 2016 @@ -16,6 +16,8 @@ */ package org.apache.felix.converter.impl; +import java.math.BigInteger; +import java.util.Collections; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -27,6 +29,7 @@ import org.osgi.service.converter.Conver import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; public class ConverterTest { private Converter converter; @@ -43,6 +46,20 @@ public class ConverterTest { @Test public void testSimpleConversions() { + 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)); + + 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)); @@ -50,7 +67,20 @@ public class ConverterTest { 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 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 Modified: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JsonCodecTest.java URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JsonCodecTest.java?rev=1738769&r1=1738768&r2=1738769&view=diff ============================================================================== --- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JsonCodecTest.java (original) +++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/JsonCodecTest.java Tue Apr 12 11:24:29 2016 @@ -63,6 +63,7 @@ public class JsonCodecTest { assertEquals(true, jo2.getBoolean("x")); assertTrue(jo2.isNull("y")); + @SuppressWarnings("rawtypes") Map m2 = jsonCodec.decode(Map.class).from(json); // m2 is not exactly equal to m, as the keys are all strings now, this is unavoidable with JSON assertEquals(m.size(), m2.size());