From 0464ea00699db51002caf7cd226c2b6a973ed725 Mon Sep 17 00:00:00 2001 From: jez04 <david.jezek@post.cz> Date: Tue, 3 Dec 2024 14:17:15 +0100 Subject: [PATCH] updated unit test --- pom.xml | 7 +- .../structure/test/ClassStructureTest.java | 120 +++++++----------- .../jez04/structure/test/StructureHelper.java | 105 +++++++++++---- 3 files changed, 134 insertions(+), 98 deletions(-) diff --git a/pom.xml b/pom.xml index 0d23916..13904e2 100644 --- a/pom.xml +++ b/pom.xml @@ -59,6 +59,11 @@ <version>0.10.2</version> <scope>test</scope> </dependency> - + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>hamcrest</artifactId> + <version>3.0</version> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/src/test/java/jez04/structure/test/ClassStructureTest.java b/src/test/java/jez04/structure/test/ClassStructureTest.java index 2a0188e..ae6627b 100644 --- a/src/test/java/jez04/structure/test/ClassStructureTest.java +++ b/src/test/java/jez04/structure/test/ClassStructureTest.java @@ -1,101 +1,71 @@ package jez04.structure.test; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.URISyntaxException; +import java.time.LocalDate; import java.util.List; +import org.hamcrest.Matcher; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; class ClassStructureTest { StructureHelper helper = new StructureHelper(); @Test - void mainScreenControllerExistenceTest() { - helper.classExist("MainScreenController"); - } - - @Test - void mainScreenControllerFxmlTest() { - helper.classExist("MainScreenController"); - Class<?> c = helper.getClass("MainScreenController"); - helper.hasPropertyWithAnnotation(c, ".*", FXML.class); - } + void dataImporterTest() throws URISyntaxException, IOException, IllegalAccessException, InvocationTargetException { + helper.classExist("DataImporter", false); + Class<?> d = helper.getClass("DataImporter", false); + String src = helper.getSourceCode(d); + helper.hasMethod(d, ".*down.*", false, String.class); + Method download = helper.getMethod(d, ".*down.*", false, String.class); + String downloadedText = (String) download.invoke(null); + assertThat(downloadedText, allOf( + endsWith("}]}"), + startsWith("{\"status\":\"OK\""), + stringContainsInOrder("firstname"), + stringContainsInOrder("lastname"), + stringContainsInOrder("birthday") + )); + + helper.hasMethod(d, ".*parse.*", false, List.class, String.class); + Method parse = helper.getMethod(d, ".*parse.*", false, List.class, String.class); + List<String> texts = (List<String>) parse.invoke(null, "{\"id\":1,\"firstname\":\"Tatum\",\"lastname\":\"Block\",\"email\":\"lonnie.bergstrom@stoltenberg.com\",\"phone\":\"+12397191764\",\"birthday\":\"1946-11-06\",\"gender\":\"male\",\"address\":{"); + assertThat(texts, not(empty())); - @Test - void mainScreenControllerButtonActionMethodTest() { - helper.classExist("MainScreenController"); - Class<?> c = helper.getClass("MainScreenController"); - long count = helper.countMethodRegexp(c, ".*", void.class, ActionEvent.class); - assertTrue(count > 1, "Only " + count+ " method handling button click found. Expected more then 1"); - } + helper.classExist("Person", false); + Class<?> p = helper.getClass("Person", false); + helper.hasMethod(d, ".*parse.*", false, p, String.class); + Method parsePerson = helper.getMethod(d, ".*parse.*", false, p, String.class); + Object person = parsePerson.invoke(null, "{\"id\":1,\"firstname\":\"Tatum\",\"lastname\":\"Block\",\"email\":\"lonnie.bergstrom@stoltenberg.com\",\"phone\":\"+12397191764\",\"birthday\":\"1946-11-06\",\"gender\":\"male\",\"address\":{"); + assertThat(person, notNullValue()); - @Test - void mainScreenControllerTableViewTest() { - helper.classExist("MainScreenController"); - Class<?> c = helper.getClass("MainScreenController"); - helper.hasProperty(c, ".*", TableView.class, false); - } - - @Test - void mainScreenControllerTableColumnTest() { - helper.classExist("MainScreenController"); - Class<?> c = helper.getClass("MainScreenController"); - helper.hasProperty(c, ".*", TableColumn.class, false); + Method age = helper.getMethod(p, ".*age.*", false, int.class); + int result = (int)age.invoke(person); + assertEquals(78, result , "Calculate wrong age."); + + Method birth50 = helper.getMethod(p, ".*50.*", false, LocalDate.class); + LocalDate ldBirth50 = (LocalDate)birth50.invoke(person); + assertEquals(LocalDate.of(1996, 11, 06), ldBirth50 , "Calculate wrong 50th birthday."); + + Method daysM = helper.getMethod(p, ".*days.*", false, long.class); + long days= (long)daysM.invoke(person); + assertEquals(338, days , "Calculate wrong days to next birthday."); } - @Test - void dbConnectorTest() { - helper.classExistRegexp(".*[Dd][Bb][cC]on.*"); - } - @Test - void dbConnectorGetAllTest() { - helper.classExistRegexp(".*[Dd][Bb][cC]on.*"); - Class<?> scoreClass = helper.getClassRegexp(".*[sS]core.*"); - Class<?> c = helper.getClassRegexp(".*[Dd][Bb][cC]on.*"); - helper.hasMethodRegexp(c, ".*[aA]ll.*", List.class); - helper.hasMethodRegexp(c, ".*[iI]nsert.*", void.class, scoreClass); - } - @Test - void dbConnectorInsertTest() { - helper.classExistRegexp(".*[Dd][Bb][cC]on.*"); - Class<?> scoreClass = helper.getClassRegexp(".*[sS]core.*"); - Class<?> c = helper.getClassRegexp(".*[Dd][Bb][cC]on.*"); - helper.hasMethodRegexp(c, ".*[iI]nsert.*", void.class, scoreClass); - } - @Test - void dbConnectorContainsJdbcTest() throws URISyntaxException, IOException { - helper.classExistRegexp(".*[Dd][Bb][cC]on.*"); - Class<?> c = helper.getClassRegexp(".*[Dd][Bb][cC]on.*"); - String src = helper.getSourceCode(c); - assertTrue(src.contains("jdbc:"), "No usage of jdbc detect."); - } - - @Test - void dbConnectorContainsDriverManagerTest() throws URISyntaxException, IOException { - helper.classExistRegexp(".*[Dd][Bb][cC]on.*"); - Class<?> c = helper.getClassRegexp(".*[Dd][Bb][cC]on.*"); - String src = helper.getSourceCode(c); - assertTrue(src.contains("DriverManager"), "No usage of DriverManager detect."); - } - - @Test - void dbConnectorContainsSqlTest() throws URISyntaxException, IOException { - helper.classExistRegexp(".*[Dd][Bb][cC]on.*"); - Class<?> c = helper.getClassRegexp(".*[Dd][Bb][cC]on.*"); - String src = helper.getSourceCode(c).toLowerCase(); - assertTrue(src.contains("create "), "No usage of SQL create."); - assertTrue(src.contains("select "), "No usage of SQL select."); - assertTrue(src.contains("insert "), "No usage of SQL table."); - assertTrue(src.contains(" from "), "No usage of SQL from."); - assertTrue(src.contains(" table"), "No usage of SQL table."); - } } diff --git a/src/test/java/jez04/structure/test/StructureHelper.java b/src/test/java/jez04/structure/test/StructureHelper.java index f3aed5d..6c18100 100644 --- a/src/test/java/jez04/structure/test/StructureHelper.java +++ b/src/test/java/jez04/structure/test/StructureHelper.java @@ -45,11 +45,18 @@ class StructureHelper { } public void classExist(String name) { - assertTrue(allClasses.stream().anyMatch(c -> c.endsWith(name)), "Class/Interface " + name + " not found"); + classExist(name, true); + } + public void classExist(String name, boolean caseSensitive) { + assertTrue(allClasses.stream().anyMatch(c -> caseSensitive?c.endsWith(name):c.toLowerCase().endsWith(name.toLowerCase())), "Class/Interface " + name + " not found"); } public void classExistRegexp(String name) { - assertTrue(allClasses.stream().anyMatch(c -> c.matches(name)), "Class/Interface " + name + " not found"); + classExistRegexp(name, true); + } + + public void classExistRegexp(String name, boolean caseSensitive) { + assertTrue(allClasses.stream().anyMatch(c -> caseSensitive?c.matches(name):c.toLowerCase().matches(name.toLowerCase())), "Class/Interface " + name + " not found"); } public Class<?> getClassDirectly(String name) { @@ -57,15 +64,25 @@ class StructureHelper { } public Class<?> getClassRegexp(String name) { - String className = allClasses.stream().filter(c -> c.matches(name)).findAny().orElse(null); + return getClassRegexp(name, true); + } + + public Class<?> getClassRegexp(String name, boolean caseSensitive) { + String className = allClasses.stream().filter(c -> + caseSensitive? + c.matches(name): + c.toLowerCase().matches(name.toLowerCase()) + ).findAny().orElse(null); if (className == null) { Assertions.fail("Class " + name + " not found."); } return loadClass(name, className); } - public Class<?> getClass(String name) { - String className = allClasses.stream().filter(c -> c.endsWith(name)).findAny().orElse(null); + return getClass(name, true); + } + public Class<?> getClass(String name, boolean caseSensitive) { + String className = allClasses.stream().filter(c -> caseSensitive?c.endsWith(name):c.toLowerCase().endsWith(name.toLowerCase())).findAny().orElse(null); if (className == null) { Assertions.fail("Class " + name + " not found."); } @@ -89,9 +106,12 @@ class StructureHelper { } public void hasProperty(Class<?> classDef, String propertyNameRegexp, Class<?> type, boolean array) { + hasProperty(classDef, propertyNameRegexp, type, array, true); + } + public void hasProperty(Class<?> classDef, String propertyNameRegexp, Class<?> type, boolean array, boolean caseSensitive) { List<Field> fields = Arrays.asList(classDef.getDeclaredFields()); assertTrue(fields.stream().anyMatch(f -> { - if (f.getName().matches(propertyNameRegexp)) { + if (caseSensitive?f.getName().matches(propertyNameRegexp):f.getName().toLowerCase().matches(propertyNameRegexp.toLowerCase())) { if (array) { return f.getType().isArray() && f.getType().getComponentType().equals(type); } else { @@ -104,9 +124,13 @@ class StructureHelper { } public void hasPropertyWithAnnotation(Class<?> classDef, String propertyNameRegexp, Class<?> annotation) { + hasPropertyWithAnnotation(classDef, propertyNameRegexp, annotation, true); + } + + public void hasPropertyWithAnnotation(Class<?> classDef, String propertyNameRegexp, Class<?> annotation, boolean caseSensitive) { List<Field> fields = Arrays.asList(classDef.getDeclaredFields()); assertTrue( - fields.stream().filter(f -> f.getName().matches(propertyNameRegexp)) + fields.stream().filter(f -> caseSensitive?f.getName().matches(propertyNameRegexp):f.getName().toLowerCase().matches(propertyNameRegexp.toLowerCase())) .flatMap(f -> Arrays.asList(f.getAnnotations()).stream()).map(a -> a.annotationType()) .anyMatch(a -> a.equals(annotation)), "No field " + propertyNameRegexp + " with annotation " + annotation.getName() + " in class " @@ -114,28 +138,39 @@ class StructureHelper { } public void hasMethod(Class<?> interfaceDef, String methodName, Class<?> returnType) { + hasMethod(interfaceDef, methodName, returnType, true); + } + public void hasMethod(Class<?> interfaceDef, String methodName, Class<?> returnType, boolean caseSensitive) { List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods()); assertTrue(methods.stream().anyMatch(m -> m.getName().contains(methodName)), "No method " + methodName); assertTrue( - methods.stream().filter(m -> m.getName().contains(methodName)) + methods.stream().filter(m -> caseSensitive?m.getName().matches(methodName):m.getName().toLowerCase().matches(methodName.toLowerCase())) .anyMatch(m -> m.getReturnType().equals(returnType)), "Method " + methodName + " not return " + returnType.getName()); } public void hasMethod(Class<?> interfaceDef, String methodName, Class<?> returnType, Class<?>... params) { + hasMethod(interfaceDef, methodName, true, returnType, params); + } + + public void hasMethod(Class<?> interfaceDef, String methodName, boolean caseSensitive, Class<?> returnType, Class<?>... params) { List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods()); - assertTrue(methods.stream().anyMatch(m -> m.getName().contains(methodName)), "No method " + methodName); + assertTrue(methods.stream().anyMatch(m -> caseSensitive?m.getName().matches(methodName):m.getName().toLowerCase().matches(methodName.toLowerCase())), "No method " + methodName); assertTrue( - methods.stream().filter(m -> m.getName().contains(methodName)) + methods.stream().filter(m -> caseSensitive?m.getName().matches(methodName):m.getName().toLowerCase().matches(methodName.toLowerCase())) .filter(m -> m.getReturnType().equals(returnType)) .anyMatch(m -> Arrays.asList(m.getParameterTypes()).containsAll(Arrays.asList(params))), "Method " + methodName + " has no all parrams:" + Arrays.asList(params).stream().map(Class::getName).collect(Collectors.joining(", "))); } - public Method getMethod(Class<?> interfaceDef, String methodName, Class<?> returnType, Class<?>... params) { + public Method getMethod(Class<?> interfaceDef, String methodName,Class<?> returnType, Class<?>... params) { + return getMethod(interfaceDef, methodName, true, returnType, params); + } + + public Method getMethod(Class<?> interfaceDef, String methodName, boolean caseSensitive, Class<?> returnType, Class<?>... params) { List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods()); - List<Method> foundMethods = methods.stream().filter(m -> m.getName().contains(methodName)) + List<Method> foundMethods = methods.stream().filter(m -> caseSensitive?m.getName().matches(methodName):m.getName().toLowerCase().matches(methodName.toLowerCase())) .filter(m -> m.getReturnType().equals(returnType)) .filter(m -> Arrays.asList(m.getParameterTypes()).containsAll(Arrays.asList(params))).toList(); if (foundMethods.isEmpty()) { @@ -148,8 +183,12 @@ class StructureHelper { } public long countMethodRegexp(Class<?> interfaceDef, String methodNameRegexp) { + return countMethodRegexp(interfaceDef, methodNameRegexp, true); + } + + public long countMethodRegexp(Class<?> interfaceDef, String methodNameRegexp, boolean caseSensitive) { List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods()); - return methods.stream().filter(m -> m.getName().matches(methodNameRegexp)).count(); + return methods.stream().filter(m -> caseSensitive?m.getName().matches(methodNameRegexp):m.getName().toLowerCase().matches(methodNameRegexp.toLowerCase())).count(); } public long countMethodReference(Class<?> interfaceDef) throws URISyntaxException, IOException { @@ -170,7 +209,10 @@ class StructureHelper { } public long countClassesRegexp(String classNameRegexp) { - return getNameOfAllClasses().stream().filter(className -> className.matches(classNameRegexp)).count(); + return countClassesRegexp(classNameRegexp, true); + } + public long countClassesRegexp(String classNameRegexp, boolean caseSensitive) { + return getNameOfAllClasses().stream().filter(className -> caseSensitive?className.matches(classNameRegexp):className.toLowerCase().matches(classNameRegexp.toLowerCase())).count(); } public void hasConstructor(Class<?> classDef, Class<?>... params) { @@ -192,14 +234,19 @@ class StructureHelper { } return foundConstructors.get(0); } - + public void hasMethodRegexp(Class<?> interfaceDef, String methodNameRegexp, Class<?> returnType, Class<?>... params) { + hasMethodRegexp(interfaceDef, methodNameRegexp, true, returnType, params); + } + + public void hasMethodRegexp(Class<?> interfaceDef, String methodNameRegexp, boolean caseSensitive, Class<?> returnType, + Class<?>... params) { List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods()); - assertTrue(methods.stream().anyMatch(m -> m.getName().matches(methodNameRegexp)), + assertTrue(methods.stream().anyMatch(m -> caseSensitive?m.getName().matches(methodNameRegexp):m.getName().toLowerCase().matches(methodNameRegexp.toLowerCase())), "No method " + methodNameRegexp); assertTrue( - methods.stream().filter(m -> m.getName().matches(methodNameRegexp)) + methods.stream().filter(m -> caseSensitive?m.getName().matches(methodNameRegexp):m.getName().toLowerCase().matches(methodNameRegexp.toLowerCase())) .filter(m -> m.getReturnType().equals(returnType)) .anyMatch(m -> Arrays.asList(m.getParameterTypes()).containsAll(Arrays.asList(params))), "Method " + methodNameRegexp + " has no all parrams:" @@ -208,20 +255,30 @@ class StructureHelper { public long countMethodRegexp(Class<?> interfaceDef, String methodNameRegexp, Class<?> returnType, Class<?>... params) { + return countMethodRegexp(interfaceDef, methodNameRegexp, true, returnType, params); + } + + public long countMethodRegexp(Class<?> interfaceDef, String methodNameRegexp, boolean caseSensitive, Class<?> returnType, + Class<?>... params) { List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods()); - assertTrue(methods.stream().anyMatch(m -> m.getName().matches(methodNameRegexp)), + assertTrue(methods.stream().anyMatch(m -> caseSensitive?m.getName().matches(methodNameRegexp):m.getName().toLowerCase().matches(methodNameRegexp.toLowerCase())), "No method " + methodNameRegexp); - return methods.stream().filter(m -> m.getName().matches(methodNameRegexp)) + return methods.stream().filter(m -> caseSensitive?m.getName().matches(methodNameRegexp):m.getName().toLowerCase().matches(methodNameRegexp.toLowerCase())) .filter(m -> m.getReturnType().equals(returnType)) .filter(m -> Arrays.asList(m.getParameterTypes()).containsAll(Arrays.asList(params))).count(); } public void hasMethod(Class<?> interfaceDef, boolean finalTag, boolean abstractTag, String methodName, Class<?> returnType, Class<?>... params) { + hasMethod(interfaceDef, finalTag, abstractTag, methodName, true, returnType, params); + } + + public void hasMethod(Class<?> interfaceDef, boolean finalTag, boolean abstractTag, String methodName, boolean caseSensitive, + Class<?> returnType, Class<?>... params) { List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods()); - assertTrue(methods.stream().anyMatch(m -> m.getName().contains(methodName)), "No method " + methodName); + assertTrue(methods.stream().anyMatch(m -> caseSensitive?m.getName().matches(methodName):m.getName().toLowerCase().matches(methodName.toLowerCase())), "No method " + methodName); assertTrue( - methods.stream().filter(m -> m.getName().contains(methodName)) + methods.stream().filter(m -> caseSensitive?m.getName().matches(methodName):m.getName().toLowerCase().matches(methodName.toLowerCase())) .filter(m -> m.getReturnType().equals(returnType) && (Modifier.isAbstract(m.getModifiers()) == abstractTag) && (Modifier.isFinal(m.getModifiers()) == finalTag)) @@ -249,8 +306,12 @@ class StructureHelper { } public void hasMethod(Class<?> interfaceDef, String methodName) { + hasMethod(interfaceDef, methodName, true); + } + + public void hasMethod(Class<?> interfaceDef, String methodName, boolean caseSensitive) { List<Method> methods = Arrays.asList(interfaceDef.getMethods()); - assertTrue(methods.stream().anyMatch(m -> m.getName().contains(methodName)), "No method " + methodName); + assertTrue(methods.stream().anyMatch(m -> caseSensitive?m.getName().matches(methodName):m.getName().toLowerCase().matches(methodName.toLowerCase())), "No method " + methodName); } public String getSourceCode(Class<?> clazz) throws URISyntaxException, IOException { -- GitLab