Skip to content

Commit e0c6e1f

Browse files
author
sheng
committed
junit3.x
1 parent 30d4ab3 commit e0c6e1f

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

+2457
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.coderising.dp.chain;
2+
3+
public class EmailLogger extends Logger{
4+
5+
EmailLogger(int level) {
6+
super(level);
7+
}
8+
9+
@Override
10+
protected void write(String message) {
11+
System.out.println("EmailLogger "+message);
12+
}
13+
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.coderising.dp.chain;
2+
3+
public class FileLogger extends Logger {
4+
5+
FileLogger(int level) {
6+
super(level);
7+
}
8+
9+
@Override
10+
protected void write(String message) {
11+
System.out.println("FileLogger " + message);
12+
}
13+
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.coderising.dp.chain;
2+
3+
public abstract class Logger {
4+
public static int DEBUG = 1;
5+
public static int NOTICE = 2;
6+
public static int ERR = 3;
7+
protected int level;
8+
9+
Logger(int level) {
10+
this.level=level;
11+
}
12+
protected Logger nextLogger;
13+
public Logger setNextLogger(Logger nextLogger) {
14+
this.nextLogger = nextLogger;
15+
return this;
16+
}
17+
18+
public void message(String message,int level){
19+
if (this.level<=level) {
20+
write(message);
21+
}
22+
if (nextLogger!=null) {
23+
nextLogger.message( message,level);
24+
}
25+
}
26+
abstract protected void write(String message);
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.coderising.dp.chain;
2+
3+
public class StdoutLogger extends Logger{
4+
5+
StdoutLogger(int level) {
6+
super(level);
7+
}
8+
9+
@Override
10+
protected void write(String message) {
11+
System.out.println("StdoutLogger "+message);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.coderising.dp.chain;
2+
3+
public class Test {
4+
5+
public static void main(String[] args) {
6+
Logger logger = new StdoutLogger(Logger.DEBUG)
7+
.setNextLogger(new EmailLogger(Logger.NOTICE).setNextLogger(new FileLogger(Logger.ERR)));
8+
logger.message("进入函数计算", Logger.DEBUG);
9+
10+
logger.message("第一步已经完成", Logger.NOTICE);
11+
12+
logger.message("一个致命的错误发生了", Logger.ERR);
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.coderising.dp.command;
2+
3+
public interface Command {
4+
void order();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.coderising.dp.command;
2+
3+
public class Cook {
4+
void cookSteak(){
5+
System.out.println("Steak is ok");
6+
}
7+
8+
void cookPork(){
9+
System.out.println("Pork is ok");
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.coderising.dp.command;
2+
3+
public class OrderPorkCommand implements Command{
4+
private Cook cook;
5+
public OrderPorkCommand(Cook cook) {
6+
this.cook=cook;
7+
}
8+
@Override
9+
public void order() {
10+
cook.cookPork();
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.coderising.dp.command;
2+
3+
public class OrderSteakCommand implements Command {
4+
private Cook cook;
5+
6+
public OrderSteakCommand(Cook cook) {
7+
this.cook = cook;
8+
}
9+
10+
@Override
11+
public void order() {
12+
cook.cookSteak();
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.coderising.dp.command;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Waiter {
7+
private List<Command> commands = new ArrayList<>();
8+
9+
public void addOrder(Command command) {
10+
commands.add(command);
11+
}
12+
13+
public void sendOrders(){
14+
for (Command command : commands) {
15+
command.order();
16+
}
17+
}
18+
19+
public static void main(String[] args) {
20+
Cook cook=new Cook();
21+
22+
Waiter waiter=new Waiter();
23+
24+
Command command1=new OrderSteakCommand(cook);
25+
Command command2=new OrderPorkCommand(cook);
26+
27+
waiter.addOrder(command1);
28+
waiter.addOrder(command2);
29+
30+
waiter.sendOrders();
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.coderising.litejunit.extension;
2+
3+
import com.coderising.litejunit.v2.Test;
4+
import com.coderising.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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.coderising.litejunit.extension;
2+
3+
import com.coderising.litejunit.v2.Assert;
4+
import com.coderising.litejunit.v2.Test;
5+
import com.coderising.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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.coderising.litejunit.extension;
2+
3+
import com.coderising.litejunit.v2.Protectable;
4+
import com.coderising.litejunit.v2.Test;
5+
import com.coderising.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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.coderising.litejunit.sample;
2+
3+
import com.coderising.litejunit.extension.RepeatedTest;
4+
import com.coderising.litejunit.extension.TestSetup;
5+
import com.coderising.litejunit.sample.calculator.CalculatorSuite;
6+
import com.coderising.litejunit.v2.Test;
7+
import com.coderising.litejunit.v2.TestSuite;
8+
9+
public class AllTest {
10+
// public static Test suite(){
11+
//
12+
// TestSuite suite= new TestSuite("All Test");
13+
// suite.addTest(CalculatorSuite.suite());
14+
// //suite.addTestSuite(PersonTest.class);
15+
// return suite;
16+
//
17+
// }
18+
19+
public static Test suite(){
20+
21+
TestSuite suite= new TestSuite("All Test");
22+
suite.addTest(CalculatorSuite.suite());
23+
suite.addTest(new RepeatedTest(new TestSuite(PersonTest.class), 2));
24+
return new OverallTestSetup(suite);
25+
}
26+
27+
28+
static class OverallTestSetup extends TestSetup{
29+
30+
public OverallTestSetup(Test test) {
31+
super(test);
32+
33+
}
34+
protected void setUp() throws Exception {
35+
System.out.println("this is overall testsetup");
36+
}
37+
protected void tearDown() throws Exception {
38+
System.out.println("this is overall teardown");
39+
}
40+
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.coderising.litejunit.sample;
2+
3+
import com.coderising.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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.coderising.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+
}

0 commit comments

Comments
 (0)