From a7cdce30d91965bc708cf22395114802796db517 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 25 May 2018 16:15:22 +0200 Subject: [PATCH] =?UTF-8?q?Praktikum=204:=20alles=20wieder=20gr=C3=BCn?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../praktikum/praktikum4/KlausurenServer.java | 4 ++ .../hs/java2/tests/KlausurenServerTest.java | 21 +++++----- .../hs/java2/tests/KlausurenServerTest2.java | 38 +++++++++++++++---- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/KlausurenServer.java b/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/KlausurenServer.java index 6e40a25..67cf11e 100644 --- a/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/KlausurenServer.java +++ b/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/KlausurenServer.java @@ -19,6 +19,10 @@ public class KlausurenServer { load(); commands.put("test", ((writer, args) -> writer.write("Hallo Welt"))); + commands.put("delall", (writer, args) -> { + data.clear(); + save(); + }); commands.put("put", (writer, args) -> { if (args.size() >= 3) { TreeSet set = new TreeSet<>(); diff --git a/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest.java b/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest.java index d570daf..edb2012 100644 --- a/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest.java +++ b/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest.java @@ -24,6 +24,7 @@ public class KlausurenServerTest { @Test @Order(order = 1) public void test() { + sendRequest("delall"); assertEquals("Hallo Welt", sendRequest("test")); assertEquals("0", sendRequest("getall")); } @@ -31,21 +32,22 @@ public class KlausurenServerTest { @Test(timeout = 120L) @Order(order = 2) public void put() { - assertEquals("1", sendRequest("put max@maier 6, 5")); - assertEquals("15, 6", sendRequest("put max@maier 7,8")); + assertEquals("1 ", sendRequest("put max@maier 6, 5")); + assertEquals("1 5,6", sendRequest("put max@maier 7,8")); assertEquals("0", sendRequest("put max@maier")); } @Test(timeout = 60L) @Order(order = 3) public void getall() { - assertEquals("1 [5, 6, 7, 8]", sendRequest("getall")); + assertEquals("1 ", sendRequest("put max@baum 5, 6")); + assertEquals("1 [5,6],[7,8]", sendRequest("getall")); } @Test(timeout = 500L) @Order(order = 4) public void get() { - assertEquals("1 5, 6, 7, 8", sendRequest("get max@maier")); + assertEquals("1 7,8", sendRequest("get max@maier")); assertEquals("0", sendRequest("get max@müller")); assertEquals("0", sendRequest("get")); assertEquals("0", sendRequest("get max@müller 42")); @@ -55,21 +57,22 @@ public class KlausurenServerTest { @Order(order = 5) public void del() { assertEquals("0", sendRequest("del max@maier baum")); - assertEquals("1 5, 6, 7, 8", sendRequest("del max@maier")); + assertEquals("1 7,8", sendRequest("del max@maier")); assertEquals("0", sendRequest("del max@maier")); assertEquals("0", sendRequest("get max@maier")); } @Test(timeout = 2000L) @Order(order = 6) public void saved() throws InterruptedException { - assertEquals("1", sendRequest("put max@maier 5, 6, 7, 8")); + assertEquals("1 ", sendRequest("put max@maier 5, 6, 7, 8")); assertEquals("1", sendRequest("stop")); //Thread.sleep(1000L); server = new KlausurenServer(6768); Thread.sleep(100L); - assertEquals("1 [5, 6, 7, 8]", sendRequest("getall", 6768)); - assertEquals("1 5, 6, 7, 8", sendRequest("del max@maier", 6768)); - //sendRequest("stop"); + assertEquals("1 [5,6,7,8]", sendRequest("getall", 6768)); + assertEquals("1 5,6,7,8", sendRequest("del max@maier", 6768)); + assertEquals("1 5,6", sendRequest("del max@baum", 6768)); + } private String sendRequest(String command, int port) { diff --git a/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest2.java b/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest2.java index e679490..d85c7d1 100644 --- a/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest2.java +++ b/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest2.java @@ -1,6 +1,7 @@ package de.joethei.hs.java2.tests; import de.joethei.hs.java2.praktikum.praktikum4.KlausurenServer; +import org.junit.Before; import org.junit.Test; import java.io.BufferedReader; @@ -20,7 +21,7 @@ public class KlausurenServerTest2 { String line; public void opensocket() throws IOException { - so = new Socket("localhost", 2000); //Port 2000 wird erwartet! + so = new Socket("localhost", 6769); //Port 6769 wird erwartet! ausSo = new PrintWriter(so.getOutputStream(), true); einSo = new BufferedReader(new InputStreamReader(so.getInputStream())); } @@ -29,9 +30,35 @@ public class KlausurenServerTest2 { so.close(); } + @Before + public void setUp() throws IOException { + new KlausurenServer(6769); + System.out.println("INITIALISIERUNG"); + opensocket(); + ausSo.println("del mail1"); + line = einSo.readLine(); + closesocket(); + opensocket(); + ausSo.println("del mail2"); + line = einSo.readLine(); + closesocket(); + opensocket(); + ausSo.println("del mail3"); + line = einSo.readLine(); + closesocket(); + opensocket(); + ausSo.println("del mail4"); + line = einSo.readLine(); + closesocket(); + opensocket(); + ausSo.println("del mail5"); + line = einSo.readLine(); + closesocket(); + System.out.println("INITIALISIERUNG OK"+"\n"); + } + @Test //alles in einer Test-Methode, weil die richtige Reihenfolge sonst nicht gewährleistet ist! public void all() throws IOException { - new KlausurenServer(2000); System.out.println("TEST: del mail1"); opensocket(); @@ -78,7 +105,7 @@ public class KlausurenServerTest2 { closesocket(); System.out.println("TEST: OK"+"\n"); - System.out.println("TEST: put mail5 44,45"); + System.out.println("TEST: put mail4 44,45"); opensocket(); ausSo.println("put mail5 44,45"); line = einSo.readLine(); @@ -161,11 +188,6 @@ public class KlausurenServerTest2 { closesocket(); System.out.println("TEST: OK"+"\n"); - opensocket(); - ausSo.println("del mail2"); - line = einSo.readLine(); - closesocket(); - System.out.println("TEST: stop"); opensocket(); ausSo.println("stop");