From f32b201e0f002cdf1b9e235e877528abd56e421e Mon Sep 17 00:00:00 2001 From: jez04 <david.jezek@post.cz> Date: Tue, 12 Nov 2024 14:20:14 +0100 Subject: [PATCH] feat: update test --- pom.xml | 2 +- .../structure/test/ClassStructureTest.java | 46 +++++++++++++------ .../jez04/structure/test/StructureHelper.java | 6 +-- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/pom.xml b/pom.xml index d63b6d0..42ec98c 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cz.vsb.fei</groupId> - <artifactId>lab08v1</artifactId> + <artifactId>lab08v2</artifactId> <version>0.0.1-SNAPSHOT</version> <name>lab08v1</name> diff --git a/src/test/java/jez04/structure/test/ClassStructureTest.java b/src/test/java/jez04/structure/test/ClassStructureTest.java index 3d40105..241d7af 100644 --- a/src/test/java/jez04/structure/test/ClassStructureTest.java +++ b/src/test/java/jez04/structure/test/ClassStructureTest.java @@ -5,10 +5,15 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; -import java.io.FileNotFoundException; +import java.io.IOException; 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.Collection; +import java.util.List; import org.junit.jupiter.api.Test; @@ -20,19 +25,20 @@ class ClassStructureTest { StructureHelper helper = new StructureHelper(); private static String className = "Score"; - + @Test void scoreExistenceTest() { // 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, "points", int.class, false); helper.hasMethod(c, "generate", Score.class); } + @Test void scoreNotLoadedExistenceTest() { // 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.hasExtends(c, Exception.class); } @@ -42,19 +48,25 @@ class ClassStructureTest { App app = new App(); assertTrue(app.generateScores(10).size() == 10); } + @Test void loadScoresFromStreamTest() throws Exception { 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 Exception { + void loadScoresFromStreamExceptionTest() throws ScoreNotLoaded, IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException { App app = new App(); try { - app.loadScoresFromStream(new InputStreamReader(App.class.getResourceAsStream("/bestScores-err.csv"))).size(); - fail("Exception not throwen"); + app.loadScoresFromStream(new InputStreamReader(App.class.getResourceAsStream("/bestScores-err.csv"))) + .size(); + fail("Exception not throwen!"); } catch (Exception e) { - assertEquals(e.getClass(), ScoreNotLoaded.class, "Expected exception of type ScoreNotLoaded"); + 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"); } } @@ -63,14 +75,22 @@ class ClassStructureTest { app.loadScores(); } - void saveScoresTest() throws FileNotFoundException { + void saveScoresTest() throws IOException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { 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.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."); } } diff --git a/src/test/java/jez04/structure/test/StructureHelper.java b/src/test/java/jez04/structure/test/StructureHelper.java index 90e27b6..88fc05e 100644 --- a/src/test/java/jez04/structure/test/StructureHelper.java +++ b/src/test/java/jez04/structure/test/StructureHelper.java @@ -182,8 +182,8 @@ class StructureHelper { public Set<String> getNameOfAllClasses() { List<String> initClassesName = new ArrayList<>(); -// dynamicaliFoundSomeClass(initClassesName); - initClassesName.addAll(List.of("cz.vsb.fei.lab.Score", "lab.Routines", "lab.App", "lab.DrawingThread")); + dynamicaliFoundSomeClass(initClassesName); + initClassesName.addAll(List.of("cz.vsb.fei.lab.App", "lab.Routines", "lab.App", "lab.DrawingThread")); for (String className : initClassesName) { try { Class.forName(className); @@ -210,7 +210,7 @@ class StructureHelper { }))); } for (String string : allClasses) { - System.out.println(":::" + string); + System.out.println(string); } return allClasses; } -- GitLab