PageRenderTime 31ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/hudson-remoting/src/test/java/hudson/remoting/RmiTestBase.java

http://github.com/hudson/hudson
Java | 84 lines | 41 code | 10 blank | 33 comment | 1 complexity | 86422679b452dc9f06a8f00649156953 MD5 | raw file
 1/*
 2 * The MIT License
 3 * 
 4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, InfraDNA, Inc.
 5 * 
 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
 7 * of this software and associated documentation files (the "Software"), to deal
 8 * in the Software without restriction, including without limitation the rights
 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 * 
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 * 
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24package hudson.remoting;
25
26import hudson.remoting.ChannelRunner.InProcess;
27import junit.framework.Test;
28import junit.framework.TestCase;
29import junit.framework.TestSuite;
30
31/**
32 * Base class for remoting tests.
33 * 
34 * @author Kohsuke Kawaguchi
35 */
36@WithRunner({
37    ChannelRunner.InProcess.class,
38    ChannelRunner.InProcessCompatibilityMode.class,
39    ChannelRunner.Fork.class
40})
41public abstract class RmiTestBase extends TestCase {
42
43    protected Channel channel;
44    protected ChannelRunner channelRunner = new InProcess();
45
46    protected void setUp() throws Exception {
47        System.out.println("Starting "+getName());
48        channel = channelRunner.start();
49    }
50
51    protected void tearDown() throws Exception {
52        channelRunner.stop(channel);
53    }
54
55    /*package*/ void setChannelRunner(Class<? extends ChannelRunner> runner) {
56        try {
57            this.channelRunner = runner.newInstance();
58        } catch (InstantiationException e) {
59            throw new Error(e);
60        } catch (IllegalAccessException e) {
61            throw new Error(e);
62        }
63    }
64
65    public String getName() {
66        return super.getName()+"-"+channelRunner.getName();
67    }
68
69    /**
70     * Can be used in the suite method of the derived class to build a
71     * {@link TestSuite} to run the test with all the available
72     * {@link ChannelRunner} configuration.
73     */
74    protected static Test buildSuite(Class<? extends RmiTestBase> testClass) {
75        TestSuite suite = new TestSuite();
76
77        WithRunner wr = testClass.getAnnotation(WithRunner.class);
78
79        for( Class<? extends ChannelRunner> r : wr.value() ) {
80            suite.addTest(new ChannelTestSuite(testClass,r));
81        }
82        return suite;
83    }
84}