Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 7D664200B6B for ; Thu, 25 Aug 2016 14:58:31 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 7BCA3160A94; Thu, 25 Aug 2016 12:58:31 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 4C167160A93 for ; Thu, 25 Aug 2016 14:58:30 +0200 (CEST) Received: (qmail 65488 invoked by uid 500); 25 Aug 2016 12:58:29 -0000 Mailing-List: contact commits-help@felix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@felix.apache.org Delivered-To: mailing list commits@felix.apache.org Received: (qmail 65479 invoked by uid 99); 25 Aug 2016 12:58:29 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 25 Aug 2016 12:58:29 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 0D204180227 for ; Thu, 25 Aug 2016 12:58:29 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.374 X-Spam-Level: X-Spam-Status: No, score=0.374 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-1.426] autolearn=disabled Received: from mx2-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id GqZhaQ5gHDj2 for ; Thu, 25 Aug 2016 12:58:25 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx2-lw-eu.apache.org (ASF Mail Server at mx2-lw-eu.apache.org) with ESMTP id 7F1E260DA1 for ; Thu, 25 Aug 2016 12:58:24 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 9F37EE0F9E for ; Thu, 25 Aug 2016 12:58:23 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 6CA833A0E75 for ; Thu, 25 Aug 2016 12:58:23 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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... Date: Thu, 25 Aug 2016 12:58:23 -0000 To: commits@felix.apache.org From: davidb@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160825125823.6CA833A0E75@svn01-us-west.apache.org> archived-at: Thu, 25 Aug 2016 12:58:31 -0000 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 m1 = new HashMap<>(); + m1.put("x", true); + m1.put("y", null); + Map 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 m1 = new HashMap<>(); + m1.put("f", new Foo("fofofo")); + Map 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("", jo1.getString("f")); + + // TODO convert back into a Map 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 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 m = jp.getParsed(); + assertEquals(2, m.size()); + assertEquals(Arrays.asList(1L, 2L, 3L, 4L, 5L), m.get("a")); + Map mb = (Map) m.get("b"); + assertEquals(3, mb.size()); + assertEquals(12L, mb.get("x")); + assertEquals(42L, mb.get("y")); + Map mz = (Map) 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 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 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 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 dm = new JsonCodecImpl().decode(Map.class).from(expected); + Map 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 m2 = new LinkedHashMap<>(); + m2.put("yes", Boolean.TRUE); + m2.put("no", Collections.singletonMap("maybe", false)); + + Map 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 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 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 dm = new YamlCodecImpl().decode(Map.class).from(expected); + Map 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 m2 = new LinkedHashMap<>(); + m2.put("a", 1); + m2.put("b", "hello"); + expected2.put("simpleObject", m2); + assertEquals(expected2, dm); + } + + @Test + public void testComplexMapSerialization2() { + Map m2 = new LinkedHashMap<>(); + m2.put("yes", Boolean.TRUE); + m2.put("no", Collections.singletonMap("maybe", false)); + + Map 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 @@ ../pom/pom.xml - Apache Felix Converter Service - org.apache.felix.converter + Apache Felix Converter Reactor + org.apache.felix.converter.reactor 0.1-SNAPSHOT - jar + pom scm:svn:http://svn.apache.org/repos/asf/felix/trunk/converter @@ -37,97 +37,7 @@ http://svn.apache.org/viewvc/felix/trunk/converter/ - - 8 - java18 - - - - - - org.apache.felix - maven-bundle-plugin - 2.5.3 - - - bundle - package - - bundle - - - - baseline - - baseline - - - - - - org.apache.felix.converter.impl.Activator - org.apache.felix.converter.*,org.yaml.snakeyaml.* - org.osgi.service.converter, org.osgi.service.converter.util - * - - - - - org.apache.rat - apache-rat-plugin - - - verify - - check - - - - - - src/** - - - src/main/resources/META-INF/services/org.osgi.service.converter.Converter - - - - - - - - - - org.osgi - osgi.annotation - 6.0.1 - provided - - - - org.osgi - osgi.core - 6.0.0 - provided - - - - org.yaml - snakeyaml - 1.17 - - - - junit - junit - test - - - - org.apache.sling - org.apache.sling.commons.json - 2.0.16 - test - - + + converter +