Skip to content

Commit 49edcc8

Browse files
author
Bruce Eckel
committed
reorg
1 parent a9f380e commit 49edcc8

981 files changed

Lines changed: 38051 additions & 3 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Copyright.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ personal and commercial software programs.
1515

1616
2. Permission is granted to use the Source Code without
1717
modification in classroom situations, including in
18-
presentation materials, provided that the book "Thinking in
18+
presentation materials, provided that the book "On
1919
Java" is cited as the origin.
2020

2121
3. Permission to incorporate the Source Code into printed
@@ -64,10 +64,10 @@ ENHANCEMENTS, OR MODIFICATIONS.
6464

6565
Please note that MindView LLC maintains a Web site which
6666
is the sole distribution point for electronic copies of the
67-
Source Code, https://github.com/BruceEckel/TIJ-Directors-Cut/,
67+
Source Code, https://github.com/BruceEckel/On-Java,
6868
where it is freely available under the terms stated above.
6969

7070
If you think you've found an error in the Source Code,
7171
please submit a correction at:
72-
https://github.com/BruceEckel/TIJ-Directors-Cut/issues
72+
https://github.com/BruceEckel/On-Java/issues
7373
///:~

annotations/AtUnitComposition.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//: annotations/AtUnitComposition.java
2+
// ©2015 MindView LLC: see Copyright.txt
3+
// Creating non-embedded tests.
4+
package annotations;
5+
import com.mindviewinc.atunit.*;
6+
import com.mindviewinc.util.*;
7+
8+
public class AtUnitComposition {
9+
AtUnitExample1 testObject = new AtUnitExample1();
10+
@Test boolean _methodOne() {
11+
return
12+
testObject.methodOne().equals("This is methodOne");
13+
}
14+
@Test boolean _methodTwo() {
15+
return testObject.methodTwo() == 2;
16+
}
17+
public static void main(String[] args) throws Exception {
18+
OSExecute.command(
19+
"java com.mindviewinc.atunit.AtUnit AtUnitComposition");
20+
}
21+
} /* Output:
22+
annotations.AtUnitComposition
23+
. _methodOne
24+
. _methodTwo This is methodTwo
25+
OK (2 tests)
26+
*///:~

annotations/AtUnitExample1.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//: annotations/AtUnitExample1.java
2+
// ©2015 MindView LLC: see Copyright.txt
3+
package annotations;
4+
import com.mindviewinc.atunit.*;
5+
import com.mindviewinc.util.*;
6+
7+
public class AtUnitExample1 {
8+
public String methodOne() {
9+
return "This is methodOne";
10+
}
11+
public int methodTwo() {
12+
System.out.println("This is methodTwo");
13+
return 2;
14+
}
15+
@Test boolean methodOneTest() {
16+
return methodOne().equals("This is methodOne");
17+
}
18+
@Test boolean m2() { return methodTwo() == 2; }
19+
@Test private boolean m3() { return true; }
20+
// Shows output for failure:
21+
@Test boolean failureTest() { return false; }
22+
@Test boolean anotherDisappointment() { return false; }
23+
public static void main(String[] args) throws Exception {
24+
OSExecute.command(
25+
"java com.mindviewinc.atunit.AtUnit AtUnitExample1");
26+
}
27+
} /* Output:
28+
annotations.AtUnitExample1
29+
. anotherDisappointment (failed)
30+
. failureTest (failed)
31+
. methodOneTest
32+
. m2 This is methodTwo
33+
. m3
34+
(5 tests)
35+
>>> 2 FAILURES <<<
36+
annotations.AtUnitExample1: anotherDisappointment
37+
annotations.AtUnitExample1: failureTest
38+
*///:~

annotations/AtUnitExample2.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//: annotations/AtUnitExample2.java
2+
// ©2015 MindView LLC: see Copyright.txt
3+
// Assertions and exceptions can be used in @Tests.
4+
package annotations;
5+
import java.io.*;
6+
import com.mindviewinc.atunit.*;
7+
import com.mindviewinc.util.*;
8+
9+
public class AtUnitExample2 {
10+
public String methodOne() {
11+
return "This is methodOne";
12+
}
13+
public int methodTwo() {
14+
System.out.println("This is methodTwo");
15+
return 2;
16+
}
17+
@Test void assertExample() {
18+
assert methodOne().equals("This is methodOne");
19+
}
20+
@Test void assertFailureExample() {
21+
assert 1 == 2: "What a surprise!";
22+
}
23+
@Test void exceptionExample() throws IOException {
24+
new FileInputStream("nofile.txt"); // Throws
25+
}
26+
@Test boolean assertAndReturn() {
27+
// Assertion with message:
28+
assert methodTwo() == 2: "methodTwo must equal 2";
29+
return methodOne().equals("This is methodOne");
30+
}
31+
public static void main(String[] args) throws Exception {
32+
OSExecute.command(
33+
"java com.mindviewinc.atunit.AtUnit AtUnitExample2");
34+
}
35+
} /* Output:
36+
annotations.AtUnitExample2
37+
. assertFailureExample java.lang.AssertionError: What a
38+
surprise!
39+
(failed)
40+
. exceptionExample java.io.FileNotFoundException:
41+
nofile.txt (The system cannot find the file specified)
42+
(failed)
43+
. assertAndReturn This is methodTwo
44+
. assertExample
45+
(4 tests)
46+
>>> 2 FAILURES <<<
47+
annotations.AtUnitExample2: assertFailureExample
48+
annotations.AtUnitExample2: exceptionExample
49+
*///:~

annotations/AtUnitExample3.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//: annotations/AtUnitExample3.java
2+
// ©2015 MindView LLC: see Copyright.txt
3+
package annotations;
4+
import com.mindviewinc.atunit.*;
5+
import com.mindviewinc.util.*;
6+
7+
public class AtUnitExample3 {
8+
private int n;
9+
public AtUnitExample3(int n) { this.n = n; }
10+
public int getN() { return n; }
11+
public String methodOne() {
12+
return "This is methodOne";
13+
}
14+
public int methodTwo() {
15+
System.out.println("This is methodTwo");
16+
return 2;
17+
}
18+
@TestObjectCreate static AtUnitExample3 create() {
19+
return new AtUnitExample3(47);
20+
}
21+
@Test boolean initialization() { return n == 47; }
22+
@Test boolean methodOneTest() {
23+
return methodOne().equals("This is methodOne");
24+
}
25+
@Test boolean m2() { return methodTwo() == 2; }
26+
public static void main(String[] args) throws Exception {
27+
OSExecute.command(
28+
"java com.mindviewinc.atunit.AtUnit AtUnitExample3");
29+
}
30+
} /* Output:
31+
annotations.AtUnitExample3
32+
. methodOneTest
33+
. initialization
34+
. m2 This is methodTwo
35+
OK (3 tests)
36+
*///:~

annotations/AtUnitExample4.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//: annotations/AtUnitExample4.java
2+
// ©2015 MindView LLC: see Copyright.txt
3+
package annotations;
4+
import java.util.*;
5+
import com.mindviewinc.atunit.*;
6+
import com.mindviewinc.util.*;
7+
import static com.mindviewinc.util.Print.*;
8+
9+
public class AtUnitExample4 {
10+
static String theory = "All brontosauruses " +
11+
"are thin at one end, much MUCH thicker in the " +
12+
"middle, and then thin again at the far end.";
13+
private String word;
14+
private Random rand = new Random(); // Time-based seed
15+
public AtUnitExample4(String word) { this.word = word; }
16+
public String getWord() { return word; }
17+
public String scrambleWord() {
18+
List<Character> chars = new ArrayList<>();
19+
for(Character c : word.toCharArray())
20+
chars.add(c);
21+
Collections.shuffle(chars, rand);
22+
StringBuilder result = new StringBuilder();
23+
for(char ch : chars)
24+
result.append(ch);
25+
return result.toString();
26+
}
27+
@TestProperty static List<String> input =
28+
Arrays.asList(theory.split(" "));
29+
@TestProperty
30+
static Iterator<String> words = input.iterator();
31+
@TestObjectCreate static AtUnitExample4 create() {
32+
if(words.hasNext())
33+
return new AtUnitExample4(words.next());
34+
else
35+
return null;
36+
}
37+
@Test boolean words() {
38+
print("'" + getWord() + "'");
39+
return getWord().equals("are");
40+
}
41+
@Test boolean scramble1() {
42+
// Change to a specific seed to get verifiable results:
43+
rand = new Random(47);
44+
print("'" + getWord() + "'");
45+
String scrambled = scrambleWord();
46+
print(scrambled);
47+
return scrambled.equals("lAl");
48+
}
49+
@Test boolean scramble2() {
50+
rand = new Random(74);
51+
print("'" + getWord() + "'");
52+
String scrambled = scrambleWord();
53+
print(scrambled);
54+
return scrambled.equals("tsaeborornussu");
55+
}
56+
public static void main(String[] args) throws Exception {
57+
System.out.println("starting");
58+
OSExecute.command(
59+
"java com.mindviewinc.atunit.AtUnit AtUnitExample4");
60+
}
61+
} /* Output:
62+
starting
63+
annotations.AtUnitExample4
64+
. scramble1 'All'
65+
lAl
66+
. scramble2 'brontosauruses'
67+
tsaeborornussu
68+
. words 'are'
69+
OK (3 tests)
70+
*///:~

annotations/AtUnitExample5.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//: annotations/AtUnitExample5.java
2+
// ©2015 MindView LLC: see Copyright.txt
3+
package annotations;
4+
import java.io.*;
5+
import com.mindviewinc.atunit.*;
6+
import com.mindviewinc.util.*;
7+
8+
public class AtUnitExample5 {
9+
private String text;
10+
public AtUnitExample5(String text) { this.text = text; }
11+
@Override
12+
public String toString() { return text; }
13+
@TestProperty static PrintWriter output;
14+
@TestProperty static int counter;
15+
@TestObjectCreate static AtUnitExample5 create() {
16+
String id = Integer.toString(counter++);
17+
try {
18+
output = new PrintWriter("Test" + id + ".txt");
19+
} catch(IOException e) {
20+
throw new RuntimeException(e);
21+
}
22+
return new AtUnitExample5(id);
23+
}
24+
@TestObjectCleanup static void
25+
cleanup(AtUnitExample5 tobj) {
26+
System.out.println("Running cleanup");
27+
output.close();
28+
}
29+
@Test boolean test1() {
30+
output.print("test1");
31+
return true;
32+
}
33+
@Test boolean test2() {
34+
output.print("test2");
35+
return true;
36+
}
37+
@Test boolean test3() {
38+
output.print("test3");
39+
return true;
40+
}
41+
public static void main(String[] args) throws Exception {
42+
OSExecute.command(
43+
"java com.mindviewinc.atunit.AtUnit AtUnitExample5");
44+
}
45+
} /* Output:
46+
annotations.AtUnitExample5
47+
. test3
48+
Running cleanup
49+
. test1
50+
Running cleanup
51+
. test2
52+
Running cleanup
53+
OK (3 tests)
54+
*///:~
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//: annotations/AtUnitExternalTest.java
2+
// ©2015 MindView LLC: see Copyright.txt
3+
// Creating non-embedded tests.
4+
package annotations;
5+
import com.mindviewinc.atunit.*;
6+
import com.mindviewinc.util.*;
7+
8+
public class AtUnitExternalTest extends AtUnitExample1 {
9+
@Test boolean _methodOne() {
10+
return methodOne().equals("This is methodOne");
11+
}
12+
@Test boolean _methodTwo() { return methodTwo() == 2; }
13+
public static void main(String[] args) throws Exception {
14+
OSExecute.command(
15+
"java com.mindviewinc.atunit.AtUnit AtUnitExternalTest");
16+
}
17+
} /* Output:
18+
annotations.AtUnitExternalTest
19+
. _methodTwo This is methodTwo
20+
. _methodOne
21+
OK (2 tests)
22+
*///:~

annotations/HashSetTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//: annotations/HashSetTest.java
2+
// ©2015 MindView LLC: see Copyright.txt
3+
package annotations;
4+
import java.util.*;
5+
import com.mindviewinc.atunit.*;
6+
import com.mindviewinc.util.*;
7+
8+
public class HashSetTest {
9+
HashSet<String> testObject = new HashSet<>();
10+
@Test void initialization() {
11+
assert testObject.isEmpty();
12+
}
13+
@Test void _contains() {
14+
testObject.add("one");
15+
assert testObject.contains("one");
16+
}
17+
@Test void _remove() {
18+
testObject.add("one");
19+
testObject.remove("one");
20+
assert testObject.isEmpty();
21+
}
22+
public static void main(String[] args) throws Exception {
23+
OSExecute.command(
24+
"java com.mindviewinc.atunit.AtUnit HashSetTest");
25+
}
26+
} /* Output:
27+
annotations.HashSetTest
28+
. initialization
29+
. _remove
30+
. _contains
31+
OK (3 tests)
32+
*///:~

annotations/PasswordUtils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//: annotations/PasswordUtils.java
2+
// ©2015 MindView LLC: see Copyright.txt
3+
import java.util.*;
4+
5+
public class PasswordUtils {
6+
@UseCase(id = 47, description =
7+
"Passwords must contain at least one numeric")
8+
public boolean validatePassword(String password) {
9+
return (password.matches("\\w*\\d\\w*"));
10+
}
11+
@UseCase(id = 48)
12+
public String encryptPassword(String password) {
13+
return new StringBuilder(password).reverse().toString();
14+
}
15+
@UseCase(id = 49, description =
16+
"New passwords can't equal previously used ones")
17+
public boolean checkForNewPassword(
18+
List<String> prevPasswords, String password) {
19+
return !prevPasswords.contains(password);
20+
}
21+
} ///:~

0 commit comments

Comments
 (0)