From e0d8285b5756fd60fbc1d79fa2b86ff3ebd2b521 Mon Sep 17 00:00:00 2001 From: senft-lap Date: Fri, 9 Nov 2012 22:29:00 +0100 Subject: [PATCH] Implemented JUnit tests --- ws2012/P2P/uebungen/2/p2p_ex2/.classpath | 1 + .../src/tests/BufferedNetworkStackTests.java | 99 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 ws2012/P2P/uebungen/2/p2p_ex2/src/tests/BufferedNetworkStackTests.java diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/.classpath b/ws2012/P2P/uebungen/2/p2p_ex2/.classpath index fb565a58..72c2ba61 100644 --- a/ws2012/P2P/uebungen/2/p2p_ex2/.classpath +++ b/ws2012/P2P/uebungen/2/p2p_ex2/.classpath @@ -2,5 +2,6 @@ + diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/src/tests/BufferedNetworkStackTests.java b/ws2012/P2P/uebungen/2/p2p_ex2/src/tests/BufferedNetworkStackTests.java new file mode 100644 index 00000000..9810ae89 --- /dev/null +++ b/ws2012/P2P/uebungen/2/p2p_ex2/src/tests/BufferedNetworkStackTests.java @@ -0,0 +1,99 @@ +package tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import server.BufferedNetworkStack; +import client.BufferedNetworkStackClient; +import client.TimeoutException; + +public class BufferedNetworkStackTests { + + private static final String HOST = "localhost"; + private static final int PORT = 9999; + + private BufferedNetworkStack server; + private BufferedNetworkStackClient client; + + @Before + public void setUp() { + client = new BufferedNetworkStackClient(); + try { + server = new BufferedNetworkStack(PORT); + } catch (IOException e) { + fail("Couldn't listen on port " + PORT + "." + e); + } + } + + @After + public void tearDown() { + server.stop(); + } + + @Test + public void testFixedShortLength() { + List values = new ArrayList(); + + for (int i = 0; i < 100; i++) { + String uuid = UUID.randomUUID().toString(); + values.add(uuid); + + try { + client.push(uuid, HOST, PORT); + } catch (IOException | TimeoutException e) { + fail("Couldn't send to server" + e); + } + } + + for (int i = values.size() - 1; i >= 0; i--) { + try { + assertEquals(values.get(i), client.pop(HOST, PORT)); + } catch (IOException | TimeoutException e) { + fail("Couldn't send to server" + e); + } + } + } + + @Test + public void testRandomLongLength() { + List values = new ArrayList(); + + for (int i = 0; i < 100; i++) { + // We generate messages of random sizes so the buffers are filled + // differently every time + int numOfIds = 1 + (int) (Math.random() * 50); + + StringBuilder uuidBuilder = new StringBuilder(numOfIds * 128); + for (int j = 0; j < numOfIds; j++) { + uuidBuilder.append(UUID.randomUUID().toString()); + } + String uuid = uuidBuilder.toString(); + + values.add(uuid); + try { + client.push(uuid, HOST, PORT); + } catch (IOException e) { + fail("Couldn't send to server. " + e); + } catch (TimeoutException e) { + fail("Connection timed out. " + e); + } + } + + for (int i = values.size() - 1; i >= 0; i--) { + try { + assertEquals(values.get(i), client.pop(HOST, PORT)); + } catch (IOException | TimeoutException e) { + fail("Couldn't send to server" + e); + } + } + } +}