Skip to content

Commit

Permalink
reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce Eckel committed Jun 16, 2015
1 parent a9f380e commit 49edcc8
Show file tree
Hide file tree
Showing 981 changed files with 38,051 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Copyright.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ personal and commercial software programs.

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

3. Permission to incorporate the Source Code into printed
Expand Down Expand Up @@ -64,10 +64,10 @@ ENHANCEMENTS, OR MODIFICATIONS.

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

If you think you've found an error in the Source Code,
please submit a correction at:
https://github.com/BruceEckel/TIJ-Directors-Cut/issues
https://github.com/BruceEckel/On-Java/issues
///:~
26 changes: 26 additions & 0 deletions annotations/AtUnitComposition.java
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)
*///:~
38 changes: 38 additions & 0 deletions annotations/AtUnitExample1.java
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
*///:~
49 changes: 49 additions & 0 deletions annotations/AtUnitExample2.java
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
*///:~
36 changes: 36 additions & 0 deletions annotations/AtUnitExample3.java
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)
*///:~
70 changes: 70 additions & 0 deletions annotations/AtUnitExample4.java
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)
*///:~
54 changes: 54 additions & 0 deletions annotations/AtUnitExample5.java
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)
*///:~
22 changes: 22 additions & 0 deletions annotations/AtUnitExternalTest.java
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)
*///:~
32 changes: 32 additions & 0 deletions annotations/HashSetTest.java
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)
*///:~
21 changes: 21 additions & 0 deletions annotations/PasswordUtils.java
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);
}
} ///:~
10 changes: 10 additions & 0 deletions annotations/SimulatingNull.java
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 "";
} ///:~
Loading

0 comments on commit 49edcc8

Please sign in to comment.