Praktikum 4: fertig

This commit is contained in:
Johannes Theiner 2018-05-25 13:18:36 +02:00
parent fc4913a024
commit 5b6b68f9fe
3 changed files with 236 additions and 35 deletions

View File

@ -2,10 +2,7 @@ package de.joethei.hs.java2.praktikum.praktikum4;
import java.io.*; import java.io.*;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.util.HashMap; import java.util.*;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.*; import java.util.concurrent.*;
public class KlausurenServer { public class KlausurenServer {
@ -21,8 +18,6 @@ public class KlausurenServer {
public KlausurenServer(int port) { public KlausurenServer(int port) {
load(); load();
commands.put("test", ((writer, args) -> writer.write("Hallo Welt"))); commands.put("test", ((writer, args) -> writer.write("Hallo Welt")));
commands.put("put", (writer, args) -> { commands.put("put", (writer, args) -> {
if (args.size() >= 3) { if (args.size() >= 3) {
@ -35,8 +30,7 @@ public class KlausurenServer {
} }
writer.write("1 "); writer.write("1 ");
if (data.containsKey(args.get(1).toLowerCase())) { if (data.containsKey(args.get(1).toLowerCase())) {
writer.write(data.get(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replaceFirst(".$","")); writer.write(data.get(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replaceAll(" ", ""));
set.addAll(data.get(args.get(1).toLowerCase()));
} }
data.put(args.get(1).toLowerCase(), set); data.put(args.get(1).toLowerCase(), set);
save(); save();
@ -47,9 +41,40 @@ public class KlausurenServer {
commands.put("getall", (writer, args) -> { commands.put("getall", (writer, args) -> {
if (!data.isEmpty()) { if (!data.isEmpty()) {
writer.write("1 "); writer.write("1 ");
Set<Integer> set = new TreeSet<>(); ConcurrentMap<String, TreeSet<Integer>> set = new ConcurrentHashMap<>(data);
data.forEach((s, integers) -> set.addAll(integers)); Set<TreeSet<Integer>> result = new HashSet<>();
writer.write(set.toString());
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<TreeSet<Integer>> iterator = result.iterator();
while (iterator.hasNext()) {
if(iterator.next().isEmpty()) {
iterator.remove();
}
}
writer.write(result.toString().substring(1, result.toString().length()-1).replace(" ", ""));
} else { } else {
writer.write("0"); writer.write("0");
} }
@ -57,14 +82,14 @@ public class KlausurenServer {
commands.put("get", (writer, args) -> { commands.put("get", (writer, args) -> {
if (args.size() == 2) { if (args.size() == 2) {
if (data.containsKey(args.get(1).toLowerCase())) { if (data.containsKey(args.get(1).toLowerCase())) {
writer.write("1" + data.get(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replaceFirst(".$","")); writer.write("1 " + data.get(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replace(" ", ""));
} else writer.write("0"); } else writer.write("0");
} else writer.write("0"); } else writer.write("0");
}); });
commands.put("del", (writer, args) -> { commands.put("del", (writer, args) -> {
if (args.size() == 2) { if (args.size() == 2) {
if (data.containsKey(args.get(1).toLowerCase())) { if (data.containsKey(args.get(1).toLowerCase())) {
writer.write("1" + data.remove(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replaceFirst(".$","")); writer.write("1 " + data.remove(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replace(" ", ""));
save(); save();
} else writer.write("0"); } else writer.write("0");
} else writer.write("0"); } else writer.write("0");
@ -102,10 +127,6 @@ public class KlausurenServer {
thread.start(); thread.start();
} }
static Map<String, CommandHandler> getCommands() {
return commands;
}
synchronized void load() { synchronized void load() {
FileReader fileReader = null; FileReader fileReader = null;
try { try {
@ -115,7 +136,6 @@ public class KlausurenServer {
} }
try (BufferedReader br = new BufferedReader(fileReader)) { try (BufferedReader br = new BufferedReader(fileReader)) {
for (String line = br.readLine(); line != null; line = br.readLine()) { for (String line = br.readLine(); line != null; line = br.readLine()) {
System.out.println(line);
TreeSet<Integer> set = new TreeSet<>(); TreeSet<Integer> set = new TreeSet<>();
String[] array = line.split(":"); String[] array = line.split(":");
for (int i = 1; i <= array.length - 1; i++) { for (int i = 1; i <= array.length - 1; i++) {
@ -140,7 +160,6 @@ public class KlausurenServer {
for (int integer : entry.getValue()) { for (int integer : entry.getValue()) {
tmp.append(":").append(integer); tmp.append(":").append(integer);
} }
System.out.println(tmp.toString());
printWriter.println(tmp.toString()); printWriter.println(tmp.toString());
} }
@ -154,6 +173,10 @@ public class KlausurenServer {
} }
} }
static Map<String, CommandHandler> getCommands() {
return commands;
}
public static void main(String[] args) { public static void main(String[] args) {
new KlausurenServer(Integer.parseInt(args[0])); new KlausurenServer(Integer.parseInt(args[0]));
} }

View File

@ -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");
}
}