1 | package com.framsticks.params; |
---|
2 | |
---|
3 | import static org.fest.assertions.Assertions.*; |
---|
4 | |
---|
5 | import java.util.Arrays; |
---|
6 | import java.util.Map; |
---|
7 | import java.util.TreeMap; |
---|
8 | |
---|
9 | import org.testng.annotations.DataProvider; |
---|
10 | import org.testng.annotations.Test; |
---|
11 | |
---|
12 | import com.framsticks.params.AccessOperations; |
---|
13 | import com.framsticks.params.FramsClass; |
---|
14 | import com.framsticks.params.ListSink; |
---|
15 | import com.framsticks.params.ReflectionAccess; |
---|
16 | import com.framsticks.params.types.UniversalParam; |
---|
17 | import com.framsticks.parsers.Savers; |
---|
18 | import com.framsticks.test.TestConfiguration; |
---|
19 | import com.framsticks.test.TestSerializedClass; |
---|
20 | import com.framsticks.util.lang.Containers; |
---|
21 | import com.framsticks.util.lang.Pair; |
---|
22 | |
---|
23 | |
---|
24 | public class ParamsUtilTest extends TestConfiguration { |
---|
25 | |
---|
26 | @Test |
---|
27 | public void testVariousSerializations() { |
---|
28 | FramsClass framsClass; |
---|
29 | ReflectionAccess access; |
---|
30 | |
---|
31 | framsClass = FramsClass.build().forClass(TestSerializedClass.class); |
---|
32 | assertThat(framsClass.getParamCount()).isEqualTo(1); |
---|
33 | assertThat(framsClass.getParam("theArray")).isInstanceOf(UniversalParam.class); |
---|
34 | |
---|
35 | access = new ReflectionAccess(TestSerializedClass.class, framsClass); |
---|
36 | |
---|
37 | assertThat(Savers.saveFramsClass(new ListSink(), framsClass).getOut()).isEqualTo( |
---|
38 | Arrays.asList( |
---|
39 | "class:", |
---|
40 | "name:TestSerializedClass", |
---|
41 | "id:TestSerializedClass", |
---|
42 | "", |
---|
43 | "prop:", |
---|
44 | "id:theArray", |
---|
45 | "name:TheArray", |
---|
46 | "type:x", |
---|
47 | "" |
---|
48 | ) |
---|
49 | ); |
---|
50 | |
---|
51 | TestSerializedClass test = new TestSerializedClass(); |
---|
52 | test.getTheArray().add(1); |
---|
53 | test.getTheArray().add(3.0); |
---|
54 | test.getTheArray().add("word"); |
---|
55 | |
---|
56 | ListSink sink = new ListSink(); |
---|
57 | AccessOperations.savePrimitives(access.select(test), sink); |
---|
58 | assertThat(sink.getOut()).isEqualTo(Arrays.asList( |
---|
59 | "TestSerializedClass:", |
---|
60 | "theArray:@Serialized:[1,3.0,\"word\"]", |
---|
61 | "" |
---|
62 | )); |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | Map<String, Object> f12 = new TreeMap<String, Object>(); |
---|
67 | f12.put("a", 123); |
---|
68 | f12.put("b", Arrays.asList(7, 8, 9)); |
---|
69 | assertThat(ParamsUtil.serialize(f12)).isEqualTo("@Serialized:{\"a\":123,\"b\":[7,8,9]}"); |
---|
70 | |
---|
71 | // List<Object> f18 = new ArrayList<>(); |
---|
72 | // List<Object> f18_0 = new ArrayList<>(); |
---|
73 | // List<Object> f18_1 = Arrays.asList("x", f18); |
---|
74 | // List<Object> f18_2 = new ArrayList<>(); |
---|
75 | // f18_2.add(f18_0); |
---|
76 | // f18_0.add(f18_2); |
---|
77 | // f18.add(f18_0); |
---|
78 | // f18.add(f18_1); |
---|
79 | // f18.add(f18_2); |
---|
80 | |
---|
81 | // root[0] is f18_0 |
---|
82 | // root[0]==root[2][0], root[1][1]==root, root[1][1][1][1][0]=="x". |
---|
83 | |
---|
84 | // assertThat(ParamsUtil.serialize(f18)).isEqualTo("@Serialized:[[^3],[\"x\",^0],[^1]]"); |
---|
85 | |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | @Test(dependsOnMethods = "testVariousSerializations", dataProvider = "serializationData") |
---|
90 | public void testSerialization(Object object, String string) { |
---|
91 | assertThat(ParamsUtil.serialize(object)).isEqualTo(string); |
---|
92 | } |
---|
93 | |
---|
94 | @Test(dependsOnMethods = "testSerialization", dataProvider = "serializationData") |
---|
95 | public void testDeserialization(Object object, String string) { |
---|
96 | assertThat(ParamsUtil.deserialize(string, Object.class)).isEqualTo(object); |
---|
97 | } |
---|
98 | |
---|
99 | @DataProvider |
---|
100 | public Object[][] serializationData() { |
---|
101 | Object array = Arrays.asList(1, 2); |
---|
102 | |
---|
103 | return new Object[][] { |
---|
104 | { 2, "2" }, |
---|
105 | { 0.5, "0.5" }, |
---|
106 | { "xx", "xx" }, |
---|
107 | { "@Serialized:", "@Serialized:\"@Serialized:\"" }, |
---|
108 | { Arrays.asList(12, null, "abc"), "@Serialized:[12,null,\"abc\"]" }, |
---|
109 | { Arrays.asList(Arrays.asList(Arrays.asList())), "@Serialized:[[[]]]" }, |
---|
110 | { Arrays.asList(1, Containers.buildMap(Pair.make("a", 2), Pair.make("b", "xx"), Pair.make("c", null)), "y"), "@Serialized:[1,{\"a\":2,\"b\":\"xx\",\"c\":null},\"y\"]" }, |
---|
111 | { new OpaqueObject("Population", 0xaabbccddL), "@Serialized:Population<0xaabbccdd>" }, |
---|
112 | { Arrays.asList("x", new OpaqueObject("Population", 0xaabbccddL)), "@Serialized:[\"x\",Population<0xaabbccdd>]" }, |
---|
113 | { Containers.buildMap(Pair.make("a", array), Pair.make("b", array)), "@Serialized:{\"a\":[1,2],\"b\":^1}"} |
---|
114 | }; |
---|
115 | } |
---|
116 | |
---|
117 | } |
---|