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