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