summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorakozak2011-11-25 01:19:08 (EST)
committer Winston Prakash2011-12-01 20:47:30 (EST)
commitd77ff9aec9485317d6ebb27d97227ead9b474325 (patch)
tree4ed6ddaf625abf526002ddf1fb7762e8c1431056
parentaa4c170a037a977698c301ee5c8d82134c55a2fe (diff)
downloadorg.eclipse.hudson.core-d77ff9aec9485317d6ebb27d97227ead9b474325.zip
org.eclipse.hudson.core-d77ff9aec9485317d6ebb27d97227ead9b474325.tar.gz
org.eclipse.hudson.core-d77ff9aec9485317d6ebb27d97227ead9b474325.tar.bz2
Added unit tests
Signed-off-by: Winston Prakash <winston.prakash@gmail.com>
-rw-r--r--hudson-core/src/test/java/hudson/util/CascadingParameterDefinitionPropertiesTest.java141
-rw-r--r--hudson-core/src/test/java/hudson/util/CascadingTriggersTest.java165
-rw-r--r--hudson-core/src/test/java/hudson/util/CascadingUtilTest.java60
3 files changed, 362 insertions, 4 deletions
diff --git a/hudson-core/src/test/java/hudson/util/CascadingParameterDefinitionPropertiesTest.java b/hudson-core/src/test/java/hudson/util/CascadingParameterDefinitionPropertiesTest.java
new file mode 100644
index 0000000..84a0f95
--- /dev/null
+++ b/hudson-core/src/test/java/hudson/util/CascadingParameterDefinitionPropertiesTest.java
@@ -0,0 +1,141 @@
+/*******************************************************************************
+ *
+ * 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:
+ *
+ * Anton Kozak
+ *
+ *
+ *******************************************************************************/
+package hudson.util;
+
+import hudson.model.BooleanParameterDefinition;
+import hudson.model.FreeStyleProjectMock;
+import hudson.model.Hudson;
+import hudson.model.Job;
+import hudson.model.ParameterDefinition;
+import hudson.model.ParametersDefinitionProperty;
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.Before;
+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.verifyAll;
+
+
+/**
+ * Test cases for {@link CascadingUtil#setParameterDefinitionProperties(hudson.model.Job, String, CopyOnWriteList)}
+ * method.
+ * Date: 11/8/11
+ */
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({Hudson.class})
+public class CascadingParameterDefinitionPropertiesTest {
+ private FreeStyleProjectMock p1, p2, p3;
+ private String propertyKey;
+ private CopyOnWriteList<ParametersDefinitionProperty> copyOnWriteList;
+
+ @Before
+ public void setUp() {
+ p1 = new FreeStyleProjectMock("p1");
+ p2 = new FreeStyleProjectMock("p2");
+ p3 = new FreeStyleProjectMock("p3");
+ p2.setCascadingProject(p1);
+ CascadingUtil.linkCascadingProjectsToChild(p1, "p2");
+ p3.setCascadingProject(p2);
+ CascadingUtil.linkCascadingProjectsToChild(p2, "p3");
+ Hudson hudson = createMock(Hudson.class);
+ expect(hudson.getItem("p2")).andReturn(p2).anyTimes();
+ expect(hudson.getItem("p3")).andReturn(p3).anyTimes();
+ mockStatic(Hudson.class);
+ expect(Hudson.getInstance()).andReturn(hudson).anyTimes();
+ replay(Hudson.class, hudson);
+ propertyKey = Job.PARAMETERS_DEFINITION_JOB_PROPERTY_PROPERTY_NAME;
+ List<ParameterDefinition> parameterDefinitionList = new ArrayList<ParameterDefinition>();
+ parameterDefinitionList.add(new BooleanParameterDefinition("name", true, "d1"));
+ ParametersDefinitionProperty property = new ParametersDefinitionProperty(parameterDefinitionList);
+ copyOnWriteList = new CopyOnWriteList<ParametersDefinitionProperty>();
+ copyOnWriteList.add(property);
+ }
+
+ /**
+ * Tests main functionality of setParameterDefinitionProperties() method.
+ *
+ * @throws Exception if any.
+ */
+ @Test
+ public void testSetParameterDefinitionProperties() throws Exception {
+ assertEquals(new CopyOnWriteList(),
+ CascadingUtil.getCopyOnWriteListProjectProperty(p1, propertyKey).getValue());
+ CascadingUtil.setParameterDefinitionProperties(p1, propertyKey, copyOnWriteList);
+ assertEquals(copyOnWriteList, CascadingUtil.getCopyOnWriteListProjectProperty(p1, propertyKey).getValue());
+ verifyAll();
+ }
+
+ /**
+ * Tests recursively setting parameters definition property.
+ *
+ * @throws Exception if any.
+ */
+ @Test
+ public void testSetParameterDefinitionPropertiesRecursively() throws Exception {
+ assertEquals(new CopyOnWriteList(),
+ CascadingUtil.getCopyOnWriteListProjectProperty(p2, propertyKey).getValue());
+ CascadingUtil.setParameterDefinitionProperties(p1, propertyKey, copyOnWriteList);
+ assertEquals(copyOnWriteList, CascadingUtil.getCopyOnWriteListProjectProperty(p2, propertyKey).getValue());
+ verifyAll();
+ }
+
+ /**
+ * Tests if parameter definition property correctly marked as overridden.
+ *
+ * @throws Exception if any.
+ */
+ @Test
+ public void testSetParameterDefinitionPropertiesOverridden() throws Exception {
+ CascadingUtil.setParameterDefinitionProperties(p1, propertyKey, copyOnWriteList);
+ CascadingUtil.setParameterDefinitionProperties(p2, propertyKey,
+ new CopyOnWriteList<ParametersDefinitionProperty>());
+ assertTrue(CascadingUtil.getCopyOnWriteListProjectProperty(p2, propertyKey).isOverridden());
+ assertFalse(CascadingUtil.getCopyOnWriteListProjectProperty(p3, propertyKey).isOverridden());
+ }
+
+ /**
+ * Tests correctly behaviour when job hasn't children..
+ *
+ * @throws Exception if any.
+ */
+ @Test
+ public void testSetParameterDefinitionPropertiesEmptyChildren() throws Exception {
+ CascadingUtil.setParameterDefinitionProperties(p3, propertyKey, copyOnWriteList);
+ assertEquals(copyOnWriteList, CascadingUtil.getCopyOnWriteListProjectProperty(p3, propertyKey).getValue());
+ }
+
+ /**
+ * Tests if trigger property marked as non overridden when its value match the cascading value.
+ *
+ * @throws Exception if any.
+ */
+ @Test
+ public void testSetParameterDefinitionPropertiesOverriddenFalse() throws Exception {
+ CascadingUtil.getCopyOnWriteListProjectProperty(p2, propertyKey).setValue(copyOnWriteList);
+ CascadingUtil.setParameterDefinitionProperties(p1, propertyKey, copyOnWriteList);
+ assertFalse(CascadingUtil.getCopyOnWriteListProjectProperty(p2, propertyKey).isOverridden());
+ }
+}
diff --git a/hudson-core/src/test/java/hudson/util/CascadingTriggersTest.java b/hudson-core/src/test/java/hudson/util/CascadingTriggersTest.java
new file mode 100644
index 0000000..4668bbf
--- /dev/null
+++ b/hudson-core/src/test/java/hudson/util/CascadingTriggersTest.java
@@ -0,0 +1,165 @@
+/*******************************************************************************
+ *
+ * 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:
+ *
+ * Anton Kozak
+ *
+ *
+ *******************************************************************************/
+package hudson.util;
+
+import antlr.ANTLRException;
+import hudson.model.FreeStyleProjectMock;
+import hudson.model.Hudson;
+import hudson.triggers.TimerTrigger;
+import hudson.triggers.TriggerDescriptor;
+import net.sf.json.JSONObject;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.kohsuke.stapler.StaplerRequest;
+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.assertNull;
+import static junit.framework.Assert.assertNotNull;
+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.verifyAll;
+
+/**
+ * Test cases for {@link CascadingUtil#setChildrenTrigger(hudson.model.Job, hudson.triggers.TriggerDescriptor, String,
+ * org.kohsuke.stapler.StaplerRequest, net.sf.json.JSONObject)} method.
+ *
+ * Date: 11/8/11
+ */
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({Hudson.class, StaplerRequest.class})
+public class CascadingTriggersTest {
+ private FreeStyleProjectMock p1, p2, p3;
+ private String propertyKey;
+ private TriggerDescriptor descriptor;
+ private JSONObject json;
+ private StaplerRequest req;
+
+ @Before
+ public void setUp() throws ANTLRException {
+ p1 = new FreeStyleProjectMock("p1");
+ p2 = new FreeStyleProjectMock("p2");
+ p3 = new FreeStyleProjectMock("p3");
+ p2.setCascadingProject(p1);
+ CascadingUtil.linkCascadingProjectsToChild(p1, "p2");
+ p3.setCascadingProject(p2);
+ CascadingUtil.linkCascadingProjectsToChild(p2, "p3");
+ descriptor = new TimerTrigger.DescriptorImpl();
+ propertyKey = descriptor.getJsonSafeClassName();
+ req = createMock(StaplerRequest.class);
+ json = new JSONObject();
+ JSONObject refspec = new JSONObject();
+ refspec.put("refspec", "* * * * *");
+ json.put(propertyKey, refspec);
+ expect(req.bindJSON(TimerTrigger.class, refspec)).andReturn(new TimerTrigger("* * * * *")).anyTimes();
+ mockStatic(Hudson.class);
+ Hudson hudson = createMock(Hudson.class);
+ expect(hudson.getDescriptorOrDie(TimerTrigger.class)).andReturn(descriptor).anyTimes();
+ expect(hudson.getItem("p2")).andReturn(p2).anyTimes();
+ expect(hudson.getItem("p3")).andReturn(p3).anyTimes();
+ expect(Hudson.getInstance()).andReturn(hudson).anyTimes();
+ replay(Hudson.class, hudson, req);
+ }
+
+ /**
+ * Tests main functionality of setChildrenTrigger() method.
+ *
+ * @throws Exception if any.
+ */
+ @Test
+ public void testSetChildrenTrigger() throws Exception {
+ assertNull(CascadingUtil.getTriggerProjectProperty(p1, propertyKey).getValue());
+ CascadingUtil.setChildrenTrigger(p1, descriptor, propertyKey, req, json);
+ assertNotNull(CascadingUtil.getTriggerProjectProperty(p1, propertyKey).getValue());
+ verifyAll();
+ }
+
+ /**
+ * Tests recursively setting trigger property,
+ *
+ * @throws Exception if any.
+ */
+ @Test
+ public void testSetChildrenTriggerRecursively() throws Exception {
+ assertNull(CascadingUtil.getTriggerProjectProperty(p2, propertyKey).getValue());
+ CascadingUtil.setChildrenTrigger(p1, descriptor, propertyKey, req, json);
+ assertNotNull(CascadingUtil.getTriggerProjectProperty(p2, propertyKey).getValue());
+ assertEquals(CascadingUtil.getTriggerProjectProperty(p1, propertyKey).getValue(),
+ CascadingUtil.getTriggerProjectProperty(p2, propertyKey).getValue());
+ verifyAll();
+ }
+
+ /**
+ * Tests if trigger property correctly marked as overridden.
+ *
+ * @throws Exception if any.
+ */
+ @Test
+ public void testSetChildrenTriggerOverridden() throws Exception {
+ CascadingUtil.setChildrenTrigger(p1, descriptor, propertyKey, req, json);
+ CascadingUtil.getTriggerProjectProperty(p2, propertyKey).setValue(new TimerTrigger("*/2 * * * *"));
+ CascadingUtil.setChildrenTrigger(p1, descriptor, propertyKey, req, json);
+ assertTrue(CascadingUtil.getTriggerProjectProperty(p2, propertyKey).isOverridden());
+ verifyAll();
+ }
+
+ /**
+ * Tests if trigger property value is null if there is no json data.
+ *
+ * @throws Exception if any.
+ */
+ @Test
+ public void testSetChildrenTriggerEmptyJSON() throws Exception {
+ CascadingUtil.setChildrenTrigger(p1, descriptor, propertyKey, req, new JSONObject());
+ assertNull(CascadingUtil.getTriggerProjectProperty(p1, propertyKey).getValue());
+ verifyAll();
+ }
+
+ /**
+ * Tests correctly behaviour if job hasn't children.
+ *
+ * @throws Exception if any.
+ */
+ @Test
+ public void testSetChildrenTriggerEmptyChildren() throws Exception {
+ CascadingUtil.setChildrenTrigger(p3, descriptor, propertyKey, req, json);
+ assertNotNull(CascadingUtil.getTriggerProjectProperty(p3, propertyKey).getValue());
+ verifyAll();
+ }
+
+ /**
+ * Tests if trigger property marked as non overridden when its value match the cascading value.
+ *
+ * @throws Exception if any
+ */
+ @Test
+ public void testSetChildrenTriggerOverridenFalse() throws Exception {
+ CascadingUtil.setChildrenTrigger(p1, descriptor, propertyKey, req, json);
+ CascadingUtil.getTriggerProjectProperty(p2, propertyKey).setValue(new TimerTrigger("*/2 * * * *"));
+ CascadingUtil.setChildrenTrigger(p1, descriptor, propertyKey, req, json);
+ assertTrue(CascadingUtil.getTriggerProjectProperty(p2, propertyKey).isOverridden());
+ CascadingUtil.getTriggerProjectProperty(p2, propertyKey).setValue(new TimerTrigger("* * * * *"));
+ CascadingUtil.setChildrenTrigger(p1, descriptor, propertyKey, req, json);
+ assertFalse(CascadingUtil.getTriggerProjectProperty(p2, propertyKey).isOverridden());
+ verifyAll();
+ }
+}
diff --git a/hudson-core/src/test/java/hudson/util/CascadingUtilTest.java b/hudson-core/src/test/java/hudson/util/CascadingUtilTest.java
index b881595..fbf8c9d 100644
--- a/hudson-core/src/test/java/hudson/util/CascadingUtilTest.java
+++ b/hudson-core/src/test/java/hudson/util/CascadingUtilTest.java
@@ -14,14 +14,19 @@
*******************************************************************************/
package hudson.util;
+import hudson.model.Descriptor;
import hudson.model.FreeStyleProject;
import hudson.model.FreeStyleProjectMock;
import hudson.model.Hudson;
import hudson.model.Job;
+import hudson.tasks.JavadocArchiver;
+import hudson.tasks.Publisher;
import java.util.ArrayList;
import java.util.List;
+import net.sf.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.kohsuke.stapler.StaplerRequest;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@@ -29,10 +34,9 @@ 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;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.powermock.api.easymock.PowerMock.*;
/**
* Test cases for cascading utils.
@@ -160,6 +164,10 @@ public class CascadingUtilTest {
assertFalse(project1.getCascadingChildrenNames().contains(oldName));
}
+ @Test
+ public void testRenameCascadingChildLinksNullProject() {
+ CascadingUtil.renameCascadingChildLinks(null, "name", "newName");
+ }
@Test
@PrepareForTest(Hudson.class)
@@ -193,6 +201,16 @@ public class CascadingUtilTest {
}
@Test
+ public void testRenameCascadingParentLinksNullName1() {
+ CascadingUtil.renameCascadingParentLinks(null, "name");
+ }
+
+ @Test
+ public void testRenameCascadingParentLinksNullName2() {
+ CascadingUtil.renameCascadingParentLinks("name", null);
+ }
+
+ @Test
@PrepareForTest(Hudson.class)
public void testGetAllItems() {
FreeStyleProject project1 = new FreeStyleProjectMock("p1");
@@ -282,6 +300,8 @@ public class CascadingUtilTest {
verifyCyclicCascadingLink(false, project3, project2);
verifyCyclicCascadingLink(false, project1, project2);
verifyCyclicCascadingLink(false, project2, project3);
+ verifyCyclicCascadingLink(false, null, project1);
+ verifyCyclicCascadingLink(false, null, project4);
verify(Hudson.class, hudson);
}
@@ -290,4 +310,36 @@ public class CascadingUtilTest {
assertEquals(expectedResult,
CascadingUtil.hasCyclicCascadingLink(candidateJob, currentJob.getCascadingChildrenNames()));
}
+
+ @Test
+ @PrepareForTest({Hudson.class, StaplerRequest.class})
+ public void testBuildExternalProperties() throws Exception {
+ Job job = new FreeStyleProjectMock("job");
+ StaplerRequest req = createMock(StaplerRequest.class);
+ String javadocArchiverKey = "hudson-tasks-JavadocArchiver";
+ JSONObject archiver = new JSONObject();
+ archiver.put("javadoc_dir", "dir");
+ archiver.put("keep_all", true);
+ JSONObject json = new JSONObject();
+ json.put(javadocArchiverKey, archiver);
+ Hudson hudson = createMock(Hudson.class);
+ Descriptor<Publisher> javadocDescriptor = new JavadocArchiver.DescriptorImpl();
+ expect(hudson.getDescriptorOrDie(JavadocArchiver.class)).andReturn(javadocDescriptor);
+ JavadocArchiver javadocArchiver = new JavadocArchiver("dir", true);
+ expect(req.bindJSON(JavadocArchiver.class, archiver)).andReturn(javadocArchiver).anyTimes();
+
+ List<Descriptor<Publisher>> descriptors = new ArrayList<Descriptor<Publisher>>();
+ descriptors.add(javadocDescriptor);
+
+ mockStatic(Hudson.class);
+ expect(Hudson.getInstance()).andReturn(hudson).anyTimes();
+ replay(Hudson.class, hudson, req);
+
+ assertNull(CascadingUtil.getExternalProjectProperty(job, javadocArchiverKey).getValue());
+ CascadingUtil.buildExternalProperties(req, archiver, descriptors, job);
+ assertNull(CascadingUtil.getExternalProjectProperty(job, javadocArchiverKey).getValue());
+ CascadingUtil.buildExternalProperties(req, json, descriptors, job);
+ assertNotNull(CascadingUtil.getExternalProjectProperty(job, javadocArchiverKey).getValue());
+ verifyAll();
+ }
}