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