Skip to content

Commit 1bc48b1

Browse files
committed
[Feature] junt-v2 init
1 parent 2d7420f commit 1bc48b1

32 files changed

+1900
-11
lines changed
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
package com.coderising.myood.litejunit.liuxinv2;
2+
3+
/**
4+
* A set of assert methods.
5+
*/
6+
7+
public class Assert {
8+
/**
9+
* Protect constructor since it is a static only class
10+
*/
11+
protected Assert() {
12+
}
13+
/**
14+
* Asserts that a condition is true. If it isn't it throws
15+
* an AssertionFailedError with the given message.
16+
* @deprecated use assertTrue
17+
*/
18+
/*static public void assert(String message, boolean condition) {
19+
if (!condition)
20+
fail(message);
21+
}*/
22+
/**
23+
* Asserts that a condition is true. If it isn't it throws
24+
* an AssertionFailedError.
25+
* @deprecated use assertTrue
26+
*
27+
*/
28+
/*static public void assert(boolean condition) {
29+
assert(null, condition);
30+
}
31+
*/
32+
/**
33+
* Asserts that a condition is true. If it isn't it throws
34+
* an AssertionFailedError with the given message.
35+
*/
36+
static public void assertTrue(String message, boolean condition) {
37+
if (!condition)
38+
fail(message);
39+
}
40+
/**
41+
* Asserts that a condition is true. If it isn't it throws
42+
* an AssertionFailedError.
43+
*/
44+
static public void assertTrue(boolean condition) {
45+
assertTrue(null, condition);
46+
}
47+
/**
48+
* Fails a test with the given message.
49+
*/
50+
static public void fail(String message) {
51+
throw new AssertionFailedError(message);
52+
}
53+
/**
54+
* Fails a test with no message.
55+
*/
56+
static public void fail() {
57+
fail(null);
58+
}
59+
/**
60+
* Asserts that two objects are equal. If they are not
61+
* an AssertionFailedError is thrown.
62+
*/
63+
static public void assertEquals(String message, Object expected, Object actual) {
64+
if (expected == null && actual == null)
65+
return;
66+
if (expected != null && expected.equals(actual))
67+
return;
68+
failNotEquals(message, expected, actual);
69+
}
70+
/**
71+
* Asserts that two objects are equal. If they are not
72+
* an AssertionFailedError is thrown.
73+
*/
74+
static public void assertEquals(Object expected, Object actual) {
75+
assertEquals(null, expected, actual);
76+
}
77+
/**
78+
* Asserts that two doubles are equal concerning a delta. If the expected
79+
* value is infinity then the delta value is ignored.
80+
*/
81+
static public void assertEquals(String message, double expected, double actual, double delta) {
82+
// handle infinity specially since subtracting to infinite values gives NaN and the
83+
// the following test fails
84+
if (Double.isInfinite(expected)) {
85+
if (!(expected == actual))
86+
failNotEquals(message, new Double(expected), new Double(actual));
87+
} else if (!(Math.abs(expected-actual) <= delta)) // Because comparison with NaN always returns false
88+
failNotEquals(message, new Double(expected), new Double(actual));
89+
}
90+
/**
91+
* Asserts that two doubles are equal concerning a delta. If the expected
92+
* value is infinity then the delta value is ignored.
93+
*/
94+
static public void assertEquals(double expected, double actual, double delta) {
95+
assertEquals(null, expected, actual, delta);
96+
}
97+
/**
98+
* Asserts that two floats are equal concerning a delta. If the expected
99+
* value is infinity then the delta value is ignored.
100+
*/
101+
static public void assertEquals(String message, float expected, float actual, float delta) {
102+
// handle infinity specially since subtracting to infinite values gives NaN and the
103+
// the following test fails
104+
if (Float.isInfinite(expected)) {
105+
if (!(expected == actual))
106+
failNotEquals(message, new Float(expected), new Float(actual));
107+
} else if (!(Math.abs(expected-actual) <= delta))
108+
failNotEquals(message, new Float(expected), new Float(actual));
109+
}
110+
/**
111+
* Asserts that two floats are equal concerning a delta. If the expected
112+
* value is infinity then the delta value is ignored.
113+
*/
114+
static public void assertEquals(float expected, float actual, float delta) {
115+
assertEquals(null, expected, actual, delta);
116+
}
117+
/**
118+
* Asserts that two longs are equal.
119+
*/
120+
static public void assertEquals(String message, long expected, long actual) {
121+
assertEquals(message, new Long(expected), new Long(actual));
122+
}
123+
/**
124+
* Asserts that two longs are equal.
125+
*/
126+
static public void assertEquals(long expected, long actual) {
127+
assertEquals(null, expected, actual);
128+
}
129+
/**
130+
* Asserts that two booleans are equal.
131+
*/
132+
static public void assertEquals(String message, boolean expected, boolean actual) {
133+
assertEquals(message, new Boolean(expected), new Boolean(actual));
134+
}
135+
/**
136+
* Asserts that two booleans are equal.
137+
*/
138+
static public void assertEquals(boolean expected, boolean actual) {
139+
assertEquals(null, expected, actual);
140+
}
141+
/**
142+
* Asserts that two bytes are equal.
143+
*/
144+
static public void assertEquals(String message, byte expected, byte actual) {
145+
assertEquals(message, new Byte(expected), new Byte(actual));
146+
}
147+
/**
148+
* Asserts that two bytes are equal.
149+
*/
150+
static public void assertEquals(byte expected, byte actual) {
151+
assertEquals(null, expected, actual);
152+
}
153+
/**
154+
* Asserts that two chars are equal.
155+
*/
156+
static public void assertEquals(String message, char expected, char actual) {
157+
assertEquals(message, new Character(expected), new Character(actual));
158+
}
159+
/**
160+
* Asserts that two chars are equal.
161+
*/
162+
static public void assertEquals(char expected, char actual) {
163+
assertEquals(null, expected, actual);
164+
}
165+
/**
166+
* Asserts that two shorts are equal.
167+
*/
168+
static public void assertEquals(String message, short expected, short actual) {
169+
assertEquals(message, new Short(expected), new Short(actual));
170+
}
171+
/**
172+
* Asserts that two shorts are equal.
173+
*/
174+
static public void assertEquals(short expected, short actual) {
175+
assertEquals(null, expected, actual);
176+
}
177+
/**
178+
* Asserts that two ints are equal.
179+
*/
180+
static public void assertEquals(String message, int expected, int actual) {
181+
assertEquals(message, new Integer(expected), new Integer(actual));
182+
}
183+
/**
184+
* Asserts that two ints are equal.
185+
*/
186+
static public void assertEquals(int expected, int actual) {
187+
assertEquals(null, expected, actual);
188+
}
189+
/**
190+
* Asserts that an object isn't null.
191+
*/
192+
static public void assertNotNull(Object object) {
193+
assertNotNull(null, object);
194+
}
195+
/**
196+
* Asserts that an object isn't null.
197+
*/
198+
static public void assertNotNull(String message, Object object) {
199+
assertTrue(message, object != null);
200+
}
201+
/**
202+
* Asserts that an object is null.
203+
*/
204+
static public void assertNull(Object object) {
205+
assertNull(null, object);
206+
}
207+
/**
208+
* Asserts that an object is null.
209+
*/
210+
static public void assertNull(String message, Object object) {
211+
assertTrue(message, object == null);
212+
}
213+
/**
214+
* Asserts that two objects refer to the same object. If they are not
215+
* an AssertionFailedError is thrown.
216+
*/
217+
static public void assertSame(String message, Object expected, Object actual) {
218+
if (expected == actual)
219+
return;
220+
failNotSame(message, expected, actual);
221+
}
222+
/**
223+
* Asserts that two objects refer to the same object. If they are not
224+
* the same an AssertionFailedError is thrown.
225+
*/
226+
static public void assertSame(Object expected, Object actual) {
227+
assertSame(null, expected, actual);
228+
}
229+
230+
static private void failNotEquals(String message, Object expected, Object actual) {
231+
String formatted= "";
232+
if (message != null)
233+
formatted= message+" ";
234+
fail(formatted+"expected:<"+expected+"> but was:<"+actual+">");
235+
}
236+
237+
static private void failNotSame(String message, Object expected, Object actual) {
238+
String formatted= "";
239+
if (message != null)
240+
formatted= message+" ";
241+
fail(formatted+"expected same");
242+
}
243+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.coderising.myood.litejunit.liuxinv2;
2+
3+
/**
4+
* Thrown when an assertion failed.
5+
*/
6+
public class AssertionFailedError extends Error {
7+
8+
public AssertionFailedError () {
9+
}
10+
public AssertionFailedError (String message) {
11+
super (message);
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.coderising.myood.litejunit.liuxinv2;
2+
3+
/**
4+
* A <em>Protectable</em> can be run and can throw a Throwable.
5+
*
6+
* @see TestResult
7+
*/
8+
public interface Protectable {
9+
10+
/**
11+
* Run the the following method protected.
12+
*/
13+
public abstract void protect() throws Throwable;
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.coderising.myood.litejunit.liuxinv2;
2+
3+
public interface Test {
4+
int countTestCases();
5+
void run(TestResult tr);
6+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.coderising.myood.litejunit.liuxinv2;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
import java.lang.reflect.Modifier;
6+
7+
8+
9+
public abstract class TestCase extends Assert implements Test {
10+
private String name;
11+
12+
13+
public TestCase(String name) {
14+
this.name = name;
15+
}
16+
17+
18+
public int countTestCases() {
19+
return 1;
20+
}
21+
22+
protected void runTest() throws Throwable{
23+
Method runMethod= null;
24+
try {
25+
runMethod= getClass().getMethod(name, null);
26+
} catch (NoSuchMethodException e) {
27+
fail("Method \""+name+"\" not found");
28+
}
29+
if (!Modifier.isPublic(runMethod.getModifiers())) {
30+
fail("Method \""+name+"\" should be public");
31+
}
32+
33+
try {
34+
runMethod.invoke(this, new Class[0]);
35+
}
36+
catch (InvocationTargetException e) {
37+
e.fillInStackTrace();
38+
throw e.getTargetException();
39+
}
40+
catch (IllegalAccessException e) {
41+
e.fillInStackTrace();
42+
throw e;
43+
}
44+
}
45+
46+
protected void setUp() {
47+
}
48+
49+
protected void tearDown() {
50+
}
51+
52+
public void run(TestResult tr) {
53+
tr.run(this);
54+
}
55+
public void doRun() throws Throwable{
56+
setUp();
57+
try{
58+
runTest();
59+
}
60+
finally{
61+
tearDown();
62+
}
63+
}
64+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.coderising.myood.litejunit.liuxinv2;
2+
3+
/**
4+
* A <code>TestFailure</code> collects a failed test together with
5+
* the caught exception.
6+
* @see TestResult
7+
*/
8+
public class TestFailure {
9+
protected Test failedTest;
10+
protected Throwable thrownException;
11+
12+
/**
13+
* Constructs a TestFailure with the given test and exception.
14+
*/
15+
public TestFailure(Test failedTest, Throwable thrownException) {
16+
this.failedTest= failedTest;
17+
this.thrownException= thrownException;
18+
}
19+
/**
20+
* Gets the failed test.
21+
*/
22+
public Test failedTest() {
23+
return failedTest;
24+
}
25+
/**
26+
* Gets the thrown exception.
27+
*/
28+
public Throwable thrownException() {
29+
return thrownException;
30+
}
31+
/**
32+
* Returns a short description of the failure.
33+
*/
34+
public String toString() {
35+
StringBuffer buffer= new StringBuffer();
36+
buffer.append(failedTest+": "+thrownException.getMessage());
37+
return buffer.toString();
38+
}
39+
}

0 commit comments

Comments
 (0)