Skip to content
Snippets Groups Projects
Commit 6f66ca39 authored by jez04's avatar jez04
Browse files

feat: update test

parent ff16d383
No related merge requests found
package jez04.structure.test; package jez04.structure.test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
import java.io.FileNotFoundException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -19,19 +25,20 @@ class ClassStructureTest { ...@@ -19,19 +25,20 @@ class ClassStructureTest {
StructureHelper helper = new StructureHelper(); StructureHelper helper = new StructureHelper();
private static String className = "Score"; private static String className = "Score";
@Test @Test
void scoreExistenceTest() { void scoreExistenceTest() {
// helper.classExist(className); // helper.classExist(className);
Class<?> c = Score.class;//helper.getClass(className); Class<?> c = Score.class;// helper.getClass(className);
helper.hasProperty(c, "name", String.class, false); helper.hasProperty(c, "name", String.class, false);
helper.hasProperty(c, "points", int.class, false); helper.hasProperty(c, "points", int.class, false);
helper.hasMethod(c, "generate", Score.class); helper.hasMethod(c, "generate", Score.class);
} }
@Test @Test
void scoreNotLoadedExistenceTest() { void scoreNotLoadedExistenceTest() {
// helper.classExist(className); // helper.classExist(className);
Class<?> c = ScoreNotLoaded.class; //helper.getClass("cz.vsb.fei.lab.Score"); Class<?> c = ScoreNotLoaded.class;// helper.getClass("ScoreNotLoaded");
helper.hasProperty(c, "count", int.class, false); helper.hasProperty(c, "count", int.class, false);
helper.hasExtends(c, Exception.class); helper.hasExtends(c, Exception.class);
} }
...@@ -41,15 +48,26 @@ class ClassStructureTest { ...@@ -41,15 +48,26 @@ class ClassStructureTest {
App app = new App(); App app = new App();
assertTrue(app.generateScores(10).size() == 10); assertTrue(app.generateScores(10).size() == 10);
} }
@Test @Test
void loadScoresFromStreamTest() throws ScoreNotLoaded { void loadScoresFromStreamTest() throws Exception {
App app = new App(); App app = new App();
assertTrue(app.loadScoresFromStream(new InputStreamReader(App.class.getResourceAsStream("/bestScore-ok.csv"))).size() > 1); assertTrue(app.loadScoresFromStream(new InputStreamReader(App.class.getResourceAsStream("/bestScore-ok.csv")))
.size() > 1);
} }
void loadScoresFromStreamExceptionTest() throws ScoreNotLoaded { void loadScoresFromStreamExceptionTest() throws ScoreNotLoaded, IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException {
App app = new App(); App app = new App();
assertThrows(ScoreNotLoaded.class, () -> app.loadScoresFromStream(new InputStreamReader(App.class.getResourceAsStream("/bestScores-err.csv"))).size() ); try {
app.loadScoresFromStream(new InputStreamReader(App.class.getResourceAsStream("/bestScores-err.csv")))
.size();
fail("Exception not throwen!");
} catch (Exception e) {
assertEquals(ScoreNotLoaded.class, e.getClass(), "Excpectin exception of type ScoreNotLoaded");
ScoreNotLoaded ex = (ScoreNotLoaded) e;
int count = (int)ScoreNotLoaded.class.getDeclaredMethod("getCount").invoke(ex);
assertTrue(count > 0, "Info about already parsed line missing");
}
} }
void loadScoresTest() { void loadScoresTest() {
...@@ -57,14 +75,22 @@ class ClassStructureTest { ...@@ -57,14 +75,22 @@ class ClassStructureTest {
app.loadScores(); app.loadScores();
} }
void saveScoresTest() throws FileNotFoundException { void saveScoresTest() throws IOException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
App app = new App(); App app = new App();
app.saveScores(app.generateScores(10), "test.csv"); app.saveScores(List.of(createScore("test", 1), createScore("test2", 15)), "test.csv");
assertEquals(2, Files.lines(Paths.get("test.csv")).count(),
"Expecting existent of file test.csv with 2 lines.");
} }
void saveScoresDirsTest() throws FileNotFoundException { private Score createScore(String name, int points) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Constructor constructor = Score.class.getConstructor(String.class, int.class);
return (Score)constructor.newInstance(name, points);
}
void saveScoresDirsTest() throws IOException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
App app = new App(); App app = new App();
app.saveScores(app.generateScores(10), "test/test.csv"); app.saveScores(List.of(createScore("test", 1), createScore("test2", 15)), "test/subtest/test.csv");
assertEquals(2, Files.lines(Paths.get("test", "subtest", "test.csv")).count(),
"Expecting existent of file test.csv with 2 lines.");
} }
} }
...@@ -182,8 +182,8 @@ class StructureHelper { ...@@ -182,8 +182,8 @@ class StructureHelper {
public Set<String> getNameOfAllClasses() { public Set<String> getNameOfAllClasses() {
List<String> initClassesName = new ArrayList<>(); List<String> initClassesName = new ArrayList<>();
// dynamicaliFoundSomeClass(initClassesName); dynamicaliFoundSomeClass(initClassesName);
initClassesName.addAll(List.of("cz.vsb.fei.lab.Score", "lab.Routines", "lab.App", "lab.DrawingThread")); initClassesName.addAll(List.of("cz.vsb.fei.lab.App", "lab.Routines", "lab.App", "lab.DrawingThread"));
for (String className : initClassesName) { for (String className : initClassesName) {
try { try {
Class.forName(className); Class.forName(className);
...@@ -210,7 +210,7 @@ class StructureHelper { ...@@ -210,7 +210,7 @@ class StructureHelper {
}))); })));
} }
for (String string : allClasses) { for (String string : allClasses) {
System.out.println(":::" + string); System.out.println(string);
} }
return allClasses; return allClasses;
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment