-
Notifications
You must be signed in to change notification settings - Fork 124
/
TestPluginManager.java
185 lines (164 loc) · 6.68 KB
/
TestPluginManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* The MIT License
*
* Copyright (c) 2010, Kohsuke Kawaguchi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jvnet.hudson.test;
import hudson.Plugin;
import hudson.PluginManager;
import hudson.PluginWrapper;
import hudson.Util;
import org.junit.Assert;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* {@link PluginManager} to speed up unit tests.
*
* <p>
* Instead of loading every plugin for every test case, this allows them to reuse a single plugin manager.
*
* <p>
* TODO: {@link Plugin} start/stop/postInitialize invocation semantics gets different. Perhaps
*
* @author Kohsuke Kawaguchi
* @see HudsonTestCase#useLocalPluginManager
*/
public class TestPluginManager extends PluginManager {
public static final PluginManager INSTANCE;
public TestPluginManager() throws IOException {
// TestPluginManager outlives a Jetty server, so can't pass in ServletContext.
super(null, Util.createTempDir());
}
@Override
protected Collection<String> loadBundledPlugins() throws Exception {
Set<String> names = new HashSet<String>();
names.addAll(loadBundledPlugins(new File(WarExploder.getExplodedDir(), "WEB-INF/plugins")));
loadBundledPlugins(new File(WarExploder.getExplodedDir(), "WEB-INF/detached-plugins"));
return names;
}
private Set<String> loadBundledPlugins(File fromDir) throws IOException, URISyntaxException {
if (!fromDir.exists()) {
LOGGER.log(Level.FINE, "No plugins loaded from " + fromDir + ". Directory doesn't exist.");
return Collections.emptySet();
}
Set<String> names = new HashSet<String>();
File[] children = fromDir.listFiles();
if (children==null)
throw new Error("Unable to find "+fromDir);
for (File child : children) {
try {
names.add(child.getName());
copyBundledPlugin(child.toURI().toURL(), child.getName());
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to extract the bundled plugin "+child,e);
}
}
// If running tests for a plugin, include the plugin being tested
URL u = getClass().getClassLoader().getResource("the.jpl");
if(u==null){
u = getClass().getClassLoader().getResource("the.hpl"); // keep backward compatible
}
if (u!=null) try {
names.add("the.jpl");
copyBundledPlugin(u, "the.jpl");
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to copy the.jpl",e);
}
// and pick up test dependency *.jpi that are placed by maven-hpi-plugin TestDependencyMojo.
// and copy them into $JENKINS_HOME/plugins.
URL index = getClass().getResource("/test-dependencies/index");
if (index!=null) {// if built with maven-hpi-plugin < 1.52 this file won't exist.
BufferedReader r = new BufferedReader(new InputStreamReader(index.openStream(),"UTF-8"));
try {
String line;
while ((line=r.readLine())!=null) {
final URL url = new URL(index, line + ".jpi");
File f = new File(url.toURI());
if(f.exists()){
copyBundledPlugin(url, line + ".jpi");
}else{
copyBundledPlugin(new URL(index, line + ".hpi"), line + ".jpi"); // fallback to hpi
}
}
} finally {
r.close();
}
}
return names;
}
/**
* Install a plugin from the resources directory.
* @param pluginName The plugin name.
* @throws IOException Error copying plugin.
*/
public void installResourcePlugin(String pluginName) throws Exception {
URL res = TestPluginManager.class.getClassLoader().getResource("plugins/" + pluginName);
if (res == null) {
Assert.fail("Plugin '" + pluginName + "' not found in /resources/plugins.");
}
copyBundledPlugin(res, pluginName);
}
// Overwrite PluginManager#stop, not to release plugins in each tests.
// Releasing plugins result fail to access files in webapp directory in following tests.
@Override
public void stop() {
for (PluginWrapper p : activePlugins)
p.stop();
}
/**
* As we don't actually shut down classloaders, we instead provide this method that does
* what {@link #stop()} normally does.
*/
private void reallyStop() {
super.stop();
}
private static final Logger LOGGER = Logger.getLogger(TestPluginManager.class.getName());
static {
try {
INSTANCE = new TestPluginManager();
Runtime.getRuntime().addShutdownHook(new Thread("delete " + INSTANCE.rootDir) {
@Override public void run() {
// Shutdown and release plugins as in PluginManager#stop
((TestPluginManager)INSTANCE).reallyStop();
// allow JVM cleanup handles of jar files...
System.gc();
try {
Util.deleteRecursive(INSTANCE.rootDir);
} catch (IOException x) {
x.printStackTrace();
}
}
});
} catch (IOException e) {
throw new Error(e);
}
}
}