From 77fe8eb1a2e67ceea9858a891d451ad15d79eef2 Mon Sep 17 00:00:00 2001 From: Sandro Heinzelmann Date: Mon, 30 Apr 2018 12:47:16 +0200 Subject: [PATCH] Fix plugin settings not updating Add test to ensure equals/hashCode contains all settings. --- .../gong/notifier/PluginSettingsBase.java | 6 +- .../notifier/email/PluginSettingsTest.java | 57 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 gong-notifier-email/src/test/java/ch/adnovum/gong/notifier/email/PluginSettingsTest.java diff --git a/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/PluginSettingsBase.java b/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/PluginSettingsBase.java index 8cddde7..037e556 100644 --- a/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/PluginSettingsBase.java +++ b/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/PluginSettingsBase.java @@ -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 @@ -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; } } diff --git a/gong-notifier-email/src/test/java/ch/adnovum/gong/notifier/email/PluginSettingsTest.java b/gong-notifier-email/src/test/java/ch/adnovum/gong/notifier/email/PluginSettingsTest.java new file mode 100644 index 0000000..0d1d7fe --- /dev/null +++ b/gong-notifier-email/src/test/java/ch/adnovum/gong/notifier/email/PluginSettingsTest.java @@ -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 setters(Class clazz) { + Method[] methods = clazz.getMethods(); + return Arrays.stream(methods) + .filter(m -> m.getName().startsWith("set")); + } + + @SuppressWarnings("unchecked") + private static T createValue(Class 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); + } + } +}