Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.litejunit.extension;

import org.litejunit.v2.Test;
import org.litejunit.v2.TestResult;

/**
* A Decorator that runs a test repeatedly.
*
*/
public class RepeatedTest extends TestDecorator {
private int fTimesRepeat;

public RepeatedTest(Test test, int repeat) {
super(test);
if (repeat < 0)
throw new IllegalArgumentException("Repetition count must be > 0");
fTimesRepeat= repeat;
}
public int countTestCases() {
return super.countTestCases()*fTimesRepeat;
}
public void run(TestResult result) {
for (int i= 0; i < fTimesRepeat; i++) {
if (result.shouldStop())
break;
super.run(result);
}
}
public String toString() {
return super.toString()+"(repeated)";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.litejunit.extension;

import org.litejunit.v2.Assert;
import org.litejunit.v2.Test;
import org.litejunit.v2.TestResult;

/**
* A Decorator for Tests. Use TestDecorator as the base class
* for defining new test decorators. Test decorator subclasses
* can be introduced to add behaviour before or after a test
* is run.
*
*/
public class TestDecorator extends Assert implements Test {
protected Test test;

public TestDecorator(Test test) {
this.test= test;
}
/**
* The basic run behaviour.
*/
public void basicRun(TestResult result) {
test.run(result);
}
public int countTestCases() {
return test.countTestCases();
}
public void run(TestResult result) {
basicRun(result);
}

public String toString() {
return test.toString();
}

public Test getTest() {
return test;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.litejunit.extension;

import org.litejunit.v2.Protectable;
import org.litejunit.v2.Test;
import org.litejunit.v2.TestResult;

/**
* A Decorator to set up and tear down additional fixture state.
* Subclass TestSetup and insert it into your tests when you want
* to set up additional state once before the tests are run.
*/
public class TestSetup extends TestDecorator {

public TestSetup(Test test) {
super(test);
}
public void run(final TestResult result) {
Protectable p= new Protectable() {
public void protect() throws Exception {
setUp();
basicRun(result);
tearDown();
}
};
result.runProtected(this, p);
}
/**
* Sets up the fixture. Override to set up additional fixture
* state.
*/
protected void setUp() throws Exception {
}
/**
* Tears down the fixture. Override to tear down the additional
* fixture state.
*/
protected void tearDown() throws Exception {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.litejunit.sample;

import org.litejunit.extension.RepeatedTest;
import org.litejunit.extension.TestSetup;
import org.litejunit.sample.calculator.CalculatorSuite;
import org.litejunit.v2.Test;
import org.litejunit.v2.TestSuite;


public class AllTest {
/*public static Test suite(){

TestSuite suite= new TestSuite("All Test");
suite.addTest(CalculatorSuite.suite());
suite.addTestSuite(PersonTest.class);
return suite;

}*/

public static Test suite(){

TestSuite suite= new TestSuite("All Test");
suite.addTest(CalculatorSuite.suite());
suite.addTest(new RepeatedTest(new TestSuite(PersonTest.class), 2));
return new OverallTestSetup(suite);
}


static class OverallTestSetup extends TestSetup{

public OverallTestSetup(Test test) {
super(test);

}
protected void setUp() throws Exception {
System.out.println("this is overall testsetup");
}
protected void tearDown() throws Exception {
System.out.println("this is overall teardown");
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.litejunit.sample;

import org.litejunit.v2.TestCase;

public class PersonTest extends TestCase {

Person p = null;
protected void setUp() {
p = new Person("andy",30);
}
public PersonTest(String name) {
super(name);
}
public void testAge(){
this.assertEquals(30, p.getAge());
}
public void testName(){
this.assertEquals("andy", p.getName());
}
}
class Person{
private String name;
private int age;

public Person(String name, int age) {

this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.litejunit.sample.calculator;

public class Calculator {

private int result = 0;
public void add(int x){
result += x;
}
public void subtract(int x){
result -=x;
}

public int getResult(){
return this.result;
}
public static void main(String[] args){
Calculator calculator = new Calculator();
calculator.add(10);
calculator.subtract(5);
System.out.println(calculator.getResult());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.litejunit.sample.calculator;

import org.litejunit.v2.Test;
import org.litejunit.v2.TestSuite;

public class CalculatorSuite {
public static Test suite(){
TestSuite suite= new TestSuite("Calculator All Test");
suite.addTestSuite(CalculatorTest.class);
return suite;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.litejunit.sample.calculator;

import org.litejunit.v2.TestCase;

public class CalculatorTest extends TestCase {
public CalculatorTest(String name) {
super(name);

}
Calculator calculator =null;
public void setUp(){
calculator = new Calculator();
}
public void tearDown(){
calculator = null;
}
public void testAdd(){

calculator.add(10);
assertEquals(5,calculator.getResult());
}
public void testSubtract(){
calculator.add(10);
calculator.subtract(5);
throw new RuntimeException("this is a test");
//assertEquals(5,calculator.getResult());
}

public static void main(String[] args){
/*{
TestCase tc1 = new CalculatorTest("testAdd"){
protected void runTest() {
testAdd();
}
};

TestCase tc2 = new CalculatorTest("testSubtract"){
protected void runTest() {
testSubtract();
}
};
tc1.run();
tc2.run();
}


TestSuite ts = new TestSuite();
ts.addTest(new CalculatorTest("testAdd"));
ts.addTest(new CalculatorTest("testSubtract"));


{
TestCase tc1 = new CalculatorTest("test1");
TestCase tc2 = new CalculatorTest("test2");
tc1.run();
tc2.run();
}*/
}
}
Loading