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