diff --git a/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/ClientRunnable.java b/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/ClientRunnable.java index 8bf08de..3933aa7 100644 --- a/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/ClientRunnable.java +++ b/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/ClientRunnable.java @@ -21,16 +21,13 @@ public class ClientRunnable implements Runnable{ @Override public void run() { - System.out.println("client connected"); try { String input = bufferedReader.readLine(); - System.out.println(input); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); - input = input.replace(",", ""); + input = input.replace(",", " "); List arguments = Arrays.asList(input.split(" ")); - String command = arguments.remove(0); - KlausurenServer.getCommands().get(command.toLowerCase()).handle(bufferedWriter, arguments); + KlausurenServer.getCommands().get(arguments.get(0).toLowerCase()).handle(bufferedWriter, arguments); bufferedWriter.flush(); bufferedWriter.close(); 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 e51dcb8..d92abff 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 @@ -3,41 +3,52 @@ package de.joethei.hs.java2.praktikum.praktikum4; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; -import java.util.*; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class KlausurenServer { - private Map> data = new HashMap<>(); + private Map> data = new HashMap<>(); private static ExecutorService executorService = Executors.newCachedThreadPool(); private static Map commands = new HashMap<>(); public KlausurenServer(int port) { + commands.put("test", ((writer, args) -> writer.write("Hallo Welt"))); commands.put("put", (writer, args) -> { - if(args.size() >= 2) { - Set set = new HashSet<>(); - for(int i = 1; i <= args.size(); i++) { - try { - set.add(Integer.parseInt(args.get(i))); - }catch (NumberFormatException ex) { - writer.write("0"); - } - } - data.put(args.get(0), set); - }else { - writer.write("0"); - } + if (args.size() >= 3) { + TreeSet set = new TreeSet<>(); + 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))) { + writer.write(data.get(args.get(1)).toString().replace('[', ' ').replace(']', ' ').replaceFirst(".$","")); + set.addAll(data.get(args.get(1))); + } + data.put(args.get(1), set); + } else { + writer.write("0"); + } }); 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()); - }else { - writer.write("0"); - } + if (!data.isEmpty()) { + writer.write("1 "); + Set set = new TreeSet<>(); + data.forEach((s, integers) -> set.addAll(integers)); + writer.write(set.toString()); + } else { + writer.write("0"); + } + }); + commands.put("get", (writer, args) -> { + }); 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 b28132d..e18542a 100644 --- a/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest.java +++ b/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest.java @@ -1,4 +1,57 @@ package de.joethei.hs.java2.tests; +import de.joethei.hs.java2.praktikum.praktikum4.KlausurenServer; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.*; +import java.net.Socket; + +import static org.junit.Assert.assertEquals; + +@RunWith(OrderedRunner.class) public class KlausurenServerTest { + + private Socket socket; + + @BeforeClass + public static void before() { + new KlausurenServer(5000); + } + + @Test + @Order(order = 1) + public void test() { + assertEquals("Hallo Welt", sendRequest("test")); + } + + @Test(timeout = 120L) + @Order(order = 2) + public void put() { + assertEquals("1", sendRequest("put max@maier 6, 5")); + assertEquals("1 5, 6", sendRequest("put max@maier 7,8")); + } + + @Test(timeout = 60L) + @Order(order = 3) + public void getall() { + assertEquals("1 [5, 6, 7, 8]", sendRequest("getall")); + } + + private String sendRequest(String command) { + try { + socket = new Socket("localhost", 5000); + BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); + writer.write(command); + writer.newLine(); + writer.flush(); + + return reader.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } } \ No newline at end of file diff --git a/src/test/java/de/joethei/hs/java2/tests/Order.java b/src/test/java/de/joethei/hs/java2/tests/Order.java new file mode 100644 index 0000000..6406428 --- /dev/null +++ b/src/test/java/de/joethei/hs/java2/tests/Order.java @@ -0,0 +1,12 @@ +package de.joethei.hs.java2.tests; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.METHOD}) +public @interface Order { + public int order(); +} \ No newline at end of file diff --git a/src/test/java/de/joethei/hs/java2/tests/OrderedRunner.java b/src/test/java/de/joethei/hs/java2/tests/OrderedRunner.java new file mode 100644 index 0000000..eb12eaf --- /dev/null +++ b/src/test/java/de/joethei/hs/java2/tests/OrderedRunner.java @@ -0,0 +1,31 @@ +package de.joethei.hs.java2.tests; + +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.InitializationError; + +import java.util.ArrayList; +import java.util.List; + +public class OrderedRunner extends BlockJUnit4ClassRunner { + + public OrderedRunner(Class clazz) throws InitializationError { + super(clazz); + } + + @Override + protected List computeTestMethods() { + List list = super.computeTestMethods(); + List copy = new ArrayList<>(list); + copy.sort((f1, f2) -> { + Order o1 = f1.getAnnotation(Order.class); + Order o2 = f2.getAnnotation(Order.class); + + if (o1 == null || o2 == null) + return -1; + + return o1.order() - o2.order(); + }); + return copy; + } +} \ No newline at end of file