-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document support for interface default methods in the User Guide
Fixes #192.
- Loading branch information
1 parent
44fca49
commit f7d9f40
Showing
5 changed files
with
172 additions
and
0 deletions.
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
44 changes: 44 additions & 0 deletions
44
documentation/src/test/java/example/defaultmethods/ComparableContract.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,44 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package example.defaultmethods; | ||
|
||
import static org.junit.gen5.api.Assertions.assertEquals; | ||
import static org.junit.gen5.api.Assertions.assertTrue; | ||
|
||
import org.junit.gen5.api.Test; | ||
|
||
// tag::user_guide[] | ||
public interface ComparableContract<T extends Comparable<T>> extends Testable<T> { | ||
|
||
T createSmallerValue(); | ||
|
||
@Test | ||
default void returnsZeroWhenComparedToItself() { | ||
T value = createValue(); | ||
assertEquals(0, value.compareTo(value)); | ||
} | ||
|
||
@Test | ||
default void returnsPositiveNumberComparedToSmallerValue() { | ||
T value = createValue(); | ||
T smallerValue = createSmallerValue(); | ||
assertTrue(value.compareTo(smallerValue) > 0); | ||
} | ||
|
||
@Test | ||
default void returnsNegativeNumberComparedToSmallerValue() { | ||
T value = createValue(); | ||
T smallerValue = createSmallerValue(); | ||
assertTrue(smallerValue.compareTo(value) < 0); | ||
} | ||
|
||
} | ||
// end::user_guide[] |
45 changes: 45 additions & 0 deletions
45
documentation/src/test/java/example/defaultmethods/EqualsContract.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,45 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package example.defaultmethods; | ||
|
||
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 org.junit.gen5.api.Test; | ||
|
||
// tag::user_guide[] | ||
public interface EqualsContract<T> extends Testable<T> { | ||
|
||
T createNotEqualValue(); | ||
|
||
@Test | ||
default void valueEqualsItself() { | ||
T value = createValue(); | ||
assertEquals(value, value); | ||
} | ||
|
||
@Test | ||
default void valueDoesNotEqualNull() { | ||
T value = createValue(); | ||
assertFalse(value.equals(null)); | ||
} | ||
|
||
@Test | ||
default void valueDoesNotEqualDifferentValue() { | ||
T value = createValue(); | ||
T differentValue = createNotEqualValue(); | ||
assertNotEquals(value, differentValue); | ||
assertNotEquals(differentValue, value); | ||
} | ||
|
||
} | ||
// end::user_guide[] |
32 changes: 32 additions & 0 deletions
32
documentation/src/test/java/example/defaultmethods/StringTests.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,32 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package example.defaultmethods; | ||
|
||
// tag::user_guide[] | ||
class StringTests implements ComparableContract<String>, EqualsContract<String> { | ||
|
||
@Override | ||
public String createValue() { | ||
return "foo"; | ||
} | ||
|
||
@Override | ||
public String createSmallerValue() { | ||
return "bar"; // 'b' < 'f' in "foo" | ||
} | ||
|
||
@Override | ||
public String createNotEqualValue() { | ||
return "baz"; | ||
} | ||
|
||
} | ||
// end::user_guide[] |
19 changes: 19 additions & 0 deletions
19
documentation/src/test/java/example/defaultmethods/Testable.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,19 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package example.defaultmethods; | ||
|
||
// tag::user_guide[] | ||
public interface Testable<T> { | ||
|
||
T createValue(); | ||
|
||
} | ||
// end::user_guide[] |