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.Test; |
---|
10 | |
---|
11 | import com.framsticks.params.AccessOperations; |
---|
12 | import com.framsticks.params.FramsClass; |
---|
13 | import com.framsticks.params.ListSink; |
---|
14 | import com.framsticks.params.ReflectionAccess; |
---|
15 | import com.framsticks.params.types.UniversalParam; |
---|
16 | import com.framsticks.parsers.Savers; |
---|
17 | import com.framsticks.test.TestConfiguration; |
---|
18 | import com.framsticks.test.TestSerializedClass; |
---|
19 | |
---|
20 | |
---|
21 | public class ParamsUtilTest extends TestConfiguration { |
---|
22 | |
---|
23 | @Test |
---|
24 | public void testSerialization() { |
---|
25 | FramsClass framsClass; |
---|
26 | ReflectionAccess access; |
---|
27 | |
---|
28 | framsClass = FramsClass.build().forClass(TestSerializedClass.class); |
---|
29 | assertThat(framsClass.getParamCount()).isEqualTo(1); |
---|
30 | assertThat(framsClass.getParam("theArray")).isInstanceOf(UniversalParam.class); |
---|
31 | |
---|
32 | access = new ReflectionAccess(TestSerializedClass.class, framsClass); |
---|
33 | |
---|
34 | assertThat(Savers.saveFramsClass(new ListSink(), framsClass).getOut()).isEqualTo( |
---|
35 | Arrays.asList( |
---|
36 | "class:", |
---|
37 | "name:TestSerializedClass", |
---|
38 | "id:TestSerializedClass", |
---|
39 | "", |
---|
40 | "prop:", |
---|
41 | "id:theArray", |
---|
42 | "name:TheArray", |
---|
43 | "type:x", |
---|
44 | "" |
---|
45 | ) |
---|
46 | ); |
---|
47 | |
---|
48 | TestSerializedClass test = new TestSerializedClass(); |
---|
49 | test.getTheArray().add(1); |
---|
50 | test.getTheArray().add(3.0); |
---|
51 | test.getTheArray().add("word"); |
---|
52 | |
---|
53 | ListSink sink = new ListSink(); |
---|
54 | AccessOperations.savePrimitives(access.select(test), sink); |
---|
55 | assertThat(sink.getOut()).isEqualTo(Arrays.asList( |
---|
56 | "TestSerializedClass:", |
---|
57 | "theArray:@Serialized:[1,3.0,\"word\"]", |
---|
58 | "" |
---|
59 | )); |
---|
60 | |
---|
61 | |
---|
62 | assertThat(ParamsUtil.serialize("@Serialized:")).isEqualTo("@Serialized:\"@Serialized:\""); |
---|
63 | assertThat(ParamsUtil.serialize(Arrays.asList(12, null, "abc"))).isEqualTo("@Serialized:[12,null,\"abc\"]"); |
---|
64 | assertThat(ParamsUtil.serialize(Arrays.asList(Arrays.asList(Arrays.asList())))).isEqualTo("@Serialized:[[[]]]"); |
---|
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 | } |
---|