Praktikum 4: bis aufs speichern fertig

This commit is contained in:
Johannes Theiner 2018-05-13 09:57:46 +02:00
parent 123f6d7883
commit 8e810511c4
2 changed files with 34 additions and 9 deletions

View File

@ -7,12 +7,14 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
public class KlausurenServer { public class KlausurenServer {
private Map<String, TreeSet<Integer>> data = new HashMap<>(); private ConcurrentMap<String, TreeSet<Integer>> data = new ConcurrentHashMap<>();
private static ExecutorService executorService = Executors.newCachedThreadPool(); private static ExecutorService executorService = Executors.newCachedThreadPool();
private static Map<String, CommandHandler> commands = new HashMap<>(); private static Map<String, CommandHandler> commands = new HashMap<>();
@ -28,11 +30,11 @@ public class KlausurenServer {
} }
} }
writer.write("1"); writer.write("1");
if(data.containsKey(args.get(1))) { if(data.containsKey(args.get(1).toLowerCase())) {
writer.write(data.get(args.get(1)).toString().replace('[', ' ').replace(']', ' ').replaceFirst(".$","")); writer.write(data.get(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replaceFirst(".$",""));
set.addAll(data.get(args.get(1))); set.addAll(data.get(args.get(1).toLowerCase()));
} }
data.put(args.get(1), set); data.put(args.get(1).toLowerCase(), set);
} else { } else {
writer.write("0"); writer.write("0");
} }
@ -48,7 +50,18 @@ public class KlausurenServer {
} }
}); });
commands.put("get", (writer, args) -> { 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) -> { commands.put("stop", ((writer, args) -> {
writer.write("1"); writer.write("1");

View File

@ -13,8 +13,6 @@ import static org.junit.Assert.assertEquals;
@RunWith(OrderedRunner.class) @RunWith(OrderedRunner.class)
public class KlausurenServerTest { public class KlausurenServerTest {
private Socket socket;
@BeforeClass @BeforeClass
public static void before() { public static void before() {
new KlausurenServer(6767); new KlausurenServer(6767);
@ -39,9 +37,23 @@ public class KlausurenServerTest {
assertEquals("1 [5, 6, 7, 8]", sendRequest("getall")); 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) { private String sendRequest(String command) {
try { try {
socket = new Socket("localhost", 6767); Socket socket = new Socket("localhost", 6767);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.write(command); writer.write(command);