Praktikum 4: alles funktioniert

This commit is contained in:
Johannes Theiner 2018-05-19 11:30:55 +02:00
parent fb99ba0d06
commit ce4f5bf449
2 changed files with 32 additions and 14 deletions

View File

@ -84,7 +84,7 @@ public class KlausurenServer {
} }
executorService.shutdown(); executorService.shutdown();
try { try {
executorService.awaitTermination(10, TimeUnit.MINUTES); executorService.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }
@ -129,7 +129,12 @@ public class KlausurenServer {
} }
synchronized void save() { synchronized void save() {
try (FileWriter fileWriter = new FileWriter(getClass().getClassLoader().getResource("praktikum4/data.txt").getFile()); PrintWriter printWriter = new PrintWriter(fileWriter)) { FileOutputStream fileWriter;
PrintWriter printWriter = null;
try {
fileWriter = new FileOutputStream(getClass().getClassLoader().getResource("praktikum4/data.txt").getFile());
printWriter = new PrintWriter(fileWriter);
for (Map.Entry<String, TreeSet<Integer>> entry : data.entrySet()) { for (Map.Entry<String, TreeSet<Integer>> entry : data.entrySet()) {
StringBuilder tmp = new StringBuilder(entry.getKey()); StringBuilder tmp = new StringBuilder(entry.getKey());
for (int integer : entry.getValue()) { for (int integer : entry.getValue()) {
@ -137,12 +142,15 @@ public class KlausurenServer {
} }
System.out.println(tmp.toString()); System.out.println(tmp.toString());
printWriter.println(tmp.toString()); printWriter.println(tmp.toString());
printWriter.flush();
} }
printWriter.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
}finally {
assert printWriter != null;
printWriter.close();
} }
} }

View File

@ -41,42 +41,52 @@ public class KlausurenServerTest {
assertEquals("1 [5, 6, 7, 8]", sendRequest("getall")); assertEquals("1 [5, 6, 7, 8]", sendRequest("getall"));
} }
@Test(timeout = 20L) @Test(timeout = 500L)
@Order(order = 4) @Order(order = 4)
public void get() { public void get() {
assertEquals("1 5, 6, 7, 8", sendRequest("get max@maier")); assertEquals("1 5, 6, 7, 8", sendRequest("get max@maier"));
assertEquals("0", sendRequest("get max@müller")); assertEquals("0", sendRequest("get max@müller"));
} }
@Test(timeout = 20L) @Test(timeout = 500L)
@Order(order = 5) @Order(order = 5)
public void del() { public void del() {
assertEquals("0", sendRequest("del max@maier baum")); assertEquals("0", sendRequest("del max@maier baum"));
assertEquals("1 5, 6, 7, 8", sendRequest("del max@maier")); assertEquals("1 5, 6, 7, 8", sendRequest("del max@maier"));
assertEquals("0", sendRequest("get max@maier")); assertEquals("0", sendRequest("get max@maier"));
} }
@Test(timeout = 700L) @Test(timeout = 2000L)
@Order(order = 6) @Order(order = 6)
public void saved() throws InterruptedException { public void saved() throws InterruptedException {
assertEquals("1", sendRequest("put max@maier 5, 6, 7, 8"));
assertEquals("1", sendRequest("stop")); assertEquals("1", sendRequest("stop"));
//Thread.sleep(500L); //Thread.sleep(1000L);
//server = new KlausurenServer(6767); server = new KlausurenServer(6768);
//assertEquals("1 [5, 6, 7, 8]", sendRequest("getall")); 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");
} }
private String sendRequest(String command) { private String sendRequest(String command, int port) {
try { try {
Socket socket = new Socket("localhost", 6767); Socket socket = new Socket("localhost", port);
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);
writer.newLine(); writer.newLine();
writer.flush(); writer.flush();
String result = reader.readLine();
return reader.readLine(); socket.close();
return result;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null; return null;
} }
private String sendRequest(String command) {
return sendRequest(command, 6767);
}
} }