1 | package com.framsticks.hosting; |
---|
2 | |
---|
3 | // import org.apache.log4j.Logger; |
---|
4 | import java.util.Arrays; |
---|
5 | import java.util.LinkedList; |
---|
6 | import java.util.List; |
---|
7 | |
---|
8 | import org.testng.annotations.Test; |
---|
9 | |
---|
10 | import com.framsticks.core.XmlBasedTest; |
---|
11 | import com.framsticks.remote.RemoteTree; |
---|
12 | import com.framsticks.structure.LocalTree; |
---|
13 | import com.framsticks.structure.Path; |
---|
14 | import com.framsticks.structure.Tree; |
---|
15 | import com.framsticks.structure.TreeOperations; |
---|
16 | import com.framsticks.structure.messages.ListChange; |
---|
17 | import com.framsticks.structure.messages.Result; |
---|
18 | import com.framsticks.structure.messages.ValueChange; |
---|
19 | |
---|
20 | import com.framsticks.test.TestClass; |
---|
21 | import com.framsticks.params.FramsClass; |
---|
22 | import com.framsticks.params.Access; |
---|
23 | import com.framsticks.params.EventListener; |
---|
24 | import com.framsticks.params.PrimitiveParam; |
---|
25 | import com.framsticks.params.PropertiesAccess; |
---|
26 | import com.framsticks.params.types.EventParam; |
---|
27 | import com.framsticks.params.types.StringParam; |
---|
28 | // import com.framsticks.params.types.EventParam; |
---|
29 | import com.framsticks.params.types.ProcedureParam; |
---|
30 | import com.framsticks.util.dispatching.Dispatching.Waiter; |
---|
31 | import com.framsticks.util.dispatching.Future; |
---|
32 | import com.framsticks.util.dispatching.RunAt; |
---|
33 | |
---|
34 | import static com.framsticks.params.ParamsUtil.arguments; |
---|
35 | |
---|
36 | import static org.fest.assertions.Assertions.*; |
---|
37 | import static com.framsticks.params.ParamsUtil.getParam; |
---|
38 | import static com.framsticks.structure.TreeOperations.*; |
---|
39 | |
---|
40 | @Test |
---|
41 | public class ServerTest extends XmlBasedTest { |
---|
42 | |
---|
43 | protected RemoteTree remote; |
---|
44 | protected FramsClass remoteTestFramsClass; |
---|
45 | protected Path remotePath; |
---|
46 | |
---|
47 | protected Server server; |
---|
48 | protected LocalTree hosted; |
---|
49 | protected TestClass hostedObject; |
---|
50 | protected EventListener<ValueChange> listener; |
---|
51 | protected EventListener<ListChange> childListener; |
---|
52 | |
---|
53 | protected List<String> listenerArguments = new LinkedList<>(); |
---|
54 | protected List<ListChange> childrenChanges = new LinkedList<>(); |
---|
55 | |
---|
56 | |
---|
57 | |
---|
58 | @Test |
---|
59 | public void runServer() { |
---|
60 | assertThat(framsticks.size()).isEqualTo(2); |
---|
61 | assertThat(framsticks.get("test")).isInstanceOf(Server.class); |
---|
62 | assertThat(framsticks.get("remote")).isInstanceOf(RemoteTree.class); |
---|
63 | |
---|
64 | server = (Server) framsticks.get("test"); |
---|
65 | remote = (RemoteTree) framsticks.get("remote"); |
---|
66 | assertThat(server.getHosted()).isInstanceOf(LocalTree.class); |
---|
67 | hosted = (LocalTree) server.getHosted(); |
---|
68 | assertThat(hosted.getRootObject()).isInstanceOf(TestClass.class); |
---|
69 | hostedObject = hosted.getRootObject(TestClass.class); |
---|
70 | } |
---|
71 | |
---|
72 | @Test(dependsOnMethods = "runServer") |
---|
73 | public void fetchInfo() { |
---|
74 | final Waiter waiter = produceWaiter(1.0); |
---|
75 | |
---|
76 | TreeOperations.tryGet(remote, "/testClass", new Future<Path>(failOnException) { |
---|
77 | @Override |
---|
78 | protected void result(Path path) { |
---|
79 | assertThat(path.isResolved()).isTrue(); |
---|
80 | remoteTestFramsClass = bindAccess(path).getFramsClass(); |
---|
81 | assertThat(remoteTestFramsClass.getName()).isEqualTo("TestClass"); |
---|
82 | waiter.pass(); |
---|
83 | } |
---|
84 | }); |
---|
85 | |
---|
86 | } |
---|
87 | |
---|
88 | @Test(dependsOnMethods = "fetchInfo") |
---|
89 | public void resolveAndfetchRootObject() { |
---|
90 | final Waiter waiter = produceWaiter(1.0); |
---|
91 | |
---|
92 | TreeOperations.tryGet(remote, "/testClass", new Future<Path>(failOnException) { |
---|
93 | @Override |
---|
94 | protected void result(Path path) { |
---|
95 | assertThat(path.isResolved()).isTrue(); |
---|
96 | remotePath = path; |
---|
97 | Access access = bindAccess(path); |
---|
98 | assertThat(access).isInstanceOf(PropertiesAccess.class); |
---|
99 | assertThat(access.get("name", String.class)).isEqualTo("a test name"); |
---|
100 | waiter.pass(); |
---|
101 | } |
---|
102 | }); |
---|
103 | } |
---|
104 | |
---|
105 | @Test(dependsOnMethods = "resolveAndfetchRootObject") |
---|
106 | public void setValueName() { |
---|
107 | final Waiter waiter = produceWaiter(2.0); |
---|
108 | |
---|
109 | set(remotePath, getParam(remoteTestFramsClass, "name", PrimitiveParam.class), "new name", new Future<Integer>(failOnException) { |
---|
110 | @Override |
---|
111 | protected void result(Integer flags) { |
---|
112 | // assertThat(flags).isEqualTo(0); |
---|
113 | /** And now check directly whether it was really set. */ |
---|
114 | hosted.dispatch(new RunAt<Tree>(failOnException) { |
---|
115 | @Override |
---|
116 | protected void runAt() { |
---|
117 | assertThat(hostedObject.getName()).isEqualTo("new name"); |
---|
118 | waiter.pass(); |
---|
119 | } |
---|
120 | }); |
---|
121 | } |
---|
122 | }); |
---|
123 | } |
---|
124 | |
---|
125 | @Test(dependsOnMethods = "setValueName") |
---|
126 | public void registerListener() { |
---|
127 | final Waiter waiter = produceWaiter(1.0); |
---|
128 | listener = new EventListener<ValueChange>() { |
---|
129 | |
---|
130 | @Override |
---|
131 | public void action(ValueChange argument) { |
---|
132 | listenerArguments.add(argument.value.toString()); |
---|
133 | } |
---|
134 | }; |
---|
135 | |
---|
136 | TreeOperations.tryGet(remote, "/cli/events", new Future<Path>(failOnException) { |
---|
137 | @Override |
---|
138 | protected void result(Path path) { |
---|
139 | waiter.pass(); |
---|
140 | } |
---|
141 | }); |
---|
142 | |
---|
143 | addListener(remotePath, getParam(remoteTestFramsClass, "history_changed", EventParam.class), listener, ValueChange.class, produceWaiter(1.0).passInFuture(Void.class)); |
---|
144 | } |
---|
145 | |
---|
146 | @Test(dependsOnMethods = "registerListener") |
---|
147 | public void callMethod() { |
---|
148 | final Waiter waiter = produceWaiter(2.0); |
---|
149 | |
---|
150 | call(remotePath, getParam(remoteTestFramsClass, "resetHistory", ProcedureParam.class), arguments(), Object.class, produceWaiter(2.0).passInFuture(Object.class)); |
---|
151 | |
---|
152 | call(remotePath, getParam(remoteTestFramsClass, "appendHistory", ProcedureParam.class), arguments("next word"), Result.class, new Future<Result>(failOnException) { |
---|
153 | @Override |
---|
154 | protected void result(final Result result) { |
---|
155 | hosted.dispatch(new RunAt<Tree>(failOnException) { |
---|
156 | @Override |
---|
157 | protected void runAt() { |
---|
158 | // assert |
---|
159 | |
---|
160 | assertThat(hostedObject.getHistory()).isEqualTo("next word|"); |
---|
161 | waiter.pass(); |
---|
162 | } |
---|
163 | }); |
---|
164 | } |
---|
165 | }); |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | @Test(dependsOnMethods = "callMethod") |
---|
170 | public void deregisterListener() { |
---|
171 | removeListener(remotePath, getParam(remoteTestFramsClass, "history_changed", EventParam.class), listener, produceWaiter(1.0).passInFuture(Void.class)); |
---|
172 | |
---|
173 | assertThat(listenerArguments).isEqualTo(Arrays.asList("", "next word|")); |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | @Test(dependsOnMethods = "deregisterListener") |
---|
178 | public void registerChildListener() { |
---|
179 | |
---|
180 | childListener = new EventListener<ListChange>() { |
---|
181 | @Override |
---|
182 | public void action(ListChange listChange) { |
---|
183 | childrenChanges.add(listChange); |
---|
184 | } |
---|
185 | }; |
---|
186 | |
---|
187 | addListener(remotePath, getParam(remoteTestFramsClass, "children_changed", EventParam.class), childListener, ListChange.class, produceWaiter(1.0).passInFuture(Void.class)); |
---|
188 | } |
---|
189 | |
---|
190 | @Test(dependsOnMethods = "registerChildListener") |
---|
191 | public void createChild() { |
---|
192 | final Waiter waiter = produceWaiter(2.0); |
---|
193 | call(remotePath, "createChild", arguments("a child"), Object.class, produceWaiter(2.0).passInFuture(Object.class)); |
---|
194 | call(remotePath, "createChild", arguments("another child"), Object.class, produceWaiter(2.0).passInFuture(Object.class)); |
---|
195 | |
---|
196 | tryGet(remote, "/testClass/children/c0", new Future<Path>(failOnException) { |
---|
197 | |
---|
198 | @Override |
---|
199 | protected void result(Path result) { |
---|
200 | set(result, getParam(result, "name", StringParam.class), "new_name", new Future<Integer>(failOnException) { |
---|
201 | |
---|
202 | @Override |
---|
203 | protected void result(Integer result) { |
---|
204 | waiter.pass(); |
---|
205 | } |
---|
206 | }); |
---|
207 | } |
---|
208 | }); |
---|
209 | } |
---|
210 | |
---|
211 | @Test(dependsOnMethods = "createChild") |
---|
212 | public void deregisterChildListener() { |
---|
213 | removeListener(remotePath, getParam(remoteTestFramsClass, "children_changed", EventParam.class), childListener, produceWaiter(1.0).passInFuture(Void.class)); |
---|
214 | } |
---|
215 | |
---|
216 | @Test(dependsOnMethods = "deregisterChildListener") |
---|
217 | public void checkListChanges() { |
---|
218 | assertThat(childrenChanges).isEqualTo(Arrays.asList( |
---|
219 | new ListChange(ListChange.Action.Add, 0, "c0"), |
---|
220 | new ListChange(ListChange.Action.Add, 1, "c1"), |
---|
221 | new ListChange(ListChange.Action.Modify, 0, "c0") |
---|
222 | )); |
---|
223 | } |
---|
224 | |
---|
225 | @Test(dependsOnMethods = "checkListChanges") |
---|
226 | public void endWait() { |
---|
227 | monitor.useFor(1.0); |
---|
228 | } |
---|
229 | } |
---|