Praktikum 4: Tests für put und getall
This commit is contained in:
parent
6b300f69de
commit
02e2165924
|
@ -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<String> 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();
|
||||
|
|
|
@ -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<String, Set<Integer>> data = new HashMap<>();
|
||||
private Map<String, TreeSet<Integer>> data = new HashMap<>();
|
||||
private static ExecutorService executorService = Executors.newCachedThreadPool();
|
||||
private static Map<String, CommandHandler> 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<Integer> set = new HashSet<>();
|
||||
for(int i = 1; i <= args.size(); i++) {
|
||||
if (args.size() >= 3) {
|
||||
TreeSet<Integer> 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("0");
|
||||
} catch (NumberFormatException ex) {
|
||||
}
|
||||
}
|
||||
data.put(args.get(0), set);
|
||||
}else {
|
||||
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()) {
|
||||
if (!data.isEmpty()) {
|
||||
writer.write("1 ");
|
||||
Set<Integer> set = new TreeSet<>();
|
||||
data.forEach((s, integers) -> set.addAll(integers));
|
||||
writer.write(set.toString());
|
||||
}else {
|
||||
} else {
|
||||
writer.write("0");
|
||||
}
|
||||
});
|
||||
commands.put("get", (writer, args) -> {
|
||||
|
||||
});
|
||||
commands.put("stop", ((writer, args) -> {
|
||||
writer.write("1");
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
|
@ -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<FrameworkMethod> computeTestMethods() {
|
||||
List<FrameworkMethod> list = super.computeTestMethods();
|
||||
List<FrameworkMethod> 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue