PageRenderTime 18ms CodeModel.GetById 10ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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