Author: mthl
Date: Sat May 25 21:58:15 2019 New Revision: 1860024 URL: http://svn.apache.org/viewvc?rev=1860024&view=rev Log: Improved: Turn ‘TestJSONConverters’ into a unit test class (OFBIZ-11067) Added: ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/TestJSONConverters.java (with props) Removed: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/conversion/test/ Modified: ofbiz/ofbiz-framework/trunk/framework/base/testdef/basetests.xml Added: ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/TestJSONConverters.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/TestJSONConverters.java?rev=1860024&view=auto ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/TestJSONConverters.java (added) +++ ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/TestJSONConverters.java Sat May 25 21:58:15 2019 @@ -0,0 +1,87 @@ +/* + * 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.ofbiz.base.conversion; + +import static org.junit.Assert.assertEquals; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.ofbiz.base.lang.JSON; +import org.apache.ofbiz.base.util.UtilGenerics; +import org.junit.Test; + +public class TestJSONConverters { + public TestJSONConverters() { + ConverterLoader loader = new JSONConverters(); + loader.loadConverters(); + } + + @Test + public void testJSONToMap() throws Exception { + Converter<JSON, Map<String,String>> converter = + UtilGenerics.cast(Converters.getConverter(JSON.class, Map.class)); + Map<String,String> map, convertedMap; + map = new HashMap<>(); + map.put("field1", "value1"); + JSON json = JSON.from(map); + convertedMap = UtilGenerics.toMap(converter.convert(json)); + assertEquals("JSON to Map", map, convertedMap); + } + + @Test + public void testJSONToList() throws Exception { + Converter<JSON, List<Object>> converter = UtilGenerics.cast(Converters.getConverter(JSON.class, List.class)); + List<Object> list, convertedList; + list = new ArrayList<>(); + list.add("field1"); + list.add("field2"); + JSON json = JSON.from(list); + convertedList = UtilGenerics.toList(converter.convert(json)); + assertEquals("JSON to List", list, convertedList); + } + + @Test + public void testMapToJSON() throws Exception { + Converter<Map<String,Object>, JSON> converter = + UtilGenerics.cast(Converters.getConverter(Map.class, JSON.class)); + JSON json; + Map<String,Object> map = new LinkedHashMap<>(); + map.put("field1", "value1"); + map.put("field2", new BigDecimal("3.7")); + json = converter.convert(map); + assertEquals("Map to JSON", "{\"field1\":\"value1\",\"field2\":3.7}", json.toString()); + } + + @Test + public void testListToJSON() throws Exception { + Converter<List<String>, JSON> converter = UtilGenerics.cast(Converters.getConverter(List.class, JSON.class)); + JSON json; + List<String> list = new ArrayList<>(); + list.add("field1"); + list.add("field2"); + json = converter.convert(list); + assertEquals("List to JSON", "[\"field1\",\"field2\"]", json.toString()); + } +} + Propchange: ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/TestJSONConverters.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/TestJSONConverters.java ------------------------------------------------------------------------------ svn:keywords = Date Rev Author URL Id Propchange: ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/TestJSONConverters.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: ofbiz/ofbiz-framework/trunk/framework/base/testdef/basetests.xml URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/testdef/basetests.xml?rev=1860024&r1=1860023&r2=1860024&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/base/testdef/basetests.xml (original) +++ ofbiz/ofbiz-framework/trunk/framework/base/testdef/basetests.xml Sat May 25 21:58:15 2019 @@ -25,7 +25,6 @@ <junit-test-suite class-name="org.apache.ofbiz.base.util.test.UtilObjectTests"/> <junit-test-suite class-name="org.apache.ofbiz.base.util.string.test.FlexibleStringExpanderTests"/> <junit-test-suite class-name="org.apache.ofbiz.base.util.collections.test.FlexibleMapAccessorTests"/> - <junit-test-suite class-name="org.apache.ofbiz.base.conversion.test.TestJSONConverters"/> <groovy-test-suite name="simple" location="component://base/groovyScripts/test/SimpleTests.groovy"/> </test-group> </test-suite> |
Free forum by Nabble | Edit this page |