package com.framsticks.hosting; // import org.apache.log4j.Logger; import org.testng.annotations.Test; import com.framsticks.core.ObjectInstance; import com.framsticks.core.Path; import com.framsticks.core.XmlBasedTest; import com.framsticks.remote.RemoteInstance; import com.framsticks.test.TestClass; import com.framsticks.core.Instance; import com.framsticks.params.FramsClass; import com.framsticks.util.AbstractStateFunctor; import com.framsticks.util.dispatching.Dispatching; import com.framsticks.params.AccessInterface; import com.framsticks.params.PropertiesAccess; import com.framsticks.params.ValueParam; import com.framsticks.params.types.ProcedureParam; import com.framsticks.util.dispatching.Dispatching.Waiter; import com.framsticks.util.dispatching.Future; import com.framsticks.util.dispatching.RunAt; import static com.framsticks.core.InstanceUtils.*; import static org.fest.assertions.Assertions.*; @Test public class ServerTest extends XmlBasedTest { protected RemoteInstance remote; protected FramsClass remoteTestFramsClass; protected Path remotePath; protected Server server; protected ObjectInstance hosted; protected TestClass hostedObject; @Override protected String getConfigurationName() { return "ServerTest.xml"; } @Test public void runServer() { assertThat(framsticks.size()).isEqualTo(2); assertThat(framsticks.get("test")).isInstanceOf(Server.class); assertThat(framsticks.get("remote")).isInstanceOf(RemoteInstance.class); server = (Server) framsticks.get("test"); remote = (RemoteInstance) framsticks.get("remote"); assertThat(server.getHosted()).isInstanceOf(ObjectInstance.class); hosted = (ObjectInstance) server.getHosted(); assertThat(hosted.getRootObject()).isInstanceOf(TestClass.class); hostedObject = hosted.getRootObject(TestClass.class); } @Test(dependsOnMethods = "runServer") public void fetchInfo() { remote.dispatch(new RunAt() { @Override public void run() { remote.fetchInfo(Path.to(remote, "/"), new Future(failOnException()) { @Override protected void result(FramsClass result) { remoteTestFramsClass = result; assertThat(result.getId()).isEqualTo("TestClass"); } }); } }); Dispatching.synchronize(remote, 1.0); } @Test(dependsOnMethods = "fetchInfo") public void resolveAndfetchRootObject() { final Waiter waiter = produceWaiter(1.0); remote.dispatch(new RunAt() { @Override public void run() { final Path path = Path.to(remote, "/"); assertThat(path.isResolved()).isFalse(); remote.resolve(path, new Future(failOnException()) { @Override protected void result(final Path result) { assertThat(result.isResolved()).isTrue(); remotePath = result; remote.fetchValues(result, new AbstractStateFunctor(failOnException()) { @Override public void call() { AccessInterface access = bindAccess(result); assertThat(access).isInstanceOf(PropertiesAccess.class); assertThat(access.get("name", String.class)).isEqualTo("a test name"); waiter.pass(); } }); } }); } }); } @Test(dependsOnMethods = "resolveAndfetchRootObject") public void setValueName() { final Waiter waiter = produceWaiter(2.0); storeValue(remotePath, remoteTestFramsClass.getParamEntry("name", ValueParam.class), "new name", new AbstractStateFunctor(failOnException()) { @Override public void call() { /** And now check directly whether it was really set. */ hosted.dispatch(new RunAt() { @Override public void run() { assertThat(hostedObject.getName()).isEqualTo("new name"); waiter.pass(); } }); } }); } @Test(dependsOnMethods = "setValueName") public void callMethod() { final Waiter firstWaiter = produceWaiter(2.0); final Waiter waiter = produceWaiter(2.0); call(remotePath, remoteTestFramsClass.getParamEntry("resetHistory", ProcedureParam.class), new Object[] {}, new Future(failOnException()) { @Override protected void result(Object result) { firstWaiter.pass(); } }); call(remotePath, remoteTestFramsClass.getParamEntry("appendHistory", ProcedureParam.class), new Object[] {"next word"}, new Future(failOnException()) { @Override protected void result(Object result) { hosted.dispatch(new RunAt() { @Override public void run() { assertThat(hostedObject.getHistory()).isEqualTo("next word|"); waiter.pass(); } }); } }); } @Test(dependsOnMethods = "callMethod") public void endWait() { monitor.useFor(1.0); } }