| author | akozak | 2011-11-22 09:33:12 (EST) |
|---|---|---|
| committer | Winston Prakash | 2011-12-01 20:47:00 (EST) |
| commit | 3b899bd0bcaeac26c474ee0e287e66b299e4984e (patch) (side-by-side diff) | |
| tree | 1b2ff40466eafa3ef14d3b99c5a9ba215d45c410 | |
| parent | b102d4bbc6d247d37e9debd76197f50b1050f7f8 (diff) | |
| download | org.eclipse.hudson.core-3b899bd0bcaeac26c474ee0e287e66b299e4984e.zip org.eclipse.hudson.core-3b899bd0bcaeac26c474ee0e287e66b299e4984e.tar.gz org.eclipse.hudson.core-3b899bd0bcaeac26c474ee0e287e66b299e4984e.tar.bz2 | |
Improve ProjectProperty logic. Implement unit test for ProjectProperties. Move FreeStyleProjectMock to separate class
Signed-off-by: Winston Prakash <winston.prakash@gmail.com>
4 files changed, 413 insertions, 20 deletions
diff --git a/hudson-core/src/main/java/hudson/model/BaseProjectProperty.java b/hudson-core/src/main/java/hudson/model/BaseProjectProperty.java index 94da37b..9e8ad62 100644 --- a/hudson-core/src/main/java/hudson/model/BaseProjectProperty.java +++ b/hudson-core/src/main/java/hudson/model/BaseProjectProperty.java @@ -27,6 +27,8 @@ import org.eclipse.hudson.api.model.IProjectProperty; * @author Nikita Levyankov */ public class BaseProjectProperty<T> implements IProjectProperty<T> { + static final String INVALID_JOB_EXCEPTION = "Project property should have not null job"; + static final String INVALID_PROPERTY_KEY_EXCEPTION = "Project property should have not null propertyKey"; private transient String propertyKey; private transient IJob job; @@ -53,17 +55,38 @@ public class BaseProjectProperty<T> implements IProjectProperty<T> { * {@inheritDoc} */ public void setJob(IJob job) { - assert job != null; + if (null == job) { + throw new IllegalArgumentException(INVALID_JOB_EXCEPTION); + } this.job = job; } /** + * @return job that property belongs to. + */ + final IJob getJob() { + return job; + } + + /** + * Sets the overridden flag. + * + * @param overridden true - mark property as overridden, false - otherwise. + */ + final void setPropertyOverridden(boolean overridden) { + propertyOverridden = overridden; + } + + /** * {@inheritDoc} */ @SuppressWarnings("unchecked") public T getCascadingValue() { - return job.hasCascadingProject() ? - (T) job.getCascadingProject().getProperty(propertyKey, this.getClass()).getValue() : getDefaultValue(); + if (null == propertyKey) { + throw new IllegalArgumentException(INVALID_PROPERTY_KEY_EXCEPTION); + } + return getJob().hasCascadingProject() ? + (T) getJob().getCascadingProject().getProperty(propertyKey, this.getClass()).getValue() : getDefaultValue(); } /** @@ -95,16 +118,19 @@ public class BaseProjectProperty<T> implements IProjectProperty<T> { */ @SuppressWarnings("unchecked") public void setValue(T value) { + if (null == propertyKey) { + throw new IllegalArgumentException(INVALID_PROPERTY_KEY_EXCEPTION); + } value = prepareValue(value); - if (!job.hasCascadingProject()) { + if (!getJob().hasCascadingProject()) { originalValue = value; } else if (allowOverrideValue( - (T) job.getCascadingProject().getProperty(propertyKey, this.getClass()).getValue(), value)) { + (T) getJob().getCascadingProject().getProperty(propertyKey, this.getClass()).getValue(), value)) { originalValue = value; - propertyOverridden = true; + setPropertyOverridden(true); } else { this.originalValue = null; - propertyOverridden = false; + setPropertyOverridden(false); } } @@ -135,4 +161,4 @@ public class BaseProjectProperty<T> implements IProjectProperty<T> { public T getOriginalValue() { return originalValue; } -} +}
\ No newline at end of file diff --git a/hudson-core/src/test/java/hudson/model/FreeStyleProjectMock.java b/hudson-core/src/test/java/hudson/model/FreeStyleProjectMock.java new file mode 100644 index 0000000..b647887 --- a/dev/null +++ b/hudson-core/src/test/java/hudson/model/FreeStyleProjectMock.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * + * 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.model; + +/** + * Mock class for FreeStyleProject + * <p/> + * Date: 9/27/11 + * + * @author Nikita Levyankov + */ +class FreeStyleProjectMock extends FreeStyleProject { + + public FreeStyleProjectMock(String name) { + super((ItemGroup) null, name); + setAllowSave(false); + } + + @Override + protected void updateTransientActions() { + } +}
\ No newline at end of file diff --git a/hudson-core/src/test/java/hudson/model/FreeStyleProjectTest.java b/hudson-core/src/test/java/hudson/model/FreeStyleProjectTest.java index da0cc82..253a5eb 100644 --- a/hudson-core/src/test/java/hudson/model/FreeStyleProjectTest.java +++ b/hudson-core/src/test/java/hudson/model/FreeStyleProjectTest.java @@ -733,16 +733,4 @@ public class FreeStyleProjectTest { //Child value is not equals to parent - override value in child. assertEquals(childConcurrentBuild, childProject.isConcurrentBuild()); } - - private class FreeStyleProjectMock extends FreeStyleProject { - - private FreeStyleProjectMock(String name) { - super((ItemGroup)null, name); - setAllowSave(false); - } - - @Override - protected void updateTransientActions() { - } - } } diff --git a/hudson-core/src/test/java/hudson/model/ProjectPropertyTest.java b/hudson-core/src/test/java/hudson/model/ProjectPropertyTest.java new file mode 100644 index 0000000..57744d7 --- a/dev/null +++ b/hudson-core/src/test/java/hudson/model/ProjectPropertyTest.java @@ -0,0 +1,344 @@ +/******************************************************************************* + * + * 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.model; + +import org.junit.Before; +import org.junit.Test; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + +/** + * Contains test-cases for IProjectProperty and its' implementations. + * <p/> + * Date: 9/27/11 + * + * @author Nikita Levyankov + */ +@SuppressWarnings("unchecked") +public class ProjectPropertyTest { + private FreeStyleProject project; + private FreeStyleProjectMock parent; + private final String propertyKey = "propertyKey"; + + @Before + public void setUp() { + project = new FreeStyleProjectMock("project"); + parent = new FreeStyleProjectMock("parent"); + } + + /** + * Verify all constructors for ProjectProperties hierarchy. + */ + @Test + public void testConstructor() { + try { + new BaseProjectProperty(null); + fail("Null should be handled by ProjectProperty constructor."); + } catch (Exception e) { + assertEquals(BaseProjectProperty.INVALID_JOB_EXCEPTION, e.getMessage()); + } + try { + new StringProjectProperty(null); + fail("Null should be handled by ProjectProperty constructor."); + } catch (Exception e) { + assertEquals(BaseProjectProperty.INVALID_JOB_EXCEPTION, e.getMessage()); + } + try { + new IntegerProjectProperty(null); + fail("Null should be handled by ProjectProperty constructor."); + } catch (Exception e) { + assertEquals(BaseProjectProperty.INVALID_JOB_EXCEPTION, e.getMessage()); + } + try { + new BooleanProjectProperty(null); + fail("Null should be handled by ProjectProperty constructor."); + } catch (Exception e) { + assertEquals(BaseProjectProperty.INVALID_JOB_EXCEPTION, e.getMessage()); + } + BaseProjectProperty property = new BaseProjectProperty(project); + assertNotNull(property.getJob()); + assertEquals(project, property.getJob()); + } + + /** + * Checks prepareValue method. + */ + @Test + public void testPrepareValue() { + //BaseProject property doesn't perform any changes with value. + BaseProjectProperty property = new BaseProjectProperty(project); + assertNull(property.prepareValue(null)); + Object value = new Object(); + assertEquals(value, property.prepareValue(value)); + + //Boolean property acts as BaseProperty + property = new BooleanProjectProperty(project); + assertNull(property.prepareValue(null)); + assertFalse((Boolean) property.prepareValue(false)); + assertTrue((Boolean) property.prepareValue(true)); + + //Integer property acts as BaseProperty + property = new IntegerProjectProperty(project); + assertNull(property.prepareValue(null)); + int intValue = 10; + assertEquals(intValue, property.prepareValue(intValue)); + + //String project property trims string values to null and uses StringUtils.trimToNull logic. + property = new StringProjectProperty(project); + assertNull(property.prepareValue(null)); + assertNull(property.prepareValue("")); + assertNull(property.prepareValue(" ")); + assertEquals("abc", property.prepareValue("abc")); + assertEquals("abc", property.prepareValue(" abc ")); + } + + /** + * Verify getDefaultValue method. + */ + @Test + public void testGetDefaultValue() { + BaseProjectProperty property = new BaseProjectProperty(project); + assertNull(property.getDefaultValue()); + property = new StringProjectProperty(project); + assertNull(property.getDefaultValue()); + property = new IntegerProjectProperty(project); + assertEquals(0, property.getDefaultValue()); + property = new BooleanProjectProperty(project); + assertFalse((Boolean) property.getDefaultValue()); + + } + + /** + * Verify allowOverrideValue method. + */ + @Test + public void testAllowOverrideValue() { + //Test BaseProjectProperty. + BaseProjectProperty property = new BaseProjectProperty(project); + assertFalse(property.allowOverrideValue(null, null)); + assertTrue(property.allowOverrideValue(new Object(), null)); + assertTrue(property.allowOverrideValue(null, new Object())); + + //Test BooleanProjectProperty. + property = new BooleanProjectProperty(project); + assertFalse(property.allowOverrideValue(null, null)); + assertFalse(property.allowOverrideValue(false, false)); + assertFalse(property.allowOverrideValue(true, true)); + assertTrue(property.allowOverrideValue(true, false)); + assertTrue(property.allowOverrideValue(false, true)); + + //Test IntegerProjectProperty. + property = new IntegerProjectProperty(project); + assertFalse(property.allowOverrideValue(null, null)); + assertFalse(property.allowOverrideValue(1, 1)); + assertTrue(property.allowOverrideValue(1, 0)); + assertTrue(property.allowOverrideValue(0, 1)); + + //Test StringProjectProperty. + property = new StringProjectProperty(project); + assertFalse(property.allowOverrideValue(null, null)); + assertFalse(property.allowOverrideValue("", "")); + assertFalse(property.allowOverrideValue("abc", "abc")); + assertFalse(property.allowOverrideValue("abc", "ABC")); + assertTrue(property.allowOverrideValue(null, "abc")); + assertTrue(property.allowOverrideValue("abc", null)); + assertTrue(property.allowOverrideValue("abc", "abcd")); + } + + /** + * Verify getCascadingValue method. + */ + @Test + public void testGetCascadingValue() { + String parentValue = "parentValue"; + + BaseProjectProperty property = new BaseProjectProperty(project); + property.setKey(propertyKey); + //If project doesn't have cascading project - default value is used as cascading value. + assertEquals(property.getDefaultValue(), property.getCascadingValue()); + + project.setCascadingProject(parent); + property = new BaseProjectProperty(project); + property.setKey(propertyKey); + //If project has cascading project and cascading value is not set - default value is used. + assertEquals(property.getDefaultValue(), property.getCascadingValue()); + + BaseProjectProperty parentProperty = new BaseProjectProperty(parent); + parentProperty.setKey(propertyKey); + parentProperty.setValue(parentValue); + parent.putJobProperty(propertyKey, parentProperty); + project.setCascadingProject(parent); + property = new BaseProjectProperty(project); + property.setKey(propertyKey); + property.setValue(parentValue); + //If project has cascading project and cascading value is set - property value will be used. + assertEquals(parentProperty.getOriginalValue(), property.getCascadingValue()); + } + + /** + * Verify getOriginalValue method. + */ + @Test + public void testGetOriginalValue() { + BaseProjectProperty property = new BaseProjectProperty(project); + assertNull(property.getOriginalValue()); + Object value = new Object(); + + property.setKey(propertyKey); + property.setValue(value); + assertEquals(value, property.getOriginalValue()); + property.setValue(null); + assertNull(property.getOriginalValue()); + + value = 10; + property = new IntegerProjectProperty(project); + property.setKey(propertyKey); + property.setValue(value); + assertEquals(value, property.getOriginalValue()); + + value = "abs"; + property = new StringProjectProperty(project); + property.setKey(propertyKey); + property.setValue(value); + assertEquals(value, property.getOriginalValue()); + + value = Boolean.TRUE; + property = new BooleanProjectProperty(project); + property.setKey(propertyKey); + property.setValue(value); + assertEquals(value, property.getOriginalValue()); + property.setValue(null); + assertFalse((Boolean) property.getOriginalValue()); + } + + /** + * Verify setValue method. + */ + @Test + public void testSetValue() { + BaseProjectProperty property = new BaseProjectProperty(project); + property.setKey(propertyKey); + property.setValue(null); + //If project doesn't have cascading - default boolean value is used for propertyOverridden flag + assertFalse(property.isPropertyOverridden()); + assertNull(property.getOriginalValue()); + + Object value = 12345; + property.setValue(value); + //If project doesn't have cascading - default boolean value is used for propertyOverridden flag + assertFalse(property.isPropertyOverridden()); + assertEquals(value, property.getOriginalValue()); + + String parentValue = "equalValue"; + BaseProjectProperty parentProperty = new BaseProjectProperty(parent); + parentProperty.setKey(propertyKey); + parentProperty.setValue(parentValue); + parent.putJobProperty(propertyKey, parentProperty); + project.setCascadingProject(parent); + + String overriddenValue = "newValue"; + property.setValue(overriddenValue); + assertTrue(property.isPropertyOverridden()); + + //Check whether current value is not null, after setting equal-to-cascading value current will be null + assertNotNull(property.getOriginalValue()); + assertTrue(property.isPropertyOverridden()); + property.setValue(parentValue); + //Reset current property to null + assertNull(property.getOriginalValue()); + //Cascading value is equal to current - reset flag to false. + assertFalse(property.isPropertyOverridden()); + } + + @Test + public void testGetValue() { + Integer propertyValue = 10; + IntegerProjectProperty property = new IntegerProjectProperty(project); + property.setKey(propertyKey); + property.setValue(propertyValue); + //if value is not null - return it + assertEquals(propertyValue, property.getValue()); + property.setValue(null); + assertNull(property.getOriginalValue()); + + //if current value is set to null and property is not overridden - defaultValue (Zero) will be taken + Integer value = property.getValue(); + assertNotNull(value); + assertFalse(property.isPropertyOverridden()); + assertEquals(property.getDefaultValue(), value); + + property.setPropertyOverridden(true); + //If property is overridden - return its actual value. + assertNull(property.getValue()); + + IntegerProjectProperty parentProperty = new IntegerProjectProperty(parent); + parentProperty.setKey(propertyKey); + parentProperty.setValue(propertyValue); + parent.putJobProperty(propertyKey, parentProperty); + + project.setCascadingProject(parent); + property = new IntegerProjectProperty(project); + property.setKey(propertyKey); + //if current value is null and is not overridden value, take from cascading + assertNull(property.getOriginalValue()); + assertEquals(propertyValue, property.getValue()); + + property.setPropertyOverridden(true); + //Property is overridden - return current value even if it is null. + assertNull(property.getOriginalValue()); + assertNull(property.getValue()); + } + + /** + * Property should have not null property key. + */ + @Test + public void testNullPropertyKey() { + BaseProjectProperty property = new BaseProjectProperty(project); + try { + property.setValue("value"); + fail("PropertyKey shouldn't be null"); + } catch (Exception e) { + assertEquals(BaseProjectProperty.INVALID_PROPERTY_KEY_EXCEPTION, e.getMessage()); + } + try { + property.getCascadingValue(); + fail("PropertyKey shouldn't be null"); + } catch (Exception e) { + assertEquals(BaseProjectProperty.INVALID_PROPERTY_KEY_EXCEPTION, e.getMessage()); + } + try { + property.getValue(); + fail("PropertyKey shouldn't be null"); + } catch (Exception e) { + assertEquals(BaseProjectProperty.INVALID_PROPERTY_KEY_EXCEPTION, e.getMessage()); + } + + property.setKey("key"); + try { + property.setValue("value"); + property.getCascadingValue(); + property.getValue(); + } catch (Exception e) { + fail("PropertyKey is valid"); + } + } +} |

