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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.coderising.dp.chain;

public class EmailLogger extends Logger{

EmailLogger(int level) {
super(level);
}

@Override
protected void write(String message) {
System.out.println("EmailLogger "+message);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.coderising.dp.chain;

public class FileLogger extends Logger {

FileLogger(int level) {
super(level);
}

@Override
protected void write(String message) {
System.out.println("FileLogger " + message);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.coderising.dp.chain;

public abstract class Logger {
public static int DEBUG = 1;
public static int NOTICE = 2;
public static int ERR = 3;
protected int level;

Logger(int level) {
this.level=level;
}
protected Logger nextLogger;
public Logger setNextLogger(Logger nextLogger) {
this.nextLogger = nextLogger;
return this;
}

public void message(String message,int level){
if (this.level<=level) {
write(message);
}
if (nextLogger!=null) {
nextLogger.message( message,level);
}
}
abstract protected void write(String message);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.coderising.dp.chain;

public class StdoutLogger extends Logger{

StdoutLogger(int level) {
super(level);
}

@Override
protected void write(String message) {
System.out.println("StdoutLogger "+message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.coderising.dp.chain;

public class Test {

public static void main(String[] args) {
Logger logger = new StdoutLogger(Logger.DEBUG)
.setNextLogger(new EmailLogger(Logger.NOTICE).setNextLogger(new FileLogger(Logger.ERR)));
logger.message("进入函数计算", Logger.DEBUG);

logger.message("第一步已经完成", Logger.NOTICE);

logger.message("一个致命的错误发生了", Logger.ERR);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.coderising.dp.command;

public interface Command {
void order();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.coderising.dp.command;

public class Cook {
void cookSteak(){
System.out.println("Steak is ok");
}

void cookPork(){
System.out.println("Pork is ok");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.coderising.dp.command;

public class OrderPorkCommand implements Command{
private Cook cook;
public OrderPorkCommand(Cook cook) {
this.cook=cook;
}
@Override
public void order() {
cook.cookPork();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.coderising.dp.command;

public class OrderSteakCommand implements Command {
private Cook cook;

public OrderSteakCommand(Cook cook) {
this.cook = cook;
}

@Override
public void order() {
cook.cookSteak();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.coderising.dp.command;

import java.util.ArrayList;
import java.util.List;

public class Waiter {
private List<Command> commands = new ArrayList<>();

public void addOrder(Command command) {
commands.add(command);
}

public void sendOrders(){
for (Command command : commands) {
command.order();
}
}

public static void main(String[] args) {
Cook cook=new Cook();

Waiter waiter=new Waiter();

Command command1=new OrderSteakCommand(cook);
Command command2=new OrderPorkCommand(cook);

waiter.addOrder(command1);
waiter.addOrder(command2);

waiter.sendOrders();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.coderising.litejunit.extension;

import com.coderising.litejunit.v2.Test;
import com.coderising.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 com.coderising.litejunit.extension;

import com.coderising.litejunit.v2.Assert;
import com.coderising.litejunit.v2.Test;
import com.coderising.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 com.coderising.litejunit.extension;

import com.coderising.litejunit.v2.Protectable;
import com.coderising.litejunit.v2.Test;
import com.coderising.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,42 @@
package com.coderising.litejunit.sample;

import com.coderising.litejunit.extension.RepeatedTest;
import com.coderising.litejunit.extension.TestSetup;
import com.coderising.litejunit.sample.calculator.CalculatorSuite;
import com.coderising.litejunit.v2.Test;
import com.coderising.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 com.coderising.litejunit.sample;

import com.coderising.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 com.coderising.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());
}
}
Loading