/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. */
  24. package hudson.remoting;
  25. import hudson.remoting.ChannelRunner.InProcess;
  26. import junit.framework.Test;
  27. import junit.framework.TestCase;
  28. import junit.framework.TestSuite;
  29. /**
  30. * Base class for remoting tests.
  31. *
  32. * @author Kohsuke Kawaguchi
  33. */
  34. @WithRunner({
  35. ChannelRunner.InProcess.class,
  36. ChannelRunner.InProcessCompatibilityMode.class,
  37. ChannelRunner.Fork.class
  38. })
  39. public abstract class RmiTestBase extends TestCase {
  40. protected Channel channel;
  41. protected ChannelRunner channelRunner = new InProcess();
  42. protected void setUp() throws Exception {
  43. System.out.println("Starting "+getName());
  44. channel = channelRunner.start();
  45. }
  46. protected void tearDown() throws Exception {
  47. channelRunner.stop(channel);
  48. }
  49. /*package*/ void setChannelRunner(Class<? extends ChannelRunner> runner) {
  50. try {
  51. this.channelRunner = runner.newInstance();
  52. } catch (InstantiationException e) {
  53. throw new Error(e);
  54. } catch (IllegalAccessException e) {
  55. throw new Error(e);
  56. }
  57. }
  58. public String getName() {
  59. return super.getName()+"-"+channelRunner.getName();
  60. }
  61. /**
  62. * Can be used in the suite method of the derived class to build a
  63. * {@link TestSuite} to run the test with all the available
  64. * {@link ChannelRunner} configuration.
  65. */
  66. protected static Test buildSuite(Class<? extends RmiTestBase> testClass) {
  67. TestSuite suite = new TestSuite();
  68. WithRunner wr = testClass.getAnnotation(WithRunner.class);
  69. for( Class<? extends ChannelRunner> r : wr.value() ) {
  70. suite.addTest(new ChannelTestSuite(testClass,r));
  71. }
  72. return suite;
  73. }
  74. }