JBuilder Weblogic

Save this PDF as:

Size: px
Start display at page:

Download "JBuilder Weblogic"


1 JUnit ( < >6 JUnit Java Erich Gamma Kent Beck JUnit JUnit 1 JUnit 1.1 JUnit JUnit java XUnit JUnit 1.2 JUnit JUnit Erich Gamma Kent Beck Erich Gamma Kent Beck XP Extreme Programming CRC XUnit The Smalltalk Best Practice Patterns Extreme Programming Explained Planning Extreme Programming Martin Fowler JUnit JUnit JUnit 2 JUnit JUnit Patterns Generate Architectures "Patterns Generate Architectures", Kent Beck and Ralph Johnson, ECOOP 94 Junit 1/1

2 3 JUnit 3.1 Command JUnit framework TestCase ( ) JUnit JUnit JUnit TestCase JUnit TestCase Command Gamma, E., et al. Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley, Reading, MA, 1995 intent "..."Command "execute " Command Test, Run Command Execute Default Adapter TestCase Test Test run() TestCase run() Test counttestcases TestCase Command Excecute run TestResult public interface Test { // Counts the number of test cases that will be run by this test. public abstract int counttestcases(); //runs a test and collects its result in a TestResult instance. public abstract void run(testresult result); Junit 2/2

3 TestCase TestCase public abstract class TestCase extends Assert implements Test { //the name of the test case private String fname; public void run(testresult result) { result.run(this); TestCase run JUnit run TestResult Command Command TestCase JUnit Command TestCase TestCase Command TestCase TestSuit Composite Command TestCase Junit Fromwork, Command Undo Redo Log, JUnit 3.2 Composite Command JUnit JUnit. TestCase TestCase TestCase Junit if issingletestcase(objectrequest) { // TestCase, run (TestCase)objectRequest.run() else if(iscompositetestcase(objectrequest)){ // TestCase, // TestCase // Junit 3/3

4 JUnit TestCase testcase testcase JUnit TestCase TestCase JUnit Composite " ' - ' Composite " ' - ' TestCase TestCase TestSuit Composite Composite Component Test run Composite TestSuit Leaf TestCase, componsite JUnit TestCase composite Component Leaf composite Java Composite Test leaf TestCase //composite Component public interface Test { Junit 4/4

5 public abstract void run(testresult result); //composite Leaf public abstract class TestCase extends Assert implements Test { public void run(testresult result) { result.run(this); Composite TestSuit TestSuit ftests (Vector ) addtest TestCase, testcount tests run() delegate addtestsuite public class TestSuite implements Test { private Vector ftests= new Vector(10); public void addtest(test test) { ftests.addelement(test); public Enumeration tests() { return ftests.elements(); public void run(testresult result) { for (Enumeration e= tests(); e.hasmoreelements(); ) { Test test= (Test)e.nextElement(); runtest(test, result); public void addtestsuite(class testclass) { addtest(new TestSuite(testClass)); Composite Test TestCase TestSuit Test addtestsuite TestSuite TestSuite TestSuit Junit 5/5

6 TestSuit TestCase public static Test suite() { TestSuite suite1 = new TestSuite(" TestSuit1"); TestSuite suite2 = new TestSuite(" TestSuit2"); suite1.addtestsuite(untitled6.testmath.class); suite2.addtestsuite(untitled6.testmulti.class); suite1.addtest(suite2); return suite1; Composite JUnit JUnit TestSuite TestCase JUnit if else TestCase TestSuite TestCase TestSuite TestCase Decorate TestCase 3.3 Template Method Connection public void testupdate(){ // Load the Oracle JDBC driver and Connect to the database DriverManager.registerDriver(new oracle.jdbc.oracledriver()); String url = Junit 6/6

7 Connection conn = DriverManager.getConnection (url, "hr", "hr"); // Select first_name and last_name column from the employees table ResultSet rset = stmt.executequery ("select FIRST_NAME, LAST_NAME from EMPLOYEES"); // Disconnect conn.close (); EJB JNDI LookUp Home TestCase Insert Update Delete Select connection JUnit Template Method Template Method Template Method AbstractClass ConcreteClass AbstractClass ConcreteClass ConcreteClass TestCase Junit 7/7

8 TestCase public abstract class TestCase extends Assert implements Test { // protected void setup() throws Exception { protected void runtest() throws Throwable { protected void teardown() throws Exception { // public void runbare() throws Throwable { setup(); runtest(); teardown(); setup EJB Home JNDI teardown runtest TestCase runbare setup, runtest, teardown, public class TestHelloWorldTestClientJUnit1 extends TestCase { public void setup() throws Exception { initialize();// JNDI create(); // EJB Home public void testgetmessage() throws RemoteException { assertnotnull(error_null_remote, helloworld); this.assertequals("hello World",helloWorld.getMessage(""));// public void teardown() throws Exception { helloworldhome = null; // EJB Home helloworld = null; // EJB setup teardown testgetmessage runtest Adapter testgetmessage runtest Junit 8/8

9 TestCase runbare() runtest() setup() teardown() TestHelloWorldTestClientJUnit1 setup() teardown() testgetmessage() Template Method TestCase JUnit 3.4 Adapter Command Command execute() TestCase run() command Testcase run testrun setup teardown testrun testgetmessage testsetmessage command JUnit Adapter testgetmessage JUnit Command TestCase run Adapter Junit 9/9

10 Target Adaptee Adapter TestCase public void testxxxxx() Adapter Command runtest java protected void runtest() throws Throwable { // testgetmessage runtest Method runmethod= getclass().getmethod(fname, null); runmethod.invoke(this, new Class[0]); Target Adapter,TestCase Adaptee TestCase runtest() TestHelloWorldTestClientJUnit1 testgetmessage() Adapter Adapter Adapter Adaptee 3.5 Observer JUnit failureserrors Junit 10/10

11 assertion ArrayIndexOutOfBoundsException GUI JUnit Text,AWT,Swing JUnit TestCase JUnit Observer Observer (Publish-Subscribe) Model/View Source/Listener JUnit, GUI Observer Observer Subject ConcreteSubject Observer ConcreteObserver Observer TestListener // A Listener for test progress public interface TestListener { // An error occurred. public void adderror(test test, Throwable t); Junit 11/11

12 // A failure occurred. public void addfailure(test test, AssertionFailedError t); // A test started. public void starttest(test test); //A test ended. public void endtest(test test); JUnit TestListener TextUI,AWTUi,SwingUI TestListener TextUi ResultPrinter public class ResultPrinter implements TestListener { PrintStream fwriter; * A test ended. public PrintStream getwriter() { return fwriter; public void starttest(test test) { getwriter().print("."); public void adderror(test test, Throwable t) { getwriter().print("e"); public void addfailure(test test, AssertionFailedError t) { getwriter().print("f"); public void endtest(test test) { JUnit TestResult Collecting Parameter The Smalltalk Best Practice Patterns ConcreteSubject JUnit Subject ConcreteSubject public class TestResult extends Object { // Vector protected Vector flisteners = new Vector(); // Registers a TestListener public synchronized void addlistener(testlistener listener) { flisteners.addelement(listener); //Unregisters a TestListener public synchronized void removelistener(testlistener listener) { flisteners.removeelement(listener); //Informs the result that a test will be started. public void starttest(test test) { for (Enumeration e= clonelisteners().elements(); e.hasmoreelements(); ) { Junit 12/12

13 ((TestListener)e.nextElement()).startTest(test); //Adds an error to the list of errors. The passed in exception //caused the error. public synchronized void adderror(test test, Throwable t) { for (Enumeration e= clonelisteners().elements(); e.hasmoreelements(); ) { ((TestListener)e.nextElement()).addError(test, t); // addfailure endtest TestRes ult flisteners : java.util.vector adderror() addfailure() addlistener() clonelisteners() endtest() removelistener() starttest() TestListener adderror() addfailure() endtest() starttest() ResultPrinter adderror() addfailure() endtest() getwriter() starttest() Observer Subject Observer TestResult TestListener TestResult TestResult TestResult ResultPrinter 3.6 Decorate TestCase Junit 13/13

14 TestCase Testcase TestCase TestCase Socket RepeatedTest TestCase RepeatedTest JUnit Decorator Decorator (Wrapper) Decorator TestCase Decorator Decorator Component ConcreteComponent Decorator Component ConcreteDecorator Decorator Test Component TestCase ConcreteComponent Decorator TestDecorator Test ftest run ftest run ConcreteComponent public class TestDecorator extends Assert implements Test { // protected Test ftest; public TestDecorator(Test test) { ftest= test; Junit 14/14

15 public void run(testresult result) { ftest.run(result); Decoretor RepeatedTest TestCase public class RepeatedTest extends TestDecorator { private int ftimesrepeat; public RepeatedTest(Test test, int repeat) { super(test); ftimesrepeat= repeat; // ConcreteComponent public void run(testresult result) { for (int i= 0; i < ftimesrepeat; i++) { if (result.shouldstop()) break; super.run(result); // TestCase Test TestDecorator RepeatedTest TestSetup TestCase public static Test suite() { TestSuite suite = new TestSuite(); suite.addtest(new TestSetup(new RepeatedTest(new Testmath("testAdd"),12))); return suite; TestCase RepeatedTest TestCase TestSetup Junit 15/15

16 3.6.4 Decorator TestCase RepeatedTest suite.addtest (new RepeatedTest(new Testmath("testAdd"),12)); TestCase Decorator TestCase suite.addtest(new TestSetup(new RepeatedTest(new Testmath("testAdd"),12))); 3.7 JUnit Junit 13 Junit 16/16