Skip to content

Commit 966ccbb

Browse files
committed
litejunitV3完成
1 parent a920b56 commit 966ccbb

Some content is hidden

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

45 files changed

+2929
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.litejunit.v3;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* If you allocate external resources in a <code>@Before</code> method you need to release them
10+
* after the test runs. Annotating a <code>public void</code> method
11+
* with <code>@After</code> causes that method to be run after the <code>@Test</code> method. All <code>@After</code>
12+
* methods are guaranteed to run even if a <code>@Before</code> or <code>@Test</code> method throws an
13+
* exception. The <code>@After</code> methods declared in superclasses will be run after those of the current
14+
* class.
15+
* <p>
16+
* Here is a simple example:<br>
17+
* <code>
18+
* public class Example {<br>
19+
* &nbsp;&nbsp;File output;<br>
20+
* &nbsp;&nbsp;@Before public void createOutputFile() {<br>
21+
* &nbsp;&nbsp;&nbsp;&nbsp;output= new File(...);<br>
22+
* &nbsp;&nbsp;}<br>
23+
* &nbsp;&nbsp;@Test public void something() {<br>
24+
* &nbsp;&nbsp;&nbsp;&nbsp;...<br>
25+
* &nbsp;&nbsp;}<br>
26+
* &nbsp;&nbsp;@After public void deleteOutputFile() {<br>
27+
* &nbsp;&nbsp;&nbsp;&nbsp;output.delete();<br>
28+
* &nbsp;&nbsp;}<br>
29+
* }<br>
30+
* </code>
31+
*
32+
* @see Before
33+
*/
34+
35+
@Retention(RetentionPolicy.RUNTIME)
36+
@Target(ElementType.METHOD)
37+
public @interface After {
38+
}
39+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.litejunit.v3;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* If you allocate expensive external resources in a <code>@BeforeClass</code> method you need to release them
10+
* after all the tests in the class have run. Annotating a <code>public static void</code> method
11+
* with <code>@AfterClass</code> causes that method to be run after all the tests in the class have been run. All <code>@AfterClass</code>
12+
* methods are guaranteed to run even if a <code>@BeforeClass</code> method throws an
13+
* exception. The <code>@AfterClass</code> methods declared in superclasses will be run after those of the current
14+
* class.
15+
* <p>
16+
* Here is a simple example:<br>
17+
* <code>
18+
* public class Example {<br>
19+
* &nbsp;&nbsp;DatabaseConnection database;<br>
20+
* &nbsp;&nbsp;@BeforeClass public void login() {<br>
21+
* &nbsp;&nbsp;&nbsp;&nbsp;database= ...;<br>
22+
* &nbsp;&nbsp;}<br>
23+
* &nbsp;&nbsp;@Test public void something() {<br>
24+
* &nbsp;&nbsp;&nbsp;&nbsp;...<br>
25+
* &nbsp;&nbsp;}<br>
26+
* &nbsp;&nbsp;@Test public void somethingElse() {<br>
27+
* &nbsp;&nbsp;&nbsp;&nbsp;...<br>
28+
* &nbsp;&nbsp;}<br>
29+
* &nbsp;&nbsp;@AfterClass public void logout() {<br>
30+
* &nbsp;&nbsp;&nbsp;&nbsp;database.logout();<br>
31+
* &nbsp;&nbsp;}<br>
32+
* }<br>
33+
* </code>
34+
*
35+
* @see BeforeClass
36+
*/
37+
38+
@Retention(RetentionPolicy.RUNTIME)
39+
@Target(ElementType.METHOD)
40+
public @interface AfterClass {
41+
}
Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
package org.litejunit.v3;
2+
3+
/**
4+
* A set of assertion methods useful for writing tests. Only failed assertions are recorded.
5+
* These methods can be used directly: <code>Assert.assertEquals(...)</code>, however, they
6+
* read better if they are referenced through static import:<br>
7+
* <code>
8+
* import static org.junit.Assert.*;<br>
9+
* ...<br>
10+
* &nbsp;&nbsp;assertEquals(...);<br>
11+
* </code>
12+
*/
13+
14+
public class Assert {
15+
/**
16+
* Protect constructor since it is a static only class
17+
*/
18+
protected Assert() {
19+
}
20+
21+
/**
22+
* Asserts that a condition is true. If it isn't it throws an
23+
* AssertionError with the given message.
24+
*/
25+
static public void assertTrue(String message, boolean condition) {
26+
if (!condition)
27+
fail(message);
28+
}
29+
30+
/**
31+
* Asserts that a condition is true. If it isn't it throws an
32+
* AssertionError.
33+
*/
34+
static public void assertTrue(boolean condition) {
35+
assertTrue(null, condition);
36+
}
37+
38+
/**
39+
* Asserts that a condition is false. If it isn't it throws an
40+
* AssertionError with the given message.
41+
*/
42+
static public void assertFalse(String message, boolean condition) {
43+
assertTrue(message, !condition);
44+
}
45+
46+
/**
47+
* Asserts that a condition is false. If it isn't it throws an
48+
* AssertionError.
49+
*/
50+
static public void assertFalse(boolean condition) {
51+
assertFalse(null, condition);
52+
}
53+
54+
/**
55+
* Fails a test with the given message.
56+
*/
57+
static public void fail(String message) {
58+
throw new AssertionError(message);
59+
}
60+
61+
/**
62+
* Fails a test with no message.
63+
*/
64+
static public void fail() {
65+
fail(null);
66+
}
67+
68+
/**
69+
* Asserts that two objects are equal. If they are not, an
70+
* AssertionError is thrown with the given message.
71+
*/
72+
static public void assertEquals(String message, Object expected, Object actual) {
73+
if (expected == null && actual == null)
74+
return;
75+
if (expected != null && expected.equals(actual))
76+
return;
77+
if (expected instanceof String && actual instanceof String)
78+
throw new ComparisonFailure(message, (String)expected, (String)actual);
79+
else
80+
failNotEquals(message, expected, actual);
81+
}
82+
83+
/**
84+
* Asserts that two objects are equal. If they are not, an
85+
* AssertionError is thrown.
86+
*/
87+
static public void assertEquals(Object expected, Object actual) {
88+
assertEquals(null, expected, actual);
89+
}
90+
91+
/**
92+
* Asserts that two object arrays are equal. If they are not, an
93+
* AssertionError is thrown with the given message.
94+
*/
95+
public static void assertEquals(String message, Object[] expecteds, Object[] actuals) {
96+
if (expecteds == actuals)
97+
return;
98+
String header = message == null ? "" : message + ": ";
99+
if (expecteds == null)
100+
fail(header + "expected array was null");
101+
if (actuals == null)
102+
fail(header + "actual array was null");
103+
if (actuals.length != expecteds.length)
104+
fail(header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length);
105+
106+
for (int i= 0; i < expecteds.length; i++) {
107+
Object o1= expecteds[i];
108+
Object o2= actuals[i];
109+
if (o1.getClass().isArray() && o2.getClass().isArray()) {
110+
Object[] expected= (Object[]) o1;
111+
Object[] actual= (Object[]) o2;
112+
assertEquals(header + "arrays first differed at element " + i + ";", expected, actual);
113+
} else
114+
assertEquals(header + "arrays first differed at element [" + i + "];", o1, o2);
115+
}
116+
}
117+
118+
/**
119+
* Asserts that two object arrays are equal. If they are not, an
120+
* AssertionError is thrown.
121+
*/
122+
public static void assertEquals(Object[] expecteds, Object[] actuals) {
123+
assertEquals(null, expecteds, actuals);
124+
}
125+
126+
/**
127+
* Asserts that two doubles are equal to within a positive delta. If they
128+
* are not, an AssertionError is thrown with the given message. If the
129+
* expected value is infinity then the delta value is ignored. NaNs are
130+
* considered equal:
131+
* assertEquals(Double.NaN, Double.NaN, *) passes
132+
*/
133+
static public void assertEquals(String message, double expected, double actual, double delta) {
134+
if (Double.compare(expected, actual) == 0)
135+
return;
136+
if (!(Math.abs(expected - actual) <= delta))
137+
failNotEquals(message, new Double(expected), new Double(actual));
138+
}
139+
140+
/**
141+
* Asserts that two doubles are equal to within a positive delta. If they
142+
* are not, an AssertionError is thrown. If the
143+
* expected value is infinity then the delta value is ignored.NaNs are
144+
* considered equal:
145+
* assertEquals(Double.NaN, Double.NaN, *) passes
146+
*/
147+
static public void assertEquals(double expected, double actual, double delta) {
148+
assertEquals(null, expected, actual, delta);
149+
}
150+
151+
/**
152+
* Asserts that two floats are equal to within a positive delta. If they
153+
* are not, an AssertionError is thrown with the given message. If the
154+
* expected value is infinity then the delta value is ignored.NaNs are
155+
* considered equal:
156+
* assertEquals(Float.NaN, Float.NaN, *) passes
157+
*/
158+
static public void assertEquals(String message, float expected, float actual, float delta) {
159+
if (Float.compare(expected, actual) == 0)
160+
return;
161+
if (!(Math.abs(expected - actual) <= delta))
162+
failNotEquals(message, new Float(expected), new Float(actual));
163+
}
164+
165+
/**
166+
* Asserts that two floats are equal to within a positive delta. If they
167+
* are not, an AssertionError is thrown. If the
168+
* expected value is infinity then the delta value is ignored.NaNs are
169+
* considered equal:
170+
* assertEquals(Float.NaN, Float.NaN, *) passes
171+
*/
172+
static public void assertEquals(float expected, float actual, float delta) {
173+
assertEquals(null, expected, actual, delta);
174+
}
175+
176+
/**
177+
* Asserts that an object isn't null. If it is an AssertionError is
178+
* thrown with the given message.
179+
*/
180+
static public void assertNotNull(String message, Object object) {
181+
assertTrue(message, object != null);
182+
}
183+
184+
/**
185+
* Asserts that an object isn't null. If it is an AssertionError is
186+
* thrown.
187+
*/
188+
static public void assertNotNull(Object object) {
189+
assertNotNull(null, object);
190+
}
191+
192+
/**
193+
* Asserts that an object is null. If it is not, an AssertionError is
194+
* thrown with the given message.
195+
*/
196+
static public void assertNull(String message, Object object) {
197+
assertTrue(message, object == null);
198+
}
199+
200+
/**
201+
* Asserts that an object is null. If it isn't an AssertionError is
202+
* thrown.
203+
*/
204+
static public void assertNull(Object object) {
205+
assertNull(null, object);
206+
}
207+
208+
/**
209+
* Asserts that two objects refer to the same object. If they are not, an
210+
* AssertionError is thrown with the given message.
211+
*/
212+
static public void assertSame(String message, Object expected, Object actual) {
213+
if (expected == actual)
214+
return;
215+
failNotSame(message, expected, actual);
216+
}
217+
218+
/**
219+
* Asserts that two objects refer to the same object. If they are not the
220+
* same, an AssertionError is thrown.
221+
*/
222+
static public void assertSame(Object expected, Object actual) {
223+
assertSame(null, expected, actual);
224+
}
225+
226+
/**
227+
* Asserts that two objects do not refer to the same object. If they do
228+
* refer to the same object, an AssertionError is thrown with the given
229+
* message.
230+
*/
231+
static public void assertNotSame(String message, Object expected, Object actual) {
232+
if (expected == actual)
233+
failSame(message);
234+
}
235+
236+
/**
237+
* Asserts that two objects do not refer to the same object. If they do
238+
* refer to the same object, an AssertionError is thrown.
239+
*/
240+
static public void assertNotSame(Object expected, Object actual) {
241+
assertNotSame(null, expected, actual);
242+
}
243+
244+
static private void failSame(String message) {
245+
String formatted= "";
246+
if (message != null)
247+
formatted= message + " ";
248+
fail(formatted + "expected not same");
249+
}
250+
251+
static private void failNotSame(String message, Object expected, Object actual) {
252+
String formatted= "";
253+
if (message != null)
254+
formatted= message + " ";
255+
fail(formatted + "expected same:<" + expected + "> was not:<" + actual + ">");
256+
}
257+
258+
static private void failNotEquals(String message, Object expected, Object actual) {
259+
fail(format(message, expected, actual));
260+
}
261+
262+
static String format(String message, Object expected, Object actual) {
263+
String formatted= "";
264+
if (message != null)
265+
formatted= message + " ";
266+
return formatted + "expected:<" + expected + "> but was:<" + actual + ">";
267+
}
268+
269+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.litejunit.v3;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* When writing tests, it is common to find that several tests need similar
10+
* objects created before they can run. Annotating a <code>public void</code> method
11+
* with <code>@Before</code> causes that method to be run before the <code>@Test</code> method.
12+
* The <code>@Before</code> methods of superclasses will be run before those of the current class.
13+
* <p>
14+
* Here is a simple example:
15+
* <code>
16+
* public class Example {<br>
17+
* &nbsp;&nbsp;List empty;<br>
18+
* &nbsp;&nbsp;@Before public static void initialize() {<br>
19+
* &nbsp;&nbsp;&nbsp;&nbsp;empty= new ArrayList();<br>
20+
* &nbsp;&nbsp;}<br>
21+
* &nbsp;&nbsp;@Test public void size() {<br>
22+
* &nbsp;&nbsp;&nbsp;&nbsp;...<br>
23+
* &nbsp;&nbsp;}<br>
24+
* &nbsp;&nbsp;@Test public void remove() {<br>
25+
* &nbsp;&nbsp;&nbsp;&nbsp;...<br>
26+
* &nbsp;&nbsp;}<br>
27+
* }<br>
28+
* </code>
29+
*
30+
* @see BeforeClass
31+
* @see After
32+
*/
33+
@Retention(RetentionPolicy.RUNTIME)
34+
@Target(ElementType.METHOD)
35+
public @interface Before {
36+
}
37+

0 commit comments

Comments
 (0)