summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorakozak2011-11-24 05:08:11 (EST)
committer Winston Prakash2011-12-01 20:47:23 (EST)
commit7b452df2fce4d66833c74f19e7f8e77ad040eaea (patch)
treef1196e9df0b6d6d540e3a39e57f05430d95f987a
parent42178ae2d1687d0ca6074711a64464757b005141 (diff)
downloadorg.eclipse.hudson.core-7b452df2fce4d66833c74f19e7f8e77ad040eaea.zip
org.eclipse.hudson.core-7b452df2fce4d66833c74f19e7f8e77ad040eaea.tar.gz
org.eclipse.hudson.core-7b452df2fce4d66833c74f19e7f8e77ad040eaea.tar.bz2
Implement unit test for cascading utils. Move cascading test cases from Functions. Improve cascading projects filtering. Exclude same project from cascading list on server-side
Signed-off-by: Winston Prakash <winston.prakash@gmail.com>
-rw-r--r--hudson-core/src/main/java/hudson/util/CascadingUtil.java3
-rw-r--r--hudson-core/src/main/resources/hudson/model/Job/configure.jelly6
-rw-r--r--hudson-core/src/main/resources/lib/hudson/project/config-scm.jelly1
-rw-r--r--hudson-core/src/test/java/hudson/FunctionsTest.java108
-rw-r--r--hudson-core/src/test/java/hudson/util/CascadingUtilTest.java253
5 files changed, 258 insertions, 113 deletions
diff --git a/hudson-core/src/main/java/hudson/util/CascadingUtil.java b/hudson-core/src/main/java/hudson/util/CascadingUtil.java
index efaa764..1c51be8 100644
--- a/hudson-core/src/main/java/hudson/util/CascadingUtil.java
+++ b/hudson-core/src/main/java/hudson/util/CascadingUtil.java
@@ -327,7 +327,8 @@ public class CascadingUtil {
List<Job> result = new ArrayList<Job>(allItems.size());
for (T item : allItems) {
Job job = (Job) item;
- if (!hasCyclicCascadingLink(job, currentJob.getCascadingChildrenNames())) {
+ if (!StringUtils.equals(currentJob.getName(), job.getName())
+ && !hasCyclicCascadingLink(job, currentJob.getCascadingChildrenNames())) {
result.add(job);
}
}
diff --git a/hudson-core/src/main/resources/hudson/model/Job/configure.jelly b/hudson-core/src/main/resources/hudson/model/Job/configure.jelly
index 8127b0e..8cc1411 100644
--- a/hudson-core/src/main/resources/hudson/model/Job/configure.jelly
+++ b/hudson-core/src/main/resources/hudson/model/Job/configure.jelly
@@ -34,14 +34,12 @@
</f:entry>
</j:if>
<j:set var="cascadingCandidates" value="${h.getAllItems(it.class, it)}"/>
- <j:if test="${cascadingCandidates.size() gt 1}">
+ <j:if test="${cascadingCandidates.size() gt 0}">
<f:entry title="${%cascadingProjectName}">
<select class="setting-input" name="cascadingProjectName">
<f:option/>
<j:forEach var="job" items="${cascadingCandidates}">
- <j:if test="${job.name != it.name}">
- <f:option selected="${job.name==it.cascadingProjectName}" value="${job.name}">${job.name}</f:option>
- </j:if>
+ <f:option selected="${job.name==it.cascadingProjectName}" value="${job.name}">${job.name}</f:option>
</j:forEach>
</select>
</f:entry>
diff --git a/hudson-core/src/main/resources/lib/hudson/project/config-scm.jelly b/hudson-core/src/main/resources/lib/hudson/project/config-scm.jelly
index 55ab426..955b833 100644
--- a/hudson-core/src/main/resources/lib/hudson/project/config-scm.jelly
+++ b/hudson-core/src/main/resources/lib/hudson/project/config-scm.jelly
@@ -25,6 +25,7 @@
<j:set var="scms" value="${h.getSCMDescriptors(it)}"/>
<j:forEach var="idx" begin="0" end="${size(scms)-1}">
<j:set var="descriptor" value="${scms[idx]}"/>
+ <j:set var="scmd" value="${descriptor}"/> <!--Left for compatibility with listScmBrowsers.jelly tag -->
<f:radioBlock name="scm" value="${idx}" title="${descriptor.displayName}" checked="${it.scm.descriptor==descriptor}"
isPropertyOverridden="${scmProperty.isOverridden()}">
<j:set var="instance" value="${it.scm.descriptor==descriptor ? it.scm : null}"/>
diff --git a/hudson-core/src/test/java/hudson/FunctionsTest.java b/hudson-core/src/test/java/hudson/FunctionsTest.java
index 65197f2..497b6b9 100644
--- a/hudson-core/src/test/java/hudson/FunctionsTest.java
+++ b/hudson-core/src/test/java/hudson/FunctionsTest.java
@@ -141,113 +141,5 @@ public class FunctionsTest {
FreeStyleProject project = Functions.getItemByName(items, TEMPLATE_NAME);
assertNotNull(project);
}
- @Test
- public void testUnlinkProjectFromCascadingParents() {
- //Prepare data
- FreeStyleProject project1 = new FreeStyleProjectMock("project1");
- FreeStyleProjectMock child1 = new FreeStyleProjectMock("child1");
- child1.setCascadingProject(project1);
- String cascadingName = "newCascadingProject";
- Functions.linkCascadingProjectsToChild(child1, cascadingName);
-
- //Can't unlink from null project
- assertFalse(Functions.unlinkProjectFromCascadingParents(null, cascadingName));
- //Can't unlink null cascading name
- assertFalse(Functions.unlinkProjectFromCascadingParents(project1, null));
-
- //Verify whether cascadingName is present in parent and child
- assertTrue(project1.getCascadingChildrenNames().contains(cascadingName));
- assertTrue(child1.getCascadingChildrenNames().contains(cascadingName));
- boolean result = Functions.unlinkProjectFromCascadingParents(child1, cascadingName);
- assertTrue(result);
- //Name should disappear from hierarchy.
- assertFalse(project1.getCascadingChildrenNames().contains(cascadingName));
- assertFalse(child1.getCascadingChildrenNames().contains(cascadingName));
-
- Functions.linkCascadingProjectsToChild(project1, cascadingName);
- assertTrue(project1.getCascadingChildrenNames().contains(cascadingName));
- result = Functions.unlinkProjectFromCascadingParents(child1, cascadingName);
- assertTrue(result);
- assertFalse(project1.getCascadingChildrenNames().contains(cascadingName));
-
- }
-
- @Test
- public void testLinkCascadingProjectsToChild() {
- FreeStyleProject project1 = new FreeStyleProjectMock("project1");
- FreeStyleProjectMock child1 = new FreeStyleProjectMock("child1");
- child1.setCascadingProject(project1);
- String cascadingName = "newCascadingProject";
- Functions.linkCascadingProjectsToChild(null, cascadingName);
- assertFalse(project1.getCascadingChildrenNames().contains(cascadingName));
- assertFalse(child1.getCascadingChildrenNames().contains(cascadingName));
-
- Functions.linkCascadingProjectsToChild(project1, cascadingName);
- assertTrue(project1.getCascadingChildrenNames().contains(cascadingName));
-
- project1 = new FreeStyleProjectMock("project1");
- child1 = new FreeStyleProjectMock("child1");
- child1.setCascadingProject(project1);
- Functions.linkCascadingProjectsToChild(child1, cascadingName);
- //Name should be included to all cascading parents up-hierarchy.
- assertTrue(project1.getCascadingChildrenNames().contains(cascadingName));
- assertTrue(child1.getCascadingChildrenNames().contains(cascadingName));
- }
-
-
- @Test
- public void testRenameCascadingChildLinks() {
- String oldName = "oldCascadingProject";
- String newName = "newCascadingProject";
- FreeStyleProject project1 = new FreeStyleProjectMock("project1");
- FreeStyleProjectMock project2 = new FreeStyleProjectMock("project2");
- FreeStyleProjectMock project3 = new FreeStyleProjectMock(oldName);
- project2.setCascadingProject(project1);
- Functions.linkCascadingProjectsToChild(project1, "project2");
- project3.setCascadingProject(project2);
- Functions.linkCascadingProjectsToChild(project2, oldName);
- assertTrue(project2.getCascadingChildrenNames().contains(oldName));
- assertTrue(project1.getCascadingChildrenNames().contains(oldName));
-
- Functions.renameCascadingChildLinks(project2, oldName, newName);
-
- assertTrue(project2.getCascadingChildrenNames().contains(newName));
- assertFalse(project2.getCascadingChildrenNames().contains(oldName));
- assertTrue(project1.getCascadingChildrenNames().contains(newName));
- assertFalse(project1.getCascadingChildrenNames().contains(oldName));
- }
-
-
- @Test
- @PrepareForTest(Hudson.class)
- public void testRenameCascadingParentLinks() {
- String oldName = "oldCascadingProject";
- String newName = "newCascadingProject";
- List<Job> jobs = new ArrayList<Job>();
- FreeStyleProject project1 = new FreeStyleProjectMock(oldName);
- FreeStyleProjectMock project2 = new FreeStyleProjectMock("child");
- project2.setCascadingProject(project1);
- jobs.add(project1);
- jobs.add(project2);
- mockStatic(Hudson.class);
- Hudson hudson = createMock(Hudson.class);
- expect(hudson.getAllItems(Job.class)).andReturn(jobs);
- expect(Hudson.getInstance()).andReturn(hudson);
- replay(Hudson.class, hudson);
- Functions.renameCascadingParentLinks(oldName, newName);
- verify(Hudson.class, hudson);
- assertEquals(newName, project2.getCascadingProjectName());
- }
-
- @Test
- public void testRenameCascadingParentLinksNullNames() {
- Functions.renameCascadingParentLinks(null, null);
- }
-
- @Test
- public void testRenameCascadingParentLinksEmptyNames() {
- Functions.renameCascadingParentLinks("", "");
- }
-
}
diff --git a/hudson-core/src/test/java/hudson/util/CascadingUtilTest.java b/hudson-core/src/test/java/hudson/util/CascadingUtilTest.java
new file mode 100644
index 0000000..7f0138c
--- /dev/null
+++ b/hudson-core/src/test/java/hudson/util/CascadingUtilTest.java
@@ -0,0 +1,253 @@
+/*******************************************************************************
+ *
+ * Copyright (c) 2011 Oracle Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *
+ * Nikita Levyankov
+ *
+ *******************************************************************************/
+package hudson.util;
+
+import hudson.model.FreeStyleProject;
+import hudson.model.FreeStyleProjectMock;
+import hudson.model.Hudson;
+import hudson.model.Job;
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertFalse;
+import static junit.framework.Assert.assertTrue;
+import static org.easymock.EasyMock.expect;
+import static org.powermock.api.easymock.PowerMock.createMock;
+import static org.powermock.api.easymock.PowerMock.mockStatic;
+import static org.powermock.api.easymock.PowerMock.replay;
+import static org.powermock.api.easymock.PowerMock.verify;
+
+/**
+ * Test cases for cascading utils.
+ * <p/>
+ * Date: 10/25/11
+ *
+ * @author Nikita Levyankov
+ */
+@RunWith(PowerMockRunner.class)
+public class CascadingUtilTest {
+
+ @Test
+ public void testUnlinkProjectFromCascadingParents() {
+ //Prepare data
+ FreeStyleProject project1 = new FreeStyleProjectMock("project1");
+ FreeStyleProjectMock child1 = new FreeStyleProjectMock("child1");
+ child1.setCascadingProject(project1);
+ String cascadingName = "newCascadingProject";
+ CascadingUtil.linkCascadingProjectsToChild(child1, cascadingName);
+
+ //Can't unlink from null project
+ assertFalse(CascadingUtil.unlinkProjectFromCascadingParents(null, cascadingName));
+ //Can't unlink null cascading name
+ assertFalse(CascadingUtil.unlinkProjectFromCascadingParents(project1, null));
+
+ //Verify whether cascadingName is present in parent and child
+ assertTrue(project1.getCascadingChildrenNames().contains(cascadingName));
+ assertTrue(child1.getCascadingChildrenNames().contains(cascadingName));
+ boolean result = CascadingUtil.unlinkProjectFromCascadingParents(child1, cascadingName);
+ assertTrue(result);
+ //Name should disappear from hierarchy.
+ assertFalse(project1.getCascadingChildrenNames().contains(cascadingName));
+ assertFalse(child1.getCascadingChildrenNames().contains(cascadingName));
+
+ CascadingUtil.linkCascadingProjectsToChild(project1, cascadingName);
+ assertTrue(project1.getCascadingChildrenNames().contains(cascadingName));
+ result = CascadingUtil.unlinkProjectFromCascadingParents(child1, cascadingName);
+ assertTrue(result);
+ assertFalse(project1.getCascadingChildrenNames().contains(cascadingName));
+
+ }
+
+ @Test
+ public void testLinkCascadingProjectsToChild() {
+ FreeStyleProject project1 = new FreeStyleProjectMock("project1");
+ FreeStyleProjectMock child1 = new FreeStyleProjectMock("child1");
+ child1.setCascadingProject(project1);
+ String cascadingName = "newCascadingProject";
+ CascadingUtil.linkCascadingProjectsToChild(null, cascadingName);
+ assertFalse(project1.getCascadingChildrenNames().contains(cascadingName));
+ assertFalse(child1.getCascadingChildrenNames().contains(cascadingName));
+
+ CascadingUtil.linkCascadingProjectsToChild(project1, cascadingName);
+ assertTrue(project1.getCascadingChildrenNames().contains(cascadingName));
+
+ project1 = new FreeStyleProjectMock("project1");
+ child1 = new FreeStyleProjectMock("child1");
+ child1.setCascadingProject(project1);
+ CascadingUtil.linkCascadingProjectsToChild(child1, cascadingName);
+ //Name should be included to all cascading parents up-hierarchy.
+ assertTrue(project1.getCascadingChildrenNames().contains(cascadingName));
+ assertTrue(child1.getCascadingChildrenNames().contains(cascadingName));
+ }
+
+
+ @Test
+ public void testRenameCascadingChildLinks() {
+ String oldName = "oldCascadingProject";
+ String newName = "newCascadingProject";
+ FreeStyleProject project1 = new FreeStyleProjectMock("project1");
+ FreeStyleProjectMock project2 = new FreeStyleProjectMock("project2");
+ FreeStyleProjectMock project3 = new FreeStyleProjectMock(oldName);
+ project2.setCascadingProject(project1);
+ CascadingUtil.linkCascadingProjectsToChild(project1, "project2");
+ project3.setCascadingProject(project2);
+ CascadingUtil.linkCascadingProjectsToChild(project2, oldName);
+ assertTrue(project2.getCascadingChildrenNames().contains(oldName));
+ assertTrue(project1.getCascadingChildrenNames().contains(oldName));
+
+ CascadingUtil.renameCascadingChildLinks(project2, oldName, newName);
+
+ assertTrue(project2.getCascadingChildrenNames().contains(newName));
+ assertFalse(project2.getCascadingChildrenNames().contains(oldName));
+ assertTrue(project1.getCascadingChildrenNames().contains(newName));
+ assertFalse(project1.getCascadingChildrenNames().contains(oldName));
+ }
+
+
+ @Test
+ @PrepareForTest(Hudson.class)
+ public void testRenameCascadingParentLinks() {
+ String oldName = "oldCascadingProject";
+ String newName = "newCascadingProject";
+ List<Job> jobs = new ArrayList<Job>();
+ FreeStyleProject project1 = new FreeStyleProjectMock(oldName);
+ FreeStyleProjectMock project2 = new FreeStyleProjectMock("child");
+ project2.setCascadingProject(project1);
+ jobs.add(project1);
+ jobs.add(project2);
+ mockStatic(Hudson.class);
+ Hudson hudson = createMock(Hudson.class);
+ expect(hudson.getAllItems(Job.class)).andReturn(jobs);
+ expect(Hudson.getInstance()).andReturn(hudson);
+ replay(Hudson.class, hudson);
+ CascadingUtil.renameCascadingParentLinks(oldName, newName);
+ verify(Hudson.class, hudson);
+ assertEquals(newName, project2.getCascadingProjectName());
+ }
+
+ @Test
+ public void testRenameCascadingParentLinksNullNames() {
+ CascadingUtil.renameCascadingParentLinks(null, null);
+ }
+
+ @Test
+ public void testRenameCascadingParentLinksEmptyNames() {
+ CascadingUtil.renameCascadingParentLinks("", "");
+ }
+
+ @Test
+ @PrepareForTest(Hudson.class)
+ public void testGetAllItems() {
+ FreeStyleProject project1 = new FreeStyleProjectMock("p1");
+ FreeStyleProjectMock project2 = new FreeStyleProjectMock("p2");
+ FreeStyleProjectMock project3 = new FreeStyleProjectMock("p3");
+ FreeStyleProjectMock project4 = new FreeStyleProjectMock("p4");
+ List<Job> jobs = new ArrayList<Job>();
+ jobs.add(project1);
+ jobs.add(project2);
+ jobs.add(project3);
+ jobs.add(project4);
+
+ Hudson hudson = createMock(Hudson.class);
+ mockStatic(Hudson.class);
+ expect(hudson.getAllItems(Job.class)).andReturn(jobs).anyTimes();
+ expect(Hudson.getInstance()).andReturn(hudson).anyTimes();
+ replay(Hudson.class, hudson);
+
+ project2.setCascadingProject(project1);
+ CascadingUtil.linkCascadingProjectsToChild(project1, "p2");
+ project3.setCascadingProject(project1);
+ CascadingUtil.linkCascadingProjectsToChild(project1, "p3");
+ project4.setCascadingProject(project3);
+ CascadingUtil.linkCascadingProjectsToChild(project3, "p4");
+
+ List<Job> jobs1 = CascadingUtil.getCascadingParents(Job.class, project1);
+ List<Job> jobs2 = CascadingUtil.getCascadingParents(Job.class, project2);
+ List<Job> jobs3 = CascadingUtil.getCascadingParents(Job.class, project3);
+ List<Job> jobs4 = CascadingUtil.getCascadingParents(Job.class, project4);
+ verify(Hudson.class, hudson);
+
+ assertEquals(0, jobs1.size());
+ assertFalse(jobs1.contains(project1));
+ assertFalse(jobs1.contains(project2));
+ assertFalse(jobs1.contains(project3));
+ assertFalse(jobs1.contains(project4));
+
+ assertEquals(3, jobs2.size());
+ assertTrue(jobs2.contains(project1));
+ assertFalse(jobs2.contains(project2));
+ assertTrue(jobs2.contains(project3));
+ assertTrue(jobs2.contains(project4));
+
+ assertEquals(2, jobs3.size());
+ assertTrue(jobs3.contains(project1));
+ assertTrue(jobs3.contains(project2));
+ assertFalse(jobs3.contains(project3));
+ assertFalse(jobs3.contains(project4));
+
+ assertEquals(3, jobs4.size());
+ assertTrue(jobs4.contains(project1));
+ assertTrue(jobs4.contains(project2));
+ assertTrue(jobs4.contains(project3));
+ assertFalse(jobs4.contains(project4));
+ }
+
+ @Test
+ @PrepareForTest(Hudson.class)
+ public void testHasCyclicCascadingLink() {
+ FreeStyleProject project1 = new FreeStyleProjectMock("p1");
+ FreeStyleProjectMock project2 = new FreeStyleProjectMock("p2");
+ FreeStyleProjectMock project3 = new FreeStyleProjectMock("p3");
+ FreeStyleProjectMock project4 = new FreeStyleProjectMock("p4");
+ List<Job> jobs = new ArrayList<Job>();
+ jobs.add(project1);
+ jobs.add(project2);
+ jobs.add(project3);
+ jobs.add(project4);
+
+ Hudson hudson = createMock(Hudson.class);
+ mockStatic(Hudson.class);
+ expect(hudson.getAllItems(Job.class)).andReturn(jobs).anyTimes();
+ expect(Hudson.getInstance()).andReturn(hudson).anyTimes();
+ replay(Hudson.class, hudson);
+
+ project2.setCascadingProject(project1);
+ CascadingUtil.linkCascadingProjectsToChild(project1, "p2");
+ project3.setCascadingProject(project1);
+ CascadingUtil.linkCascadingProjectsToChild(project1, "p3");
+ project4.setCascadingProject(project3);
+ CascadingUtil.linkCascadingProjectsToChild(project3, "p4");
+
+ verifyCyclicCascadingLink(true, project2, project1);
+ verifyCyclicCascadingLink(true, project3, project1);
+ verifyCyclicCascadingLink(true, project4, project3);
+ verifyCyclicCascadingLink(true, project4, project1);
+ verifyCyclicCascadingLink(false, project3, project2);
+ verifyCyclicCascadingLink(false, project1, project2);
+ verifyCyclicCascadingLink(false, project2, project3);
+
+ verify(Hudson.class, hudson);
+ }
+
+ private void verifyCyclicCascadingLink(boolean expectedResult, Job candidateJob, Job currentJob) {
+ assertEquals(expectedResult,
+ CascadingUtil.hasCyclicCascadingLink(candidateJob, currentJob.getCascadingChildrenNames()));
+ }
+}