Andrea Aime ha scritto:

I've attached the latest version of that junit3 based thing.

... this time with attachment
Cheers
Andrea
package junit3;

import junit.extensions.TestSetup;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Abstract class for tests that do need to run a one time setup/teardown 
phase.<p>
 * Candidate tests are the ones that setup an expensive test fixture that 
happens
 * to be reusable in subsequent tests (that is, its state is not modified 
running
 * the tests).
 * <p>Such tests can extend this class, implement the expensive setup/teardown
 * phases in [EMAIL PROTECTED] #oneTimeSetUp()} and [EMAIL PROTECTED] 
#oneTimeTearDown()}, and eventual
 * per method setup/teardown in [EMAIL PROTECTED] #setUpInternal()} and [EMAIL 
PROTECTED] #tearDownInternal()}.
 * <p>
 * In order to activate the one time setup the generic MyOneTimeSetupTest class 
will
 * also have to add the following static method:
 * <pre>
 * public static Test suite() {
 *     return new OneTimeTestSetup(new MyOneTimeSetupTest()); 
 * }
 * </pre>
 * @author Andrea Aime - TOPP
 * @author Gabriel Roldan - TOPP
 */
public abstract class OneTimeSetupTest extends TestCase {
    private static boolean oneTimeSetupDone;
    private static boolean forceOneTimeTearDown;

    /**
     * Test suite wrapper 
     * @author Andrea Aime - TOPP
     * @author Gabriel Roldan - TOPP
     */
    protected static class OneTimeTestSetup extends TestSetup {

        private OneTimeSetupTest test;

        public OneTimeTestSetup(OneTimeSetupTest test) {
            super(new TestSuite(test.getClass()));
            this.test = test;
        }
        
        @Override
        protected void setUp() throws Exception {
            super.setUp();
            oneTimeSetupDone = true;
            test.oneTimeSetUp();
        }
        
        @Override
        protected void tearDown() throws Exception {
            super.tearDown();
            test.oneTimeTearDown();
        }
        
    }

    /**
     * This provides the one time setup for the expensive fixture. The fields 
making up the
     * fixture will have to be static ones, to avoid loosing their values as 
JUnit goes
     * through the test methods (for each one a new instance of the class will 
be created)
     */
    public abstract void oneTimeSetUp();
    
    /**
     * This provides the one time teardown for the expensive fixture. 
     */
    public abstract void oneTimeTearDown();
    
    /**
     * Provides the proper behavior so that the one time setup is run once 
matter how the
     * test is started. If you need to implement a per test method setup, 
override [EMAIL PROTECTED] #setUpInternal()}
     */
    @Override
    protected final void setUp() throws Exception {
        if(!oneTimeSetupDone) {
            oneTimeSetUp();
            forceOneTimeTearDown = true;
        }
            
        setUpInternal();
    }
    
    /**
     * Provides the proper behavior so that the one time tear down is once no 
matter how the
     * test is started. If you need to implement a per test method setup, 
overide [EMAIL PROTECTED] #tearDownInternal()}
     */
    @Override
    protected final void tearDown() throws Exception {
        tearDownInternal();
        if(forceOneTimeTearDown)
            oneTimeTearDown();
    }
    
    /**
     * Per method setup (fixture can be stored in non static fields)
     * @throws Exception
     */
    protected void setUpInternal() throws Exception {
    }
    
    /**
     * Per method tear down 
     * @throws Exception
     */
    protected void tearDownInternal() throws Exception {
        
    }
}
package junit3;

import junit.framework.Test;


public class OneTimeSetupTestTest extends OneTimeSetupTest {
    
//    Comment to have one time setup run only once
    public static Test suite() {
        return new OneTimeTestSetup(new OneTimeSetupTestTest()); 
    }
    
    public void oneTimeSetUp() {
        System.out.println("One time setup for JUnit3");
    }
    
    public void oneTimeTearDown() {
        System.out.println("One time teardown");
    }
    
    protected void setUpInternal() throws Exception {
        System.out.println("Local setup");
    }
    
    protected void tearDownInternal() throws Exception {
        System.out.println("Local teardown");
    }
    
    public void testOne() {
        System.out.println("First test");
    }
    
    public void testTwo() {
        System.out.println("Second test");
    }
}
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Geoserver-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geoserver-devel

Reply via email to