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.LocalTree; |
---|
11 | import com.framsticks.core.Path; |
---|
12 | import com.framsticks.core.TreeOperations; |
---|
13 | import com.framsticks.core.XmlBasedTest; |
---|
14 | import com.framsticks.remote.RemoteTree; |
---|
15 | |
---|
16 | import com.framsticks.test.ChangeEvent; |
---|
17 | import com.framsticks.test.TestClass; |
---|
18 | import com.framsticks.core.Tree; |
---|
19 | import com.framsticks.params.FramsClass; |
---|
20 | import com.framsticks.params.AccessInterface; |
---|
21 | import com.framsticks.params.EventListener; |
---|
22 | import com.framsticks.params.PrimitiveParam; |
---|
23 | import com.framsticks.params.PropertiesAccess; |
---|
24 | import com.framsticks.params.types.EventParam; |
---|
25 | // import com.framsticks.params.types.EventParam; |
---|
26 | import com.framsticks.params.types.ProcedureParam; |
---|
27 | import com.framsticks.util.dispatching.Dispatching.Waiter; |
---|
28 | import com.framsticks.util.dispatching.FutureHandler; |
---|
29 | import com.framsticks.util.dispatching.RunAt; |
---|
30 | |
---|
31 | import static com.framsticks.core.TreeOperations.*; |
---|
32 | |
---|
33 | import static org.fest.assertions.Assertions.*; |
---|
34 | |
---|
35 | @Test |
---|
36 | public class ServerTest extends XmlBasedTest { |
---|
37 | |
---|
38 | protected RemoteTree remote; |
---|
39 | protected FramsClass remoteTestFramsClass; |
---|
40 | protected Path remotePath; |
---|
41 | |
---|
42 | protected Server server; |
---|
43 | protected LocalTree hosted; |
---|
44 | protected TestClass hostedObject; |
---|
45 | protected EventListener<ChangeEvent> listener; |
---|
46 | protected List<String> listenerArguments = new LinkedList<>(); |
---|
47 | |
---|
48 | @Override |
---|
49 | protected String getConfigurationName() { |
---|
50 | return "ServerTest.xml"; |
---|
51 | } |
---|
52 | |
---|
53 | @Test |
---|
54 | public void runServer() { |
---|
55 | assertThat(framsticks.size()).isEqualTo(2); |
---|
56 | assertThat(framsticks.get("test")).isInstanceOf(Server.class); |
---|
57 | assertThat(framsticks.get("remote")).isInstanceOf(RemoteTree.class); |
---|
58 | |
---|
59 | server = (Server) framsticks.get("test"); |
---|
60 | remote = (RemoteTree) framsticks.get("remote"); |
---|
61 | assertThat(server.getHosted()).isInstanceOf(LocalTree.class); |
---|
62 | hosted = (LocalTree) server.getHosted(); |
---|
63 | assertThat(hosted.getRootObject()).isInstanceOf(TestClass.class); |
---|
64 | hostedObject = hosted.getRootObject(TestClass.class); |
---|
65 | } |
---|
66 | |
---|
67 | @Test(dependsOnMethods = "runServer") |
---|
68 | public void fetchInfo() { |
---|
69 | final Waiter waiter = produceWaiter(1.0); |
---|
70 | |
---|
71 | TreeOperations.tryGet(remote, "/testClass", new FutureHandler<Path>(failOnException) { |
---|
72 | @Override |
---|
73 | protected void result(Path path) { |
---|
74 | assertThat(path.isResolved()).isTrue(); |
---|
75 | remoteTestFramsClass = bindAccess(path).getFramsClass(); |
---|
76 | assertThat(remoteTestFramsClass.getName()).isEqualTo("TestClass"); |
---|
77 | waiter.pass(); |
---|
78 | } |
---|
79 | }); |
---|
80 | |
---|
81 | } |
---|
82 | |
---|
83 | @Test(dependsOnMethods = "fetchInfo") |
---|
84 | public void resolveAndfetchRootObject() { |
---|
85 | final Waiter waiter = produceWaiter(1.0); |
---|
86 | |
---|
87 | TreeOperations.tryGet(remote, "/testClass", new FutureHandler<Path>(failOnException) { |
---|
88 | @Override |
---|
89 | protected void result(Path path) { |
---|
90 | assertThat(path.isResolved()).isTrue(); |
---|
91 | remotePath = path; |
---|
92 | AccessInterface access = bindAccess(path); |
---|
93 | assertThat(access).isInstanceOf(PropertiesAccess.class); |
---|
94 | assertThat(access.get("name", String.class)).isEqualTo("a test name"); |
---|
95 | waiter.pass(); |
---|
96 | } |
---|
97 | }); |
---|
98 | } |
---|
99 | |
---|
100 | @Test(dependsOnMethods = "resolveAndfetchRootObject") |
---|
101 | public void setValueName() { |
---|
102 | final Waiter waiter = produceWaiter(2.0); |
---|
103 | |
---|
104 | set(remotePath, remoteTestFramsClass.getParamEntry("name", PrimitiveParam.class), "new name", new FutureHandler<Integer>(failOnException) { |
---|
105 | @Override |
---|
106 | protected void result(Integer flags) { |
---|
107 | // assertThat(flags).isEqualTo(0); |
---|
108 | /** And now check directly whether it was really set. */ |
---|
109 | hosted.dispatch(new RunAt<Tree>(failOnException) { |
---|
110 | @Override |
---|
111 | protected void runAt() { |
---|
112 | assertThat(hostedObject.getName()).isEqualTo("new name"); |
---|
113 | waiter.pass(); |
---|
114 | } |
---|
115 | }); |
---|
116 | } |
---|
117 | }); |
---|
118 | } |
---|
119 | |
---|
120 | @Test(dependsOnMethods = "setValueName") |
---|
121 | public void registerListener() { |
---|
122 | final Waiter waiter = produceWaiter(1.0); |
---|
123 | listener = new EventListener<ChangeEvent>() { |
---|
124 | |
---|
125 | @Override |
---|
126 | public void action(ChangeEvent argument) { |
---|
127 | listenerArguments.add(argument.history); |
---|
128 | } |
---|
129 | }; |
---|
130 | |
---|
131 | TreeOperations.tryGet(remote, "/cli/events", new FutureHandler<Path>(failOnException) { |
---|
132 | @Override |
---|
133 | protected void result(Path path) { |
---|
134 | waiter.pass(); |
---|
135 | } |
---|
136 | }); |
---|
137 | |
---|
138 | addListener(remotePath, remoteTestFramsClass.getParamEntry("history_changed", EventParam.class), listener, ChangeEvent.class, produceWaiter(1.0).passInFuture(Void.class)); |
---|
139 | } |
---|
140 | |
---|
141 | @Test(dependsOnMethods = "registerListener") |
---|
142 | public void callMethod() { |
---|
143 | final Waiter waiter = produceWaiter(2.0); |
---|
144 | |
---|
145 | call(remotePath, remoteTestFramsClass.getParamEntry("resetHistory", ProcedureParam.class), new Object[] {}, produceWaiter(2.0).passInFuture(Object.class)); |
---|
146 | |
---|
147 | call(remotePath, remoteTestFramsClass.getParamEntry("appendHistory", ProcedureParam.class), new Object[] {"next word"}, new FutureHandler<Object>(failOnException) { |
---|
148 | @Override |
---|
149 | protected void result(Object result) { |
---|
150 | hosted.dispatch(new RunAt<Tree>(failOnException) { |
---|
151 | @Override |
---|
152 | protected void runAt() { |
---|
153 | assertThat(hostedObject.getHistory()).isEqualTo("next word|"); |
---|
154 | waiter.pass(); |
---|
155 | } |
---|
156 | }); |
---|
157 | } |
---|
158 | }); |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | @Test(dependsOnMethods = "callMethod") |
---|
163 | public void deregisterListener() { |
---|
164 | removeListener(remotePath, remoteTestFramsClass.getParamEntry("history_changed", EventParam.class), listener, produceWaiter(1.0).passInFuture(Void.class)); |
---|
165 | |
---|
166 | assertThat(listenerArguments).isEqualTo(Arrays.asList("", "next word|")); |
---|
167 | } |
---|
168 | |
---|
169 | @Test(dependsOnMethods = "callMethod") |
---|
170 | public void endWait() { |
---|
171 | monitor.useFor(1.0); |
---|
172 | } |
---|
173 | } |
---|