-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test to ensure equals/hashCode contains all settings.
- Loading branch information
Sandro Heinzelmann
committed
Apr 30, 2018
1 parent
542e553
commit 77fe8eb
Showing
2 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
gong-notifier-email/src/test/java/ch/adnovum/gong/notifier/email/PluginSettingsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |