| author | akozak | 2011-11-22 04:08:22 (EST) |
|---|---|---|
| committer | Winston Prakash | 2011-12-01 20:46:55 (EST) |
| commit | ff323d2eb7c0136e7522a103bb256102ea7e49e3 (patch) (side-by-side diff) | |
| tree | ac71862c47c4a8c4ee57bd469e02a213069a14f3 | |
| parent | 29169f0c536e93587dc884eddd6a89daf7530df7 (diff) | |
| download | org.eclipse.hudson.core-ff323d2eb7c0136e7522a103bb256102ea7e49e3.zip org.eclipse.hudson.core-ff323d2eb7c0136e7522a103bb256102ea7e49e3.tar.gz org.eclipse.hudson.core-ff323d2eb7c0136e7522a103bb256102ea7e49e3.tar.bz2 | |
Implement BooleanProperty. Add ability to return default value for property instead of null. Code clean-up. Apply boolean properties for 'Clean workspace before build' logic
Signed-off-by: Winston Prakash <winston.prakash@gmail.com>
12 files changed, 146 insertions, 95 deletions
diff --git a/hudson-core/src/main/java/hudson/model/AbstractProject.java b/hudson-core/src/main/java/hudson/model/AbstractProject.java index 0b2a67b..e94328e 100644 --- a/hudson-core/src/main/java/hudson/model/AbstractProject.java +++ b/hudson-core/src/main/java/hudson/model/AbstractProject.java @@ -305,54 +305,21 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A */ @Exported public boolean isConcurrentBuild() { - return isConcurrentBuild(true); - } - - public boolean isConcurrentBuild(boolean useParentValue) { - if (!useParentValue || !hasCascadingProject() || isOverriddenProperty(CONCURRENT_BUILD_PROPERTY_NAME)) { - return Hudson.CONCURRENT_BUILD && concurrentBuild; - } else { - return hasCascadingProject() && getCascadingProject().isConcurrentBuild(); - } + return getBooleanProperty(CONCURRENT_BUILD_PROPERTY_NAME).getValue(); } public void setConcurrentBuild(boolean b) throws IOException { - if (!hasCascadingProject()) { - this.concurrentBuild = b; - } else if (!ObjectUtils.equals(getCascadingProject().isConcurrentBuild(), b)) { - this.concurrentBuild = b; - registerOverriddenProperty(CONCURRENT_BUILD_PROPERTY_NAME); - } else { - this.concurrentBuild = false; - unRegisterOverriddenProperty(CONCURRENT_BUILD_PROPERTY_NAME); - } + getBooleanProperty(CONCURRENT_BUILD_PROPERTY_NAME).setValue(b); save(); } public boolean isCleanWorkspaceRequired() { - return isCleanWorkspaceRequired(true); - } - - public boolean isCleanWorkspaceRequired(boolean useParentValue) { - if (!useParentValue || !hasCascadingProject() || isOverriddenProperty(CLEAN_WORKSPACE_REQUIRED_PROPERTY_NAME)) { - return cleanWorkspaceRequired; - } else { - return hasCascadingProject() && getCascadingProject().isCleanWorkspaceRequired(); - } + return getBooleanProperty(CLEAN_WORKSPACE_REQUIRED_PROPERTY_NAME).getValue(); } public void setCleanWorkspaceRequired(boolean cleanWorkspaceRequired) { - if (!hasCascadingProject()) { - this.cleanWorkspaceRequired = cleanWorkspaceRequired; - } else if (!ObjectUtils.equals(getCascadingProject().isCleanWorkspaceRequired(), cleanWorkspaceRequired)) { - registerOverriddenProperty(CLEAN_WORKSPACE_REQUIRED_PROPERTY_NAME); - this.cleanWorkspaceRequired = cleanWorkspaceRequired; - } else { - this.cleanWorkspaceRequired = false; - unRegisterOverriddenProperty(CLEAN_WORKSPACE_REQUIRED_PROPERTY_NAME); - } + getBooleanProperty(CLEAN_WORKSPACE_REQUIRED_PROPERTY_NAME).setValue(cleanWorkspaceRequired); } - /** * If this project is configured to be always built on this node, * return that {@link Node}. Otherwise null. diff --git a/hudson-core/src/main/java/hudson/model/BaseProperty.java b/hudson-core/src/main/java/hudson/model/BaseProperty.java index a1b7b77..aa0278e 100644 --- a/hudson-core/src/main/java/hudson/model/BaseProperty.java +++ b/hudson-core/src/main/java/hudson/model/BaseProperty.java @@ -15,7 +15,6 @@ package hudson.model; -import java.io.IOException; import org.eclipse.hudson.api.model.IJob; import org.eclipse.hudson.api.model.IProperty; @@ -52,9 +51,9 @@ public abstract class BaseProperty<T> implements IProperty<T> { * {@inheritDoc} */ @SuppressWarnings("unchecked") - public T getCascadingValue() throws IOException { + public T getCascadingValue() { return job.hasCascadingProject() ? - (T) job.getCascadingProject().getProperty(propertyKey, this.getClass()).getValue() : null; + (T) job.getCascadingProject().getProperty(propertyKey, this.getClass()).getValue() : getDefaultValue(); } /** @@ -67,8 +66,15 @@ public abstract class BaseProperty<T> implements IProperty<T> { /** * {@inheritDoc} */ - public T getValue() throws IOException { - if (isPropertyOverridden() || null != getOriginalValue()) { + public T getDefaultValue() { + return null; + } + + /** + * {@inheritDoc} + */ + public T getValue() { + if (isPropertyOverridden() || null != originalValue) { return getOriginalValue(); } return getCascadingValue(); @@ -78,7 +84,7 @@ public abstract class BaseProperty<T> implements IProperty<T> { * {@inheritDoc} */ @SuppressWarnings("unchecked") - public void setValue(T value) throws IOException { + public void setValue(T value) { value = prepareValue(value); if (!job.hasCascadingProject()) { originalValue = value; diff --git a/hudson-core/src/main/java/hudson/model/BooleanProperty.java b/hudson-core/src/main/java/hudson/model/BooleanProperty.java new file mode 100644 index 0000000..dd44767 --- a/dev/null +++ b/hudson-core/src/main/java/hudson/model/BooleanProperty.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * + * 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.apache.commons.lang3.ObjectUtils; + +/** + * Represents boolean property. + * <p/> + * Date: 9/22/11 + * + * @author Nikita Levyankov + */ +public class BooleanProperty extends BaseProperty<Boolean> { + + /** + * {@inheritDoc} + */ + protected boolean allowOverrideValue(Boolean cascadingValue, Boolean candidateValue) { + return !ObjectUtils.equals(cascadingValue, candidateValue); + } + + /** + * {@inheritDoc} + */ + @Override + public Boolean getOriginalValue() { + Boolean originalValue = super.getOriginalValue(); + return null != originalValue ? originalValue : getDefaultValue(); + } + + /** + * {@inheritDoc} + */ + @Override + public Boolean getDefaultValue() { + return false; + } +} diff --git a/hudson-core/src/main/java/hudson/model/FreeStyleProject.java b/hudson-core/src/main/java/hudson/model/FreeStyleProject.java index 2adf096..76f1e6b 100644 --- a/hudson-core/src/main/java/hudson/model/FreeStyleProject.java +++ b/hudson-core/src/main/java/hudson/model/FreeStyleProject.java @@ -36,7 +36,7 @@ import javax.servlet.ServletException; public class FreeStyleProject extends Project<FreeStyleProject,FreeStyleBuild> implements TopLevelItem, IFreeStyleProject { - public static final String CUSTOM_WORKSPACE_PROPERTY_KEY = "customWorkspace"; + public static final String CUSTOM_WORKSPACE_PROPERTY_NAME = "customWorkspace"; /** * See {@link #setCustomWorkspace(String)}. @@ -61,16 +61,8 @@ public class FreeStyleProject extends Project<FreeStyleProject,FreeStyleBuild> i return FreeStyleBuild.class; } - public String getCustomWorkspace(boolean useParentValue) throws IOException { - StringProperty jobProperty = getStringProperty(CUSTOM_WORKSPACE_PROPERTY_KEY); - if (!useParentValue) { - return jobProperty.getOriginalValue(); - } - return jobProperty.getValue(); - } - public String getCustomWorkspace() throws IOException { - return getCustomWorkspace(true); + return getStringProperty(CUSTOM_WORKSPACE_PROPERTY_NAME).getValue(); } /** @@ -93,8 +85,7 @@ public class FreeStyleProject extends Project<FreeStyleProject,FreeStyleBuild> i * @throws IOException if any. */ public void setCustomWorkspace(String customWorkspace) throws IOException { - StringProperty jobProperty = getStringProperty(CUSTOM_WORKSPACE_PROPERTY_KEY); - jobProperty.setValue(customWorkspace); + getStringProperty(CUSTOM_WORKSPACE_PROPERTY_NAME).setValue(customWorkspace); save(); } diff --git a/hudson-core/src/main/java/hudson/model/IntegerProperty.java b/hudson-core/src/main/java/hudson/model/IntegerProperty.java new file mode 100644 index 0000000..3298a8a --- a/dev/null +++ b/hudson-core/src/main/java/hudson/model/IntegerProperty.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * + * 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.apache.commons.lang3.ObjectUtils; + +/** + * Represents integer property. + * <p/> + * Date: 9/22/11 + * + * @author Nikita Levyankov + */ +public class IntegerProperty extends BaseProperty<Integer> { + /** + * {@inheritDoc} + */ + protected boolean allowOverrideValue(Integer cascadingValue, Integer candidateValue) { + return !ObjectUtils.equals(cascadingValue, candidateValue); + } +} diff --git a/hudson-core/src/main/java/hudson/model/Job.java b/hudson-core/src/main/java/hudson/model/Job.java index 2e67b5b..47834ac 100644 --- a/hudson-core/src/main/java/hudson/model/Job.java +++ b/hudson-core/src/main/java/hudson/model/Job.java @@ -219,22 +219,16 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R * Returns job property by specified key. * * @param key key. - * @return {@link org.hudsonci.api.model.IProperty} instance or null. - * @throws IOException if any. + * @return {@link IProperty} instance or null. */ - public IProperty getProperty(String key) throws IOException { + public IProperty getProperty(String key){ return getProperty(key, null); } /** - * Returns null safe job property by specified key. if property is not present, try instantiate it. - * - * @param key key. - * @param clazz type of property.. - * @return {@link org.hudsonci.api.model.IProperty} instance or null. - * @throws IOException if any. + * {@inheritDoc} */ - public IProperty getProperty(String key, Class clazz) throws IOException { + public IProperty getProperty(String key, Class clazz) { IProperty t = jobProperties.get(key); if (null == t && null != clazz) { try { @@ -243,18 +237,22 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R t.setKey(key); putJobProperty(key, t); } catch (InstantiationException e) { - throw new IOException(e); + e.printStackTrace(); } catch (IllegalAccessException e) { - throw new IOException(e); + e.printStackTrace(); } } return t; } - public StringProperty getStringProperty(String key) throws IOException { + public StringProperty getStringProperty(String key) { return (StringProperty) getProperty(key, StringProperty.class); } + public BooleanProperty getBooleanProperty(String key){ + return (BooleanProperty) getProperty(key, BooleanProperty.class); + } + @Override public synchronized void save() throws IOException { if (allowSave.get()) { diff --git a/hudson-core/src/main/java/org/eclipse/hudson/api/model/IJob.java b/hudson-core/src/main/java/org/eclipse/hudson/api/model/IJob.java index ec4e560..c582dd3 100644 --- a/hudson-core/src/main/java/org/eclipse/hudson/api/model/IJob.java +++ b/hudson-core/src/main/java/org/eclipse/hudson/api/model/IJob.java @@ -17,9 +17,7 @@ package org.eclipse.hudson.api.model; import hudson.model.JobProperty; import hudson.model.JobPropertyDescriptor; import hudson.tasks.LogRotator; -import java.io.IOException; import java.util.Map; -import org.eclipse.hudson.api.model.IProperty; /** * Interface that represents Job. @@ -50,9 +48,8 @@ public interface IJob<T extends IJob> { * @param key key. * @param clazz IProperty subclass. * @return {@link IProperty} instance or null. - * @throws java.io.IOException if any. */ - IProperty getProperty(String key, Class<? extends IProperty> clazz) throws IOException; + IProperty getProperty(String key, Class<? extends IProperty> clazz); /** * Checks whether current job is inherited from other project. diff --git a/hudson-core/src/main/java/org/eclipse/hudson/api/model/IProperty.java b/hudson-core/src/main/java/org/eclipse/hudson/api/model/IProperty.java index 9516460..72a524d 100644 --- a/hudson-core/src/main/java/org/eclipse/hudson/api/model/IProperty.java +++ b/hudson-core/src/main/java/org/eclipse/hudson/api/model/IProperty.java @@ -15,7 +15,6 @@ package org.eclipse.hudson.api.model; -import java.io.IOException; import java.io.Serializable; /** @@ -45,9 +44,8 @@ public interface IProperty<T> extends Serializable { * Sets property value. * * @param value value to set. - * @throws IOException if any. */ - void setValue(T value) throws IOException; + void setValue(T value); /** * Returns original property value. @@ -60,9 +58,8 @@ public interface IProperty<T> extends Serializable { * Returns cascading value if any. * * @return string. - * @throws IOException if any. */ - T getCascadingValue() throws IOException; + T getCascadingValue(); /** * @return true if value inherited from cascading project, false - otherwise, @@ -74,8 +71,13 @@ public interface IProperty<T> extends Serializable { * property - call {@link #getOriginalValue()}, otherwise call {@link #getCascadingValue()}. * * @return string. - * @throws IOException if any. */ - T getValue() throws IOException; + T getValue(); + /** + * This value will be taken if both cascading project and current project don't have values. Null by default. + * + * @return value + */ + T getDefaultValue(); } diff --git a/hudson-core/src/main/resources/lib/hudson/project/config-cleanWorkspace.jelly b/hudson-core/src/main/resources/lib/hudson/project/config-cleanWorkspace.jelly index 2f385dd..9bcf190 100644 --- a/hudson-core/src/main/resources/lib/hudson/project/config-cleanWorkspace.jelly +++ b/hudson-core/src/main/resources/lib/hudson/project/config-cleanWorkspace.jelly @@ -17,8 +17,10 @@ <!-- clean workspace --> <j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"> + <j:set var="cleanWorkspaceRequiredProperty" value="${it.getBooleanProperty(it.CLEAN_WORKSPACE_REQUIRED_PROPERTY_NAME)}"/> + <j:set var="cleanWorkspaceRequired" value="${cleanWorkspaceRequired.getValue()}"/> <f:optionalBlock name="cleanWorkspaceRequired" title="${%Clean workspace before build}" - checked="${it.cleanWorkspaceRequired}" - isCascadingValue="${it.isOverriddenProperty(it.CLEAN_WORKSPACE_REQUIRED_PROPERTY_NAME)}" + checked="${cleanWorkspaceRequired}" + isCascadingValue="${cleanWorkspaceRequiredProperty.isPropertyOverridden()}" help="/help/project-config/cleanWorkspace.html" /> </j:jelly> diff --git a/hudson-core/src/main/resources/lib/hudson/project/config-customWorkspace.jelly b/hudson-core/src/main/resources/lib/hudson/project/config-customWorkspace.jelly index 5fc8939..c591a95 100644 --- a/hudson-core/src/main/resources/lib/hudson/project/config-customWorkspace.jelly +++ b/hudson-core/src/main/resources/lib/hudson/project/config-customWorkspace.jelly @@ -17,7 +17,7 @@ <!-- custom workspace --> <j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"> - <j:set var="customWorkspaceProperty" value="${it.getStringProperty(it.CUSTOM_WORKSPACE_PROPERTY_KEY)}"/> + <j:set var="customWorkspaceProperty" value="${it.getStringProperty(it.CUSTOM_WORKSPACE_PROPERTY_NAME)}"/> <j:set var="customWorkspace" value="${customWorkspaceProperty.getValue()}"/> <f:optionalBlock name="customWorkspace" title="${%Use custom workspace}" checked="${customWorkspace!=null}" isCascadingValue="${customWorkspaceProperty.isPropertyOverridden()}" @@ -27,3 +27,4 @@ </f:entry> </f:optionalBlock> </j:jelly> + diff --git a/hudson-core/src/test/java/hudson/model/FreeStyleProjectTest.java b/hudson-core/src/test/java/hudson/model/FreeStyleProjectTest.java index 9bd1531..314b844 100644 --- a/hudson-core/src/test/java/hudson/model/FreeStyleProjectTest.java +++ b/hudson-core/src/test/java/hudson/model/FreeStyleProjectTest.java @@ -651,7 +651,6 @@ public class FreeStyleProjectTest { assertEquals(childBlockBuildWhenUpstreamBuilding, childProject.blockBuildWhenUpstreamBuilding()); } -// --- @Test public void testSetCleanWorkspaceRequiredEqualsWithParent() throws IOException { boolean cleanWorkspaceRequired = true; @@ -662,7 +661,8 @@ public class FreeStyleProjectTest { childProject.allowSave.set(false); childProject.setCascadingProject(parentProject); childProject.setCleanWorkspaceRequired(cleanWorkspaceRequired); - assertFalse(childProject.isCleanWorkspaceRequired(false)); + assertFalse( + childProject.getBooleanProperty(AbstractProject.CLEAN_WORKSPACE_REQUIRED_PROPERTY_NAME).getOriginalValue()); } @Test @@ -677,17 +677,19 @@ public class FreeStyleProjectTest { childProject.setCascadingProject(parentProject); childProject.setCleanWorkspaceRequired(childCleanWorkspaceRequired); //if child value is not equals to parent one, field should be populated - assertFalse(childProject.isCleanWorkspaceRequired(false)); + assertFalse( + childProject.getBooleanProperty(AbstractProject.CLEAN_WORKSPACE_REQUIRED_PROPERTY_NAME).getOriginalValue()); } @Test public void testSetCleanWorkspaceRequiredParentNull() throws IOException { - boolean cleanWorkspaceRequired = true; + Boolean cleanWorkspaceRequired = true; FreeStyleProject childProject = new FreeStyleProjectMock("child"); childProject.allowSave.set(false); childProject.setCleanWorkspaceRequired(cleanWorkspaceRequired); //if parent is not set, value should be populated according to existing logic - assertEquals(cleanWorkspaceRequired, childProject.isCleanWorkspaceRequired(false)); + assertEquals(cleanWorkspaceRequired, + childProject.getBooleanProperty(AbstractProject.CLEAN_WORKSPACE_REQUIRED_PROPERTY_NAME).getOriginalValue()); } @Test @@ -718,7 +720,7 @@ public class FreeStyleProjectTest { childProject.allowSave.set(false); childProject.setCascadingProject(parentProject); childProject.setConcurrentBuild(concurrentBuild); - assertFalse(childProject.isConcurrentBuild(false)); + assertFalse(childProject.getBooleanProperty(AbstractProject.CONCURRENT_BUILD_PROPERTY_NAME).getOriginalValue()); } @Test @@ -733,7 +735,8 @@ public class FreeStyleProjectTest { childProject.setCascadingProject(parentProject); childProject.setConcurrentBuild(childConcurrentBuild); //if child value is not equals to parent one, field should be populated - assertEquals(childConcurrentBuild, (Boolean) childProject.isConcurrentBuild(false)); + assertEquals(childConcurrentBuild, + childProject.getBooleanProperty(AbstractProject.CONCURRENT_BUILD_PROPERTY_NAME).getOriginalValue()); } @Test @@ -743,13 +746,14 @@ public class FreeStyleProjectTest { childProject.allowSave.set(false); childProject.setConcurrentBuild(concurrentBuild); //if parent is not set, value should be populated according to existing logic - assertEquals(concurrentBuild, (Boolean) childProject.isConcurrentBuild(false)); + assertEquals(concurrentBuild, + childProject.getBooleanProperty(AbstractProject.CONCURRENT_BUILD_PROPERTY_NAME).getOriginalValue()); } @Test public void testIsConcurrentBuild() throws IOException { - Boolean childConcurrentBuild = false; - Boolean parentConcurrentBuild = true; + boolean childConcurrentBuild = false; + boolean parentConcurrentBuild = true; FreeStyleProject parentProject = new FreeStyleProjectMock("parent"); parentProject.allowSave.set(false); parentProject.setConcurrentBuild(parentConcurrentBuild); @@ -758,10 +762,10 @@ public class FreeStyleProjectTest { childProject.setCascadingProject(parentProject); childProject.setConcurrentBuild(true); //Value should be taken from cascadingProject - assertEquals(parentConcurrentBuild, (Boolean) childProject.isConcurrentBuild()); + assertEquals(parentConcurrentBuild, childProject.isConcurrentBuild()); childProject.setConcurrentBuild(childConcurrentBuild); //Child value is not equals to parent - override value in child. - assertEquals(childConcurrentBuild, (Boolean) childProject.isConcurrentBuild()); + assertEquals(childConcurrentBuild, childProject.isConcurrentBuild()); } private class FreeStyleProjectMock extends FreeStyleProject { diff --git a/hudson-service/src/main/java/org/eclipse/hudson/service/internal/DummyStaplerRequest.java b/hudson-service/src/main/java/org/eclipse/hudson/service/internal/DummyStaplerRequest.java index 897c4e2..c797fcb 100644 --- a/hudson-service/src/main/java/org/eclipse/hudson/service/internal/DummyStaplerRequest.java +++ b/hudson-service/src/main/java/org/eclipse/hudson/service/internal/DummyStaplerRequest.java @@ -386,7 +386,6 @@ public class DummyStaplerRequest return 0; } - public String createJavaScriptProxy(Object o) { return null; } |

