From af21daf8d9667fe44341fbb2581fb93c9ecef730 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Tue, 24 Feb 2009 18:32:42 +0000 Subject: [PATCH] added Hudson.getDescriptorByType() to simplify the retrieval of the descriptor instance. Fixed test cases. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@15656 71c3de6d-444a-0410-be80-ed276b4c234a --- .../java/hudson/maven/MavenModuleSet.java | 2 +- core/src/main/java/hudson/model/Hudson.java | 10 +++++++ .../java/hudson/model/UsageStatistics.java | 2 +- .../org/jvnet/hudson/test/HudsonTestCase.java | 4 +-- .../test/java/hudson/ExtensionListTest.java | 27 +++++++++++++++++++ .../test/java/hudson/model/HudsonTest.java | 3 ++- test/src/test/java/hudson/tasks/AntTest.java | 2 +- .../tasks/EnvVarsInConfigTasksTest.java | 8 +++--- .../src/test/java/hudson/tasks/MavenTest.java | 8 +++--- 9 files changed, 52 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/hudson/maven/MavenModuleSet.java b/core/src/main/java/hudson/maven/MavenModuleSet.java index 4ac493fc70..0889222e8f 100644 --- a/core/src/main/java/hudson/maven/MavenModuleSet.java +++ b/core/src/main/java/hudson/maven/MavenModuleSet.java @@ -650,7 +650,7 @@ public final class MavenModuleSet extends AbstractMavenProject, Stapl return null; } + /** + * Gets the {@link Descriptor} instance in the current Hudson by its type. + */ + public T getDescriptorByType(Class type) { + for( Descriptor d : getExtensionList(Descriptor.class) ) + if(d.getClass()==type) + return type.cast(d); + return null; + } + /** * Gets the {@link SecurityRealm} descriptors by name. Primarily used for making them web-visible. */ diff --git a/core/src/main/java/hudson/model/UsageStatistics.java b/core/src/main/java/hudson/model/UsageStatistics.java index eea1d369e7..0463ef4211 100644 --- a/core/src/main/java/hudson/model/UsageStatistics.java +++ b/core/src/main/java/hudson/model/UsageStatistics.java @@ -142,7 +142,7 @@ public class UsageStatistics extends PageDecorator { n.put("jvm-version", System.getProperty("java.version")); } n.put("executors",c.getNumExecutors()); - DescriptorImpl descriptor = (DescriptorImpl) h.getDescriptor(ArchitectureMonitor.class); + DescriptorImpl descriptor = h.getDescriptorByType(DescriptorImpl.class); n.put("os", descriptor.get(c)); nodes.add(n); } diff --git a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java index 80b7e9d5f5..c8fc4651f5 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java @@ -299,7 +299,7 @@ public abstract class HudsonTestCase extends TestCase { String home = System.getProperty("maven.home"); if(home!=null) { MavenInstallation mavenInstallation = new MavenInstallation("default",home); - ((Maven.DescriptorImpl)hudson.getDescriptor(Maven.class)).setInstallations(mavenInstallation); + hudson.getDescriptorByType(Maven.DescriptorImpl.class).setInstallations(mavenInstallation); return mavenInstallation; } @@ -319,7 +319,7 @@ public abstract class HudsonTestCase extends TestCase { MavenInstallation mavenInstallation = new MavenInstallation("default", new File(mvnHome,"maven-2.0.7").getAbsolutePath()); - ((Maven.DescriptorImpl)hudson.getDescriptor(Maven.class)).setInstallations(mavenInstallation); + hudson.getDescriptorByType(Maven.DescriptorImpl.class).setInstallations(mavenInstallation); return mavenInstallation; } diff --git a/test/src/test/java/hudson/ExtensionListTest.java b/test/src/test/java/hudson/ExtensionListTest.java index ab224f73b5..c99eab83ed 100644 --- a/test/src/test/java/hudson/ExtensionListTest.java +++ b/test/src/test/java/hudson/ExtensionListTest.java @@ -6,6 +6,11 @@ import hudson.model.Describable; import hudson.model.Hudson; import hudson.util.DescriptorList; +import java.util.List; +import java.util.Set; +import java.util.HashSet; +import java.util.Collection; + /** * @author Kohsuke Kawaguchi */ @@ -35,6 +40,28 @@ public class ExtensionListTest extends HudsonTestCase { assertNotNull(list.get(Cat.class)); } + public void testExtensionListView() throws Exception { + // this is how legacy list like UserNameResolver.LIST gets created. + List LIST = ExtensionListView.createList(Animal.class); + + // we should see auto-registered instances here + assertEquals(2,LIST.size()); + assertTrue(hasInstanceOf(LIST,Dog.class)); + assertTrue(hasInstanceOf(LIST,Cat.class)); + + Animal lion = new Animal() {}; + LIST.add(lion); + assertEquals(3,LIST.size()); + assertTrue(LIST.contains(lion)); + } + + private boolean hasInstanceOf(Collection c, Class type) { + for (Object o : c) + if(o.getClass()==type) + return true; + return false; + } + // // // Descriptor extension point diff --git a/test/src/test/java/hudson/model/HudsonTest.java b/test/src/test/java/hudson/model/HudsonTest.java index 179cad6ca7..26ce1a6b91 100644 --- a/test/src/test/java/hudson/model/HudsonTest.java +++ b/test/src/test/java/hudson/model/HudsonTest.java @@ -37,6 +37,7 @@ import hudson.security.AuthorizationStrategy; import hudson.security.SecurityRealm; import hudson.tasks.Ant; import hudson.tasks.Ant.AntInstallation; +import hudson.tasks.Ant.DescriptorImpl; import org.jvnet.hudson.test.Email; import org.jvnet.hudson.test.HudsonTestCase; import org.jvnet.hudson.test.recipes.LocalData; @@ -73,7 +74,7 @@ public class HudsonTest extends HudsonTestCase { assertJDK(jdks.get(0),"jdk1","/tmp"); assertJDK(jdks.get(1),"jdk2","/tmp"); - AntInstallation[] ants = Ant.DESCRIPTOR.getInstallations(); + AntInstallation[] ants = hudson.getDescriptorByType(Ant.DescriptorImpl.class).getInstallations(); assertEquals(2,ants.length); assertAnt(ants[0],"ant1","/tmp"); assertAnt(ants[1],"ant2","/tmp"); diff --git a/test/src/test/java/hudson/tasks/AntTest.java b/test/src/test/java/hudson/tasks/AntTest.java index 99654213d1..7ee3470068 100644 --- a/test/src/test/java/hudson/tasks/AntTest.java +++ b/test/src/test/java/hudson/tasks/AntTest.java @@ -45,7 +45,7 @@ public class AntTest extends HudsonTestCase { HtmlForm form = page.getFormByName("config"); submit(form); - Ant a = (Ant)p.getBuildersList().get(Ant.DESCRIPTOR); + Ant a = p.getBuildersList().get(Ant.class); assertNotNull(a); assertEquals("a",a.getTargets()); assertNull(a.getAnt()); diff --git a/test/src/test/java/hudson/tasks/EnvVarsInConfigTasksTest.java b/test/src/test/java/hudson/tasks/EnvVarsInConfigTasksTest.java index b3b62ef8d1..b284449a8c 100644 --- a/test/src/test/java/hudson/tasks/EnvVarsInConfigTasksTest.java +++ b/test/src/test/java/hudson/tasks/EnvVarsInConfigTasksTest.java @@ -32,10 +32,10 @@ public class EnvVarsInConfigTasksTest extends HudsonTestCase { // Maven with a variable in its path configureDefaultMaven(); - MavenInstallation defaultMaven = Maven.DESCRIPTOR.getInstallations()[0]; + MavenInstallation defaultMaven = hudson.getDescriptorByType(Maven.DescriptorImpl.class).getInstallations()[0]; MavenInstallation varMaven = new MavenInstallation("varMaven", withVariable(defaultMaven.getMavenHome())); - Maven.DESCRIPTOR.setInstallations(varMaven); + hudson.getDescriptorByType(Maven.DescriptorImpl.class).setInstallations(varMaven); // Ant with a variable in its path // TODO: create HudsonTestCase.configureDefaultAnt() @@ -43,7 +43,7 @@ public class EnvVarsInConfigTasksTest extends HudsonTestCase { if (guessedAntHome != null) { AntInstallation antInstallation = new AntInstallation("varAnt", withVariable(guessedAntHome)); - Ant.DESCRIPTOR.setInstallations(antInstallation); + hudson.getDescriptorByType(Ant.DescriptorImpl.class).setInstallations(antInstallation); } // createSlaves @@ -96,7 +96,7 @@ public class EnvVarsInConfigTasksTest extends HudsonTestCase { } public void testFreeStyleAntOnSlave() throws Exception { - if (Ant.DESCRIPTOR.getInstallations().length == 0) { + if (hudson.getDescriptorByType(Ant.DescriptorImpl.class).getInstallations().length == 0) { System.out.println("Cannot do testFreeStyleAntOnSlave without ANT_HOME"); return; } diff --git a/test/src/test/java/hudson/tasks/MavenTest.java b/test/src/test/java/hudson/tasks/MavenTest.java index 400c0a3e5a..f45717f25e 100644 --- a/test/src/test/java/hudson/tasks/MavenTest.java +++ b/test/src/test/java/hudson/tasks/MavenTest.java @@ -51,7 +51,7 @@ public class MavenTest extends HudsonTestCase { * Tests the round-tripping of the configuration. */ public void testConfigRoundtrip() throws Exception { - Maven.DESCRIPTOR.setInstallations(); // reset + hudson.getDescriptorByType(Maven.DescriptorImpl.class).setInstallations(); // reset FreeStyleProject p = createFreeStyleProject(); p.getBuildersList().add(new Maven("a", null, "b.pom", "c=d", "-e")); @@ -62,7 +62,7 @@ public class MavenTest extends HudsonTestCase { HtmlForm form = page.getFormByName("config"); submit(form); - Maven m = (Maven) p.getBuildersList().get(Maven.DESCRIPTOR); + Maven m = p.getBuildersList().get(Maven.class); assertNotNull(m); assertEquals("a", m.targets); assertNull("found " + m.mavenName, m.mavenName); @@ -78,7 +78,7 @@ public class MavenTest extends HudsonTestCase { String mavenVar = mavenHome.substring(0, 3); MavenInstallation varMaven = new MavenInstallation("varMaven", mavenHomeVar); - Maven.DESCRIPTOR.setInstallations(maven, varMaven); + hudson.getDescriptorByType(Maven.DescriptorImpl.class).setInstallations(maven, varMaven); JDK jdk = hudson.getJDK("default"); String javaHome = jdk.getJavaHome(); @@ -107,7 +107,7 @@ public class MavenTest extends HudsonTestCase { String mavenVar = mavenHome.substring(0, 3); MavenInstallation varMaven = new MavenInstallation("varMaven", mavenHomeVar); - Maven.DESCRIPTOR.setInstallations(maven, varMaven); + hudson.getDescriptorByType(Maven.DescriptorImpl.class).setInstallations(maven, varMaven); JDK jdk = hudson.getJDK("default"); String javaHome = jdk.getJavaHome();