File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\main\java\org\jvnet\hudson\test\TestEnvironment.java.html

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
26 import hudson.model.Computer;
27
28 import java.io.IOException;
29 import javax.annotation.CheckForNull;
30 import javax.annotation.Nonnull;
31 import org.junit.runner.Description;
32
33 /**
34 * TODO: deprecate this, and just consolidate this to {@link HudsonTestCase}.
35 * We can then pin down the current HudsonTestCase to the thread for easier access.
36 *
37 * @author Kohsuke Kawaguchi
38 */
39 public class TestEnvironment {
40 /**
41 * Current test case being run (null for a JUnit 4 test).
42 */
43 public final @CheckForNull HudsonTestCase testCase;
44
45 private final @CheckForNull Description description;
46
47 public final TemporaryDirectoryAllocator temporaryDirectoryAllocator = new TemporaryDirectoryAllocator();
48
49 public TestEnvironment(@Nonnull HudsonTestCase testCase) {
50 this.testCase = testCase;
51 this.description = null;
52 }
53
54 public TestEnvironment(@Nonnull Description description) {
55 this.testCase = null;
56 this.description = description;
57 }
58
59 /**
60 * Current test case being run (works for JUnit 3 or 4).
61 * Warning: {@link Description#getTestClass} is currently broken in some environments (claimed fixed in JUnit 4.11). Use {@link Description#getClassName} instead.
62 */
63 public @Nonnull Description description() {
64 if (description != null) {
65 return description;
66 } else {
67 assert testCase != null;
68 // Initialize lazily; testCase.getName() may be null in the TestEnvironment constructor.
69 return Description.createTestDescription(testCase.getClass(), testCase.getName());
70 }
71 }
72
73 public void pin() {
74 CURRENT = this;
75 }
76
77 public void dispose() throws IOException, InterruptedException {
78 temporaryDirectoryAllocator.dispose();
79 CURRENT = null;
80 }
81
82 /**
83 * We used to use {@link InheritableThreadLocal} here, but it turns out this is not reliable,
84 * especially in the {@link Computer#threadPoolForRemoting}, where threads can inherit
85 * the wrong test environment depending on when it's created.
86 *
87 * <p>
88 * Since the rest of Hudson still relies on static {@link jenkins.model.Jenkins#theInstance}, changing this
89 * to a static field for now shouldn't cause any problem.
90 */
91 private static TestEnvironment CURRENT;
92
93 public static TestEnvironment get() {
94 return CURRENT;
95 }
96 }
97