forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bruce Eckel
committed
Jun 16, 2015
1 parent
a9f380e
commit 49edcc8
Showing
981 changed files
with
38,051 additions
and
3 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
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,26 @@ | ||
//: annotations/AtUnitComposition.java | ||
// ©2015 MindView LLC: see Copyright.txt | ||
// Creating non-embedded tests. | ||
package annotations; | ||
import com.mindviewinc.atunit.*; | ||
import com.mindviewinc.util.*; | ||
|
||
public class AtUnitComposition { | ||
AtUnitExample1 testObject = new AtUnitExample1(); | ||
@Test boolean _methodOne() { | ||
return | ||
testObject.methodOne().equals("This is methodOne"); | ||
} | ||
@Test boolean _methodTwo() { | ||
return testObject.methodTwo() == 2; | ||
} | ||
public static void main(String[] args) throws Exception { | ||
OSExecute.command( | ||
"java com.mindviewinc.atunit.AtUnit AtUnitComposition"); | ||
} | ||
} /* Output: | ||
annotations.AtUnitComposition | ||
. _methodOne | ||
. _methodTwo This is methodTwo | ||
OK (2 tests) | ||
*///:~ |
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,38 @@ | ||
//: annotations/AtUnitExample1.java | ||
// ©2015 MindView LLC: see Copyright.txt | ||
package annotations; | ||
import com.mindviewinc.atunit.*; | ||
import com.mindviewinc.util.*; | ||
|
||
public class AtUnitExample1 { | ||
public String methodOne() { | ||
return "This is methodOne"; | ||
} | ||
public int methodTwo() { | ||
System.out.println("This is methodTwo"); | ||
return 2; | ||
} | ||
@Test boolean methodOneTest() { | ||
return methodOne().equals("This is methodOne"); | ||
} | ||
@Test boolean m2() { return methodTwo() == 2; } | ||
@Test private boolean m3() { return true; } | ||
// Shows output for failure: | ||
@Test boolean failureTest() { return false; } | ||
@Test boolean anotherDisappointment() { return false; } | ||
public static void main(String[] args) throws Exception { | ||
OSExecute.command( | ||
"java com.mindviewinc.atunit.AtUnit AtUnitExample1"); | ||
} | ||
} /* Output: | ||
annotations.AtUnitExample1 | ||
. anotherDisappointment (failed) | ||
. failureTest (failed) | ||
. methodOneTest | ||
. m2 This is methodTwo | ||
. m3 | ||
(5 tests) | ||
>>> 2 FAILURES <<< | ||
annotations.AtUnitExample1: anotherDisappointment | ||
annotations.AtUnitExample1: failureTest | ||
*///:~ |
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,49 @@ | ||
//: annotations/AtUnitExample2.java | ||
// ©2015 MindView LLC: see Copyright.txt | ||
// Assertions and exceptions can be used in @Tests. | ||
package annotations; | ||
import java.io.*; | ||
import com.mindviewinc.atunit.*; | ||
import com.mindviewinc.util.*; | ||
|
||
public class AtUnitExample2 { | ||
public String methodOne() { | ||
return "This is methodOne"; | ||
} | ||
public int methodTwo() { | ||
System.out.println("This is methodTwo"); | ||
return 2; | ||
} | ||
@Test void assertExample() { | ||
assert methodOne().equals("This is methodOne"); | ||
} | ||
@Test void assertFailureExample() { | ||
assert 1 == 2: "What a surprise!"; | ||
} | ||
@Test void exceptionExample() throws IOException { | ||
new FileInputStream("nofile.txt"); // Throws | ||
} | ||
@Test boolean assertAndReturn() { | ||
// Assertion with message: | ||
assert methodTwo() == 2: "methodTwo must equal 2"; | ||
return methodOne().equals("This is methodOne"); | ||
} | ||
public static void main(String[] args) throws Exception { | ||
OSExecute.command( | ||
"java com.mindviewinc.atunit.AtUnit AtUnitExample2"); | ||
} | ||
} /* Output: | ||
annotations.AtUnitExample2 | ||
. assertFailureExample java.lang.AssertionError: What a | ||
surprise! | ||
(failed) | ||
. exceptionExample java.io.FileNotFoundException: | ||
nofile.txt (The system cannot find the file specified) | ||
(failed) | ||
. assertAndReturn This is methodTwo | ||
. assertExample | ||
(4 tests) | ||
>>> 2 FAILURES <<< | ||
annotations.AtUnitExample2: assertFailureExample | ||
annotations.AtUnitExample2: exceptionExample | ||
*///:~ |
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,36 @@ | ||
//: annotations/AtUnitExample3.java | ||
// ©2015 MindView LLC: see Copyright.txt | ||
package annotations; | ||
import com.mindviewinc.atunit.*; | ||
import com.mindviewinc.util.*; | ||
|
||
public class AtUnitExample3 { | ||
private int n; | ||
public AtUnitExample3(int n) { this.n = n; } | ||
public int getN() { return n; } | ||
public String methodOne() { | ||
return "This is methodOne"; | ||
} | ||
public int methodTwo() { | ||
System.out.println("This is methodTwo"); | ||
return 2; | ||
} | ||
@TestObjectCreate static AtUnitExample3 create() { | ||
return new AtUnitExample3(47); | ||
} | ||
@Test boolean initialization() { return n == 47; } | ||
@Test boolean methodOneTest() { | ||
return methodOne().equals("This is methodOne"); | ||
} | ||
@Test boolean m2() { return methodTwo() == 2; } | ||
public static void main(String[] args) throws Exception { | ||
OSExecute.command( | ||
"java com.mindviewinc.atunit.AtUnit AtUnitExample3"); | ||
} | ||
} /* Output: | ||
annotations.AtUnitExample3 | ||
. methodOneTest | ||
. initialization | ||
. m2 This is methodTwo | ||
OK (3 tests) | ||
*///:~ |
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,70 @@ | ||
//: annotations/AtUnitExample4.java | ||
// ©2015 MindView LLC: see Copyright.txt | ||
package annotations; | ||
import java.util.*; | ||
import com.mindviewinc.atunit.*; | ||
import com.mindviewinc.util.*; | ||
import static com.mindviewinc.util.Print.*; | ||
|
||
public class AtUnitExample4 { | ||
static String theory = "All brontosauruses " + | ||
"are thin at one end, much MUCH thicker in the " + | ||
"middle, and then thin again at the far end."; | ||
private String word; | ||
private Random rand = new Random(); // Time-based seed | ||
public AtUnitExample4(String word) { this.word = word; } | ||
public String getWord() { return word; } | ||
public String scrambleWord() { | ||
List<Character> chars = new ArrayList<>(); | ||
for(Character c : word.toCharArray()) | ||
chars.add(c); | ||
Collections.shuffle(chars, rand); | ||
StringBuilder result = new StringBuilder(); | ||
for(char ch : chars) | ||
result.append(ch); | ||
return result.toString(); | ||
} | ||
@TestProperty static List<String> input = | ||
Arrays.asList(theory.split(" ")); | ||
@TestProperty | ||
static Iterator<String> words = input.iterator(); | ||
@TestObjectCreate static AtUnitExample4 create() { | ||
if(words.hasNext()) | ||
return new AtUnitExample4(words.next()); | ||
else | ||
return null; | ||
} | ||
@Test boolean words() { | ||
print("'" + getWord() + "'"); | ||
return getWord().equals("are"); | ||
} | ||
@Test boolean scramble1() { | ||
// Change to a specific seed to get verifiable results: | ||
rand = new Random(47); | ||
print("'" + getWord() + "'"); | ||
String scrambled = scrambleWord(); | ||
print(scrambled); | ||
return scrambled.equals("lAl"); | ||
} | ||
@Test boolean scramble2() { | ||
rand = new Random(74); | ||
print("'" + getWord() + "'"); | ||
String scrambled = scrambleWord(); | ||
print(scrambled); | ||
return scrambled.equals("tsaeborornussu"); | ||
} | ||
public static void main(String[] args) throws Exception { | ||
System.out.println("starting"); | ||
OSExecute.command( | ||
"java com.mindviewinc.atunit.AtUnit AtUnitExample4"); | ||
} | ||
} /* Output: | ||
starting | ||
annotations.AtUnitExample4 | ||
. scramble1 'All' | ||
lAl | ||
. scramble2 'brontosauruses' | ||
tsaeborornussu | ||
. words 'are' | ||
OK (3 tests) | ||
*///:~ |
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,54 @@ | ||
//: annotations/AtUnitExample5.java | ||
// ©2015 MindView LLC: see Copyright.txt | ||
package annotations; | ||
import java.io.*; | ||
import com.mindviewinc.atunit.*; | ||
import com.mindviewinc.util.*; | ||
|
||
public class AtUnitExample5 { | ||
private String text; | ||
public AtUnitExample5(String text) { this.text = text; } | ||
@Override | ||
public String toString() { return text; } | ||
@TestProperty static PrintWriter output; | ||
@TestProperty static int counter; | ||
@TestObjectCreate static AtUnitExample5 create() { | ||
String id = Integer.toString(counter++); | ||
try { | ||
output = new PrintWriter("Test" + id + ".txt"); | ||
} catch(IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return new AtUnitExample5(id); | ||
} | ||
@TestObjectCleanup static void | ||
cleanup(AtUnitExample5 tobj) { | ||
System.out.println("Running cleanup"); | ||
output.close(); | ||
} | ||
@Test boolean test1() { | ||
output.print("test1"); | ||
return true; | ||
} | ||
@Test boolean test2() { | ||
output.print("test2"); | ||
return true; | ||
} | ||
@Test boolean test3() { | ||
output.print("test3"); | ||
return true; | ||
} | ||
public static void main(String[] args) throws Exception { | ||
OSExecute.command( | ||
"java com.mindviewinc.atunit.AtUnit AtUnitExample5"); | ||
} | ||
} /* Output: | ||
annotations.AtUnitExample5 | ||
. test3 | ||
Running cleanup | ||
. test1 | ||
Running cleanup | ||
. test2 | ||
Running cleanup | ||
OK (3 tests) | ||
*///:~ |
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,22 @@ | ||
//: annotations/AtUnitExternalTest.java | ||
// ©2015 MindView LLC: see Copyright.txt | ||
// Creating non-embedded tests. | ||
package annotations; | ||
import com.mindviewinc.atunit.*; | ||
import com.mindviewinc.util.*; | ||
|
||
public class AtUnitExternalTest extends AtUnitExample1 { | ||
@Test boolean _methodOne() { | ||
return methodOne().equals("This is methodOne"); | ||
} | ||
@Test boolean _methodTwo() { return methodTwo() == 2; } | ||
public static void main(String[] args) throws Exception { | ||
OSExecute.command( | ||
"java com.mindviewinc.atunit.AtUnit AtUnitExternalTest"); | ||
} | ||
} /* Output: | ||
annotations.AtUnitExternalTest | ||
. _methodTwo This is methodTwo | ||
. _methodOne | ||
OK (2 tests) | ||
*///:~ |
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 @@ | ||
//: annotations/HashSetTest.java | ||
// ©2015 MindView LLC: see Copyright.txt | ||
package annotations; | ||
import java.util.*; | ||
import com.mindviewinc.atunit.*; | ||
import com.mindviewinc.util.*; | ||
|
||
public class HashSetTest { | ||
HashSet<String> testObject = new HashSet<>(); | ||
@Test void initialization() { | ||
assert testObject.isEmpty(); | ||
} | ||
@Test void _contains() { | ||
testObject.add("one"); | ||
assert testObject.contains("one"); | ||
} | ||
@Test void _remove() { | ||
testObject.add("one"); | ||
testObject.remove("one"); | ||
assert testObject.isEmpty(); | ||
} | ||
public static void main(String[] args) throws Exception { | ||
OSExecute.command( | ||
"java com.mindviewinc.atunit.AtUnit HashSetTest"); | ||
} | ||
} /* Output: | ||
annotations.HashSetTest | ||
. initialization | ||
. _remove | ||
. _contains | ||
OK (3 tests) | ||
*///:~ |
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,21 @@ | ||
//: annotations/PasswordUtils.java | ||
// ©2015 MindView LLC: see Copyright.txt | ||
import java.util.*; | ||
|
||
public class PasswordUtils { | ||
@UseCase(id = 47, description = | ||
"Passwords must contain at least one numeric") | ||
public boolean validatePassword(String password) { | ||
return (password.matches("\\w*\\d\\w*")); | ||
} | ||
@UseCase(id = 48) | ||
public String encryptPassword(String password) { | ||
return new StringBuilder(password).reverse().toString(); | ||
} | ||
@UseCase(id = 49, description = | ||
"New passwords can't equal previously used ones") | ||
public boolean checkForNewPassword( | ||
List<String> prevPasswords, String password) { | ||
return !prevPasswords.contains(password); | ||
} | ||
} ///:~ |
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,10 @@ | ||
//: annotations/SimulatingNull.java | ||
// ©2015 MindView LLC: see Copyright.txt | ||
import java.lang.annotation.*; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface SimulatingNull { | ||
public int id() default -1; | ||
public String description() default ""; | ||
} ///:~ |
Oops, something went wrong.