http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/json/JsonTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/json/JsonTest.java b/juneau-core-test/src/test/java/org/apache/juneau/json/JsonTest.java new file mode 100755 index 0000000..0963c4f --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/json/JsonTest.java @@ -0,0 +1,307 @@ +// *************************************************************************************************************************** +// * 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.juneau.json; + +import static org.apache.juneau.TestUtils.*; +import static org.apache.juneau.json.JsonSerializerContext.*; +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.*; +import org.apache.juneau.json.annotation.*; +import org.apache.juneau.serializer.*; +import org.junit.*; + +@SuppressWarnings({"serial","javadoc"}) +public class JsonTest { + + //==================================================================================================== + // testBasic + //==================================================================================================== + @Test + public void testBasic() throws Exception { + Map<String,Object> m = new LinkedHashMap<String,Object>(); + List<Object> l = new LinkedList<Object>(); + + WriterSerializer s1 = new JsonSerializer.Simple().setProperty(SERIALIZER_trimNullProperties, false); + WriterSerializer s2 = new JsonSerializer.Simple().setProperty(SERIALIZER_trimNullProperties, false).setProperty(SERIALIZER_quoteChar, '"'); + String r; + + // Null keys and values + m.clear(); + m.put(null, null); + m.put("aaa", "bbb"); + assertEquals("A1", "{null:null,aaa:'bbb'}", s1.serialize(m)); + + // Escapes. + // String = ["] + m.clear(); + m.put("x", "[\"]"); + assertEquals("{x:\"[\\\"]\"}", s2.serialize(m)); + // String = [\"] + // JSON = {x:"\\\""} + m.clear(); + m.put("x", "[\\\"]"); + assertEquals("{x:\"[\\\\\\\"]\"}", s2.serialize(m)); + + // String = [\w[\w\-\.]{3,}\w] + // JSON = {x:"\\w[\\w\\-\\.]{3,}\\w"} + m.clear(); + r = "\\w[\\w\\-\\.]{3,}\\w"; + m.put("x", r); + assertEquals("{x:\"\\\\w[\\\\w\\\\-\\\\.]{3,}\\\\w\"}", s2.serialize(m)); + assertEquals(r, new ObjectMap(s2.serialize(m)).getString("x")); + + // String = [foo\bar] + // JSON = {x:"foo\\bar"} + m.clear(); + m.put("x", "foo\\bar"); + assertEquals("{x:\"foo\\\\bar\"}", s2.serialize(m)); + + m.clear(); + m.put("null", null); + m.put("aaa", "bbb"); + assertEquals("A2", "{'null':null,aaa:'bbb'}", s1.serialize(m)); + + m.clear(); + m.put(null, "null"); + m.put("aaa", "bbb"); + assertEquals("A3", "{null:'null',aaa:'bbb'}", s1.serialize(m)); + + // Arrays + m.clear(); + l.clear(); + m.put("J", "f1"); + m.put("B", "b"); + m.put("C", "c"); + l.add("1"); + l.add("2"); + l.add("3"); + Object o = new Object[] { m, l }; + Object o2 = new Object[] { o, "foo", "bar", new Integer(1), new Boolean(false), new Float(1.2), null }; + assertEquals("K1", "[[{J:'f1',B:'b',C:'c'},['1','2','3']],'foo','bar',1,false,1.2,null]", s1.serialize(o2)); + } + + @Test + public void testReservedKeywordAttributes() throws Exception { + Map<String,Object> m = new LinkedHashMap<String,Object>(); + + // Keys with reserved names. + for (String attr : new String[]{"","true","false","null","try","123","1x","-123",".123"}) { + m.clear(); + m.put(attr,1); + assertObjectEquals("{'"+attr+"':1}", m); + } + } + + //==================================================================================================== + // Validate various backslashes in strings. + //==================================================================================================== + @Test + public void testBackslashesInStrings() throws Exception { + JsonSerializer s = new JsonSerializer.Simple().setProperty(SERIALIZER_trimNullProperties, false).setProperty(SERIALIZER_quoteChar, '"'); + String r, r2; + + // [\\] + r = "\\"; + r2 = s.serialize(r); + assertEquals(r2, "\"\\\\\""); + assertEquals(JsonParser.DEFAULT.parse(r2, Object.class), r); + + // [\b\f\n\t] + r = "\b\f\n\t"; + r2 = s.serialize(r); + assertEquals("\"\\b\\f\\n\\t\"", r2); + assertEquals(r, JsonParser.DEFAULT.parse(r2, Object.class)); + + // Special JSON case: Forward slashes can OPTIONALLY be escaped. + // [\/] + assertEquals(JsonParser.DEFAULT.parse("\"\\/\"", Object.class), "/"); + + // Unicode + r = "\u1234\u1ABC\u1abc"; + r2 = s.serialize(r); + assertEquals("\"\u1234\u1ABC\u1abc\"", r2); + + assertEquals("\u1234", JsonParser.DEFAULT.parse("\"\\u1234\"", Object.class)); + } + + //==================================================================================================== + // Indentation + //==================================================================================================== + @Test + public void testIndentation() throws Exception { + ObjectMap m = new ObjectMap("{J:{B:['c',{D:'e'},['f',{G:'h'},1,false]]},I:'j'}"); + String e = "" + + "{" + + "\n J: {" + + "\n B: [" + + "\n 'c', " + + "\n {" + + "\n D: 'e'" + + "\n }, " + + "\n [" + + "\n 'f', " + + "\n {" + + "\n G: 'h'" + + "\n }, " + + "\n 1, " + + "\n false" + + "\n ]" + + "\n ]" + + "\n }, " + + "\n I: 'j'" + + "\n}"; + assertEquals(e, JsonSerializer.DEFAULT_LAX_READABLE.serialize(m)); + } + + //==================================================================================================== + // Escaping double quotes + //==================================================================================================== + @Test + public void testEscapingDoubleQuotes() throws Exception { + JsonSerializer s = JsonSerializer.DEFAULT; + String r = s.serialize(new ObjectMap().append("f1", "x'x\"x")); + assertEquals("{\"f1\":\"x'x\\\"x\"}", r); + JsonParser p = JsonParser.DEFAULT; + assertEquals("x'x\"x", p.parse(r, ObjectMap.class).getString("f1")); + } + + //==================================================================================================== + // Escaping single quotes + //==================================================================================================== + @Test + public void testEscapingSingleQuotes() throws Exception { + JsonSerializer s = JsonSerializer.DEFAULT_LAX; + String r = s.serialize(new ObjectMap().append("f1", "x'x\"x")); + assertEquals("{f1:'x\\'x\"x'}", r); + JsonParser p = JsonParser.DEFAULT; + assertEquals("x'x\"x", p.parse(r, ObjectMap.class).getString("f1")); + } + + //==================================================================================================== + // testWrapperAttrAnnotationOnBean + //==================================================================================================== + @Test + @SuppressWarnings("unchecked") + public void testWrapperAttrAnnotationOnBean() throws Exception { + JsonSerializer s = JsonSerializer.DEFAULT_LAX; + JsonParser p = JsonParser.DEFAULT; + String r; + + A t = A.create(); + r = s.serialize(t); + assertEquals("{foo:{f1:1}}", r); + t = p.parse(r, A.class); + assertEquals(1, t.f1); + + Map<String,A> m = new LinkedHashMap<String,A>(); + m.put("bar", A.create()); + r = s.serialize(m); + assertEquals("{bar:{foo:{f1:1}}}", r); + + m = p.parseMap(r, LinkedHashMap.class, String.class, A.class); + assertEquals(1, m.get("bar").f1); + } + + @Json(wrapperAttr="foo") + public static class A { + public int f1; + + static A create() { + A a = new A(); + a.f1 = 1; + return a; + } + } + + //==================================================================================================== + // testWrapperAttrAnnotationOnNonBean + //==================================================================================================== + @Test + @SuppressWarnings("unchecked") + public void testWrapperAttrAnnotationOnNonBean() throws Exception { + JsonSerializer s = JsonSerializer.DEFAULT_LAX; + JsonParser p = JsonParser.DEFAULT; + String r; + + B t = B.create(); + r = s.serialize(t); + assertEquals("{foo:'1'}", r); + t = p.parse(r, B.class); + assertEquals(1, t.f1); + + Map<String,B> m = new LinkedHashMap<String,B>(); + m.put("bar", B.create()); + r = s.serialize(m); + assertEquals("{bar:{foo:'1'}}", r); + + m = p.parseMap(r, LinkedHashMap.class, String.class, B.class); + assertEquals(1, m.get("bar").f1); + } + + @Json(wrapperAttr="foo") + public static class B { + int f1; + + static B create() { + B b = new B(); + b.f1 = 1; + return b; + } + + @Override /* Object */ + public String toString() { + return String.valueOf(f1); + } + + public static B valueOf(String s) { + B b = new B(); + b.f1 = Integer.parseInt(s); + return b; + } + } + + //==================================================================================================== + // testSubclassedList + //==================================================================================================== + @Test + public void testSubclassedList() throws Exception { + JsonSerializer s = new JsonSerializer(); + Map<String,Object> o = new HashMap<String,Object>(); + o.put("c", new C()); + assertEquals("{\"c\":[]}", s.serialize(o)); + } + + public static class C extends LinkedList<String> { + } + + //==================================================================================================== + // testEscapeSolidus + //==================================================================================================== + @Test + public void testEscapeSolidus() throws Exception { + JsonSerializer s = new JsonSerializer().setProperty(JSON_escapeSolidus, false); + String r = s.serialize("foo/bar"); + assertEquals("\"foo/bar\"", r); + r = JsonParser.DEFAULT.parse(r, String.class); + assertEquals("foo/bar", r); + + s = new JsonSerializer().setProperty(JSON_escapeSolidus, true); + r = s.serialize("foo/bar"); + assertEquals("\"foo\\/bar\"", r); + r = JsonParser.DEFAULT.parse(r, String.class); + assertEquals("foo/bar", r); + } +} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/msgpack/MsgPackSerialzierTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/msgpack/MsgPackSerialzierTest.java b/juneau-core-test/src/test/java/org/apache/juneau/msgpack/MsgPackSerialzierTest.java new file mode 100755 index 0000000..450d7ee --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/msgpack/MsgPackSerialzierTest.java @@ -0,0 +1,215 @@ +// *************************************************************************************************************************** +// * 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.juneau.msgpack; + +import static org.junit.Assert.*; + +import org.apache.juneau.*; +import org.junit.*; + +@SuppressWarnings({"javadoc"}) +public class MsgPackSerialzierTest { + + //==================================================================================================== + // testBasic + //==================================================================================================== + @Test + public void testBasic() throws Exception { + + test(null, "C0"); + + test(false, "C2"); + test(true, "C3"); + + // positive fixnum stores 7-bit positive integer + // +--------+ + // |0XXXXXXX| + // +--------+ + // + // int 8 stores a 8-bit signed integer + // +--------+--------+ + // | 0xd0 |ZZZZZZZZ| + // +--------+--------+ + // + // int 16 stores a 16-bit big-endian signed integer + // +--------+--------+--------+ + // | 0xd1 |ZZZZZZZZ|ZZZZZZZZ| + // +--------+--------+--------+ + // + // int 32 stores a 32-bit big-endian signed integer + // +--------+--------+--------+--------+--------+ + // | 0xd2 |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ| + // +--------+--------+--------+--------+--------+ + // + // int 64 stores a 64-bit big-endian signed integer + // +--------+--------+--------+--------+--------+--------+--------+--------+--------+ + // | 0xd3 |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ| + // +--------+--------+--------+--------+--------+--------+--------+--------+--------+ + // + // negative fixnum stores 5-bit negative integer + // +--------+ + // |111YYYYY| + // +--------+ + // + // * 0XXXXXXX is 8-bit unsigned integer + // * 111YYYYY is 8-bit signed integer + // + + test(0, "00"); + test(0x7F, "7F"); + + test(0x80, "D1 00 80"); + test(0x0100, "D1 01 00"); + test(0x7FFF, "D1 7F FF"); + test(0x8000, "D2 00 00 80 00"); + test(0xFFFF, "D2 00 00 FF FF"); + test(0x00010000, "D2 00 01 00 00"); + test(Long.decode("0x000000007FFFFFFF").longValue(), "D2 7F FF FF FF"); + test(Long.decode("0x0000000080000000").longValue(), "D3 00 00 00 00 80 00 00 00"); + test(Long.decode("0x0000000100000000").longValue(), "D3 00 00 00 01 00 00 00 00"); + test(Long.decode("0x7FFFFFFFFFFFFFFF").longValue(), "D3 7F FF FF FF FF FF FF FF"); + test(-Long.decode("0x7FFFFFFFFFFFFFFF").longValue(), "D3 80 00 00 00 00 00 00 01"); + test(-1, "E1"); + test(-63, "FF"); + test(-64, "D0 C0"); + + test(-0x7F, "D0 81"); + test(-0x80, "D1 FF 80"); + test(-0x0100, "D1 FF 00"); + test(-0x7FFF, "D1 80 01"); + test(-0x8000, "D2 FF FF 80 00"); + test(-0xFFFF, "D2 FF FF 00 01"); + test(-0x00010000, "D2 FF FF 00 00"); + test(-Long.decode("0x000000007FFFFFFF").longValue(), "D2 80 00 00 01"); + test(-Long.decode("0x0000000080000000").longValue(), "D3 FF FF FF FF 80 00 00 00"); + test(-Long.decode("0x0000000100000000").longValue(), "D3 FF FF FF FF 00 00 00 00"); + test(-Long.decode("0x7FFFFFFFFFFFFFFF").longValue(), "D3 80 00 00 00 00 00 00 01"); + + // float 32 stores a floating point number in IEEE 754 single precision floating point number format: + // +--------+--------+--------+--------+--------+ + // | 0xca |XXXXXXXX|XXXXXXXX|XXXXXXXX|XXXXXXXX| + // +--------+--------+--------+--------+--------+ + // + // float 64 stores a floating point number in IEEE 754 double precision floating point number format: + // +--------+--------+--------+--------+--------+--------+--------+--------+--------+ + // | 0xcb |YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY| + // +--------+--------+--------+--------+--------+--------+--------+--------+--------+ + // + // where + // * XXXXXXXX_XXXXXXXX_XXXXXXXX_XXXXXXXX is a big-endian IEEE 754 single precision floating point number. + // Extension of precision from single-precision to double-precision does not lose precision. + // * YYYYYYYY_YYYYYYYY_YYYYYYYY_YYYYYYYY_YYYYYYYY_YYYYYYYY_YYYYYYYY_YYYYYYYY is a big-endian + // IEEE 754 double precision floating point number + + test(0f, "CA 00 00 00 00"); + test(1f, "CA 3F 80 00 00"); + test(-1f, "CA BF 80 00 00"); + test(1d, "CB 3F F0 00 00 00 00 00 00"); + test(-1d, "CB BF F0 00 00 00 00 00 00"); + + // fixstr stores a byte array whose length is upto 31 bytes: + // +--------+========+ + // |101XXXXX| data | + // +--------+========+ + // + // str 8 stores a byte array whose length is upto (2^8)-1 bytes: + // +--------+--------+========+ + // | 0xd9 |YYYYYYYY| data | + // +--------+--------+========+ + // + // str 16 stores a byte array whose length is upto (2^16)-1 bytes: + // +--------+--------+--------+========+ + // | 0xda |ZZZZZZZZ|ZZZZZZZZ| data | + // +--------+--------+--------+========+ + // + // str 32 stores a byte array whose length is upto (2^32)-1 bytes: + // +--------+--------+--------+--------+--------+========+ + // | 0xdb |AAAAAAAA|AAAAAAAA|AAAAAAAA|AAAAAAAA| data | + // +--------+--------+--------+--------+--------+========+ + // + // where + // * XXXXX is a 5-bit unsigned integer which represents N + // * YYYYYYYY is a 8-bit unsigned integer which represents N + // * ZZZZZZZZ_ZZZZZZZZ is a 16-bit big-endian unsigned integer which represents N + // * AAAAAAAA_AAAAAAAA_AAAAAAAA_AAAAAAAA is a 32-bit big-endian unsigned integer which represents N + // * N is the length of data + + test("", "A0"); + test("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "BF 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61"); + test("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "D9 20 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61"); + + // fixarray stores an array whose length is upto 15 elements: + // +--------+~~~~~~~~~~~~~~~~~+ + // |1001XXXX| N objects | + // +--------+~~~~~~~~~~~~~~~~~+ + // + // array 16 stores an array whose length is upto (2^16)-1 elements: + // +--------+--------+--------+~~~~~~~~~~~~~~~~~+ + // | 0xdc |YYYYYYYY|YYYYYYYY| N objects | + // +--------+--------+--------+~~~~~~~~~~~~~~~~~+ + // + // array 32 stores an array whose length is upto (2^32)-1 elements: + // +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+ + // | 0xdd |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ| N objects | + // +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+ + // + // where + // * XXXX is a 4-bit unsigned integer which represents N + // * YYYYYYYY_YYYYYYYY is a 16-bit big-endian unsigned integer which represents N + // * ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ is a 32-bit big-endian unsigned integer which represents N + // N is the size of a array + + test(new int[0], "90"); + test(new int[]{1}, "91 01"); + test(new int[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, "9F 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01"); + test(new int[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, "DC 00 10 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01"); + + // fixmap stores a map whose length is upto 15 elements + // +--------+~~~~~~~~~~~~~~~~~+ + // |1000XXXX| N*2 objects | + // +--------+~~~~~~~~~~~~~~~~~+ + // + // map 16 stores a map whose length is upto (2^16)-1 elements + // +--------+--------+--------+~~~~~~~~~~~~~~~~~+ + // | 0xde |YYYYYYYY|YYYYYYYY| N*2 objects | + // +--------+--------+--------+~~~~~~~~~~~~~~~~~+ + // + // map 32 stores a map whose length is upto (2^32)-1 elements + // +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+ + // | 0xdf |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ| N*2 objects | + // +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+ + // + // where + // * XXXX is a 4-bit unsigned integer which represents N + // * YYYYYYYY_YYYYYYYY is a 16-bit big-endian unsigned integer which represents N + // * ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ is a 32-bit big-endian unsigned integer which represents N + // * N is the size of a map + // * odd elements in objects are keys of a map + // * the next element of a key is its associated value + + test(new ObjectMap("{}"), "80"); + test(new ObjectMap("{1:1}"), "81 A1 31 01"); + test(new ObjectMap("{1:1,2:1,3:1,4:1,5:1,6:1,7:1,8:1,9:1,a:1,b:1,c:1,d:1,e:1,f:1}"), "8F A1 31 01 A1 32 01 A1 33 01 A1 34 01 A1 35 01 A1 36 01 A1 37 01 A1 38 01 A1 39 01 A1 61 01 A1 62 01 A1 63 01 A1 64 01 A1 65 01 A1 66 01"); + test(new ObjectMap("{1:1,2:1,3:1,4:1,5:1,6:1,7:1,8:1,9:1,a:1,b:1,c:1,d:1,e:1,f:1,g:1}"), "DE 00 10 A1 31 01 A1 32 01 A1 33 01 A1 34 01 A1 35 01 A1 36 01 A1 37 01 A1 38 01 A1 39 01 A1 61 01 A1 62 01 A1 63 01 A1 64 01 A1 65 01 A1 66 01 A1 67 01"); + } + + public static class Person { + public String name = "John Smith"; + public int age = 21; + } + + private void test(Object input, String expected) throws Exception { + byte[] b = MsgPackSerializer.DEFAULT.serialize(input); + assertEquals(expected, TestUtils.toReadableBytes2(b)); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/testbeans/PrimitiveAtomicObjectsBean.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/testbeans/PrimitiveAtomicObjectsBean.java b/juneau-core-test/src/test/java/org/apache/juneau/testbeans/PrimitiveAtomicObjectsBean.java new file mode 100755 index 0000000..31fc045 --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/testbeans/PrimitiveAtomicObjectsBean.java @@ -0,0 +1,76 @@ +// *************************************************************************************************************************** +// * 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.juneau.testbeans; + +import java.util.*; +import java.util.concurrent.atomic.*; + +/** + * Test bean fields of type AtomicInteger and AtomicLong. + * Note that Jena parsers cannot handle these types, so we only test non-Jena parsers. + */ +@SuppressWarnings({"serial","javadoc"}) +public class PrimitiveAtomicObjectsBean { + + // primitive objects + public AtomicInteger poAtomicInteger; + public AtomicLong poAtomicLong; + + // uninitialized primitive objects + public AtomicInteger pouAtomicInteger; + public AtomicLong pouAtomicLong; + + // primitive object arrays + public AtomicInteger[][] poaAtomicInteger; + public AtomicLong[][] poaAtomicLong; + + // primitive object arrays + public AtomicInteger[][] poauAtomicInteger; + public AtomicLong[][] poauAtomicLong; + + // Anonymous list of primitives (types not erased on objects + public List<AtomicInteger[]> poalAtomicInteger; + public List<AtomicLong[]> poalAtomicLong; + + // Regular list of primitives (types erased on objects) + public List<AtomicInteger[]> polAtomicInteger; + public List<AtomicLong[]> polAtomicLong; + + public PrimitiveAtomicObjectsBean init() { + // primitive objects + poAtomicInteger = new AtomicInteger(1); + poAtomicLong = new AtomicLong(2); + + // primitive object arrays + poaAtomicInteger = new AtomicInteger[][]{{new AtomicInteger(1)}, {new AtomicInteger(2)}, null}; + poaAtomicLong = new AtomicLong[][]{{new AtomicLong(1)}, {new AtomicLong(2)}, null}; + + // Anonymous list of primitives + poalAtomicInteger = new ArrayList<AtomicInteger[]>() {{ + add(new AtomicInteger[]{new AtomicInteger(1)}); add(null); + }}; + poalAtomicLong = new ArrayList<AtomicLong[]>() {{ + add(new AtomicLong[]{new AtomicLong(1)}); add(null); + }}; + + // Regular list of primitives + polAtomicInteger = new ArrayList<AtomicInteger[]>(); + polAtomicInteger.add(new AtomicInteger[]{new AtomicInteger(1)}); + polAtomicInteger.add(null); + polAtomicLong = new ArrayList<AtomicLong[]>(); + polAtomicLong.add(new AtomicLong[]{new AtomicLong(1)}); + polAtomicLong.add(null); + + return this; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/testbeans/PrimitiveObjectsBean.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/testbeans/PrimitiveObjectsBean.java b/juneau-core-test/src/test/java/org/apache/juneau/testbeans/PrimitiveObjectsBean.java new file mode 100755 index 0000000..e3c221e --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/testbeans/PrimitiveObjectsBean.java @@ -0,0 +1,198 @@ +// *************************************************************************************************************************** +// * 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.juneau.testbeans; + +import java.math.*; +import java.util.*; + +@SuppressWarnings({"serial","javadoc"}) +public class PrimitiveObjectsBean { + + // primitive objects + public Boolean poBoolean; + public Byte poByte; + public Character poChar; + public Short poShort; + public Integer poInt; + public Long poLong; + public Float poFloat; + public Double poDouble; + public Number poNumber; + public BigInteger poBigInteger; + public BigDecimal poBigDecimal; + + // uninitialized primitive objects + public Boolean pouBoolean; + public Byte pouByte; + public Character pouChar; + public Short pouShort; + public Integer pouInt; + public Long pouLong; + public Float pouFloat; + public Double pouDouble; + public Number pouNumber; + public BigInteger pouBigInteger; + public BigDecimal pouBigDecimal; + + // primitive object arrays + public Boolean[][] poaBoolean; + public Byte[][] poaByte; + public Character[][] poaChar; + public Short[][] poaShort; + public Integer[][] poaInt; + public Long[][] poaLong; + public Float[][] poaFloat; + public Double[][] poaDouble; + public Number[][] poaNumber; + public BigInteger[][] poaBigInteger; + public BigDecimal[][] poaBigDecimal; + + // primitive object arrays + public Boolean[][] poauBoolean; + public Byte[][] poauByte; + public Character[][] poauChar; + public Short[][] poauShort; + public Integer[][] poauInt; + public Long[][] poauLong; + public Float[][] poauFloat; + public Double[][] poauDouble; + public Number[][] poauNumber; + public BigInteger[][] poauBigInteger; + public BigDecimal[][] poauBigDecimal; + + // Anonymous list of primitives (types not erased on objects + public List<Boolean[]> poalBoolean; + public List<Byte[]> poalByte; + public List<Character[]> poalChar; + public List<Short[]> poalShort; + public List<Integer[]> poalInt; + public List<Long[]> poalLong; + public List<Float[]> poalFloat; + public List<Double[]> poalDouble; + public List<Number[]> poalNumber; + public List<BigInteger[]> poalBigInteger; + public List<BigDecimal[]> poalBigDecimal; + + // Regular list of primitives (types erased on objects) + public List<Boolean[]> polBoolean; + public List<Byte[]> polByte; + public List<Character[]> polChar; + public List<Short[]> polShort; + public List<Integer[]> polInt; + public List<Long[]> polLong; + public List<Float[]> polFloat; + public List<Double[]> polDouble; + public List<Number[]> polNumber; + public List<BigInteger[]> polBigInteger; + public List<BigDecimal[]> polBigDecimal; + + public PrimitiveObjectsBean init() { + // primitive objects + poBoolean = true; + poByte = 1; + poChar = 'a'; + poShort = 2; + poInt = 3; + poLong = 4l; + poFloat = 5f; + poDouble = 6d; + poNumber = 7; + poBigInteger = new BigInteger("8"); + poBigDecimal = new BigDecimal("9"); + + // primitive object arrays + poaBoolean = new Boolean[][]{{true},{false},null}; + poaByte = new Byte[][]{{1},{2},null}; + poaChar = new Character[][]{{'a'},{'b'},null}; + poaShort = new Short[][]{{1},{2},null}; + poaInt = new Integer[][]{{1},{2},null}; + poaLong = new Long[][]{{1l},{2l},null}; + poaFloat = new Float[][]{{1f},{2f},null}; + poaDouble = new Double[][]{{1d},{2d},null}; + poaNumber = new Number[][]{{1},{2},null}; + poaBigInteger = new BigInteger[][]{{new BigInteger("1")}, {new BigInteger("2")}, null}; + poaBigDecimal = new BigDecimal[][]{{new BigDecimal("1")}, {new BigDecimal("2")}, null}; + + // Anonymous list of primitives + poalBoolean = new ArrayList<Boolean[]>() {{ + add(new Boolean[]{Boolean.TRUE}); add(null); + }}; + poalByte = new ArrayList<Byte[]>() {{ + add(new Byte[]{1}); add(null); + }}; + poalChar = new ArrayList<Character[]>() {{ + add(new Character[]{'a'}); add(null); + }}; + poalShort = new ArrayList<Short[]>() {{ + add(new Short[]{1}); add(null); + }}; + poalInt = new ArrayList<Integer[]>() {{ + add(new Integer[]{1}); add(null); + }}; + poalLong = new ArrayList<Long[]>() {{ + add(new Long[]{1l}); add(null); + }}; + poalFloat = new ArrayList<Float[]>() {{ + add(new Float[]{1f}); add(null); + }}; + poalDouble = new ArrayList<Double[]>() {{ + add(new Double[]{1d}); add(null); + }}; + poalNumber = new ArrayList<Number[]>() {{ + add(new Integer[]{1}); add(null); + }}; + poalBigInteger = new ArrayList<BigInteger[]>() {{ + add(new BigInteger[]{new BigInteger("1")}); add(null); + }}; + poalBigDecimal = new ArrayList<BigDecimal[]>() {{ + add(new BigDecimal[]{new BigDecimal("1")}); add(null); + }}; + + // Regular list of primitives + polBoolean = new ArrayList<Boolean[]>(); + polBoolean.add(new Boolean[]{Boolean.TRUE}); + polBoolean.add(null); + polByte = new ArrayList<Byte[]>(); + polByte.add(new Byte[]{1}); + polByte.add(null); + polChar = new ArrayList<Character[]>(); + polChar.add(new Character[]{'a'}); + polChar.add(null); + polShort = new ArrayList<Short[]>(); + polShort.add(new Short[]{1}); + polShort.add(null); + polInt = new ArrayList<Integer[]>(); + polInt.add(new Integer[]{1}); + polInt.add(null); + polLong = new ArrayList<Long[]>(); + polLong.add(new Long[]{1l}); + polLong.add(null); + polFloat = new ArrayList<Float[]>(); + polFloat.add(new Float[]{1f}); + polFloat.add(null); + polDouble = new ArrayList<Double[]>(); + polDouble.add(new Double[]{1d}); + polDouble.add(null); + polNumber = new ArrayList<Number[]>(); + polNumber.add(new Number[]{1}); + polNumber.add(null); + polBigInteger = new ArrayList<BigInteger[]>(); + polBigInteger.add(new BigInteger[]{new BigInteger("1")}); + polBigInteger.add(null); + polBigDecimal = new ArrayList<BigDecimal[]>(); + polBigDecimal.add(new BigDecimal[]{new BigDecimal("1")}); + polBigDecimal.add(null); + + return this; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/testbeans/TestURI.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/testbeans/TestURI.java b/juneau-core-test/src/test/java/org/apache/juneau/testbeans/TestURI.java new file mode 100755 index 0000000..b257cb5 --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/testbeans/TestURI.java @@ -0,0 +1,70 @@ +// *************************************************************************************************************************** +// * 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.juneau.testbeans; + +import java.net.*; +import java.net.URI; + +import org.apache.juneau.annotation.*; +import org.apache.juneau.jena.annotation.*; +import org.apache.juneau.xml.annotation.*; + +@SuppressWarnings("javadoc") +@Bean(sort=true) +public class TestURI { + @org.apache.juneau.annotation.URI + @Rdf(beanUri=true) + @Xml(format=XmlFormat.ATTR) + public String f0 = "f0/x0"; + + public URI f1, f2, f3; + + @org.apache.juneau.annotation.URI + public String f4, f5, f6; + + public URL f7; + + public TestURIb f8; + + public String fa, fb, fc, fd, fe; + + @org.apache.juneau.annotation.URI + public String getF9() { + return "f9/x9"; + } + + public TestURI() throws Exception { + f1 = new URI("f1/x1"); + f2 = new URI("/f2/x2"); + f3 = new URI("http://www.apache.org/f3/x3"); + f4 = "f4/x4"; + f5 = "/f5/x5"; + f6 = "http://www.apache.org/f6/x6"; + f7 = new URL("http://www.apache.org/f7/x7"); + f8 = new TestURIb(); + fa = "http://www.apache.org/fa/xa#MY_LABEL"; + fb = "http://www.apache.org/fb/xb?label=MY_LABEL&foo=bar"; + fc = "http://www.apache.org/fc/xc?foo=bar&label=MY_LABEL"; + fd = "http://www.apache.org/fd/xd?label2=MY_LABEL&foo=bar"; + fe = "http://www.apache.org/fe/xe?foo=bar&label2=MY_LABEL"; + } + + @org.apache.juneau.annotation.URI + public static class TestURIb { + @Override /* Object */ + public String toString() { + return "f8/x8"; + } + } +} + http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/transforms/BeanFilterTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/transforms/BeanFilterTest.java b/juneau-core-test/src/test/java/org/apache/juneau/transforms/BeanFilterTest.java new file mode 100755 index 0000000..a1d24df --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/transforms/BeanFilterTest.java @@ -0,0 +1,205 @@ +// *************************************************************************************************************************** +// * 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.juneau.transforms; + +import static org.apache.juneau.TestUtils.*; +import static org.junit.Assert.*; + +import org.apache.juneau.*; +import org.apache.juneau.annotation.*; +import org.junit.*; + +@SuppressWarnings("javadoc") +public class BeanFilterTest { + + //==================================================================================================== + // Interface bean filters + //==================================================================================================== + @Test + public void testInterfaceBeanFilters() throws Exception { + BeanSession session; + BeanMap<A3> bm; + + session = ContextFactory.create().addBeanFilters(A1.class).getBeanContext().createSession(); + bm = session.newBeanMap(A3.class); + assertEquals("f1", bm.get("f1")); + assertNull(bm.get("f2")); + assertNull(bm.get("f3")); + + session = ContextFactory.create().addBeanFilters(A2.class).getBeanContext().createSession(); + bm = session.newBeanMap(A3.class); + assertEquals("f1", bm.get("f1")); + assertEquals("f2", bm.get("f2")); + assertNull(bm.get("f3")); + } + + public static interface A1 { + public String getF1(); + } + + public static interface A2 extends A1 { + public String getF2(); + } + + public static class A3 implements A2 { + @Override /* A1 */ + public String getF1() { + return "f1"; + } + @Override /* A2 */ + public String getF2() { + return "f2"; + } + public String getF3() { + return "f3"; + } + } + + //==================================================================================================== + // Abstract class bean filters + //==================================================================================================== + @Test + public void testAbstractClassBeanFilters() throws Exception { + BeanSession session; + BeanMap<Test2> bm; + + session = ContextFactory.create().addBeanFilters(B1.class).getBeanContext().createSession(); + bm = session.newBeanMap(Test2.class); + assertEquals("f1", bm.get("f1")); + assertNull(bm.get("f2")); + assertNull(bm.get("f3")); + + session = ContextFactory.create().addBeanFilters(B2.class).getBeanContext().createSession(); + bm = session.newBeanMap(Test2.class); + assertEquals("f1", bm.get("f1")); + assertEquals("f2", bm.get("f2")); + assertNull(bm.get("f3")); + } + + public abstract static class B1 { + public abstract String getF1(); + } + + public abstract static class B2 extends B1 { + @Override /* B1 */ + public abstract String getF1(); + public abstract String getF2(); + } + + public static class Test2 extends B2 { + @Override /* B1 */ + public String getF1() { + return "f1"; + } + @Override /* B2 */ + public String getF2() { + return "f2"; + } + public String getF3() { + return "f3"; + } + } + + //==================================================================================================== + // Filtered with stop classes + //==================================================================================================== + @Test + public void testFilteredWithStopClass() throws Exception { + C3 c3 = new C3(); + assertObjectEquals("{f3:3,p3:3}", c3); + } + + public class C1 { + public int f1 = 1; + public int getP1() { return 1; } + } + + public class C2 extends C1 { + public int f2 = 2; + public int getP2() { return 2; } + } + + @Bean(stopClass=C2.class) + public class C3 extends C2 { + public int f3 = 3; + public int getP3() { return 3; } + } + + @Test + public void testFilterWithStopClassOnParentClass() throws Exception { + D3 d3 = new D3(); + assertObjectEquals("{f3:3,p3:3}", d3); + } + + public class D1 { + public int f1 = 1; + public int getP1() { return 1; } + } + + @Bean(stopClass=D2.class) + public class D2 extends D1 { + public int f2 = 2; + public int getP2() { return 2; } + } + + public class D3 extends D2 { + public int f3 = 3; + public int getP3() { return 3; } + } + + @Test + public void testFilteredWithStopClassOnParentClassWithOverriddenAnnotation() throws Exception { + E3 e3 = new E3(); + assertObjectEquals("{f3:3,p3:3}", e3); + } + + public class E1 { + public int f1 = 1; + public int getP1() { return 1; } + } + + @Bean(stopClass=E2.class) + public class E2 extends E1 { + public int f2 = 2; + public int getP2() { return 2; } + } + + @Bean(excludeProperties="foo") + public class E3 extends E2 { + public int f3 = 3; + public int getP3() { return 3; } + } + + @Test + public void testFilteredWithStopClassesAtMulitpleLevels() throws Exception { + F3 e3 = new F3(); + assertObjectEquals("{f3:3,p3:3}", e3); + } + + @Bean(stopClass=F1.class) + public class F1 { + public int f1 = 1; + public int getP1() { return 1; } + } + + public class F2 extends F1 { + public int f2 = 2; + public int getP2() { return 2; } + } + + @Bean(stopClass=F2.class) + public class F3 extends F2 { + public int f3 = 3; + public int getP3() { return 3; } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/transforms/BeanMapTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/transforms/BeanMapTest.java b/juneau-core-test/src/test/java/org/apache/juneau/transforms/BeanMapTest.java new file mode 100755 index 0000000..0f44984 --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/transforms/BeanMapTest.java @@ -0,0 +1,97 @@ +// *************************************************************************************************************************** +// * 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.juneau.transforms; + +import static org.junit.Assert.*; + +import org.apache.juneau.*; +import org.apache.juneau.transform.*; +import org.junit.*; + +@SuppressWarnings("javadoc") +public class BeanMapTest { + + //==================================================================================================== + // testFilteredEntry + //==================================================================================================== + @Test + public void testFilteredEntry() throws Exception { + BeanSession session = ContextFactory.create().addPojoSwaps(ByteArrayBase64Swap.class).getBeanContext().createSession(); + BeanMap<A> m = session.toBeanMap(new A()); + + assertEquals("AQID", m.get("f1")); + m.put("f1", "BAUG"); + assertEquals("BAUG", m.get("f1")); + assertEquals(4, m.getBean().f1[0]); + + assertNull(m.get("f3")); + } + + public static class A { + public byte[] f1 = new byte[]{1,2,3}; + public byte[] f3 = null; + } + + //==================================================================================================== + // testFilteredEntryWithMultipleMatchingFilters + // When bean properties can have multiple filters applied to them, pick the first match. + //==================================================================================================== + @Test + public void testFilteredEntryWithMultipleMatchingFilters() throws Exception { + BeanSession session = ContextFactory.create().addPojoSwaps(B2Swap.class,B1Swap.class).getBeanContext().createSession(); + BeanMap<B> bm = session.toBeanMap(B.create()); + ObjectMap om = (ObjectMap)bm.get("b1"); + assertEquals("b2", om.getString("type")); + + session = ContextFactory.create().addPojoSwaps(B1Swap.class,B2Swap.class).getBeanContext().createSession(); + bm = session.toBeanMap(B.create()); + om = (ObjectMap)bm.get("b1"); + assertEquals("b1", om.getString("type")); + } + + + public static class B { + public B1 b1; + + static B create() { + B b = new B(); + B2 b2 = new B2(); + b2.f1 = "f1"; + b2.f2 = "f2"; + b.b1 = b2; + return b; + } + } + + public static class B1 { + public String f1; + } + + public static class B2 extends B1 { + public String f2; + } + + public static class B1Swap extends MapSwap<B1> { + @Override /* PojoSwap */ + public ObjectMap swap(BeanSession session, B1 b1) { + return new ObjectMap().append("type", "b1").append("f1", b1.f1); + } + } + + public static class B2Swap extends MapSwap<B2> { + @Override /* PojoSwap */ + public ObjectMap swap(BeanSession session, B2 b2) { + return new ObjectMap().append("type", "b2").append("f1", b2.f1); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e4dfdf81/juneau-core-test/src/test/java/org/apache/juneau/transforms/ByteArrayBase64SwapTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/transforms/ByteArrayBase64SwapTest.java b/juneau-core-test/src/test/java/org/apache/juneau/transforms/ByteArrayBase64SwapTest.java new file mode 100755 index 0000000..ab8565f --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/transforms/ByteArrayBase64SwapTest.java @@ -0,0 +1,175 @@ +// *************************************************************************************************************************** +// * 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.juneau.transforms; + +import static org.apache.juneau.serializer.SerializerContext.*; +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.juneau.*; +import org.apache.juneau.a.rttests.*; +import org.apache.juneau.json.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.serializer.*; +import org.junit.*; + +@SuppressWarnings({"unchecked","serial","javadoc"}) +public class ByteArrayBase64SwapTest extends RoundTripTest { + + public ByteArrayBase64SwapTest(String label, Serializer s, Parser p, int flags) throws Exception { + super(label, s, p, flags); + } + + @Override /* RoundTripTest */ + public Class<?>[] getPojoSwaps() { + return new Class<?>[] { + ByteArrayBase64Swap.class + }; + } + + //==================================================================================================== + // testPrimitiveArrays + //==================================================================================================== + @Test + public void testPrimitiveArrays() throws Exception { + WriterSerializer s = new JsonSerializer.Simple().addPojoSwaps(ByteArrayBase64Swap.class).setProperty(SERIALIZER_trimNullProperties, false); + + byte[] a1 = {1,2,3}; + assertEquals("'AQID'", s.serialize(a1)); + a1 = roundTrip(a1, byte[].class); + assertEquals(1, a1[0]); + + byte[][] a2 = {{1,2,3},{4,5,6},null}; + assertEquals("['AQID','BAUG',null]", s.serialize(a2)); + a2 = roundTrip(a2, byte[][].class); + assertEquals(1, a2[0][0]); + assertNull(a2[2]); + + byte[] a3 = null; + assertEquals("null", s.serialize(a3)); + a3 = roundTrip(a3, byte[].class); + assertNull(a3); + + if (p == null) + return; + + BeanSession session = p.getBeanContext().createSession(); + + List<byte[]> fl = new ArrayList<byte[]>() {{ + add(new byte[]{1,2,3}); + add(new byte[]{4,5,6}); + add(null); + }}; + assertEquals("['AQID','BAUG',null]", s.serialize(fl)); + fl = roundTrip(fl, session.getCollectionClassMeta(List.class, byte[].class)); + assertEquals(1, fl.get(0)[0]); + assertEquals(5, fl.get(1)[1]); + assertNull(fl.get(2)); + + Map<String,byte[]> fm = new LinkedHashMap<String,byte[]>() {{ + put("foo", new byte[]{1,2,3}); + put("bar", null); + put(null, new byte[]{4,5,6}); + put("null", new byte[]{7,8,9}); + }}; + fm = roundTrip(fm, session.getMapClassMeta(Map.class, String.class, byte[].class)); + assertEquals(1, fm.get("foo")[0]); + assertNull(fm.get(1)); + assertEquals(5, fm.get(null)[1]); + assertEquals(8, fm.get("null")[1]); + } + + //==================================================================================================== + // testBean + //==================================================================================================== + @Test + public void testBean() throws Exception { + A t = new A().init(); + t = roundTrip(t, A.class); + assertEquals(1, t.f1[0]); + assertEquals(4, t.f2[1][0]); + assertNull(t.f2[2]); + assertNull(t.f3); + assertEquals(1, t.fl.get(0)[0]); + assertNull(t.fl.get(2)); + assertEquals(1, t.fm.get("foo")[0]); + assertNull(t.fm.get("bar")); + assertEquals(4, t.fm.get(null)[0]); + assertEquals(1, t.flb.get(0).fl.get(0)[0]); + assertNull(t.flb.get(1)); + assertEquals(1, t.fmb.get("foo").fl.get(0)[0]); + assertNull(t.fmb.get("bar")); + } + + public static class A { + public byte[] f1; + public byte[][] f2; + public byte[] f3; + public List<byte[]> fl; + public Map<String,byte[]> fm; + public List<B> flb; + public Map<String,B> fmb; + + public A init() { + f1 = new byte[]{1,2,3}; + f2 = new byte[][]{{1,2,3},{4,5,6},null}; + f3 = null; + fl = new ArrayList<byte[]>() {{ + add(new byte[]{1,2,3}); + add(new byte[]{4,5,6}); + add(null); + }}; + fm = new LinkedHashMap<String,byte[]>() {{ + put("foo", new byte[]{1,2,3}); + put("bar", null); + put(null, new byte[]{4,5,6}); + }}; + flb = new ArrayList<B>() {{ + add(new B().init()); + add(null); + }}; + fmb = new LinkedHashMap<String,B>() {{ + put("foo", new B().init()); + put("bar", null); + put(null, new B().init()); + }}; + return this; + } + } + + public static class B { + public byte[] f1; + public byte[][] f2; + public byte[] f3; + public List<byte[]> fl; + public Map<String,byte[]> fm; + + public B init() { + f1 = new byte[]{1,2,3}; + f2 = new byte[][]{{1,2,3},{4,5,6},null}; + f3 = null; + fl = new ArrayList<byte[]>() {{ + add(new byte[]{1,2,3}); + add(new byte[]{4,5,6}); + add(null); + }}; + fm = new LinkedHashMap<String,byte[]>() {{ + put("foo", new byte[]{1,2,3}); + put("bar", null); + put(null, new byte[]{4,5,6}); + }}; + return this; + } + } +} \ No newline at end of file
