diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-11 14:58:48 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-11 14:58:48 +0100 |
commit | 47e43eefebae63afee1a1743602c15e3b1ede8ff (patch) | |
tree | 288ca3ee04e3365de9e2d78d51375be0b1bbac94 /jsonrpc | |
parent | 14205be283ad5b54a94c07308ab3e336f3069653 (diff) |
Add SameState test.
Test network participation.
Diffstat (limited to 'jsonrpc')
-rw-r--r-- | jsonrpc/src/main/java/com/orbekk/same/SameState.java | 12 | ||||
-rw-r--r-- | jsonrpc/src/test/java/com/orbekk/same/SameStateTest.java | 60 |
2 files changed, 71 insertions, 1 deletions
diff --git a/jsonrpc/src/main/java/com/orbekk/same/SameState.java b/jsonrpc/src/main/java/com/orbekk/same/SameState.java index c3d03f9..9638252 100644 --- a/jsonrpc/src/main/java/com/orbekk/same/SameState.java +++ b/jsonrpc/src/main/java/com/orbekk/same/SameState.java @@ -143,9 +143,19 @@ public class SameState extends Thread implements UrlReceiver { pendingParticipants.clear(); } + /** + * This method runs the pending commands to SameState. + * + * It should be called by the worker thread, but can be called directly + * for testing purposes to avoid threading in unit tests. + */ + synchronized void internalRun() { + handleNewParticipants(); + } + public synchronized void run() { while (!stopped) { - handleNewParticipants(); + internalRun(); try { wait(1000); } catch (InterruptedException e) { diff --git a/jsonrpc/src/test/java/com/orbekk/same/SameStateTest.java b/jsonrpc/src/test/java/com/orbekk/same/SameStateTest.java new file mode 100644 index 0000000..77bbbee --- /dev/null +++ b/jsonrpc/src/test/java/com/orbekk/same/SameStateTest.java @@ -0,0 +1,60 @@ +package com.orbekk.same; + +import static org.junit.Assert.*; + +import java.util.Map; +import java.util.HashMap; +import org.junit.Test; +import org.junit.Before; + +public class SameStateTest { + private MockConnectionManager connections; + private SameState state1, state2, state3; + private SameService service1, service2, service3; + + public static class MockConnectionManager implements ConnectionManager { + public Map<String, SameService> connections = + new HashMap<String, SameService>(); + + @Override + public SameService getConnection(String url) { + return connections.get(url); + } + } + + public SameStateTest() { + } + + @Before public void setUp() { + connections = new MockConnectionManager(); + + state1 = new SameState("Network1", "Client1", connections); + state1.setUrl("test://client1"); + service1 = new SameServiceImpl(state1); + state2 = new SameState("Network2", "Client2", connections); + state2.setUrl("test://client2"); + service2 = new SameServiceImpl(state2); + state3 = new SameState("Network3", "Client3", connections); + state3.setUrl("test://client3"); + service3 = new SameServiceImpl(state3); + + connections.connections.put(state1.getUrl(), service1); + connections.connections.put(state2.getUrl(), service2); + connections.connections.put(state3.getUrl(), service3); + } + + @Test public void testJoinNetwork() { + connections.getConnection(state1.getUrl()). + participateNetwork("Network1", state2.getClientId(), + state2.getUrl()); + assertTrue(state1.getParticipants().size() == 1); + assertTrue(state2.getParticipants().size() == 1); + + state1.internalRun(); + state2.internalRun(); + + assertTrue(state1.getParticipants().size() == 2); + assertTrue(state2.getParticipants().size() == 2); + assertEquals(state1.getNetworkName(), state2.getNetworkName()); + } +} |