Merge pull request #5690 from magefree/copy_constructor_watchers

refactor the copy functionality for no-args watchers
This commit is contained in:
Oleg Agafonov 2020-01-02 18:51:37 +01:00 committed by GitHub
commit 0930bdd016
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
227 changed files with 196 additions and 2136 deletions

View file

@ -433,7 +433,7 @@ public class VerifyCardDataTest {
}
@Test
@Ignore // TODO: enable it on copy() methods removing
//@Ignore // TODO: enable it on copy() methods removing
public void checkWatcherCopyMethods() {
Collection<String> errorsList = new ArrayList<>();
@ -443,6 +443,13 @@ public class VerifyCardDataTest {
Set<Class<? extends Watcher>> watcherClassesList = reflections.getSubTypesOf(Watcher.class);
for (Class<? extends Watcher> watcherClass : watcherClassesList) {
// only watcher class can be extended (e.g. final)
if (!watcherClass.getSuperclass().equals(Watcher.class)) {
errorsList.add("error, only Watcher class can be extended: " + watcherClass.getName());
}
// no copy methods
try {
Method m = watcherClass.getMethod("copy");
if (!m.getGenericReturnType().getTypeName().equals("T")) {
@ -451,6 +458,49 @@ public class VerifyCardDataTest {
} catch (NoSuchMethodException e) {
errorsList.add("error, can't find copy() method in watcher class: " + watcherClass.getName());
}
// no constructor for copy
try {
Constructor<? extends Watcher> constructor = watcherClass.getDeclaredConstructor(watcherClass);
errorsList.add("error, copy constructor is not allowed in watcher class: " + watcherClass.getName());
} catch (NoSuchMethodException e) {
// all fine, no needs in copy constructors
}
// errors on create
try {
List<?> constructors = Arrays.asList(watcherClass.getDeclaredConstructors());
Constructor<? extends Watcher> constructor = (Constructor<? extends Watcher>) constructors.get(0);
Object[] args = new Object[constructor.getParameterCount()];
for (int index = 0; index < constructor.getParameterTypes().length; index++) {
Class<?> parameterType = constructor.getParameterTypes()[index];
if(parameterType.getSimpleName().equalsIgnoreCase("boolean")){
args[index]=false;
}
else {
args[index] = null;
}
}
constructor.setAccessible(true);
Watcher w1 = constructor.newInstance(args);
// errors on copy
try {
Watcher w2 = w1.copy();
if (w2 == null) {
errorsList.add("error, can't copy watcher with unknown error, look at error logs above: " + watcherClass.getName());
}
} catch (Exception e) {
errorsList.add("error, can't copy watcher: " + watcherClass.getName() + " (" + e.getMessage() + ")");
}
} catch (Exception e) {
errorsList.add("error, can't create watcher: " + watcherClass.getName() + " (" + e.getMessage() + ")");
}
}
printMessages(warningsList);