Praktikum 4: mehr Tests
This commit is contained in:
parent
8e810511c4
commit
05c36113a4
5
pom.xml
5
pom.xml
|
@ -54,11 +54,6 @@
|
|||
<version>4.12.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testifyproject.mock</groupId>
|
||||
<artifactId>mockito</artifactId>
|
||||
<version>1.0.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -68,7 +68,6 @@ public class GrosseZahl {
|
|||
if(this.equals(b)) {
|
||||
return new GrosseZahl(0);
|
||||
} else if(this.less(b)) {
|
||||
System.out.println("sub() kann keine negativen Werte zurückgeben.");
|
||||
return new GrosseZahl(0);
|
||||
}
|
||||
GrosseZahl rueckgabe = new GrosseZahl(this.toString());
|
||||
|
|
|
@ -2,22 +2,22 @@ package de.joethei.hs.java2.praktikum.praktikum4;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class KlausurenServer {
|
||||
|
||||
private ConcurrentMap<String, TreeSet<Integer>> data = new ConcurrentHashMap<>();
|
||||
private static ExecutorService executorService = Executors.newCachedThreadPool();
|
||||
private static ServerSocket serverSocket;
|
||||
|
||||
private static Map<String, CommandHandler> commands = new HashMap<>();
|
||||
|
||||
private boolean running;
|
||||
|
||||
public KlausurenServer(int port) {
|
||||
commands.put("test", ((writer, args) -> writer.write("Hallo Welt")));
|
||||
commands.put("put", (writer, args) -> {
|
||||
|
@ -65,18 +65,26 @@ public class KlausurenServer {
|
|||
});
|
||||
commands.put("stop", ((writer, args) -> {
|
||||
writer.write("1");
|
||||
executorService.shutdown();
|
||||
System.exit(0);
|
||||
running = false;
|
||||
}));
|
||||
|
||||
running = true;
|
||||
Runnable runnable = () -> {
|
||||
try {
|
||||
ServerSocket serverSocket = new ServerSocket(port);
|
||||
serverSocket = new ServerSocket(port);
|
||||
System.out.println("waiting for clients");
|
||||
while (true) {
|
||||
Socket clientSocket = serverSocket.accept();
|
||||
executorService.submit(new ClientRunnable(clientSocket));
|
||||
while (running) {
|
||||
executorService.submit(new ClientRunnable(serverSocket.accept()));
|
||||
}
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
|
@ -13,9 +13,11 @@ import static org.junit.Assert.assertEquals;
|
|||
@RunWith(OrderedRunner.class)
|
||||
public class KlausurenServerTest {
|
||||
|
||||
private static KlausurenServer server;
|
||||
|
||||
@BeforeClass
|
||||
public static void before() {
|
||||
new KlausurenServer(6767);
|
||||
server = new KlausurenServer(6767);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -29,6 +31,7 @@ public class KlausurenServerTest {
|
|||
public void put() {
|
||||
assertEquals("1", sendRequest("put max@maier 6, 5"));
|
||||
assertEquals("1 5, 6", sendRequest("put max@maier 7,8"));
|
||||
assertEquals("0", sendRequest("put max@maier"));
|
||||
}
|
||||
|
||||
@Test(timeout = 60L)
|
||||
|
@ -49,8 +52,20 @@ public class KlausurenServerTest {
|
|||
public void del() {
|
||||
assertEquals("0", sendRequest("del max@maier baum"));
|
||||
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) {
|
||||
try {
|
||||
Socket socket = new Socket("localhost", 6767);
|
||||
|
|
Loading…
Reference in New Issue