| author | akozak | 2011-11-22 06:57:13 (EST) |
|---|---|---|
| committer | Winston Prakash | 2011-12-01 20:46:56 (EST) |
| commit | d813b79674f801b99f2208deb2d6248ac4a55f37 (patch) (side-by-side diff) | |
| tree | 5c25b3ba06310a57ab75e4661edede656064838c | |
| parent | 441990c31540b44420c9d4ecedc82c81456c7b1f (diff) | |
| download | org.eclipse.hudson.core-d813b79674f801b99f2208deb2d6248ac4a55f37.zip org.eclipse.hudson.core-d813b79674f801b99f2208deb2d6248ac4a55f37.tar.gz org.eclipse.hudson.core-d813b79674f801b99f2208deb2d6248ac4a55f37.tar.bz2 | |
Draft implementation which supports legacy properties. Implement unit-test for Legacy configuration testing
Signed-off-by: Winston Prakash <winston.prakash@gmail.com>
4 files changed, 136 insertions, 12 deletions
diff --git a/hudson-core/src/main/java/hudson/model/FreeStyleProject.java b/hudson-core/src/main/java/hudson/model/FreeStyleProject.java index 76f1e6b..2e9e578 100644 --- a/hudson-core/src/main/java/hudson/model/FreeStyleProject.java +++ b/hudson-core/src/main/java/hudson/model/FreeStyleProject.java @@ -42,6 +42,8 @@ public class FreeStyleProject extends Project<FreeStyleProject,FreeStyleBuild> i * See {@link #setCustomWorkspace(String)}. * * @since 1.216 + * @deprecated left for backward compatibility + * @since 2.1.2 */ private String customWorkspace; @@ -97,6 +99,16 @@ public class FreeStyleProject extends Project<FreeStyleProject,FreeStyleBuild> i super.submit(req, rsp); } + @Override + protected void buildProjectProperties() throws IOException { + super.buildProjectProperties(); + //Convert legacy cust omWorkspace property to IProjectProperty logic + if (null != customWorkspace && null == getProperty(CUSTOM_WORKSPACE_PROPERTY_NAME)) { + setCustomWorkspace(customWorkspace); + customWorkspace = null;//Reset to null. No longer needed. + } + } + public DescriptorImpl getDescriptor() { return DESCRIPTOR; } diff --git a/hudson-core/src/main/java/hudson/model/Job.java b/hudson-core/src/main/java/hudson/model/Job.java index 36f04be..6fa678f 100644 --- a/hudson-core/src/main/java/hudson/model/Job.java +++ b/hudson-core/src/main/java/hudson/model/Job.java @@ -178,6 +178,9 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R * @param allowSave allow save. */ protected void setAllowSave(Boolean allowSave) { + if (null == this.allowSave) { + initAllowSave(); + } this.allowSave.set(allowSave); } @@ -209,7 +212,6 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R /** * {@inheritDoc} */ - //TODO improve error handling for this method public IProjectProperty getProperty(String key, Class clazz) { IProjectProperty t = jobProperties.get(key); if (null == t && null != clazz) { @@ -240,6 +242,9 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R @Override public synchronized void save() throws IOException { + if (null == allowSave) { + initAllowSave(); + } if (allowSave.get()) { super.save(); holdOffBuildUntilSave = false; @@ -253,14 +258,7 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R super.onLoad(parent, name); cascadingProject = (JobT) Functions.getItemByName(Hudson.getInstance().getAllItems(this.getClass()), cascadingProjectName); - if (null == allowSave) {// Initialize property if null for legacy config. - allowSave = new ThreadLocal<Boolean>() { - @Override - protected Boolean initialValue() { - return true; - } - }; - } + initAllowSave(); TextFile f = getNextBuildNumberFile(); if (f.exists()) { // starting 1.28, we store nextBuildNumber in a separate file. @@ -285,9 +283,26 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R for (JobProperty p : properties) p.setOwner(this); - if (null == jobProperties) { - jobProperties = new ConcurrentHashMap<String, IProjectProperty>(); - } + buildProjectProperties(); + } + + protected void initAllowSave() { + allowSave = new ThreadLocal<Boolean>() { + @Override + protected Boolean initialValue() { + return true; + } + }; + } + + /** + * Initializes and builds project properties. Also converts legacy properties to IProjectProperties. + * Subclasses should inherit and override this behavior. + * + * @throws IOException if any. + */ + protected void buildProjectProperties() throws IOException { + initProjectProperties(); for (Map.Entry<String, IProjectProperty> entry : jobProperties.entrySet()) { IProjectProperty property = entry.getValue(); property.setKey(entry.getKey()); @@ -295,6 +310,15 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R } } + /** + * Initialize project properties if null. + */ + protected final void initProjectProperties() { + if (null == jobProperties) { + jobProperties = new ConcurrentHashMap<String, IProjectProperty>(); + } + } + @Override public void onCopiedFrom(Item src) { super.onCopiedFrom(src); diff --git a/hudson-core/src/test/java/hudson/model/LegacyProjectTest.java b/hudson-core/src/test/java/hudson/model/LegacyProjectTest.java new file mode 100644 index 0000000..1ddccb2 --- a/dev/null +++ b/hudson-core/src/test/java/hudson/model/LegacyProjectTest.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * + * 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 java.io.File; +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.assertNotNull; +import static junit.framework.Assert.assertNull; + +/** + * Test for legacy + * <p/> + * Date: 9/23/11 + * + * @author Nikita Levyankov + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Hudson.class}) +public class LegacyProjectTest { + + /** + * Tests unmarshalls FreeStyleProject configuration and checks whether CustomWorkspace property is set, + * + * @throws Exception if any. + */ + @Test + public void testLoadFreeStyleProject() throws Exception { + File freeStyleProjectConfig = new File(FreeStyleProject.class.getResource("/hudson/model/freestyle").toURI()); + FreeStyleProject freeStyleProject = (FreeStyleProject) Items.getConfigFile(freeStyleProjectConfig).read(); + freeStyleProject.setAllowSave(false); + freeStyleProject.initProjectProperties(); + assertNull(freeStyleProject.getProperty(FreeStyleProject.CUSTOM_WORKSPACE_PROPERTY_NAME)); + freeStyleProject.buildProjectProperties(); + assertNotNull(freeStyleProject.getCustomWorkspace()); + } +} diff --git a/hudson-core/src/test/resources/hudson/model/freestyle/config.xml b/hudson-core/src/test/resources/hudson/model/freestyle/config.xml new file mode 100644 index 0000000..ff6dedb --- a/dev/null +++ b/hudson-core/src/test/resources/hudson/model/freestyle/config.xml @@ -0,0 +1,36 @@ +<?xml version='1.0' encoding='UTF-8'?> +<project> + <actions/> + <description>test application</description> + <logRotator> + <daysToKeep>5</daysToKeep> + <numToKeep>10</numToKeep> + <artifactDaysToKeep>-1</artifactDaysToKeep> + <artifactNumToKeep>-1</artifactNumToKeep> + </logRotator> + <keepDependencies>false</keepDependencies> + <creationTime>1316762043103</creationTime> + <quietPeriod>5</quietPeriod> + <scmCheckoutRetryCount>5</scmCheckoutRetryCount> + <advancedAffinityChooser>false</advancedAffinityChooser> + <canRoam>true</canRoam> + <disabled>false</disabled> + <blockBuildWhenDownstreamBuilding>true</blockBuildWhenDownstreamBuilding> + <blockBuildWhenUpstreamBuilding>true</blockBuildWhenUpstreamBuilding> + <triggers class="vector"> + <hudson.triggers.SCMTrigger> + <spec>5 * * * *</spec> + </hudson.triggers.SCMTrigger> + </triggers> + <concurrentBuild>true</concurrentBuild> + <cleanWorkspaceRequired>true</cleanWorkspaceRequired> + <publishers> + <hudson.tasks.Mailer> + <recipients>admin@gmail.com</recipients> + <dontNotifyEveryUnstableBuild>false</dontNotifyEveryUnstableBuild> + <sendToIndividuals>true</sendToIndividuals> + </hudson.tasks.Mailer> + </publishers> + <buildWrappers/> + <customWorkspace>/tmp</customWorkspace> +</project>
\ No newline at end of file |

