提交 9062a944 编写于 作者: J Jesse Glick

JUnit 4.

上级 e0d0490a
...@@ -42,8 +42,12 @@ import java.util.List; ...@@ -42,8 +42,12 @@ import java.util.List;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.tools.ant.filters.StringInputStream; import org.apache.tools.ant.filters.StringInputStream;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.Bug; import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.HudsonTestCase; import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.Url; import org.jvnet.hudson.test.Url;
import org.jvnet.hudson.test.recipes.WithPlugin; import org.jvnet.hudson.test.recipes.WithPlugin;
import org.jvnet.hudson.test.recipes.WithPluginManager; import org.jvnet.hudson.test.recipes.WithPluginManager;
...@@ -51,67 +55,70 @@ import org.jvnet.hudson.test.recipes.WithPluginManager; ...@@ -51,67 +55,70 @@ import org.jvnet.hudson.test.recipes.WithPluginManager;
/** /**
* @author Kohsuke Kawaguchi * @author Kohsuke Kawaguchi
*/ */
public class PluginManagerTest extends HudsonTestCase { public class PluginManagerTest {
@Override
protected void setUp() throws Exception { @Rule public JenkinsRule r = new JenkinsRule() {
setPluginManager(null); // use a fresh instance @Override public void before() throws Throwable {
super.setUp(); setPluginManager(null);
} super.before();
}
};
@Rule public TemporaryFolder tmp = new TemporaryFolder();
/** /**
* Manual submission form. * Manual submission form.
*/ */
public void testUploadJpi() throws Exception { @Test public void uploadJpi() throws Exception {
HtmlPage page = new WebClient().goTo("pluginManager/advanced"); HtmlPage page = r.createWebClient().goTo("pluginManager/advanced");
HtmlForm f = page.getFormByName("uploadPlugin"); HtmlForm f = page.getFormByName("uploadPlugin");
File dir = env.temporaryDirectoryAllocator.allocate(); File dir = tmp.newFolder();
File plugin = new File(dir, "tasks.jpi"); File plugin = new File(dir, "tasks.jpi");
FileUtils.copyURLToFile(getClass().getClassLoader().getResource("plugins/tasks.jpi"),plugin); FileUtils.copyURLToFile(getClass().getClassLoader().getResource("plugins/tasks.jpi"),plugin);
f.getInputByName("name").setValueAttribute(plugin.getAbsolutePath()); f.getInputByName("name").setValueAttribute(plugin.getAbsolutePath());
submit(f); r.submit(f);
assertTrue( new File(jenkins.getRootDir(),"plugins/tasks.jpi").exists() ); assertTrue( new File(r.jenkins.getRootDir(),"plugins/tasks.jpi").exists() );
} }
/** /**
* Manual submission form. * Manual submission form.
*/ */
public void testUploadHpi() throws Exception { @Test public void uploadHpi() throws Exception {
HtmlPage page = new WebClient().goTo("pluginManager/advanced"); HtmlPage page = r.createWebClient().goTo("pluginManager/advanced");
HtmlForm f = page.getFormByName("uploadPlugin"); HtmlForm f = page.getFormByName("uploadPlugin");
File dir = env.temporaryDirectoryAllocator.allocate(); File dir = tmp.newFolder();
File plugin = new File(dir, "legacy.hpi"); File plugin = new File(dir, "legacy.hpi");
FileUtils.copyURLToFile(getClass().getClassLoader().getResource("plugins/legacy.hpi"),plugin); FileUtils.copyURLToFile(getClass().getClassLoader().getResource("plugins/legacy.hpi"),plugin);
f.getInputByName("name").setValueAttribute(plugin.getAbsolutePath()); f.getInputByName("name").setValueAttribute(plugin.getAbsolutePath());
submit(f); r.submit(f);
// uploaded legacy plugins get renamed to *.jpi // uploaded legacy plugins get renamed to *.jpi
assertTrue( new File(jenkins.getRootDir(),"plugins/legacy.jpi").exists() ); assertTrue( new File(r.jenkins.getRootDir(),"plugins/legacy.jpi").exists() );
} }
/** /**
* Tests the effect of {@link WithPlugin}. * Tests the effect of {@link WithPlugin}.
*/ */
@WithPlugin("tasks.jpi") @WithPlugin("tasks.jpi")
public void testWithRecipeJpi() throws Exception { @Test public void withRecipeJpi() throws Exception {
assertNotNull(jenkins.getPlugin("tasks")); assertNotNull(r.jenkins.getPlugin("tasks"));
} }
/** /**
* Tests the effect of {@link WithPlugin}. * Tests the effect of {@link WithPlugin}.
*/ */
@WithPlugin("legacy.hpi") @WithPlugin("legacy.hpi")
public void testWithRecipeHpi() throws Exception { @Test public void withRecipeHpi() throws Exception {
assertNotNull(jenkins.getPlugin("legacy")); assertNotNull(r.jenkins.getPlugin("legacy"));
} }
/** /**
* Makes sure that plugins can see Maven2 plugin that's refactored out in 1.296. * Makes sure that plugins can see Maven2 plugin that's refactored out in 1.296.
*/ */
@WithPlugin("tasks.jpi") @WithPlugin("tasks.jpi")
public void testOptionalMavenDependency() throws Exception { @Test public void optionalMavenDependency() throws Exception {
PluginWrapper.Dependency m2=null; PluginWrapper.Dependency m2=null;
PluginWrapper tasks = jenkins.getPluginManager().getPlugin("tasks"); PluginWrapper tasks = r.jenkins.getPluginManager().getPlugin("tasks");
for( PluginWrapper.Dependency d : tasks.getOptionalDependencies() ) { for( PluginWrapper.Dependency d : tasks.getOptionalDependencies() ) {
if(d.shortName.equals("maven-plugin")) { if(d.shortName.equals("maven-plugin")) {
assertNull(m2); assertNull(m2);
...@@ -134,11 +141,11 @@ public class PluginManagerTest extends HudsonTestCase { ...@@ -134,11 +141,11 @@ public class PluginManagerTest extends HudsonTestCase {
*/ */
@WithPlugin("tasks.jpi") @WithPlugin("tasks.jpi")
@WithPluginManager(PluginManagerImpl_for_testUberClassLoaderIsAvailableDuringStart.class) @WithPluginManager(PluginManagerImpl_for_testUberClassLoaderIsAvailableDuringStart.class)
public void testUberClassLoaderIsAvailableDuringStart() { @Test public void uberClassLoaderIsAvailableDuringStart() {
assertTrue(((PluginManagerImpl_for_testUberClassLoaderIsAvailableDuringStart) jenkins.pluginManager).tested); assertTrue(((PluginManagerImpl_for_testUberClassLoaderIsAvailableDuringStart) r.jenkins.pluginManager).tested);
} }
public class PluginManagerImpl_for_testUberClassLoaderIsAvailableDuringStart extends LocalPluginManager { public static class PluginManagerImpl_for_testUberClassLoaderIsAvailableDuringStart extends LocalPluginManager {
boolean tested; boolean tested;
public PluginManagerImpl_for_testUberClassLoaderIsAvailableDuringStart(File rootDir) { public PluginManagerImpl_for_testUberClassLoaderIsAvailableDuringStart(File rootDir) {
...@@ -170,10 +177,10 @@ public class PluginManagerTest extends HudsonTestCase { ...@@ -170,10 +177,10 @@ public class PluginManagerTest extends HudsonTestCase {
* infinite cycle ensues. * infinite cycle ensues.
*/ */
@Url("http://jenkins.361315.n4.nabble.com/channel-example-and-plugin-classes-gives-ClassNotFoundException-td3756092.html") @Url("http://jenkins.361315.n4.nabble.com/channel-example-and-plugin-classes-gives-ClassNotFoundException-td3756092.html")
public void testUberClassLoaderDoesntUseContextClassLoader() throws Exception { @Test public void uberClassLoaderDoesntUseContextClassLoader() throws Exception {
Thread t = Thread.currentThread(); Thread t = Thread.currentThread();
URLClassLoader ucl = new URLClassLoader(new URL[0], jenkins.pluginManager.uberClassLoader); URLClassLoader ucl = new URLClassLoader(new URL[0], r.jenkins.pluginManager.uberClassLoader);
ClassLoader old = t.getContextClassLoader(); ClassLoader old = t.getContextClassLoader();
t.setContextClassLoader(ucl); t.setContextClassLoader(ucl);
...@@ -191,33 +198,33 @@ public class PluginManagerTest extends HudsonTestCase { ...@@ -191,33 +198,33 @@ public class PluginManagerTest extends HudsonTestCase {
} }
} }
public void testInstallWithoutRestart() throws Exception { @Test public void installWithoutRestart() throws Exception {
URL res = getClass().getClassLoader().getResource("plugins/htmlpublisher.jpi"); URL res = getClass().getClassLoader().getResource("plugins/htmlpublisher.jpi");
File f = new File(jenkins.getRootDir(), "plugins/htmlpublisher.jpi"); File f = new File(r.jenkins.getRootDir(), "plugins/htmlpublisher.jpi");
FileUtils.copyURLToFile(res, f); FileUtils.copyURLToFile(res, f);
jenkins.pluginManager.dynamicLoad(f); r.jenkins.pluginManager.dynamicLoad(f);
Class c = jenkins.getPluginManager().uberClassLoader.loadClass("htmlpublisher.HtmlPublisher$DescriptorImpl"); Class c = r.jenkins.getPluginManager().uberClassLoader.loadClass("htmlpublisher.HtmlPublisher$DescriptorImpl");
assertNotNull(jenkins.getDescriptorByType(c)); assertNotNull(r.jenkins.getDescriptorByType(c));
} }
public void testPrevalidateConfig() throws Exception { @Test public void prevalidateConfig() throws Exception {
PersistedList<UpdateSite> sites = jenkins.getUpdateCenter().getSites(); PersistedList<UpdateSite> sites = r.jenkins.getUpdateCenter().getSites();
sites.clear(); sites.clear();
URL url = PluginManagerTest.class.getResource("/plugins/tasks-update-center.json"); URL url = PluginManagerTest.class.getResource("/plugins/tasks-update-center.json");
UpdateSite site = new UpdateSite(UpdateCenter.ID_DEFAULT, url.toString()); UpdateSite site = new UpdateSite(UpdateCenter.ID_DEFAULT, url.toString());
sites.add(site); sites.add(site);
assertEquals(FormValidation.ok(), site.updateDirectly(false).get()); assertEquals(FormValidation.ok(), site.updateDirectly(false).get());
assertNotNull(site.getData()); assertNotNull(site.getData());
assertEquals(Collections.emptyList(), jenkins.getPluginManager().prevalidateConfig(new StringInputStream("<whatever><runant plugin=\"ant@1.1\"/></whatever>"))); assertEquals(Collections.emptyList(), r.jenkins.getPluginManager().prevalidateConfig(new StringInputStream("<whatever><runant plugin=\"ant@1.1\"/></whatever>")));
assertNull(jenkins.getPluginManager().getPlugin("tasks")); assertNull(r.jenkins.getPluginManager().getPlugin("tasks"));
List<Future<UpdateCenterJob>> jobs = jenkins.getPluginManager().prevalidateConfig(new StringInputStream("<whatever><tasks plugin=\"tasks@2.23\"/></whatever>")); List<Future<UpdateCenterJob>> jobs = r.jenkins.getPluginManager().prevalidateConfig(new StringInputStream("<whatever><tasks plugin=\"tasks@2.23\"/></whatever>"));
assertEquals(1, jobs.size()); assertEquals(1, jobs.size());
UpdateCenterJob job = jobs.get(0).get(); // blocks for completion UpdateCenterJob job = jobs.get(0).get(); // blocks for completion
assertEquals("InstallationJob", job.getType()); assertEquals("InstallationJob", job.getType());
UpdateCenter.InstallationJob ijob = (UpdateCenter.InstallationJob) job; UpdateCenter.InstallationJob ijob = (UpdateCenter.InstallationJob) job;
assertEquals("tasks", ijob.plugin.name); assertEquals("tasks", ijob.plugin.name);
assertNotNull(jenkins.getPluginManager().getPlugin("tasks")); assertNotNull(r.jenkins.getPluginManager().getPlugin("tasks"));
// TODO restart scheduled (SuccessButRequiresRestart) after upgrade or Support-Dynamic-Loading: false // TODO restart scheduled (SuccessButRequiresRestart) after upgrade or Support-Dynamic-Loading: false
// TODO dependencies installed or upgraded too // TODO dependencies installed or upgraded too
// TODO required plugin installed but inactive // TODO required plugin installed but inactive
...@@ -257,7 +264,7 @@ public class PluginManagerTest extends HudsonTestCase { ...@@ -257,7 +264,7 @@ public class PluginManagerTest extends HudsonTestCase {
* @throws Exception * @throws Exception
*/ */
private String callDependerValue() throws Exception { private String callDependerValue() throws Exception {
Class<?> c = jenkins.getPluginManager().uberClassLoader.loadClass("org.jenkinsci.plugins.dependencytest.depender.Depender"); Class<?> c = r.jenkins.getPluginManager().uberClassLoader.loadClass("org.jenkinsci.plugins.dependencytest.depender.Depender");
Method m = c.getMethod("getValue"); Method m = c.getMethod("getValue");
return (String)m.invoke(null); return (String)m.invoke(null);
} }
...@@ -268,14 +275,14 @@ public class PluginManagerTest extends HudsonTestCase { ...@@ -268,14 +275,14 @@ public class PluginManagerTest extends HudsonTestCase {
* *
* @throws Exception * @throws Exception
*/ */
public void testInstallDependingPluginWithoutRestart() throws Exception { @Test public void installDependingPluginWithoutRestart() throws Exception {
// Load dependee. // Load dependee.
{ {
String target = "dependee.hpi"; String target = "dependee.hpi";
URL src = getClass().getClassLoader().getResource(String.format("plugins/%s", target)); URL src = getClass().getClassLoader().getResource(String.format("plugins/%s", target));
File dest = new File(jenkins.getRootDir(), String.format("plugins/%s", target)); File dest = new File(r.jenkins.getRootDir(), String.format("plugins/%s", target));
FileUtils.copyURLToFile(src, dest); FileUtils.copyURLToFile(src, dest);
jenkins.pluginManager.dynamicLoad(dest); r.jenkins.pluginManager.dynamicLoad(dest);
} }
// before load depender, of course failed to call Depender.getValue() // before load depender, of course failed to call Depender.getValue()
...@@ -286,22 +293,22 @@ public class PluginManagerTest extends HudsonTestCase { ...@@ -286,22 +293,22 @@ public class PluginManagerTest extends HudsonTestCase {
} }
// No extensions exist. // No extensions exist.
assertTrue(jenkins.getExtensionList("org.jenkinsci.plugins.dependencytest.dependee.DependeeExtensionPoint").isEmpty()); assertTrue(r.jenkins.getExtensionList("org.jenkinsci.plugins.dependencytest.dependee.DependeeExtensionPoint").isEmpty());
// Load depender. // Load depender.
{ {
String target = "depender.hpi"; String target = "depender.hpi";
URL src = getClass().getClassLoader().getResource(String.format("plugins/%s", target)); URL src = getClass().getClassLoader().getResource(String.format("plugins/%s", target));
File dest = new File(jenkins.getRootDir(), String.format("plugins/%s", target)); File dest = new File(r.jenkins.getRootDir(), String.format("plugins/%s", target));
FileUtils.copyURLToFile(src, dest); FileUtils.copyURLToFile(src, dest);
jenkins.pluginManager.dynamicLoad(dest); r.jenkins.pluginManager.dynamicLoad(dest);
} }
// depender successfully accesses to dependee. // depender successfully accesses to dependee.
assertEquals("dependee", callDependerValue()); assertEquals("dependee", callDependerValue());
// Extension in depender is loaded. // Extension in depender is loaded.
assertFalse(jenkins.getExtensionList("org.jenkinsci.plugins.dependencytest.dependee.DependeeExtensionPoint").isEmpty()); assertFalse(r.jenkins.getExtensionList("org.jenkinsci.plugins.dependencytest.dependee.DependeeExtensionPoint").isEmpty());
} }
/** /**
...@@ -311,14 +318,14 @@ public class PluginManagerTest extends HudsonTestCase { ...@@ -311,14 +318,14 @@ public class PluginManagerTest extends HudsonTestCase {
* @throws Exception * @throws Exception
*/ */
@Bug(19976) @Bug(19976)
public void testInstallDependedPluginWithoutRestart() throws Exception { @Test public void installDependedPluginWithoutRestart() throws Exception {
// Load depender. // Load depender.
{ {
String target = "depender.hpi"; String target = "depender.hpi";
URL src = getClass().getClassLoader().getResource(String.format("plugins/%s", target)); URL src = getClass().getClassLoader().getResource(String.format("plugins/%s", target));
File dest = new File(jenkins.getRootDir(), String.format("plugins/%s", target)); File dest = new File(r.jenkins.getRootDir(), String.format("plugins/%s", target));
FileUtils.copyURLToFile(src, dest); FileUtils.copyURLToFile(src, dest);
jenkins.pluginManager.dynamicLoad(dest); r.jenkins.pluginManager.dynamicLoad(dest);
} }
// before load dependee, depender does not access to dependee. // before load dependee, depender does not access to dependee.
...@@ -326,7 +333,7 @@ public class PluginManagerTest extends HudsonTestCase { ...@@ -326,7 +333,7 @@ public class PluginManagerTest extends HudsonTestCase {
// before load dependee, of course failed to list extensions for dependee. // before load dependee, of course failed to list extensions for dependee.
try { try {
jenkins.getExtensionList("org.jenkinsci.plugins.dependencytest.dependee.DependeeExtensionPoint"); r.jenkins.getExtensionList("org.jenkinsci.plugins.dependencytest.dependee.DependeeExtensionPoint");
fail(); fail();
} catch( ClassNotFoundException _ ){ } catch( ClassNotFoundException _ ){
} }
...@@ -335,9 +342,9 @@ public class PluginManagerTest extends HudsonTestCase { ...@@ -335,9 +342,9 @@ public class PluginManagerTest extends HudsonTestCase {
{ {
String target = "dependee.hpi"; String target = "dependee.hpi";
URL src = getClass().getClassLoader().getResource(String.format("plugins/%s", target)); URL src = getClass().getClassLoader().getResource(String.format("plugins/%s", target));
File dest = new File(jenkins.getRootDir(), String.format("plugins/%s", target)); File dest = new File(r.jenkins.getRootDir(), String.format("plugins/%s", target));
FileUtils.copyURLToFile(src, dest); FileUtils.copyURLToFile(src, dest);
jenkins.pluginManager.dynamicLoad(dest); r.jenkins.pluginManager.dynamicLoad(dest);
} }
// (MUST) Not throws an exception // (MUST) Not throws an exception
...@@ -346,7 +353,7 @@ public class PluginManagerTest extends HudsonTestCase { ...@@ -346,7 +353,7 @@ public class PluginManagerTest extends HudsonTestCase {
// No extensions exist. // No extensions exist.
// extensions in depender is not loaded. // extensions in depender is not loaded.
assertTrue(jenkins.getExtensionList("org.jenkinsci.plugins.dependencytest.dependee.DependeeExtensionPoint").isEmpty()); assertTrue(r.jenkins.getExtensionList("org.jenkinsci.plugins.dependencytest.dependee.DependeeExtensionPoint").isEmpty());
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册