You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by da...@apache.org on 2016/08/25 12:58:23 UTC

svn commit: r1757677 [3/3] - in /felix/trunk/converter: ./ converter/ converter/src/ converter/src/main/ converter/src/main/java/ converter/src/main/java/org/ converter/src/main/java/org/apache/ converter/src/main/java/org/apache/felix/ converter/src/m...

Added: felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/json/JsonCodecTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/json/JsonCodecTest.java?rev=1757677&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/json/JsonCodecTest.java (added)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/json/JsonCodecTest.java Thu Aug 25 12:58:22 2016
@@ -0,0 +1,146 @@
+/*
+ * 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.apache.felix.converter.impl.json;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.felix.converter.impl.ConverterImpl;
+import org.apache.felix.converter.impl.MyDTO;
+import org.apache.felix.converter.impl.MyDTO.Count;
+import org.apache.felix.converter.impl.MyEmbeddedDTO;
+import org.apache.felix.converter.impl.MyEmbeddedDTO.Alpha;
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.commons.json.JSONObject;
+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.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class JsonCodecTest {
+    private Converter converter;
+
+    @Before
+    public void setUp() {
+        converter = new ConverterImpl();
+    }
+
+    @After
+    public void tearDown() {
+        converter = null;
+    }
+
+    @Test
+    public void testJSONCodec() throws Exception {
+        Map<Object, Object> m1 = new HashMap<>();
+        m1.put("x", true);
+        m1.put("y", null);
+        Map<Object, Object> m = new HashMap<>();
+        m.put(1, 11L);
+        m.put("ab", "cd");
+        m.put(true, m1);
+
+        JsonCodecImpl jsonCodec = new JsonCodecImpl();
+        String json = jsonCodec.encode(m).toString();
+
+        JSONObject jo = new JSONObject(json);
+        assertEquals(11, jo.getInt("1"));
+        assertEquals("cd", jo.getString("ab"));
+        JSONObject jo2 = jo.getJSONObject("true");
+        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());
+        assertEquals(m.get(1), m2.get("1"));
+        assertEquals(m.get("ab"), m2.get("ab"));
+        assertEquals(m.get(true), m2.get("true"));
+    }
+
+    @Test
+    public void testCodecWithAdapter() throws JSONException {
+        Map<String, Foo> m1 = new HashMap<>();
+        m1.put("f", new Foo("fofofo"));
+        Map<String, Object> m = new HashMap<>();
+        m.put("submap", m1);
+
+        Adapter ca = converter.getAdapter();
+        ca.rule(Foo.class, String.class, Foo::tsFun, v -> Foo.fsFun(v));
+
+        JsonCodecImpl jsonCodec = new JsonCodecImpl();
+        String json = jsonCodec.with(ca).encode(m).toString();
+
+        JSONObject jo = new JSONObject(json);
+        assertEquals(1, jo.length());
+        JSONObject jo1 = jo.getJSONObject("submap");
+        assertEquals("<fofofo>", jo1.getString("f"));
+
+        // TODO convert back into a Map<String, Foo> via TypeReference
+    }
+
+    @Test
+    public void testDTO() {
+        MyDTO dto = new MyDTO();
+        dto.count = Count.ONE;
+        dto.ping = "'";
+        dto.pong = Long.MIN_VALUE;
+
+        MyEmbeddedDTO embedded = new MyEmbeddedDTO();
+        embedded.alpha = Alpha.B;
+        embedded.marco = "jo !";
+        embedded.polo = 327;
+        dto.embedded = embedded;
+
+        JsonCodecImpl jsonCodec = new JsonCodecImpl();
+        String json = jsonCodec.encode(dto).toString();
+        assertEquals(
+            "{\"ping\":\"'\",\"count\":\"ONE\",\"pong\":-9223372036854775808,"
+            + "\"embedded\":{\"polo\":327,\"alpha\":\"B\",\"marco\":\"jo !\"}}",
+            json);
+
+        MyDTO dto2 = jsonCodec.decode(MyDTO.class).from(json);
+        assertEquals(Count.ONE, dto2.count);
+        assertEquals("'", dto2.ping);
+        assertEquals(Long.MIN_VALUE, dto2.pong);
+        MyEmbeddedDTO embedded2 = dto2.embedded;
+        assertEquals(Alpha.B, embedded2.alpha);
+        assertEquals("jo !", embedded2.marco);
+        assertEquals(327, embedded2.polo);
+    }
+
+    static class Foo {
+        private final String val;
+
+        public Foo(String s) {
+            val = s;
+        }
+
+        public String tsFun() {
+            return "<" + val + ">";
+        }
+
+        public static Foo fsFun(String s) {
+            return new Foo(s.substring(1, s.length() - 1));
+        }
+    }
+}

Added: felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/json/JsonParserTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/json/JsonParserTest.java?rev=1757677&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/json/JsonParserTest.java (added)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/json/JsonParserTest.java Thu Aug 25 12:58:22 2016
@@ -0,0 +1,63 @@
+/*
+ * 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.apache.felix.converter.impl.json;
+
+import java.util.Arrays;
+import java.util.Map;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class JsonParserTest {
+    @Test
+    public void testJsonSimple() {
+        String json = "{\"hi\": \"ho\", \"ha\": true}";
+        JsonParser jp = new JsonParser(json);
+        Map<String, Object> m = jp.getParsed();
+        assertEquals(2, m.size());
+        assertEquals("ho", m.get("hi"));
+        assertTrue((Boolean) m.get("ha"));
+    }
+
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testJsonComplex() {
+        String json = "{\"a\": [1,2,3,4,5], \"b\": {\"x\": 12, \"y\": 42, \"z\": {\"test test\": \"hello hello\"}}}";
+        JsonParser jp = new JsonParser(json);
+        Map<String, Object> m = jp.getParsed();
+        assertEquals(2, m.size());
+        assertEquals(Arrays.asList(1L, 2L, 3L, 4L, 5L), m.get("a"));
+        Map<String, Object> mb = (Map<String, Object>) m.get("b");
+        assertEquals(3, mb.size());
+        assertEquals(12L, mb.get("x"));
+        assertEquals(42L, mb.get("y"));
+        Map<String, Object> mz = (Map<String, Object>) mb.get("z");
+        assertEquals(1, mz.size());
+        assertEquals("hello hello", mz.get("test test"));
+    }
+
+    @Test
+    public void testJsonArray() {
+        String json = "{\"abc\": [\"x\", \"y\", \"z\"]}";
+        JsonParser jp = new JsonParser(json);
+        Map<String, Object> m = jp.getParsed();
+        assertEquals(1, m.size());
+        assertEquals(Arrays.asList("x", "y", "z"), m.get("abc"));
+    }
+}

Added: felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/json/JsonSerializationTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/json/JsonSerializationTest.java?rev=1757677&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/json/JsonSerializationTest.java (added)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/json/JsonSerializationTest.java Thu Aug 25 12:58:22 2016
@@ -0,0 +1,80 @@
+/*
+ * 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.apache.felix.converter.impl.json;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class JsonSerializationTest {
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testComplexMapSerialization() {
+        Map<String, Object> m = new LinkedHashMap<>();
+        m.put("sKey", "a string");
+        m.put("iKey", 42);
+        m.put("bKey",  true);
+        m.put("noKey", null);
+        m.put("simpleArray", new int[] {1,2,3});
+
+        Map<String, Object> m1 = new LinkedHashMap<>();
+        m1.put("a", 1L);
+        m1.put("b", "hello");
+        m.put("simpleObject", m1);
+
+        String expected = "{\"sKey\":\"a string\","
+                + "\"iKey\":42,"
+                + "\"bKey\":true,"
+                + "\"noKey\":null,"
+                + "\"simpleArray\":[1,2,3],"
+                + "\"simpleObject\":{\"a\":1,\"b\":\"hello\"}}";
+        assertEquals(expected, new JsonCodecImpl().encode(m).toString());
+
+        Map<String, Object> dm = new JsonCodecImpl().decode(Map.class).from(expected);
+        Map<String, Object> expected2 = new LinkedHashMap<>();
+        expected2.put("sKey", "a string");
+        expected2.put("iKey", 42L);
+        expected2.put("bKey",  true);
+        expected2.put("noKey", null);
+        expected2.put("simpleArray", Arrays.asList(1L,2L,3L));
+        expected2.put("simpleObject", m1);
+        assertEquals(expected2, dm);
+    }
+
+    @Test
+    public void testComplexMapSerialization2() {
+        Map<String, Object> m2 = new LinkedHashMap<>();
+        m2.put("yes", Boolean.TRUE);
+        m2.put("no", Collections.singletonMap("maybe", false));
+
+        Map<String, Object> cm = new LinkedHashMap<>();
+        cm.put("list", Arrays.asList(
+                Collections.singletonMap("x", "y"),
+                Collections.singletonMap("x", "b")));
+        cm.put("embedded", m2);
+
+        String expected = "{\"list\":[{\"x\":\"y\"},{\"x\":\"b\"}],"
+                + "\"embedded\":"
+                + "{\"yes\":true,\"no\":{\"maybe\":false}}}";
+        assertEquals(expected, new JsonCodecImpl().encode(cm).toString());
+    }
+}

Added: felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java?rev=1757677&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java (added)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java Thu Aug 25 12:58:22 2016
@@ -0,0 +1,95 @@
+/*
+ * 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.apache.felix.converter.impl.yaml;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class YamlSerializationTest {
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testComplexMapSerialization() {
+        Map<String, Object> m = new LinkedHashMap<>();
+        m.put("sKey", "a string");
+        m.put("iKey", 42);
+        m.put("bKey",  true);
+        m.put("noKey", null);
+        m.put("simpleArray", new int[] {1,2,3});
+
+        Map<String, Object> m1 = new LinkedHashMap<>();
+        m1.put("a", 1L);
+        m1.put("b", "hello");
+        m.put("simpleObject", m1);
+
+        String expected = "sKey: 'a string'\n" +
+                "iKey: 42\n" +
+                "bKey: true\n" +
+                "noKey: \n" +
+                "simpleArray: \n" +
+                "  - 1\n" +
+                "  - 2\n" +
+                "  - 3\n" +
+                "simpleObject: \n" +
+                "  a: 1\n" +
+                "  b: 'hello'";
+        assertEquals(expected, new YamlCodecImpl().encode(m).toString().trim());
+
+        Map<String, Object> dm = new YamlCodecImpl().decode(Map.class).from(expected);
+        Map<String, Object> expected2 = new LinkedHashMap<>();
+        expected2.put("sKey", "a string");
+        expected2.put("iKey", 42);
+        expected2.put("bKey",  true);
+        expected2.put("noKey", null);
+        expected2.put("simpleArray", Arrays.asList(1,2,3));
+
+        Map<String, Object> m2 = new LinkedHashMap<>();
+        m2.put("a", 1);
+        m2.put("b", "hello");
+        expected2.put("simpleObject", m2);
+        assertEquals(expected2, dm);
+    }
+
+    @Test
+    public void testComplexMapSerialization2() {
+        Map<String, Object> m2 = new LinkedHashMap<>();
+        m2.put("yes", Boolean.TRUE);
+        m2.put("no", Collections.singletonMap("maybe", false));
+
+        Map<String, Object> cm = new LinkedHashMap<>();
+        cm.put("list", Arrays.asList(
+                Collections.singletonMap("x", "y"),
+                Collections.singletonMap("x", "b")));
+        cm.put("embedded", m2);
+
+        String expected = "list: \n" +
+                "  - \n" +
+                "    x: 'y'\n" +
+                "  - \n" +
+                "    x: 'b'\n" +
+                "embedded: \n" +
+                "  yes: true\n" +
+                "  no: \n" +
+                "    maybe: false";
+        assertEquals(expected, new YamlCodecImpl().encode(cm).toString().trim());
+    }
+}

Modified: felix/trunk/converter/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/converter/pom.xml?rev=1757677&r1=1757676&r2=1757677&view=diff
==============================================================================
--- felix/trunk/converter/pom.xml (original)
+++ felix/trunk/converter/pom.xml Thu Aug 25 12:58:22 2016
@@ -26,10 +26,10 @@
         <relativePath>../pom/pom.xml</relativePath>
     </parent>
 
-    <name>Apache Felix Converter Service</name>
-    <artifactId>org.apache.felix.converter</artifactId>
+    <name>Apache Felix Converter Reactor</name>
+    <artifactId>org.apache.felix.converter.reactor</artifactId>
     <version>0.1-SNAPSHOT</version>
-    <packaging>jar</packaging>
+    <packaging>pom</packaging>
 
     <scm>
         <connection>scm:svn:http://svn.apache.org/repos/asf/felix/trunk/converter</connection>
@@ -37,97 +37,7 @@
         <url>http://svn.apache.org/viewvc/felix/trunk/converter/</url>
     </scm>
 
-    <properties>
-        <felix.java.version>8</felix.java.version>
-        <felix.java.signature.artifactId>java18</felix.java.signature.artifactId>
-    </properties>
-
-    <build>
-        <plugins>
-            <plugin>
-                <groupId>org.apache.felix</groupId>
-                <artifactId>maven-bundle-plugin</artifactId>
-                <version>2.5.3</version>
-                <executions>
-                    <execution>
-                        <id>bundle</id>
-                        <phase>package</phase>
-                        <goals>
-                            <goal>bundle</goal>
-                        </goals>
-                    </execution>
-                    <execution>
-                        <id>baseline</id>
-                        <goals>
-                            <goal>baseline</goal>
-                        </goals>
-                    </execution>
-                </executions>
-                <configuration>
-                    <instructions>
-                        <Bundle-Activator>org.apache.felix.converter.impl.Activator</Bundle-Activator>
-                        <Private-Package>org.apache.felix.converter.*,org.yaml.snakeyaml.*</Private-Package>
-                        <Export-Package>org.osgi.service.converter, org.osgi.service.converter.util</Export-Package>
-                        <Import-Package>*</Import-Package>
-                    </instructions>
-                </configuration>
-            </plugin>
-            <plugin>
-                <groupId>org.apache.rat</groupId>
-                <artifactId>apache-rat-plugin</artifactId>
-                <executions>
-                    <execution>
-                        <phase>verify</phase>
-                        <goals>
-                            <goal>check</goal>
-                        </goals>
-                    </execution>
-                </executions>
-                <configuration>
-                    <includes>
-                        <include>src/**</include>
-                    </includes>
-                    <excludes>
-                        <exclude>src/main/resources/META-INF/services/org.osgi.service.converter.Converter</exclude>
-                    </excludes>
-                </configuration>
-            </plugin>
-            
-        </plugins>
-    </build>
-
-    <dependencies>
-        <dependency>
-            <groupId>org.osgi</groupId>
-            <artifactId>osgi.annotation</artifactId>
-            <version>6.0.1</version>
-            <scope>provided</scope>
-        </dependency>
-        
-        <dependency>
-            <groupId>org.osgi</groupId>
-            <artifactId>osgi.core</artifactId>
-            <version>6.0.0</version>
-            <scope>provided</scope>
-        </dependency>
-        
-        <dependency>
-            <groupId>org.yaml</groupId>
-            <artifactId>snakeyaml</artifactId>
-            <version>1.17</version>
-        </dependency>
-        
-        <dependency>
-            <groupId>junit</groupId>
-            <artifactId>junit</artifactId>
-            <scope>test</scope>
-        </dependency>
-        
-        <dependency>
-            <groupId>org.apache.sling</groupId>
-            <artifactId>org.apache.sling.commons.json</artifactId>
-            <version>2.0.16</version>
-            <scope>test</scope>            
-        </dependency>
-    </dependencies>
+    <modules>
+        <module>converter</module>
+    </modules>
 </project>