1 | package com.framsticks.params; |
---|
2 | |
---|
3 | import java.util.Arrays; |
---|
4 | |
---|
5 | import org.testng.annotations.BeforeClass; |
---|
6 | import org.testng.annotations.Test; |
---|
7 | |
---|
8 | import com.framsticks.params.types.EventParam; |
---|
9 | import com.framsticks.params.types.ProcedureParam; |
---|
10 | import com.framsticks.params.types.StringParam; |
---|
11 | import com.framsticks.parsers.Savers; |
---|
12 | import com.framsticks.structure.messages.ValueChange; |
---|
13 | import com.framsticks.test.TestClass; |
---|
14 | import com.framsticks.test.TestConfiguration; |
---|
15 | import com.framsticks.util.lang.Holder; |
---|
16 | |
---|
17 | import static org.fest.assertions.Assertions.*; |
---|
18 | import static com.framsticks.params.ParamsUtil.arguments; |
---|
19 | import static com.framsticks.params.ParamsUtil.getParam; |
---|
20 | |
---|
21 | @Test |
---|
22 | public class FramsClassBuilderTest extends TestConfiguration { |
---|
23 | |
---|
24 | FramsClass framsClass; |
---|
25 | ReflectionAccess access; |
---|
26 | TestClass test; |
---|
27 | |
---|
28 | @BeforeClass |
---|
29 | public void setUp() { |
---|
30 | test = new TestClass(); |
---|
31 | test.setName("test"); |
---|
32 | test.appendHistory("first"); |
---|
33 | framsClass = FramsClass.build().forClass(TestClass.class); |
---|
34 | } |
---|
35 | |
---|
36 | @Test |
---|
37 | public void checkProcedureParams() { |
---|
38 | assertThat(framsClass.getParamCount()).isEqualTo(8); |
---|
39 | |
---|
40 | assertThat(framsClass.getParam("name")).isInstanceOf(StringParam.class); |
---|
41 | assertThat(framsClass.getParam("history")).isInstanceOf(StringParam.class); |
---|
42 | assertThat(framsClass.getParam("history_changed")).isInstanceOf(EventParam.class); |
---|
43 | |
---|
44 | assertThat(framsClass.getParam("appendHistory")).isInstanceOf(ProcedureParam.class); |
---|
45 | assertThat(framsClass.getParam("resetHistory")).isInstanceOf(ProcedureParam.class); |
---|
46 | |
---|
47 | |
---|
48 | ProcedureParam appendHistory = getParam(framsClass, "appendHistory", ProcedureParam.class); |
---|
49 | assertThat(appendHistory.getArgumentsType().size()).isEqualTo(1); |
---|
50 | assertThat(appendHistory.getArgumentsType().get(0).getId()).isEqualTo("arg0"); |
---|
51 | } |
---|
52 | |
---|
53 | @Test(dependsOnMethods = "checkProcedureParams") |
---|
54 | public void print() { |
---|
55 | assertThat(Savers.saveFramsClass(new ListSink(), framsClass).getOut()).isEqualTo( |
---|
56 | Arrays.asList( |
---|
57 | "class:", |
---|
58 | "name:TestClass", |
---|
59 | "id:TestClass", |
---|
60 | "", |
---|
61 | "prop:", |
---|
62 | "id:name", |
---|
63 | "name:Name", |
---|
64 | "type:s", |
---|
65 | "", |
---|
66 | "prop:", |
---|
67 | "id:history", |
---|
68 | "name:History", |
---|
69 | "type:s", |
---|
70 | "", |
---|
71 | "prop:", |
---|
72 | "id:history_changed", |
---|
73 | "name:HistoryListener", |
---|
74 | "type:e ValueChange", |
---|
75 | "", |
---|
76 | "prop:", |
---|
77 | "id:appendHistory", |
---|
78 | "name:AppendHistory", |
---|
79 | "type:p d(s arg0)", |
---|
80 | "", |
---|
81 | "prop:", |
---|
82 | "id:resetHistory", |
---|
83 | "name:ResetHistory", |
---|
84 | "type:p()", |
---|
85 | "", |
---|
86 | "prop:", |
---|
87 | "id:children", |
---|
88 | "name:Children", |
---|
89 | "type:l TestChild uid", |
---|
90 | "flags:1", |
---|
91 | "", |
---|
92 | "prop:", |
---|
93 | "id:createChild", |
---|
94 | "name:CreateChild", |
---|
95 | "type:p(s arg0)", |
---|
96 | "", |
---|
97 | "prop:", |
---|
98 | "id:children_changed", |
---|
99 | "name:ChildrenListener", |
---|
100 | "type:e ListChange", |
---|
101 | "" |
---|
102 | ) |
---|
103 | ); |
---|
104 | } |
---|
105 | |
---|
106 | @Test(dependsOnMethods = "print") |
---|
107 | public void createAccess() { |
---|
108 | access = new ReflectionAccess(TestClass.class, framsClass); |
---|
109 | access.select(test); |
---|
110 | } |
---|
111 | |
---|
112 | @Test(dependsOnMethods = "createAccess") |
---|
113 | public void callProcedures() { |
---|
114 | |
---|
115 | assertThat(access.get("history", String.class)).isEqualTo("initial|first|"); |
---|
116 | Object result = access.call("appendHistory", arguments("second")); |
---|
117 | |
---|
118 | assertThat(result).isInstanceOf(Integer.class); |
---|
119 | assertThat(result).isEqualTo(21); |
---|
120 | assertThat(access.get("history", String.class)).isEqualTo("initial|first|second|"); |
---|
121 | |
---|
122 | result = access.call("resetHistory"); |
---|
123 | assertThat(result).isNull(); |
---|
124 | |
---|
125 | assertThat(access.get("history", String.class)).isEqualTo(""); |
---|
126 | } |
---|
127 | |
---|
128 | @Test(dependsOnMethods = "callProcedures") |
---|
129 | public void listeners() { |
---|
130 | |
---|
131 | final Holder<String> called = new Holder<>(); |
---|
132 | |
---|
133 | final EventListener<ValueChange> listener = new EventListener<ValueChange>() { |
---|
134 | |
---|
135 | @Override |
---|
136 | public void action(ValueChange argument) { |
---|
137 | called.set(argument.value.toString()); |
---|
138 | } |
---|
139 | }; |
---|
140 | |
---|
141 | final EventParam eventParam = getParam(access, "history_changed", EventParam.class); |
---|
142 | access.reg(eventParam, listener); |
---|
143 | |
---|
144 | final String currentHistory = access.get("history", String.class); |
---|
145 | final String addition = "test"; |
---|
146 | |
---|
147 | access.call("appendHistory", arguments(addition)); |
---|
148 | |
---|
149 | String expected = currentHistory + addition + "|"; |
---|
150 | assertThat(access.get("history", String.class)).isEqualTo(expected); |
---|
151 | assertThat(called.get()).isEqualTo(expected); |
---|
152 | access.regRemove(eventParam, listener); |
---|
153 | } |
---|
154 | |
---|
155 | } |
---|