Praktikum 4: mehr Tests

This commit is contained in:
Johannes Theiner 2018-05-14 13:53:47 +02:00
parent 8e810511c4
commit 05c36113a4
5 changed files with 36 additions and 19 deletions

View File

@ -54,11 +54,6 @@
<version>4.12.1</version> <version>4.12.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.testifyproject.mock</groupId>
<artifactId>mockito</artifactId>
<version>1.0.4</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -68,7 +68,6 @@ public class GrosseZahl {
if(this.equals(b)) { if(this.equals(b)) {
return new GrosseZahl(0); return new GrosseZahl(0);
} else if(this.less(b)) { } else if(this.less(b)) {
System.out.println("sub() kann keine negativen Werte zurückgeben.");
return new GrosseZahl(0); return new GrosseZahl(0);
} }
GrosseZahl rueckgabe = new GrosseZahl(this.toString()); GrosseZahl rueckgabe = new GrosseZahl(this.toString());

View File

@ -2,22 +2,22 @@ package de.joethei.hs.java2.praktikum.praktikum4;
import java.io.IOException; import java.io.IOException;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap; 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.*;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class KlausurenServer { public class KlausurenServer {
private ConcurrentMap<String, TreeSet<Integer>> data = new ConcurrentHashMap<>(); private ConcurrentMap<String, TreeSet<Integer>> data = new ConcurrentHashMap<>();
private static ExecutorService executorService = Executors.newCachedThreadPool(); private static ExecutorService executorService = Executors.newCachedThreadPool();
private static ServerSocket serverSocket;
private static Map<String, CommandHandler> commands = new HashMap<>(); private static Map<String, CommandHandler> commands = new HashMap<>();
private boolean running;
public KlausurenServer(int port) { public KlausurenServer(int port) {
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) -> {
@ -65,18 +65,26 @@ public class KlausurenServer {
}); });
commands.put("stop", ((writer, args) -> { commands.put("stop", ((writer, args) -> {
writer.write("1"); writer.write("1");
executorService.shutdown(); running = false;
System.exit(0);
})); }));
running = true;
Runnable runnable = () -> { Runnable runnable = () -> {
try { try {
ServerSocket serverSocket = new ServerSocket(port); serverSocket = new ServerSocket(port);
System.out.println("waiting for clients"); System.out.println("waiting for clients");
while (true) { while (running) {
Socket clientSocket = serverSocket.accept(); executorService.submit(new ClientRunnable(serverSocket.accept()));
executorService.submit(new ClientRunnable(clientSocket));
} }
executorService.shutdown();
try {
executorService.awaitTermination(10, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
}
serverSocket.close();
System.out.println("server shutdown");
Thread.currentThread().interrupt();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

View File

@ -13,9 +13,11 @@ import static org.junit.Assert.assertEquals;
@RunWith(OrderedRunner.class) @RunWith(OrderedRunner.class)
public class KlausurenServerTest { public class KlausurenServerTest {
private static KlausurenServer server;
@BeforeClass @BeforeClass
public static void before() { public static void before() {
new KlausurenServer(6767); server = new KlausurenServer(6767);
} }
@Test @Test
@ -29,6 +31,7 @@ public class KlausurenServerTest {
public void put() { public void put() {
assertEquals("1", sendRequest("put max@maier 6, 5")); assertEquals("1", sendRequest("put max@maier 6, 5"));
assertEquals("1 5, 6", sendRequest("put max@maier 7,8")); assertEquals("1 5, 6", sendRequest("put max@maier 7,8"));
assertEquals("0", sendRequest("put max@maier"));
} }
@Test(timeout = 60L) @Test(timeout = 60L)
@ -49,8 +52,20 @@ public class KlausurenServerTest {
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"));
} }
/*
@Test(timeout = 700L)
@Order(order = 6)
public void saved() throws InterruptedException {
assertEquals("1", sendRequest("stop"));
Thread.sleep(500L);
server = new KlausurenServer(6767);
assertEquals("1 [5, 6, 7, 8]", sendRequest("getall"));
}
*/
private String sendRequest(String command) { private String sendRequest(String command) {
try { try {
Socket socket = new Socket("localhost", 6767); Socket socket = new Socket("localhost", 6767);