Skip to content

Commit

Permalink
Fix plugin settings not updating
Browse files Browse the repository at this point in the history
Add test to ensure equals/hashCode contains all settings.
  • Loading branch information
Sandro Heinzelmann committed Apr 30, 2018
1 parent 542e553 commit 77fe8eb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ public boolean equals(Object o) {
if (getRestUser() != null ? !getRestUser().equals(that.getRestUser()) : that.getRestUser() != null) {
return false;
}
return getRestPassword() != null ? getRestPassword().equals(that.getRestPassword()) : that.getRestPassword() == null;
if (getRestPassword() != null ? !getRestPassword().equals(that.getRestPassword()) : that.getRestPassword() != null) {
return false;
}
return getDefaultEvents() != null ? getDefaultEvents().equals(that.getDefaultEvents()) : that.getDefaultEvents() == null;
}

@Override
Expand All @@ -110,6 +113,7 @@ public int hashCode() {
result = 31 * result + (getServerDisplayUrl() != null ? getServerDisplayUrl().hashCode() : 0);
result = 31 * result + (getRestUser() != null ? getRestUser().hashCode() : 0);
result = 31 * result + (getRestPassword() != null ? getRestPassword().hashCode() : 0);
result = 31 * result + (getDefaultEvents() != null ? getDefaultEvents().hashCode() : 0);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ch.adnovum.gong.notifier.email;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Stream;

import org.junit.Assert;
import org.junit.Test;

public class PluginSettingsTest {

/**
* It's important that changes in the settings can be detected so the plugin can reinitialize itself.
*/
@Test
public void shouldCheckAllFieldsInEqualsAndHashCode() throws Exception {
PluginSettings base = new PluginSettings();

setters(PluginSettings.class).forEach(m -> {
PluginSettings settings = new PluginSettings();
Class<?> paramType = m.getParameterTypes()[0];
try {
m.invoke(settings, createValue(paramType));
}
catch (Exception e) {
e.printStackTrace();
Assert.fail("Could not change plugin settings");
}

if (base.equals(settings)) {
Assert.fail("The equals method does not appear to use the field for setter " + m.getName());
}
if (base.hashCode() == settings.hashCode()) {
Assert.fail("The hashCode method does not appear to use the field for setter " + m.getName());
}
});
}

private static Stream<Method> setters(Class<?> clazz) {
Method[] methods = clazz.getMethods();
return Arrays.stream(methods)
.filter(m -> m.getName().startsWith("set"));
}

@SuppressWarnings("unchecked")
private static <T> T createValue(Class<T> type) {
if (type.equals(String.class)) {
return (T) "str";
}
else if (type.equals(Integer.class)) {
return (T) Integer.valueOf(77);
}
else {
throw new UnsupportedOperationException("Unsupported param type " + type);
}
}
}

0 comments on commit 77fe8eb

Please sign in to comment.