Skip to content

Commit ab8980d

Browse files
committed
2 parents 9139171 + 6ce9ad6 commit ab8980d

Some content is hidden

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

48 files changed

+3320
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.litejunit.extension;
2+
3+
import org.litejunit.v2.Test;
4+
import org.litejunit.v2.TestResult;
5+
6+
/**
7+
* A Decorator that runs a test repeatedly.
8+
*
9+
*/
10+
public class RepeatedTest extends TestDecorator {
11+
private int fTimesRepeat;
12+
13+
public RepeatedTest(Test test, int repeat) {
14+
super(test);
15+
if (repeat < 0)
16+
throw new IllegalArgumentException("Repetition count must be > 0");
17+
fTimesRepeat= repeat;
18+
}
19+
public int countTestCases() {
20+
return super.countTestCases()*fTimesRepeat;
21+
}
22+
public void run(TestResult result) {
23+
for (int i= 0; i < fTimesRepeat; i++) {
24+
if (result.shouldStop())
25+
break;
26+
super.run(result);
27+
}
28+
}
29+
public String toString() {
30+
return super.toString()+"(repeated)";
31+
}
32+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.litejunit.extension;
2+
3+
import org.litejunit.v2.Assert;
4+
import org.litejunit.v2.Test;
5+
import org.litejunit.v2.TestResult;
6+
7+
/**
8+
* A Decorator for Tests. Use TestDecorator as the base class
9+
* for defining new test decorators. Test decorator subclasses
10+
* can be introduced to add behaviour before or after a test
11+
* is run.
12+
*
13+
*/
14+
public class TestDecorator extends Assert implements Test {
15+
protected Test test;
16+
17+
public TestDecorator(Test test) {
18+
this.test= test;
19+
}
20+
/**
21+
* The basic run behaviour.
22+
*/
23+
public void basicRun(TestResult result) {
24+
test.run(result);
25+
}
26+
public int countTestCases() {
27+
return test.countTestCases();
28+
}
29+
public void run(TestResult result) {
30+
basicRun(result);
31+
}
32+
33+
public String toString() {
34+
return test.toString();
35+
}
36+
37+
public Test getTest() {
38+
return test;
39+
}
40+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.litejunit.extension;
2+
3+
import org.litejunit.v2.Protectable;
4+
import org.litejunit.v2.Test;
5+
import org.litejunit.v2.TestResult;
6+
7+
/**
8+
* A Decorator to set up and tear down additional fixture state.
9+
* Subclass TestSetup and insert it into your tests when you want
10+
* to set up additional state once before the tests are run.
11+
*/
12+
public class TestSetup extends TestDecorator {
13+
14+
public TestSetup(Test test) {
15+
super(test);
16+
}
17+
public void run(final TestResult result) {
18+
Protectable p= new Protectable() {
19+
public void protect() throws Exception {
20+
setUp();
21+
basicRun(result);
22+
tearDown();
23+
}
24+
};
25+
result.runProtected(this, p);
26+
}
27+
/**
28+
* Sets up the fixture. Override to set up additional fixture
29+
* state.
30+
*/
31+
protected void setUp() throws Exception {
32+
}
33+
/**
34+
* Tears down the fixture. Override to tear down the additional
35+
* fixture state.
36+
*/
37+
protected void tearDown() throws Exception {
38+
}
39+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.litejunit.sample;
2+
3+
import org.litejunit.extension.RepeatedTest;
4+
import org.litejunit.extension.TestSetup;
5+
import org.litejunit.sample.calculator.CalculatorSuite;
6+
import org.litejunit.v2.Test;
7+
import org.litejunit.v2.TestSuite;
8+
9+
10+
public class AllTest {
11+
/*public static Test suite(){
12+
13+
TestSuite suite= new TestSuite("All Test");
14+
suite.addTest(CalculatorSuite.suite());
15+
suite.addTestSuite(PersonTest.class);
16+
return suite;
17+
18+
}*/
19+
20+
public static Test suite(){
21+
22+
TestSuite suite= new TestSuite("All Test");
23+
suite.addTest(CalculatorSuite.suite());
24+
suite.addTest(new RepeatedTest(new TestSuite(PersonTest.class), 2));
25+
return new OverallTestSetup(suite);
26+
}
27+
28+
29+
static class OverallTestSetup extends TestSetup{
30+
31+
public OverallTestSetup(Test test) {
32+
super(test);
33+
34+
}
35+
protected void setUp() throws Exception {
36+
System.out.println("this is overall testsetup");
37+
}
38+
protected void tearDown() throws Exception {
39+
System.out.println("this is overall teardown");
40+
}
41+
42+
}
43+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.litejunit.sample;
2+
3+
import org.litejunit.v2.TestCase;
4+
5+
public class PersonTest extends TestCase {
6+
7+
Person p = null;
8+
protected void setUp() {
9+
p = new Person("andy",30);
10+
}
11+
public PersonTest(String name) {
12+
super(name);
13+
}
14+
public void testAge(){
15+
this.assertEquals(30, p.getAge());
16+
}
17+
public void testName(){
18+
this.assertEquals("andy", p.getName());
19+
}
20+
}
21+
class Person{
22+
private String name;
23+
private int age;
24+
25+
public Person(String name, int age) {
26+
27+
this.name = name;
28+
this.age = age;
29+
}
30+
public String getName() {
31+
return name;
32+
}
33+
public int getAge() {
34+
return age;
35+
}
36+
37+
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.litejunit.sample.calculator;
2+
3+
public class Calculator {
4+
5+
private int result = 0;
6+
public void add(int x){
7+
result += x;
8+
}
9+
public void subtract(int x){
10+
result -=x;
11+
}
12+
13+
public int getResult(){
14+
return this.result;
15+
}
16+
public static void main(String[] args){
17+
Calculator calculator = new Calculator();
18+
calculator.add(10);
19+
calculator.subtract(5);
20+
System.out.println(calculator.getResult());
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.litejunit.sample.calculator;
2+
3+
import org.litejunit.v2.Test;
4+
import org.litejunit.v2.TestSuite;
5+
6+
public class CalculatorSuite {
7+
public static Test suite(){
8+
TestSuite suite= new TestSuite("Calculator All Test");
9+
suite.addTestSuite(CalculatorTest.class);
10+
return suite;
11+
}
12+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.litejunit.sample.calculator;
2+
3+
import org.litejunit.v2.TestCase;
4+
5+
public class CalculatorTest extends TestCase {
6+
public CalculatorTest(String name) {
7+
super(name);
8+
9+
}
10+
Calculator calculator =null;
11+
public void setUp(){
12+
calculator = new Calculator();
13+
}
14+
public void tearDown(){
15+
calculator = null;
16+
}
17+
public void testAdd(){
18+
19+
calculator.add(10);
20+
assertEquals(5,calculator.getResult());
21+
}
22+
public void testSubtract(){
23+
calculator.add(10);
24+
calculator.subtract(5);
25+
throw new RuntimeException("this is a test");
26+
//assertEquals(5,calculator.getResult());
27+
}
28+
29+
public static void main(String[] args){
30+
/*{
31+
TestCase tc1 = new CalculatorTest("testAdd"){
32+
protected void runTest() {
33+
testAdd();
34+
}
35+
};
36+
37+
TestCase tc2 = new CalculatorTest("testSubtract"){
38+
protected void runTest() {
39+
testSubtract();
40+
}
41+
};
42+
tc1.run();
43+
tc2.run();
44+
}
45+
46+
47+
TestSuite ts = new TestSuite();
48+
ts.addTest(new CalculatorTest("testAdd"));
49+
ts.addTest(new CalculatorTest("testSubtract"));
50+
51+
52+
{
53+
TestCase tc1 = new CalculatorTest("test1");
54+
TestCase tc2 = new CalculatorTest("test2");
55+
tc1.run();
56+
tc2.run();
57+
}*/
58+
}
59+
}

0 commit comments

Comments
 (0)