Skip to content
Snippets Groups Projects
Commit 7f714782 authored by jez04's avatar jez04
Browse files

feat: :tada: solution

parent 8e204918
No related merge requests found
package jez04.structure.test;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.regex.Pattern;
import org.hamcrest.Description;
public class SrcContains extends StructureMatcher<Class<?>> {
private String regexp;
private boolean caseInsensitive;
private boolean srcFound;
public SrcContains(String regexp, boolean caseInsensitive) {
this.regexp = regexp;
this.caseInsensitive = caseInsensitive;
}
@Override
public boolean matches(Object actual) {
srcFound = true;
if (actual instanceof Class c) {
Pattern p = Pattern.compile(regexp);
if (caseInsensitive) {
p = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
}
try {
return p.matcher(structureHelper.getSourceCode(c)).find();
} catch (URISyntaxException | IOException e) {
srcFound = false;
e.printStackTrace();
return false;
}
}
return false;
}
@Override
public void describeTo(Description description) {
description.appendText(String.format("Source code of class shoud contains regexp '%s'%s", regexp,
caseInsensitive ? " in case insensitive mode" : ""));
}
@Override
public void describeMismatch(Object item, Description description) {
if (srcFound) {
description
.appendText(String.format("source code of class %s do not contains substring that match reg exp", item));
} else {
description.appendText(String.format("source code of class %s was not found"));
}
}
}
...@@ -29,7 +29,10 @@ import java.util.function.Predicate; ...@@ -29,7 +29,10 @@ import java.util.function.Predicate;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.reflections.Configuration; import org.reflections.Configuration;
import org.reflections.Reflections; import org.reflections.Reflections;
...@@ -38,8 +41,25 @@ import org.reflections.util.ConfigurationBuilder; ...@@ -38,8 +41,25 @@ import org.reflections.util.ConfigurationBuilder;
class StructureHelper { class StructureHelper {
private static StructureHelper singeltonInstance;
public static StructureHelper getInstance() {
if (singeltonInstance == null) {
singeltonInstance = new StructureHelper();
}
return singeltonInstance;
}
Set<String> allClasses = getNameOfAllClasses(); Set<String> allClasses = getNameOfAllClasses();
private StructureHelper() {
/* hide public one */
}
public Set<String> getAllClasses() {
return allClasses;
}
public void isInterface(Class<?> c) { public void isInterface(Class<?> c) {
assertTrue(c.isInterface(), c.getName() + " have to be interface."); assertTrue(c.isInterface(), c.getName() + " have to be interface.");
} }
...@@ -47,16 +67,23 @@ class StructureHelper { ...@@ -47,16 +67,23 @@ class StructureHelper {
public void classExist(String name) { public void classExist(String name) {
classExist(name, true); classExist(name, true);
} }
public void classExist(String name, boolean caseSensitive) { 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"); assertTrue(
allClasses.stream()
.anyMatch(c -> caseSensitive ? c.endsWith(name) : c.toLowerCase().endsWith(name.toLowerCase())),
"Class/Interface " + name + " not found");
} }
public void classExistRegexp(String name) { public void classExistRegexp(String name) {
classExistRegexp(name, true); classExistRegexp(name, true);
} }
public void classExistRegexp(String name, boolean caseSensitive) { 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"); assertTrue(
allClasses.stream()
.anyMatch(c -> caseSensitive ? c.matches(name) : c.toLowerCase().matches(name.toLowerCase())),
"Class/Interface " + name + " not found");
} }
public Class<?> getClassDirectly(String name) { public Class<?> getClassDirectly(String name) {
...@@ -66,23 +93,25 @@ class StructureHelper { ...@@ -66,23 +93,25 @@ class StructureHelper {
public Class<?> getClassRegexp(String name) { public Class<?> getClassRegexp(String name) {
return getClassRegexp(name, true); return getClassRegexp(name, true);
} }
public Class<?> getClassRegexp(String name, boolean caseSensitive) { public Class<?> getClassRegexp(String name, boolean caseSensitive) {
String className = allClasses.stream().filter(c -> String className = allClasses.stream()
caseSensitive? .filter(c -> caseSensitive ? c.matches(name) : c.toLowerCase().matches(name.toLowerCase())).findAny()
c.matches(name): .orElse(null);
c.toLowerCase().matches(name.toLowerCase())
).findAny().orElse(null);
if (className == null) { if (className == null) {
Assertions.fail("Class " + name + " not found."); Assertions.fail("Class " + name + " not found.");
} }
return loadClass(name, className); return loadClass(name, className);
} }
public Class<?> getClass(String name) { public Class<?> getClass(String name) {
return getClass(name, true); return getClass(name, true);
} }
public Class<?> getClass(String name, boolean caseSensitive) { 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); String className = allClasses.stream()
.filter(c -> caseSensitive ? c.endsWith(name) : c.toLowerCase().endsWith(name.toLowerCase())).findAny()
.orElse(null);
if (className == null) { if (className == null) {
Assertions.fail("Class " + name + " not found."); Assertions.fail("Class " + name + " not found.");
} }
...@@ -105,13 +134,27 @@ class StructureHelper { ...@@ -105,13 +134,27 @@ class StructureHelper {
} }
} }
public org.hamcrest.Matcher<Class<?>> hasProperty(String propertyNameRegexp, Class<?> type, boolean array) {
return new HasProperty(propertyNameRegexp, type, array);
}
public void hasProperty(Class<?> classDef, String propertyNameRegexp, Class<?> type, boolean array) { public void hasProperty(Class<?> classDef, String propertyNameRegexp, Class<?> type, boolean array) {
hasProperty(classDef, propertyNameRegexp, type, array, true); hasProperty(classDef, propertyNameRegexp, type, array, true);
} }
public void hasProperty(Class<?> classDef, String propertyNameRegexp, Class<?> type, boolean array, boolean caseSensitive) {
public void hasProperty(Class<?> classDef, String propertyNameRegexp, Class<?> type, boolean array,
boolean caseSensitive) {
assertTrue(hasPropertyB(classDef, propertyNameRegexp, type, array, caseSensitive),
"No field " + propertyNameRegexp + " of type " + type.getName() + " (is array " + array + ") in class "
+ classDef.getName());
}
public boolean hasPropertyB(Class<?> classDef, String propertyNameRegexp, Class<?> type, boolean array,
boolean caseSensitive) {
List<Field> fields = Arrays.asList(classDef.getDeclaredFields()); List<Field> fields = Arrays.asList(classDef.getDeclaredFields());
assertTrue(fields.stream().anyMatch(f -> { return fields.stream().anyMatch(f -> {
if (caseSensitive?f.getName().matches(propertyNameRegexp):f.getName().toLowerCase().matches(propertyNameRegexp.toLowerCase())) { if (caseSensitive ? f.getName().matches(propertyNameRegexp)
: f.getName().toLowerCase().matches(propertyNameRegexp.toLowerCase())) {
if (array) { if (array) {
return f.getType().isArray() && f.getType().getComponentType().equals(type); return f.getType().isArray() && f.getType().getComponentType().equals(type);
} else { } else {
...@@ -119,18 +162,20 @@ class StructureHelper { ...@@ -119,18 +162,20 @@ class StructureHelper {
} }
} }
return false; return false;
}), "No field " + propertyNameRegexp + " of type " + type.getName() + " (is array " + array + ") in class " });
+ classDef.getName());
} }
public void hasPropertyWithAnnotation(Class<?> classDef, String propertyNameRegexp, Class<?> annotation) { public void hasPropertyWithAnnotation(Class<?> classDef, String propertyNameRegexp, Class<?> annotation) {
hasPropertyWithAnnotation(classDef, propertyNameRegexp, annotation, true); hasPropertyWithAnnotation(classDef, propertyNameRegexp, annotation, true);
} }
public void hasPropertyWithAnnotation(Class<?> classDef, String propertyNameRegexp, Class<?> annotation, boolean caseSensitive) { public void hasPropertyWithAnnotation(Class<?> classDef, String propertyNameRegexp, Class<?> annotation,
boolean caseSensitive) {
List<Field> fields = Arrays.asList(classDef.getDeclaredFields()); List<Field> fields = Arrays.asList(classDef.getDeclaredFields());
assertTrue( assertTrue(
fields.stream().filter(f -> caseSensitive?f.getName().matches(propertyNameRegexp):f.getName().toLowerCase().matches(propertyNameRegexp.toLowerCase())) 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()) .flatMap(f -> Arrays.asList(f.getAnnotations()).stream()).map(a -> a.annotationType())
.anyMatch(a -> a.equals(annotation)), .anyMatch(a -> a.equals(annotation)),
"No field " + propertyNameRegexp + " with annotation " + annotation.getName() + " in class " "No field " + propertyNameRegexp + " with annotation " + annotation.getName() + " in class "
...@@ -140,11 +185,14 @@ class StructureHelper { ...@@ -140,11 +185,14 @@ class StructureHelper {
public void hasMethod(Class<?> interfaceDef, String methodName, Class<?> returnType) { public void hasMethod(Class<?> interfaceDef, String methodName, Class<?> returnType) {
hasMethod(interfaceDef, methodName, returnType, true); hasMethod(interfaceDef, methodName, returnType, true);
} }
public void hasMethod(Class<?> interfaceDef, String methodName, Class<?> returnType, boolean caseSensitive) { public void hasMethod(Class<?> interfaceDef, String methodName, Class<?> returnType, boolean caseSensitive) {
List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods()); List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods());
assertTrue(methods.stream().anyMatch(m -> m.getName().contains(methodName)), "No method " + methodName); assertTrue(methods.stream().anyMatch(m -> m.getName().contains(methodName)), "No method " + methodName);
assertTrue( assertTrue(
methods.stream().filter(m -> caseSensitive?m.getName().matches(methodName):m.getName().toLowerCase().matches(methodName.toLowerCase())) methods.stream()
.filter(m -> caseSensitive ? m.getName().matches(methodName)
: m.getName().toLowerCase().matches(methodName.toLowerCase()))
.anyMatch(m -> m.getReturnType().equals(returnType)), .anyMatch(m -> m.getReturnType().equals(returnType)),
"Method " + methodName + " not return " + returnType.getName()); "Method " + methodName + " not return " + returnType.getName());
} }
...@@ -152,25 +200,35 @@ class StructureHelper { ...@@ -152,25 +200,35 @@ class StructureHelper {
public void hasMethod(Class<?> interfaceDef, String methodName, Class<?> returnType, Class<?>... params) { public void hasMethod(Class<?> interfaceDef, String methodName, Class<?> returnType, Class<?>... params) {
hasMethod(interfaceDef, methodName, true, returnType, params); hasMethod(interfaceDef, methodName, true, returnType, params);
} }
public void hasMethod(Class<?> interfaceDef, String methodName, boolean caseSensitive, Class<?> returnType, Class<?>... params) { public void hasMethod(Class<?> interfaceDef, String methodName, boolean caseSensitive, Class<?> returnType,
Class<?>... params) {
List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods()); List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods());
assertTrue(methods.stream().anyMatch(m -> caseSensitive?m.getName().matches(methodName):m.getName().toLowerCase().matches(methodName.toLowerCase())), "No method " + methodName);
assertTrue( assertTrue(
methods.stream().filter(m -> caseSensitive?m.getName().matches(methodName):m.getName().toLowerCase().matches(methodName.toLowerCase())) methods.stream()
.anyMatch(m -> caseSensitive ? m.getName().matches(methodName)
: m.getName().toLowerCase().matches(methodName.toLowerCase())),
"No method " + methodName);
assertTrue(
methods.stream()
.filter(m -> caseSensitive ? m.getName().matches(methodName)
: m.getName().toLowerCase().matches(methodName.toLowerCase()))
.filter(m -> m.getReturnType().equals(returnType)) .filter(m -> m.getReturnType().equals(returnType))
.anyMatch(m -> Arrays.asList(m.getParameterTypes()).containsAll(Arrays.asList(params))), .anyMatch(m -> Arrays.asList(m.getParameterTypes()).containsAll(Arrays.asList(params))),
"Method " + methodName + " has no all parrams:" "Method " + methodName + " has no all parrams:"
+ Arrays.asList(params).stream().map(Class::getName).collect(Collectors.joining(", "))); + 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); return getMethod(interfaceDef, methodName, true, returnType, params);
} }
public Method getMethod(Class<?> interfaceDef, String methodName, boolean caseSensitive, Class<?> returnType, Class<?>... params) { public Method getMethod(Class<?> interfaceDef, String methodName, boolean caseSensitive, Class<?> returnType,
Class<?>... params) {
List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods()); List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods());
List<Method> foundMethods = methods.stream().filter(m -> caseSensitive?m.getName().matches(methodName):m.getName().toLowerCase().matches(methodName.toLowerCase())) 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 -> m.getReturnType().equals(returnType))
.filter(m -> Arrays.asList(m.getParameterTypes()).containsAll(Arrays.asList(params))).toList(); .filter(m -> Arrays.asList(m.getParameterTypes()).containsAll(Arrays.asList(params))).toList();
if (foundMethods.isEmpty()) { if (foundMethods.isEmpty()) {
...@@ -185,10 +243,11 @@ class StructureHelper { ...@@ -185,10 +243,11 @@ class StructureHelper {
public long countMethodRegexp(Class<?> interfaceDef, String methodNameRegexp) { public long countMethodRegexp(Class<?> interfaceDef, String methodNameRegexp) {
return countMethodRegexp(interfaceDef, methodNameRegexp, true); return countMethodRegexp(interfaceDef, methodNameRegexp, true);
} }
public long countMethodRegexp(Class<?> interfaceDef, String methodNameRegexp, boolean caseSensitive) { public long countMethodRegexp(Class<?> interfaceDef, String methodNameRegexp, boolean caseSensitive) {
List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods()); List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods());
return methods.stream().filter(m -> caseSensitive?m.getName().matches(methodNameRegexp):m.getName().toLowerCase().matches(methodNameRegexp.toLowerCase())).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 { public long countMethodReference(Class<?> interfaceDef) throws URISyntaxException, IOException {
...@@ -211,8 +270,10 @@ class StructureHelper { ...@@ -211,8 +270,10 @@ class StructureHelper {
public long countClassesRegexp(String classNameRegexp) { public long countClassesRegexp(String classNameRegexp) {
return countClassesRegexp(classNameRegexp, true); return countClassesRegexp(classNameRegexp, true);
} }
public long countClassesRegexp(String classNameRegexp, boolean caseSensitive) { public long countClassesRegexp(String classNameRegexp, boolean caseSensitive) {
return getNameOfAllClasses().stream().filter(className -> caseSensitive?className.matches(classNameRegexp):className.toLowerCase().matches(classNameRegexp.toLowerCase())).count(); return getNameOfAllClasses().stream().filter(className -> caseSensitive ? className.matches(classNameRegexp)
: className.toLowerCase().matches(classNameRegexp.toLowerCase())).count();
} }
public void hasConstructor(Class<?> classDef, Class<?>... params) { public void hasConstructor(Class<?> classDef, Class<?>... params) {
...@@ -234,51 +295,106 @@ class StructureHelper { ...@@ -234,51 +295,106 @@ class StructureHelper {
} }
return foundConstructors.get(0); return foundConstructors.get(0);
} }
public void hasMethodRegexp(Class<?> interfaceDef, String methodNameRegexp, Class<?> returnType, public void hasMethodRegexp(Class<?> interfaceDef, String methodNameRegexp, Class<?> returnType,
Class<?>... params) { Class<?>... params) {
hasMethodRegexp(interfaceDef, methodNameRegexp, true, returnType, params); hasMethodRegexp(interfaceDef, methodNameRegexp, true, returnType, params);
} }
public void hasMethodRegexp(Class<?> interfaceDef, String methodNameRegexp, boolean caseSensitive, Class<?> returnType, public void hasMethodRegexp(Class<?> interfaceDef, String methodNameRegexp, boolean caseSensitive,
Class<?>... params) { Class<?> returnType, Class<?>... params) {
List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods()); List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods());
assertTrue(methods.stream().anyMatch(m -> caseSensitive?m.getName().matches(methodNameRegexp):m.getName().toLowerCase().matches(methodNameRegexp.toLowerCase())), assertTrue(
methods.stream()
.anyMatch(m -> caseSensitive ? m.getName().matches(methodNameRegexp)
: m.getName().toLowerCase().matches(methodNameRegexp.toLowerCase())),
"No method " + methodNameRegexp); "No method " + methodNameRegexp);
assertTrue( assertTrue(
methods.stream().filter(m -> caseSensitive?m.getName().matches(methodNameRegexp):m.getName().toLowerCase().matches(methodNameRegexp.toLowerCase())) methods.stream()
.filter(m -> caseSensitive ? m.getName().matches(methodNameRegexp)
: m.getName().toLowerCase().matches(methodNameRegexp.toLowerCase()))
.filter(m -> m.getReturnType().equals(returnType)) .filter(m -> m.getReturnType().equals(returnType))
.anyMatch(m -> Arrays.asList(m.getParameterTypes()).containsAll(Arrays.asList(params))), .anyMatch(m -> Arrays.asList(m.getParameterTypes()).containsAll(Arrays.asList(params))),
"Method " + methodNameRegexp + " has no all parrams:" "Method " + methodNameRegexp + " has no all parrams:"
+ Arrays.asList(params).stream().map(Class::getName).collect(Collectors.joining(", "))); + Arrays.asList(params).stream().map(Class::getName).collect(Collectors.joining(", ")));
} }
public boolean hasMethodRegexpTest(Class<?> interfaceDef, String methodNameRegexp, boolean caseSensitive,
Class<?> returnType, Class<?>... params) {
return hasMethodRegexpTest(interfaceDef, methodNameRegexp, caseSensitive, returnType, List.of(params));
}
public boolean hasMethodRegexpTest(Class<?> interfaceDef, String methodNameRegexp, boolean caseSensitive,
Class<?> returnType, List<Class<?>> params) {
List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods());
if (!methods.stream().anyMatch(m -> caseSensitive ? m.getName().matches(methodNameRegexp)
: m.getName().toLowerCase().matches(methodNameRegexp.toLowerCase()))) {
return false;
}
return 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(params));
}
public long countMethodRegexp(Class<?> interfaceDef, String methodNameRegexp, Class<?> returnType, public long countMethodRegexp(Class<?> interfaceDef, String methodNameRegexp, Class<?> returnType,
Class<?>... params) { Class<?>... params) {
return countMethodRegexp(interfaceDef, methodNameRegexp, true, returnType, params); return countMethodRegexp(interfaceDef, methodNameRegexp, true, returnType, params);
} }
public long countMethodRegexp(Class<?> interfaceDef, String methodNameRegexp, boolean caseSensitive, Class<?> returnType, public long countMethodRegexp(Class<?> interfaceDef, String methodNameRegexp, boolean caseSensitive,
Class<?>... params) { Class<?> returnType, Class<?>... params) {
List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods()); List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods());
assertTrue(methods.stream().anyMatch(m -> caseSensitive?m.getName().matches(methodNameRegexp):m.getName().toLowerCase().matches(methodNameRegexp.toLowerCase())), assertTrue(
methods.stream()
.anyMatch(m -> caseSensitive ? m.getName().matches(methodNameRegexp)
: m.getName().toLowerCase().matches(methodNameRegexp.toLowerCase())),
"No method " + methodNameRegexp); "No method " + methodNameRegexp);
return methods.stream().filter(m -> caseSensitive?m.getName().matches(methodNameRegexp):m.getName().toLowerCase().matches(methodNameRegexp.toLowerCase())) return methods.stream()
.filter(m -> caseSensitive ? m.getName().matches(methodNameRegexp)
: m.getName().toLowerCase().matches(methodNameRegexp.toLowerCase()))
.filter(m -> m.getReturnType().equals(returnType)) .filter(m -> m.getReturnType().equals(returnType))
.filter(m -> Arrays.asList(m.getParameterTypes()).containsAll(Arrays.asList(params))).count(); .filter(m -> Arrays.asList(m.getParameterTypes()).containsAll(Arrays.asList(params))).count();
} }
public boolean hasMethodTest(Class<?> interfaceDef, boolean finalTag, boolean abstractTag, String methodName,
boolean caseSensitive, Class<?> returnType, Class<?>... params) {
return hasMethodTest(interfaceDef, finalTag, abstractTag, methodName, caseSensitive, returnType, List.of(params));
}
public boolean hasMethodTest(Class<?> interfaceDef, boolean finalTag, boolean abstractTag, String methodName,
boolean caseSensitive, Class<?> returnType, List<Class<?>> params) {
List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods());
if (!methods.stream().anyMatch(m -> caseSensitive ? m.getName().matches(methodName)
: m.getName().toLowerCase().matches(methodName.toLowerCase()))) {
return false;
}
return 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))
.anyMatch(m -> Arrays.asList(m.getParameterTypes()).containsAll(params));
}
public void hasMethod(Class<?> interfaceDef, boolean finalTag, boolean abstractTag, String methodName, public void hasMethod(Class<?> interfaceDef, boolean finalTag, boolean abstractTag, String methodName,
Class<?> returnType, Class<?>... params) { Class<?> returnType, Class<?>... params) {
hasMethod(interfaceDef, finalTag, abstractTag, methodName, true, returnType, params); hasMethod(interfaceDef, finalTag, abstractTag, methodName, true, returnType, params);
} }
public void hasMethod(Class<?> interfaceDef, boolean finalTag, boolean abstractTag, String methodName, boolean caseSensitive, public void hasMethod(Class<?> interfaceDef, boolean finalTag, boolean abstractTag, String methodName,
Class<?> returnType, Class<?>... params) { boolean caseSensitive, Class<?> returnType, Class<?>... params) {
List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods()); List<Method> methods = Arrays.asList(interfaceDef.getDeclaredMethods());
assertTrue(methods.stream().anyMatch(m -> caseSensitive?m.getName().matches(methodName):m.getName().toLowerCase().matches(methodName.toLowerCase())), "No method " + methodName);
assertTrue( assertTrue(
methods.stream().filter(m -> caseSensitive?m.getName().matches(methodName):m.getName().toLowerCase().matches(methodName.toLowerCase())) methods.stream()
.anyMatch(m -> caseSensitive ? m.getName().matches(methodName)
: m.getName().toLowerCase().matches(methodName.toLowerCase())),
"No method " + methodName);
assertTrue(
methods.stream()
.filter(m -> caseSensitive ? m.getName().matches(methodName)
: m.getName().toLowerCase().matches(methodName.toLowerCase()))
.filter(m -> m.getReturnType().equals(returnType) .filter(m -> m.getReturnType().equals(returnType)
&& (Modifier.isAbstract(m.getModifiers()) == abstractTag) && (Modifier.isAbstract(m.getModifiers()) == abstractTag)
&& (Modifier.isFinal(m.getModifiers()) == finalTag)) && (Modifier.isFinal(m.getModifiers()) == finalTag))
...@@ -287,6 +403,10 @@ class StructureHelper { ...@@ -287,6 +403,10 @@ class StructureHelper {
+ Arrays.asList(params).stream().map(Class::getName).collect(Collectors.joining(", "))); + Arrays.asList(params).stream().map(Class::getName).collect(Collectors.joining(", ")));
} }
public boolean isDescendatOf(Class<?> clazz, String interfaceName) {
return getClass(interfaceName).isAssignableFrom(clazz);
}
public void hasImplements(Class<?> clazz, String... interfaceNames) { public void hasImplements(Class<?> clazz, String... interfaceNames) {
List<Class<?>> interfaces = new ArrayList<>(); List<Class<?>> interfaces = new ArrayList<>();
Arrays.asList(interfaceNames).stream().map(name -> getClass(name)).forEach(c -> interfaces.add(c)); Arrays.asList(interfaceNames).stream().map(name -> getClass(name)).forEach(c -> interfaces.add(c));
...@@ -308,10 +428,14 @@ class StructureHelper { ...@@ -308,10 +428,14 @@ class StructureHelper {
public void hasMethod(Class<?> interfaceDef, String methodName) { public void hasMethod(Class<?> interfaceDef, String methodName) {
hasMethod(interfaceDef, methodName, true); hasMethod(interfaceDef, methodName, true);
} }
public void hasMethod(Class<?> interfaceDef, String methodName, boolean caseSensitive) { public void hasMethod(Class<?> interfaceDef, String methodName, boolean caseSensitive) {
List<Method> methods = Arrays.asList(interfaceDef.getMethods()); List<Method> methods = Arrays.asList(interfaceDef.getMethods());
assertTrue(methods.stream().anyMatch(m -> caseSensitive?m.getName().matches(methodName):m.getName().toLowerCase().matches(methodName.toLowerCase())), "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 { public String getSourceCode(Class<?> clazz) throws URISyntaxException, IOException {
......
package jez04.structure.test;
public abstract class StructureMatcher<T> extends org.hamcrest.BaseMatcher<T>{
protected StructureHelper structureHelper = StructureHelper.getInstance();
}
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