Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cfaacad

Browse files
committed
TDD section (not great yet, but runs)
1 parent 0dbbb64 commit cfaacad

File tree

7 files changed

+178
-1
lines changed

7 files changed

+178
-1
lines changed

verifying/StringInverter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// verifying/StringInverter.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
6+
interface StringInverter {
7+
public String invert(String str);
8+
}

verifying/StringInverter1.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// verifying/StringInverter1.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
// {java StringInverterTest StringInverter1}
6+
7+
public class StringInverter1 implements StringInverter {
8+
public String invert(String str) { return ""; }
9+
}
10+
/* Output:
11+
StringInverter1 has 3 FAILURES:
12+
Failure 1:
13+
allowedCharacters_Fail(StringInverterTest)
14+
Expected exception: java.lang.RuntimeException
15+
Failure 2:
16+
basicInversion_Succeed(StringInverterTest)
17+
expected:<[]> but was:<[eXIT, pURSUED BY A bEAR.]>
18+
Failure 3:
19+
lengthLessThan26_Fail(StringInverterTest)
20+
Expected exception: java.lang.RuntimeException
21+
*/

verifying/StringInverter2.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// verifying/StringInverter2.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
// {java StringInverterTest StringInverter2}
6+
import static java.lang.Character.*;
7+
8+
public class StringInverter2 implements StringInverter {
9+
public String invert(String str) {
10+
String result = "";
11+
for(int i = 0; i < str.length(); i++) {
12+
char c = str.charAt(i);
13+
result += isUpperCase(c) ?
14+
toLowerCase(c) :
15+
toUpperCase(c);
16+
}
17+
return result;
18+
}
19+
}
20+
/* Output:
21+
StringInverter2 has 2 FAILURES:
22+
Failure 1:
23+
allowedCharacters_Fail(StringInverterTest)
24+
Expected exception: java.lang.RuntimeException
25+
Failure 2:
26+
lengthLessThan26_Fail(StringInverterTest)
27+
Expected exception: java.lang.RuntimeException
28+
*/

verifying/StringInverter3.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// verifying/StringInverter3.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
// {java StringInverterTest StringInverter3}
6+
import static java.lang.Character.*;
7+
8+
public class StringInverter3 implements StringInverter {
9+
public String invert(String str) {
10+
if(str.length() > 25)
11+
throw new RuntimeException("argument too long!");
12+
String result = "";
13+
for(int i = 0; i < str.length(); i++) {
14+
char c = str.charAt(i);
15+
result += isUpperCase(c) ?
16+
toLowerCase(c) :
17+
toUpperCase(c);
18+
}
19+
return result;
20+
}
21+
}
22+
/* Output:
23+
StringInverter3 has 1 FAILURES:
24+
Failure 1:
25+
allowedCharacters_Fail(StringInverterTest)
26+
Expected exception: java.lang.RuntimeException
27+
*/

verifying/StringInverter4.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// verifying/StringInverter4.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
// {java StringInverterTest StringInverter4}
6+
import static java.lang.Character.*;
7+
8+
public class StringInverter4 implements StringInverter {
9+
static final String allowed =
10+
"abcdefghijklmnopqrstuvwxyz ,." +
11+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
12+
public String invert(String str) {
13+
if(str.length() > 25)
14+
throw new RuntimeException("argument too long!");
15+
String result = "";
16+
for(int i = 0; i < str.length(); i++) {
17+
char c = str.charAt(i);
18+
if(allowed.indexOf(c) == -1)
19+
throw new RuntimeException(c + " Not allowed");
20+
result += isUpperCase(c) ?
21+
toLowerCase(c) :
22+
toUpperCase(c);
23+
}
24+
return result;
25+
}
26+
}
27+
/* Output:
28+
StringInverter4 has 0 FAILURES:
29+
*/

verifying/StringInverterTest.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// verifying/StringInverterTest.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
// {ValidateByHand} // Don't run by itself
6+
import java.util.*;
7+
import org.junit.*;
8+
import static org.junit.Assert.*;
9+
import org.junit.runner.*;
10+
import org.junit.runner.notification.Failure;
11+
12+
public class StringInverterTest {
13+
static StringInverter inverter;
14+
@Test
15+
public final void basicInversion_Succeed() {
16+
String in = "Exit, Pursued by a Bear.";
17+
String out = "eXIT, pURSUED BY A bEAR.";
18+
assertEquals(inverter.invert(in), out);
19+
}
20+
@Test(expected = ComparisonFailure.class)
21+
public final void basicInversion_Fail() {
22+
assertEquals(inverter.invert("X"), "X");
23+
}
24+
@Test(expected = RuntimeException.class)
25+
public final void allowedCharacters_Fail() {
26+
inverter.invert(";-_()*&^%$#@!~`");
27+
inverter.invert("0123456789");
28+
}
29+
/*@Test
30+
public final void allowedCharacters_Succeed() {
31+
inverter.invert("abcdefghijklmnopqrstuvwxyz ,.");
32+
inverter.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.");
33+
assertTrue(true); // No exception thrown
34+
}*/
35+
@Test(expected = RuntimeException.class)
36+
public final void lengthLessThan26_Fail() {
37+
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
38+
assertTrue(str.length() > 25);
39+
inverter.invert(str);
40+
}
41+
@Test
42+
public final void lengthLessThan26_Succeed() {
43+
String str = "xxxxxxxxxxxxxxxxxxxxxxxxx";
44+
assertTrue(str.length() < 26);
45+
inverter.invert(str);
46+
assertTrue(true); // No exception thrown
47+
}
48+
public static void main(String[] args) throws Exception{
49+
assertEquals(args.length, 1);
50+
inverter = (StringInverter)
51+
Class.forName(args[0]).newInstance();
52+
Result result = JUnitCore.runClasses(
53+
StringInverterTest.class);
54+
List<Failure> failures = result.getFailures();
55+
System.out.printf("%s has %d FAILURES:\n",
56+
args[0], failures.size());
57+
int count = 1;
58+
for(Failure f : failures) {
59+
System.out.printf("Failure %d:\n", count++);
60+
System.out.println(f.getDescription());
61+
System.out.println(f.getMessage());
62+
}
63+
}
64+
}

verifying/logback.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
<root level="TRACE">
1010
<appender-ref ref="STDOUT" />
1111
</root>
12-
</configuration>
12+
</configuration>

0 commit comments

Comments
 (0)