/hudson-test-framework/src/main/java/org/jvnet/hudson/test/TestEnvironment.java

http://github.com/hudson/hudson · Java · 72 lines · 22 code · 9 blank · 41 comment · 0 complexity · eeecfd28cc50641d16f68af2b7fc2fd9 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
  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 org.jvnet.hudson.test;
  25. import hudson.model.Computer;
  26. import hudson.model.Hudson;
  27. import java.io.IOException;
  28. /**
  29. * TODO: deprecate this, and just consolidate this to {@link HudsonTestCase}.
  30. * We can then pin down the current HudsonTestCase to the thread for easier access.
  31. *
  32. * @author Kohsuke Kawaguchi
  33. */
  34. public class TestEnvironment {
  35. /**
  36. * Current test case being run.
  37. */
  38. public final HudsonTestCase testCase;
  39. public final TemporaryDirectoryAllocator temporaryDirectoryAllocator = new TemporaryDirectoryAllocator();
  40. public TestEnvironment(HudsonTestCase testCase) {
  41. this.testCase = testCase;
  42. }
  43. public void pin() {
  44. CURRENT = this;
  45. }
  46. public void dispose() throws IOException, InterruptedException {
  47. temporaryDirectoryAllocator.dispose();
  48. CURRENT = null;
  49. }
  50. /**
  51. * We used to use {@link InheritableThreadLocal} here, but it turns out this is not reliable,
  52. * especially in the {@link Computer#threadPoolForRemoting}, where threads can inherit
  53. * the wrong test environment depending on when it's created.
  54. *
  55. * <p>
  56. * Since the rest of Hudson still relies on static {@link Hudson#theInstance}, changing this
  57. * to a static field for now shouldn't cause any problem.
  58. */
  59. private static TestEnvironment CURRENT;
  60. public static TestEnvironment get() {
  61. return CURRENT;
  62. }
  63. }