File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\main\java\org\jvnet\hudson\test\HudsonHomeLoader.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.FilePath;
27 import org.apache.commons.io.FileUtils;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.lang.reflect.Method;
32 import java.net.MalformedURLException;
33 import java.net.URL;
34
35 /**
36 * Controls how a {@link HudsonTestCase} initializes <tt>JENKINS_HOME</tt>.
37 *
38 * @author Kohsuke Kawaguchi
39 */
40 public interface HudsonHomeLoader {
41 /**
42 * Returns a directory to be used as <tt>JENKINS_HOME</tt>
43 *
44 * @throws Exception
45 * to cause a test to fail.
46 */
47 File allocate() throws Exception;
48
49 /**
50 * Allocates a new empty directory, meaning this will emulate the fresh Hudson installation.
51 */
52 HudsonHomeLoader NEW = new HudsonHomeLoader() {
53 public File allocate() throws IOException {
54 return TestEnvironment.get().temporaryDirectoryAllocator.allocate();
55 }
56 };
57
58 /**
59 * Allocates a new directory by copying from an existing directory, or unzipping from a zip file.
60 */
61 final class CopyExisting implements HudsonHomeLoader {
62 private final URL source;
63
64 /**
65 * Either a zip file or a directory that contains the home image.
66 */
67 public CopyExisting(File source) throws MalformedURLException {
68 this(source.toURI().toURL());
69 }
70
71 /**
72 * Extracts from a zip file in the resource.
73 *
74 * <p>
75 * This is useful in case you want to have a test data in the resources.
76 * Only file URL is supported.
77 */
78 public CopyExisting(URL source) {
79 this.source = source;
80 }
81
82 public File allocate() throws Exception {
83 File target = NEW.allocate();
84 if(source.getProtocol().equals("file")) {
85 File src = new File(source.toURI());
86 if(src.isDirectory())
87 new FilePath(src).copyRecursiveTo("**/*",new FilePath(target));
88 else
89 if(src.getName().endsWith(".zip"))
90 new FilePath(src).unzip(new FilePath(target));
91 } else {
92 File tmp = File.createTempFile("hudson","zip");
93 try {
94 FileUtils.copyURLToFile(source,tmp);
95 new FilePath(tmp).unzip(new FilePath(target));
96 } finally {
97 tmp.delete();
98 }
99 }
100 return target;
101 }
102 }
103
104 /**
105 * Allocates a new directory by copying from a test resource
106 */
107 final class Local implements HudsonHomeLoader {
108 private final Method testMethod;
109
110 public Local(Method testMethod) {
111 this.testMethod = testMethod;
112 }
113
114 public File allocate() throws Exception {
115 URL res = findDataResource();
116 if(!res.getProtocol().equals("file"))
117 throw new AssertionError("Test data is not available in the file system: "+res);
118 // if we picked up a directory, it's one level above config.xml
119 File home = new File(res.toURI());
120 if(!home.getName().endsWith(".zip"))
121 home = home.getParentFile();
122
123 return new CopyExisting(home).allocate();
124 }
125
126 private URL findDataResource() {
127 // first, check method specific resource
128 Class<?> clazz = testMethod.getDeclaringClass();
129
130 for( String middle : new String[]{ '/'+testMethod.getName(), "" }) {
131 for( String suffix : SUFFIXES ) {
132 URL res = clazz.getResource(clazz.getSimpleName() + middle+suffix);
133 if(res!=null) return res;
134 }
135 }
136
137 throw new AssertionError("No test resource was found for "+testMethod);
138 }
139
140 private static final String[] SUFFIXES = {"/config.xml",".zip"};
141 }
142 }
143