Skip to content

Commit

Permalink
Improve equals() & hashCode() tests for TestSources
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed May 23, 2016
1 parent 02fe21f commit da13426
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import static org.junit.gen5.api.Assertions.assertEquals;
import static org.junit.gen5.api.Assertions.assertFalse;
import static org.junit.gen5.api.Assertions.assertNotEquals;
import static org.junit.gen5.api.Assertions.assertNotNull;
import static org.junit.gen5.api.Assertions.assertNotSame;
import static org.junit.gen5.api.Assertions.assertTrue;
Expand All @@ -25,16 +26,21 @@
*/
abstract class AbstractTestSourceTests {

protected void assertEqualsAndHashCode(TestSource source1, TestSource source2) {
assertNotNull(source1);
assertNotNull(source2);
assertNotSame(source1, source2);
assertFalse(source1.equals(null));
protected void assertEqualsAndHashCode(TestSource equal1, TestSource equal2, TestSource different) {
assertNotNull(equal1);
assertNotNull(equal2);
assertNotNull(different);

assertTrue(source1.equals(source1));
assertTrue(source1.equals(source2));
assertTrue(source2.equals(source1));
assertEquals(source1.hashCode(), source2.hashCode());
assertNotSame(equal1, equal2);
assertFalse(equal1.equals(null));
assertFalse(equal1.equals(different));
assertFalse(different.equals(equal1));
assertNotEquals(equal1.hashCode(), different.hashCode());

assertTrue(equal1.equals(equal1));
assertTrue(equal1.equals(equal2));
assertTrue(equal2.equals(equal1));
assertEquals(equal1.hashCode(), equal2.hashCode());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ void createCompositeTestSourceFromClassAndFileSources() {

@Test
void equalsAndHashCode() {
List<TestSource> sources = Arrays.asList(new JavaClassSource(getClass()));
assertEqualsAndHashCode(new CompositeTestSource(sources), new CompositeTestSource(sources));
List<TestSource> sources1 = Arrays.asList(new JavaClassSource(Number.class));
List<TestSource> sources2 = Arrays.asList(new JavaClassSource(String.class));
assertEqualsAndHashCode(new CompositeTestSource(sources1), new CompositeTestSource(sources1),
new CompositeTestSource(sources2));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ void fileWithPosition() throws Exception {

@Test
void equalsAndHashCodeForFileSource() {
File file = new File("test.txt");
assertEqualsAndHashCode(new FileSource(file), new FileSource(file));
File file1 = new File("foo.txt");
File file2 = new File("bar.txt");
assertEqualsAndHashCode(new FileSource(file1), new FileSource(file1), new FileSource(file2));
}

@Test
void equalsAndHashCodeForDirectorySource() {
File file = new File(".");
assertEqualsAndHashCode(new DirectorySource(file), new DirectorySource(file));
File dir1 = new File(".");
File dir2 = new File("..");
assertEqualsAndHashCode(new DirectorySource(dir1), new DirectorySource(dir1), new DirectorySource(dir2));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void classSource() {

@Test
void methodSource() throws Exception {
Method testMethod = getExampleMethod();
Method testMethod = getMethod("method1");
JavaMethodSource source = new JavaMethodSource(testMethod);

assertThat(source.getJavaClass()).isEqualTo(getClass());
Expand All @@ -78,27 +78,34 @@ void methodSource() throws Exception {

@Test
void equalsAndHashCodeForJavaPackageSource() {
Package testPackage = getClass().getPackage();
assertEqualsAndHashCode(new JavaPackageSource(testPackage), new JavaPackageSource(testPackage));
Package pkg1 = getClass().getPackage();
Package pkg2 = String.class.getPackage();
assertEqualsAndHashCode(new JavaPackageSource(pkg1), new JavaPackageSource(pkg1), new JavaPackageSource(pkg2));
}

@Test
void equalsAndHashCodeForJavaClassSource() {
Class<?> testClass = getClass();
assertEqualsAndHashCode(new JavaClassSource(testClass), new JavaClassSource(testClass));
Class<?> class1 = String.class;
Class<?> class2 = Number.class;
assertEqualsAndHashCode(new JavaClassSource(class1), new JavaClassSource(class1), new JavaClassSource(class2));
}

@Test
void equalsAndHashCodeForJavaMethodSource(TestInfo testInfo) throws Exception {
Method testMethod = getExampleMethod();
assertEqualsAndHashCode(new JavaMethodSource(testMethod), new JavaMethodSource(testMethod));
Method method1 = getMethod("method1");
Method method2 = getMethod("method2");
assertEqualsAndHashCode(new JavaMethodSource(method1), new JavaMethodSource(method1),
new JavaMethodSource(method2));
}

private Method getMethod(String name) throws Exception {
return getClass().getDeclaredMethod(name, String.class);
}

void exampleMethod(String text) {
void method1(String text) {
}

private Method getExampleMethod() throws Exception {
return getClass().getDeclaredMethod("exampleMethod", String.class);
void method2(String text) {
}

}

0 comments on commit da13426

Please # to comment.