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 d92abff..10c46e1 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 @@ -7,12 +7,14 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.TreeSet; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class KlausurenServer { - private Map> data = new HashMap<>(); + private ConcurrentMap> data = new ConcurrentHashMap<>(); private static ExecutorService executorService = Executors.newCachedThreadPool(); private static Map commands = new HashMap<>(); @@ -28,11 +30,11 @@ public class KlausurenServer { } } writer.write("1"); - if(data.containsKey(args.get(1))) { - writer.write(data.get(args.get(1)).toString().replace('[', ' ').replace(']', ' ').replaceFirst(".$","")); - set.addAll(data.get(args.get(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())); } - data.put(args.get(1), set); + data.put(args.get(1).toLowerCase(), set); } else { writer.write("0"); } @@ -48,7 +50,18 @@ public class KlausurenServer { } }); 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"); + }); + 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(".$","")); + }else writer.write("0"); + }else writer.write("0"); }); commands.put("stop", ((writer, args) -> { writer.write("1"); 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 12e3c91..48d43ae 100644 --- a/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest.java +++ b/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest.java @@ -13,8 +13,6 @@ import static org.junit.Assert.assertEquals; @RunWith(OrderedRunner.class) public class KlausurenServerTest { - private Socket socket; - @BeforeClass public static void before() { new KlausurenServer(6767); @@ -39,9 +37,23 @@ public class KlausurenServerTest { assertEquals("1 [5, 6, 7, 8]", sendRequest("getall")); } + @Test(timeout = 20L) + @Order(order = 4) + public void get() { + assertEquals("1 5, 6, 7, 8", sendRequest("get max@maier")); + assertEquals("0", sendRequest("get max@müller")); + } + + @Test(timeout = 20L) + @Order(order = 5) + public void del() { + assertEquals("0", sendRequest("del max@maier baum")); + assertEquals("1 5, 6, 7, 8", sendRequest("del max@maier")); + } + private String sendRequest(String command) { try { - socket = new Socket("localhost", 6767); + Socket socket = new Socket("localhost", 6767); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); writer.write(command);