so jetzt ?

This commit is contained in:
Johannes Theiner 2018-06-25 17:41:20 +02:00
parent d0e0cfb56a
commit 1b3edc0e23
6 changed files with 59 additions and 32 deletions

View File

@ -15,7 +15,7 @@ public class ClientRunnable implements Runnable{
try { try {
bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) { } catch (IOException e) {
KlausurenServer.getLogger().fatal(this, e.getCause()); KlausurenServer.getLogger().fatal("Fehler beim erstellen des Sockets", e.getCause());
} }
} }
@ -35,7 +35,7 @@ public class ClientRunnable implements Runnable{
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch (IOException e) { } catch (IOException e) {
KlausurenServer.getLogger().error(this, e.getCause()); KlausurenServer.getLogger().error("Fehler beim einlesen der Eingabe", e.getCause());
} }
} }
} }

View File

@ -113,7 +113,7 @@ public class KlausurenServer {
logger.info("server shutdown"); logger.info("server shutdown");
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch (IOException e) { } catch (IOException e) {
logger.error(this, e.getCause()); logger.error("Fehler beim Server beenden", e.getCause());
} }
}; };
@ -138,7 +138,7 @@ public class KlausurenServer {
data.put(array[0], set); data.put(array[0], set);
} }
} catch (IOException e) { } catch (IOException e) {
logger.error(this, e.getCause()); logger.error("Fehler beim laden der Datei", e.getCause());
} }
} }
@ -158,7 +158,7 @@ public class KlausurenServer {
} }
} catch (IOException e) { } catch (IOException e) {
logger.error(this, e.getCause()); logger.error("Fehler beim speichern", e.getCause());
} finally { } finally {
assert printWriter != null; assert printWriter != null;
printWriter.close(); printWriter.close();

View File

@ -1,8 +1,6 @@
# Root logger option log4j.rootCategory=INFO, CONSOLE
log4j.rootLogger=INFO, stdout log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
# Direct log messages to stdout log4j.appender.CONSOLE.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender # un-comment this will fail the test
log4j.appender.stdout.Target=System.out #log4j.logger.com.haim.logging=DEBUG
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

View File

@ -67,12 +67,12 @@ public class KlausurenServerTest {
assertEquals("1 ", sendRequest("put max@maier 5, 6, 7, 8")); assertEquals("1 ", sendRequest("put max@maier 5, 6, 7, 8"));
assertEquals("1", sendRequest("stop")); assertEquals("1", sendRequest("stop"));
//Thread.sleep(1000L); //Thread.sleep(1000L);
server = new KlausurenServer(6768); KlausurenServer.main(new String[]{"6768"});
Thread.sleep(100L); Thread.sleep(100L);
assertEquals("1 [5,6,7,8]", sendRequest("getall", 6768)); assertEquals("1 [5,6,7,8]", sendRequest("getall", 6768));
assertEquals("1 5,6,7,8", sendRequest("del max@maier", 6768)); assertEquals("1 5,6,7,8", sendRequest("del max@maier", 6768));
assertEquals("1 5,6", sendRequest("del max@baum", 6768)); assertEquals("1 5,6", sendRequest("del max@baum", 6768));
assertEquals("1", sendRequest("stop", 6768));
} }
private String sendRequest(String command, int port) { private String sendRequest(String command, int port) {

View File

@ -1,8 +1,8 @@
package de.joethei.hs.java2.tests; package de.joethei.hs.java2.tests;
import de.joethei.hs.java2.praktikum.praktikum4.KlausurenServer; import de.joethei.hs.java2.praktikum.praktikum4.KlausurenServer;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.apache.log4j.spi.LoggingEvent; import org.apache.log4j.spi.LoggingEvent;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -10,14 +10,11 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Captor; import org.mockito.Captor;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import java.io.*;
import java.net.Socket; import java.net.Socket;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.mockito.Mockito.verify;
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
public class KlausurenServerTest3 { public class KlausurenServerTest3 {
@ -27,24 +24,29 @@ public class KlausurenServerTest3 {
@Mock @Mock
private Socket socket; private Socket socket;
@Mock
AppenderSkeleton appender; TestAppender appender = new TestAppender();
@Captor @Captor
ArgumentCaptor<LoggingEvent> logCaptor; ArgumentCaptor<LoggingEvent> logCaptor;
@Test(expected = IOException.class) @Test
public void ioException() throws Exception { public void ioException() {
PowerMockito.whenNew(OutputStreamWriter.class).withArguments(new Socket().getOutputStream()).thenAnswer(invocationOnMock -> {
throw new IOException("TestCase"); appender.setThreshold(Priority.INFO);
});
Logger.getLogger("KlausurenServer").addAppender(appender); Logger.getLogger("KlausurenServer").addAppender(appender);
verify(appender).doAppend(logCaptor.capture());
assertEquals("test", "test", logCaptor.getValue().getRenderedMessage());
KlausurenServer klausurenServer = new KlausurenServer(6770); KlausurenServer klausurenServer = new KlausurenServer(6770);
assertTrue(appender.messages.contains("Fehler beim Server beenden"));
assertFalse(appender.messages.contains("Fehler beim erstellen des Sockets"));
assertFalse(appender.messages.contains("Hallo Welt"));
}
@Test
public void getLogger() {
assertEquals(Logger.getLogger("KlausurenServer"), KlausurenServer.getLogger());
} }
} }

View File

@ -0,0 +1,27 @@
package de.joethei.hs.java2.tests;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;
import java.util.ArrayList;
import java.util.List;
public class TestAppender extends AppenderSkeleton {
List<String> messages = new ArrayList<>();
@Override
protected void append(LoggingEvent loggingEvent) {
messages.add(loggingEvent.getMessage().toString());
}
@Override
public void close() {
}
@Override
public boolean requiresLayout() {
return false;
}
}