| author | akozak | 2011-11-21 05:50:46 (EST) |
|---|---|---|
| committer | Winston Prakash | 2011-12-01 20:46:48 (EST) |
| commit | c969d5370cc5390c5373bb148b4832649db5fd69 (patch) (side-by-side diff) | |
| tree | 90cc205ce0b449b47aaa8d7491461c7a612bdccd | |
| parent | 22a84e6119d57feaa479e8d8d7058e297156d0cf (diff) | |
| download | org.eclipse.hudson.core-c969d5370cc5390c5373bb148b4832649db5fd69.zip org.eclipse.hudson.core-c969d5370cc5390c5373bb148b4832649db5fd69.tar.gz org.eclipse.hudson.core-c969d5370cc5390c5373bb148b4832649db5fd69.tar.bz2 | |
Improved getItemByName, added tests.
Signed-off-by: Winston Prakash <winston.prakash@gmail.com>
5 files changed, 71 insertions, 31 deletions
diff --git a/hudson-core/src/main/java/hudson/Functions.java b/hudson-core/src/main/java/hudson/Functions.java index 38dad40..e2adbe1 100644 --- a/hudson-core/src/main/java/hudson/Functions.java +++ b/hudson-core/src/main/java/hudson/Functions.java @@ -16,6 +16,8 @@ package hudson; +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; import hudson.console.ConsoleAnnotationDescriptor; import hudson.console.ConsoleAnnotatorFactory; import hudson.model.AbstractProject; @@ -64,6 +66,7 @@ import hudson.security.captcha.CaptchaSupport; import hudson.util.Secret; import hudson.views.MyViewsTabBar; import hudson.views.ViewsTabBar; +import org.apache.commons.lang3.StringUtils; import org.springframework.security.providers.anonymous.AnonymousAuthenticationToken; import org.apache.commons.jelly.JellyContext; import org.apache.commons.jelly.JellyTagException; @@ -1348,4 +1351,23 @@ public class Functions { } return obj; } + + /** + * Returns item by name from the list. + * + * @param items for filtering. + * @param name the name of the item. + * @return template. + */ + public static <T extends Item> T getItemByName(List<T> items, final String name) { + if (StringUtils.isBlank(name)) { + return null; + } + Iterable<T> templates = Iterables.filter(items, new Predicate<T>() { + public boolean apply(T item) { + return name.equalsIgnoreCase(item.getName()); + } + }); + return templates.iterator().hasNext() ? templates.iterator().next() : null; + } } diff --git a/hudson-core/src/main/java/hudson/model/AbstractProject.java b/hudson-core/src/main/java/hudson/model/AbstractProject.java index 768c766..3cb29ff 100644 --- a/hudson-core/src/main/java/hudson/model/AbstractProject.java +++ b/hudson-core/src/main/java/hudson/model/AbstractProject.java @@ -21,6 +21,7 @@ import hudson.AbortException; import hudson.CopyOnWrite; import hudson.FeedAdapter; import hudson.FilePath; +import hudson.Functions; import hudson.Launcher; import hudson.Util; import hudson.cli.declarative.CLIMethod; @@ -225,7 +226,7 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A /** * Selected template for this project. */ - private transient AbstractProject template; + private transient P template; private boolean concurrentBuild; @@ -237,7 +238,7 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A protected AbstractProject(ItemGroup parent, String name) { super(parent, name); - if(!Hudson.getInstance().getNodes().isEmpty()) { + if (Hudson.getInstance() != null && !Hudson.getInstance().getNodes().isEmpty()) { // if a new job is configured with Hudson that already has slave nodes // make it roamable by default canRoam = true; @@ -261,6 +262,9 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOException { super.onLoad(parent, name); + //TODO fix it + template = (P) Functions.getItemByName(Hudson.getInstance().getAllItems(this.getClass()), templateName); + this.builds = new RunMap<R>(); this.builds.load(this,new Constructor<R>() { public R create(File dir) throws IOException { @@ -1967,9 +1971,11 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A * * @param templateName template name. */ + @SuppressWarnings({"unchecked"}) public void setTemplateName(String templateName) { this.templateName = templateName; - this.template = Hudson.getInstance().getTemplate(this.getClass(), templateName); + //TODO fix it + this.template = (P)Functions.getItemByName(Hudson.getInstance().getAllItems(this.getClass()), templateName); } /** @@ -1979,10 +1985,7 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A */ @SuppressWarnings({"unchecked"}) public P getTemplate() { - if (null == template) { - template = Hudson.getInstance().getTemplate(this.getClass(),templateName); - } - return (P)template; + return template; } /** @@ -1992,4 +1995,4 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A protected boolean hasParentTemplate() { return null != getTemplate(); } -}
\ No newline at end of file +} diff --git a/hudson-core/src/main/java/hudson/model/FreeStyleProject.java b/hudson-core/src/main/java/hudson/model/FreeStyleProject.java index 956b1fa..efcf06b 100644 --- a/hudson-core/src/main/java/hudson/model/FreeStyleProject.java +++ b/hudson-core/src/main/java/hudson/model/FreeStyleProject.java @@ -64,6 +64,7 @@ public class FreeStyleProject extends Project<FreeStyleProject,FreeStyleBuild> i return hasParentTemplate()? getTemplate().getCustomWorkspace() : null; } + /** * User-specified workspace directory, or null if it's up to Hudson. * @@ -80,6 +81,7 @@ public class FreeStyleProject extends Project<FreeStyleProject,FreeStyleBuild> i * If this path is relative, it's resolved against {@link Node#getRootPath()} on the node where this workspace * is prepared. * + * @param customWorkspace customWorkspace. * @since 1.320 */ public void setCustomWorkspace(String customWorkspace) throws IOException { diff --git a/hudson-core/src/main/java/hudson/model/Hudson.java b/hudson-core/src/main/java/hudson/model/Hudson.java index 86298b9..6f24bd3 100644 --- a/hudson-core/src/main/java/hudson/model/Hudson.java +++ b/hudson-core/src/main/java/hudson/model/Hudson.java @@ -1626,25 +1626,6 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl } /** - * Returns template by project type and template name. - * - * @param type type of the project. - * @param templateName name of the template - * @return template. - */ - public <T extends AbstractProject> T getTemplate(Class<T> type, final String templateName) { - if (StringUtils.isBlank(templateName)) { - return null; - } - Iterable<T> templates = Iterables.filter(getAllItems(type), new Predicate<T>() { - public boolean apply(T project) { - return templateName.equalsIgnoreCase(project.getName()); - } - }); - return templates.iterator().hasNext() ? templates.iterator().next() : null; - } - - /** * Updates the slave list. * * @deprecated diff --git a/hudson-core/src/test/java/hudson/FunctionsTest.java b/hudson-core/src/test/java/hudson/FunctionsTest.java index de080c4..80ec0a9 100644 --- a/hudson-core/src/test/java/hudson/FunctionsTest.java +++ b/hudson-core/src/test/java/hudson/FunctionsTest.java @@ -16,16 +16,21 @@ package hudson; import hudson.model.FreeStyleProject; +import hudson.model.Hudson; import hudson.model.Job; import hudson.model.User; +import java.util.ArrayList; +import java.util.List; +import junit.framework.Assert; import org.junit.Test; import org.junit.runner.RunWith; +import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.*; import static org.easymock.EasyMock.expect; +import static org.junit.Assert.assertNotNull; import static org.powermock.api.easymock.PowerMock.createMock; import static org.powermock.api.easymock.PowerMock.mockStatic; import static org.powermock.api.easymock.PowerMock.replay; @@ -39,11 +44,12 @@ import static org.powermock.api.easymock.PowerMock.verify; * @author Anton Kozak */ @RunWith(PowerMockRunner.class) -@PrepareForTest(User.class) public class FunctionsTest { private static final String USER = "admin"; + private static final String TEMPLATE_NAME = "project_template"; @Test + @PrepareForTest(User.class) public void testIsAuthorTrue() throws Exception { mockStatic(User.class); User user = createMock(User.class); @@ -58,6 +64,7 @@ public class FunctionsTest { } @Test + @PrepareForTest(User.class) public void testIsAuthorFalse() throws Exception { mockStatic(User.class); User user = createMock(User.class); @@ -72,6 +79,7 @@ public class FunctionsTest { } @Test + @PrepareForTest(User.class) public void testIsAuthorJobCreatedByNull() throws Exception { mockStatic(User.class); User user = createMock(User.class); @@ -85,6 +93,7 @@ public class FunctionsTest { } @Test + @PrepareForTest(User.class) public void testIsAuthorUserIdNull() throws Exception { mockStatic(User.class); User user = createMock(User.class); @@ -99,6 +108,7 @@ public class FunctionsTest { } @Test + @PrepareForTest(User.class) public void testIsAuthorUserNull() throws Exception { mockStatic(User.class); expect(User.current()).andReturn(null); @@ -108,4 +118,26 @@ public class FunctionsTest { verify(User.class, job); assertFalse(result); } -} + + @Test + public void testGetTemplateWithNullTemplateName(){ + List<FreeStyleProject> items = new ArrayList<FreeStyleProject>(); + FreeStyleProject project = Functions.getItemByName(items, null); + Assert.assertNull(project); + } + + @Test + public void testGetTemplateWithoutTemplates(){ + List<FreeStyleProject> items = new ArrayList<FreeStyleProject>(); + FreeStyleProject project = Functions.getItemByName(items, TEMPLATE_NAME); + assertNull(project); + } + + @Test + public void testGetTemplatePresentTemplate(){ + FreeStyleProject parentProject = new FreeStyleProject(null, TEMPLATE_NAME); + List<FreeStyleProject> items = new ArrayList<FreeStyleProject>(); + items.add(parentProject); + FreeStyleProject project = Functions.getItemByName(items, TEMPLATE_NAME); + assertNotNull(project); + }} |

