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