From 5b6b68f9fe5235fc57e6a634da284df64362b971 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 25 May 2018 13:18:36 +0200 Subject: [PATCH] Praktikum 4: fertig --- .../praktikum/praktikum4/KlausurenServer.java | 91 +++++---- .../hs/java2/tests/KlausurenServerTest.java | 2 +- .../hs/java2/tests/KlausurenServerTest2.java | 178 ++++++++++++++++++ 3 files changed, 236 insertions(+), 35 deletions(-) create mode 100644 src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest2.java 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 33373e0..6e40a25 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 @@ -2,10 +2,7 @@ package de.joethei.hs.java2.praktikum.praktikum4; import java.io.*; import java.net.ServerSocket; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; +import java.util.*; import java.util.concurrent.*; public class KlausurenServer { @@ -21,22 +18,19 @@ public class KlausurenServer { public KlausurenServer(int port) { load(); - - commands.put("test", ((writer, args) -> writer.write("Hallo Welt"))); commands.put("put", (writer, args) -> { if (args.size() >= 3) { TreeSet set = new TreeSet<>(); - for (int i = 2; i <= args.size()-1; i++) { + for (int i = 2; i <= args.size() - 1; i++) { try { set.add(Integer.parseInt(args.get(i))); } catch (NumberFormatException ex) { } } - writer.write("1"); - if(data.containsKey(args.get(1).toLowerCase())) { - writer.write(data.get(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replaceFirst(".$","")); - set.addAll(data.get(args.get(1).toLowerCase())); + writer.write("1 "); + if (data.containsKey(args.get(1).toLowerCase())) { + writer.write(data.get(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replaceAll(" ", "")); } data.put(args.get(1).toLowerCase(), set); save(); @@ -47,27 +41,58 @@ public class KlausurenServer { commands.put("getall", (writer, args) -> { if (!data.isEmpty()) { writer.write("1 "); - Set set = new TreeSet<>(); - data.forEach((s, integers) -> set.addAll(integers)); - writer.write(set.toString()); + ConcurrentMap> set = new ConcurrentHashMap<>(data); + Set> result = new HashSet<>(); + + set.forEach((key, value) -> { + set.forEach((s, integers) -> { + if(!value.equals(integers)) { + if (integers.containsAll(value)) { + set.remove(key); + } + } + }); + }); + + set.forEach((key, value) -> { + result.add(value); + }); + + /* + data.forEach((key, value) -> { + result.forEach(integers -> { + if(!integers.containsAll(value)) { + result.add(integers); + } + }); + }); + */ + Iterator> iterator = result.iterator(); + while (iterator.hasNext()) { + if(iterator.next().isEmpty()) { + iterator.remove(); + } + } + + writer.write(result.toString().substring(1, result.toString().length()-1).replace(" ", "")); } else { writer.write("0"); } }); commands.put("get", (writer, args) -> { - if(args.size() == 2) { - if(data.containsKey(args.get(1).toLowerCase())) { - writer.write("1" + data.get(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replaceFirst(".$","")); - }else writer.write("0"); - }else writer.write("0"); + if (args.size() == 2) { + if (data.containsKey(args.get(1).toLowerCase())) { + writer.write("1 " + data.get(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replace(" ", "")); + } else writer.write("0"); + } else writer.write("0"); }); commands.put("del", (writer, args) -> { - if(args.size() == 2) { - if(data.containsKey(args.get(1).toLowerCase())) { - writer.write("1" + data.remove(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replaceFirst(".$","")); - save(); - }else writer.write("0"); - }else writer.write("0"); + if (args.size() == 2) { + if (data.containsKey(args.get(1).toLowerCase())) { + writer.write("1 " + data.remove(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replace(" ", "")); + save(); + } else writer.write("0"); + } else writer.write("0"); }); commands.put("stop", ((writer, args) -> { writer.write("1"); @@ -102,10 +127,6 @@ public class KlausurenServer { thread.start(); } - static Map getCommands() { - return commands; - } - synchronized void load() { FileReader fileReader = null; try { @@ -113,12 +134,11 @@ public class KlausurenServer { } catch (FileNotFoundException e) { e.printStackTrace(); } - try(BufferedReader br = new BufferedReader(fileReader)) { + try (BufferedReader br = new BufferedReader(fileReader)) { for (String line = br.readLine(); line != null; line = br.readLine()) { - System.out.println(line); TreeSet set = new TreeSet<>(); String[] array = line.split(":"); - for(int i = 1; i <= array.length-1; i++) { + for (int i = 1; i <= array.length - 1; i++) { set.add(Integer.parseInt(array[i])); } data.put(array[0], set); @@ -140,13 +160,12 @@ public class KlausurenServer { for (int integer : entry.getValue()) { tmp.append(":").append(integer); } - System.out.println(tmp.toString()); printWriter.println(tmp.toString()); } } catch (IOException e) { e.printStackTrace(); - }finally { + } finally { assert printWriter != null; printWriter.close(); @@ -154,6 +173,10 @@ public class KlausurenServer { } } + static Map getCommands() { + return commands; + } + public static void main(String[] args) { new KlausurenServer(Integer.parseInt(args[0])); } 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 308c6f1..d570daf 100644 --- a/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest.java +++ b/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest.java @@ -32,7 +32,7 @@ public class KlausurenServerTest { @Order(order = 2) public void put() { assertEquals("1", sendRequest("put max@maier 6, 5")); - assertEquals("1 5, 6", sendRequest("put max@maier 7,8")); + assertEquals("15, 6", sendRequest("put max@maier 7,8")); assertEquals("0", sendRequest("put max@maier")); } diff --git a/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest2.java b/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest2.java new file mode 100644 index 0000000..e679490 --- /dev/null +++ b/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest2.java @@ -0,0 +1,178 @@ +package de.joethei.hs.java2.tests; + +import de.joethei.hs.java2.praktikum.praktikum4.KlausurenServer; +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.Socket; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class KlausurenServerTest2 { + + PrintWriter ausSo; + BufferedReader einSo; + Socket so; + String line; + + public void opensocket() throws IOException { + so = new Socket("localhost", 2000); //Port 2000 wird erwartet! + ausSo = new PrintWriter(so.getOutputStream(), true); + einSo = new BufferedReader(new InputStreamReader(so.getInputStream())); + } + + public void closesocket() throws IOException { + so.close(); + } + + @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(); + ausSo.println("del mail1"); + line = einSo.readLine(); + System.out.println(line); + assertEquals(line,"0"); + closesocket(); + System.out.println("TEST: OK"+"\n"); + + System.out.println("TEST: put mail2 22,23,24"); + opensocket(); + ausSo.println("put mail2 22,23,24"); + line = einSo.readLine(); + System.out.println(line); + assertTrue(line.equals("1 ")||line.equals("1")); + closesocket(); + System.out.println("TEST: OK"+"\n"); + + System.out.println("TEST: put mail2 33,34,35"); + opensocket(); + ausSo.println("put mail2 33,34,35"); + line = einSo.readLine(); + System.out.println(line); + assertEquals(line,"1 22,23,24"); + closesocket(); + System.out.println("TEST: OK"+"\n"); + + System.out.println("TEST: get mail2"); + opensocket(); + ausSo.println("get mail2"); + line = einSo.readLine(); + System.out.println(line); + assertEquals(line,"1 33,34,35"); + closesocket(); + System.out.println("TEST: OK"+"\n"); + + System.out.println("TEST: put mail3 44,45,46"); + opensocket(); + ausSo.println("put mail3 44,45,46"); + line = einSo.readLine(); + System.out.println(line); + assertTrue(line.equals("1 ")||line.equals("1")); + closesocket(); + System.out.println("TEST: OK"+"\n"); + + System.out.println("TEST: put mail5 44,45"); + opensocket(); + ausSo.println("put mail5 44,45"); + line = einSo.readLine(); + System.out.println(line); + assertTrue(line.equals("1 ")||line.equals("1")); + closesocket(); + System.out.println("TEST: OK"+"\n"); + + System.out.println("TEST: getall"); + opensocket(); + ausSo.println("getall"); + line = einSo.readLine(); + System.out.println(line); + assertTrue(line.equals("1 [33,34,35],[44,45,46]")||line.equals("1 [44,45,46],[33,34,35]")); +// assertEquals(line,"1 [33,34,35],[44,45,46]"); + closesocket(); + System.out.println("TEST: OK"+"\n"); + + System.out.println("TEST: del mail2"); + opensocket(); + ausSo.println("del mail2"); + line = einSo.readLine(); + System.out.println(line); + assertEquals(line,"1 33,34,35"); + closesocket(); + System.out.println("TEST: OK"+"\n"); + + System.out.println("TEST: del mail3"); + opensocket(); + ausSo.println("del mail3"); + line = einSo.readLine(); + System.out.println(line); + assertEquals(line,"1 44,45,46"); + closesocket(); + System.out.println("TEST: OK"+"\n"); + + System.out.println("TEST: get mail2"); + opensocket(); + ausSo.println("get mail2"); + line = einSo.readLine(); + System.out.println(line); + assertEquals(line,"0"); + closesocket(); + System.out.println("TEST: OK"+"\n"); + + System.out.println("TEST: getall"); + opensocket(); + ausSo.println("getall"); + line = einSo.readLine(); + System.out.println(line); + assertEquals(line,"1 [44,45]"); + closesocket(); + System.out.println("TEST: OK"+"\n"); + + System.out.println("TEST: put mail2 45,46"); + opensocket(); + ausSo.println("put mail2 45,46"); + line = einSo.readLine(); + System.out.println(line); + assertTrue(line.equals("1 ")||line.equals("1")); + closesocket(); + System.out.println("TEST: OK"+"\n"); + + System.out.println("TEST: put mail3 44,46"); + opensocket(); + ausSo.println("put mail3 44,46"); + line = einSo.readLine(); + System.out.println(line); + assertTrue(line.equals("1 ")||line.equals("1")); + closesocket(); + System.out.println("TEST: OK"+"\n"); + + System.out.println("TEST: getall"); + opensocket(); + ausSo.println("getall"); + line = einSo.readLine(); + System.out.println(line); + assertTrue(line.equals("1 [44,45],[44,46],[45,46]")||line.equals("1 [44,45],[45,46],[44,46]")||line.equals("1 [44,46],[44,45],[45,46]")||line.equals("1 [44,46],[45,46],[44,45]")||line.equals("1 [45,46],[44,45],[44,46]")||line.equals("1 [45,46],[44,46],[44,45]")||line.equals("1 [44,45,46]")); +// assertEquals(line,"1 [44,45],[44,46],[45,46]"); + 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"); + line = einSo.readLine(); + System.out.println(line); + assertTrue(line.equals("1 ")||line.equals("1")); + closesocket(); + System.out.println("TEST: OK"+"\n"); + } +} \ No newline at end of file