kleiner commit, damit SonarQube zufrieden ist

außerdem schauen ob alles aus der Prüfung so sinn macht
This commit is contained in:
Johannes Theiner 2018-06-25 12:10:59 +02:00
parent 62f4018a68
commit d0e0cfb56a
11 changed files with 146 additions and 9 deletions

27
pom.xml
View File

@ -54,6 +54,33 @@
<version>4.12.1</version> <version>4.12.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito-common</artifactId>
<version>1.7.3</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.7.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>1.7.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -0,0 +1,12 @@
package de.joethei.hs.java2;
public class Klausur {
public boolean istPalindrom(String satz) {
if(satz.length() <= 1) return true;
if(satz.charAt(0) == satz.charAt(satz.length()-1)) {
return istPalindrom(satz.substring(1, satz.length()-1));
}
return false;
}
}

View File

@ -160,6 +160,10 @@ public class GrosseZahl {
@Override @Override
public int hashCode() { public int hashCode() {
return super.hashCode(); int result = 0;
for(int i : zahl) {
result += i;
}
return result * zahl.length;
} }
} }

View File

@ -106,6 +106,11 @@ public class CharCollection implements Cloneable{
@Override @Override
public int hashCode() { public int hashCode() {
return super.hashCode(); int result = 0;
for(char c : list) {
result += c;
}
return result * list.size();
} }
} }

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) {
e.printStackTrace(); KlausurenServer.getLogger().fatal(this, e.getCause());
} }
} }
@ -35,7 +35,7 @@ public class ClientRunnable implements Runnable{
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); KlausurenServer.getLogger().error(this, e.getCause());
} }
} }
} }

View File

@ -1,10 +1,11 @@
package de.joethei.hs.java2.praktikum.praktikum4; package de.joethei.hs.java2.praktikum.praktikum4;
import org.apache.log4j.Logger;
import java.io.*; import java.io.*;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.logging.Logger;
public class KlausurenServer { public class KlausurenServer {
@ -112,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.throwing("KlausurenServer", "Constructor", e.getCause()); logger.error(this, e.getCause());
} }
}; };
@ -125,7 +126,7 @@ public class KlausurenServer {
try { try {
fileReader = new FileReader(Objects.requireNonNull(getClass().getClassLoader().getResource("praktikum4/data.txt")).getFile()); fileReader = new FileReader(Objects.requireNonNull(getClass().getClassLoader().getResource("praktikum4/data.txt")).getFile());
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
logger.throwing("KlausurenServer", "load", e.getCause()); logger.error(this, e.getCause());
} }
try (BufferedReader br = new BufferedReader(Objects.requireNonNull(fileReader))) { try (BufferedReader br = new BufferedReader(Objects.requireNonNull(fileReader))) {
for (String line = br.readLine(); line != null; line = br.readLine()) { for (String line = br.readLine(); line != null; line = br.readLine()) {
@ -137,7 +138,7 @@ public class KlausurenServer {
data.put(array[0], set); data.put(array[0], set);
} }
} catch (IOException e) { } catch (IOException e) {
logger.throwing("KlausurenServer", "load", e.getCause()); logger.error(this, e.getCause());
} }
} }
@ -157,7 +158,7 @@ public class KlausurenServer {
} }
} catch (IOException e) { } catch (IOException e) {
logger.throwing("KlausurenServer", "save", e.getCause()); logger.error(this, e.getCause());
} finally { } finally {
assert printWriter != null; assert printWriter != null;
printWriter.close(); printWriter.close();

View File

@ -0,0 +1,8 @@
# Root logger option
log4j.rootLogger=INFO, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
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

@ -62,4 +62,9 @@ public class CharCollectionTest {
assertTrue(new CharCollection("Hallo").isSubset(new CharCollection("Hao"))); assertTrue(new CharCollection("Hallo").isSubset(new CharCollection("Hao")));
} }
@Test
public void hashCodeTest() {
assertEquals(new CharCollection("HalloWorld").hashCode(), new CharCollection("HalloWorld").hashCode());
}
} }

View File

@ -83,4 +83,9 @@ public class GrosseZahlTest {
new GrosseZahl("42/"); new GrosseZahl("42/");
} }
@Test
public void hashCodeTest() {
assertEquals(new GrosseZahl(42).hashCode(), new GrosseZahl("42").hashCode());
}
} }

View File

@ -0,0 +1,20 @@
package de.joethei.hs.java2.tests;
import de.joethei.hs.java2.Klausur;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class KlausurTest {
@Test
public void istPalindrom() {
Klausur klausur = new Klausur();
assertTrue(klausur.istPalindrom("ABBA"));
assertTrue(klausur.istPalindrom("lagerregal"));
assertTrue(klausur.istPalindrom("a"));
assertTrue(klausur.istPalindrom(""));
assertFalse(klausur.istPalindrom("Hallo Welt"));
}
}

View File

@ -0,0 +1,50 @@
package de.joethei.hs.java2.tests;
import de.joethei.hs.java2.praktikum.praktikum4.KlausurenServer;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.*;
import java.net.Socket;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
@RunWith(PowerMockRunner.class)
public class KlausurenServerTest3 {
@InjectMocks
private KlausurenServer klausurenServer = new KlausurenServer(6770);
@Mock
private Socket socket;
@Mock
AppenderSkeleton appender;
@Captor
ArgumentCaptor<LoggingEvent> logCaptor;
@Test(expected = IOException.class)
public void ioException() throws Exception {
PowerMockito.whenNew(OutputStreamWriter.class).withArguments(new Socket().getOutputStream()).thenAnswer(invocationOnMock -> {
throw new IOException("TestCase");
});
Logger.getLogger("KlausurenServer").addAppender(appender);
verify(appender).doAppend(logCaptor.capture());
assertEquals("test", "test", logCaptor.getValue().getRenderedMessage());
KlausurenServer klausurenServer = new KlausurenServer(6770);
}
}