1 | package com.framsticks.hosting; |
---|
2 | |
---|
3 | // import org.apache.log4j.Logger; |
---|
4 | import org.testng.annotations.Test; |
---|
5 | |
---|
6 | import com.framsticks.core.Mode; |
---|
7 | import com.framsticks.core.ObjectTree; |
---|
8 | import com.framsticks.core.Path; |
---|
9 | import com.framsticks.core.XmlBasedTest; |
---|
10 | import com.framsticks.remote.RemoteTree; |
---|
11 | |
---|
12 | import com.framsticks.test.TestClass; |
---|
13 | import com.framsticks.core.Tree; |
---|
14 | import com.framsticks.params.FramsClass; |
---|
15 | import com.framsticks.util.dispatching.Dispatching; |
---|
16 | import com.framsticks.params.AccessInterface; |
---|
17 | import com.framsticks.params.PrimitiveParam; |
---|
18 | import com.framsticks.params.PropertiesAccess; |
---|
19 | import com.framsticks.params.types.ProcedureParam; |
---|
20 | import com.framsticks.util.dispatching.Dispatching.Waiter; |
---|
21 | import com.framsticks.util.dispatching.FutureHandler; |
---|
22 | import com.framsticks.util.dispatching.RunAt; |
---|
23 | |
---|
24 | import static com.framsticks.core.TreeOperations.*; |
---|
25 | |
---|
26 | import static org.fest.assertions.Assertions.*; |
---|
27 | |
---|
28 | @Test |
---|
29 | public class ServerTest extends XmlBasedTest { |
---|
30 | |
---|
31 | protected RemoteTree remote; |
---|
32 | protected FramsClass remoteTestFramsClass; |
---|
33 | protected Path remotePath; |
---|
34 | |
---|
35 | protected Server server; |
---|
36 | protected ObjectTree hosted; |
---|
37 | protected TestClass hostedObject; |
---|
38 | |
---|
39 | @Override |
---|
40 | protected String getConfigurationName() { |
---|
41 | return "ServerTest.xml"; |
---|
42 | } |
---|
43 | |
---|
44 | @Test |
---|
45 | public void runServer() { |
---|
46 | assertThat(framsticks.size()).isEqualTo(2); |
---|
47 | assertThat(framsticks.get("test")).isInstanceOf(Server.class); |
---|
48 | assertThat(framsticks.get("remote")).isInstanceOf(RemoteTree.class); |
---|
49 | |
---|
50 | server = (Server) framsticks.get("test"); |
---|
51 | remote = (RemoteTree) framsticks.get("remote"); |
---|
52 | assertThat(server.getHosted()).isInstanceOf(ObjectTree.class); |
---|
53 | hosted = (ObjectTree) server.getHosted(); |
---|
54 | assertThat(hosted.getRootObject()).isInstanceOf(TestClass.class); |
---|
55 | hostedObject = hosted.getRootObject(TestClass.class); |
---|
56 | } |
---|
57 | |
---|
58 | @Test(dependsOnMethods = "runServer") |
---|
59 | public void fetchInfo() { |
---|
60 | remote.dispatch(new RunAt<Tree>(failOnException) { |
---|
61 | @Override |
---|
62 | protected void runAt() { |
---|
63 | remote.info(Path.to(remote, "/"), new FutureHandler<FramsClass>(failOnException) { |
---|
64 | @Override |
---|
65 | protected void result(FramsClass result) { |
---|
66 | remoteTestFramsClass = result; |
---|
67 | assertThat(result.getId()).isEqualTo("TestClass"); |
---|
68 | } |
---|
69 | }); |
---|
70 | } |
---|
71 | }); |
---|
72 | |
---|
73 | Dispatching.synchronize(remote, 1.0); |
---|
74 | } |
---|
75 | |
---|
76 | @Test(dependsOnMethods = "fetchInfo") |
---|
77 | public void resolveAndfetchRootObject() { |
---|
78 | final Waiter waiter = produceWaiter(1.0); |
---|
79 | |
---|
80 | remote.dispatch(new RunAt<Tree>(failOnException) { |
---|
81 | @Override |
---|
82 | protected void runAt() { |
---|
83 | final Path path = Path.to(remote, "/"); |
---|
84 | assertThat(path.isResolved()).isFalse(); |
---|
85 | |
---|
86 | remote.resolve(path, new FutureHandler<Path>(failOnException) { |
---|
87 | @Override |
---|
88 | protected void result(final Path result) { |
---|
89 | assertThat(result.isResolved()).isTrue(); |
---|
90 | remotePath = result; |
---|
91 | remote.get(result, Mode.FETCH, new FutureHandler<Object>(failOnException) { |
---|
92 | @Override |
---|
93 | protected void result(Object object) { |
---|
94 | AccessInterface access = bindAccess(result); |
---|
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 | } |
---|
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 callMethod() { |
---|
128 | final Waiter firstWaiter = produceWaiter(2.0); |
---|
129 | final Waiter waiter = produceWaiter(2.0); |
---|
130 | |
---|
131 | call(remotePath, remoteTestFramsClass.getParamEntry("resetHistory", ProcedureParam.class), new Object[] {}, new FutureHandler<Object>(failOnException) { |
---|
132 | @Override |
---|
133 | protected void result(Object result) { |
---|
134 | firstWaiter.pass(); |
---|
135 | } |
---|
136 | }); |
---|
137 | |
---|
138 | call(remotePath, remoteTestFramsClass.getParamEntry("appendHistory", ProcedureParam.class), new Object[] {"next word"}, new FutureHandler<Object>(failOnException) { |
---|
139 | @Override |
---|
140 | protected void result(Object result) { |
---|
141 | hosted.dispatch(new RunAt<Tree>(failOnException) { |
---|
142 | @Override |
---|
143 | protected void runAt() { |
---|
144 | assertThat(hostedObject.getHistory()).isEqualTo("next word|"); |
---|
145 | waiter.pass(); |
---|
146 | } |
---|
147 | }); |
---|
148 | } |
---|
149 | }); |
---|
150 | |
---|
151 | } |
---|
152 | |
---|
153 | @Test(dependsOnMethods = "callMethod") |
---|
154 | public void endWait() { |
---|
155 | monitor.useFor(1.0); |
---|
156 | } |
---|
157 | } |
---|