Search in sources :

Example 1 with CopyOnWriteListProjectProperty

use of org.hudsonci.model.project.property.CopyOnWriteListProjectProperty in project hudson-2.x by hudson.

the class CascadingUtil method setParameterDefinitionProperties.

/**
     * Sets parameterDefinitionProperties for current job. This method is recursively executed for cascading children
     * for setting valid {@link ParametersDefinitionProperty#owner} value.
     *
     * @param job job.
     * @param key parameter key,
     * @param parameterDefinitionProperties new properties to set.
     */
@SuppressWarnings("unchecked")
public static void setParameterDefinitionProperties(Job job, String key, CopyOnWriteList<ParametersDefinitionProperty> parameterDefinitionProperties) {
    CopyOnWriteListProjectProperty projectProperty = getCopyOnWriteListProjectProperty(job, key);
    CopyOnWriteList<ParametersDefinitionProperty> pdProperties = new CopyOnWriteList<ParametersDefinitionProperty>();
    //Create new instance for each parameter in order to set owner and use in cascading children.
    for (ParametersDefinitionProperty pdp : parameterDefinitionProperties) {
        ParametersDefinitionProperty copiedDefinitionProperty = new ParametersDefinitionProperty(new ArrayList<ParameterDefinition>(pdp.getParameterDefinitions()));
        copiedDefinitionProperty.setOwner((AbstractProject) job);
        pdProperties.add(copiedDefinitionProperty);
    }
    projectProperty.setValue(pdProperties);
    Set<String> cascadingChildrenNames = job.getCascadingChildrenNames();
    //Iterate through cascading children and recursively update property for each child.
    if (null != cascadingChildrenNames) {
        for (String childName : cascadingChildrenNames) {
            Job childJob = (Job) Hudson.getInstance().getItem(childName);
            //Check only direct children in order to avoid deep checking for properties overridden properties.
            if (null != childJob && StringUtils.equals(job.getName(), childJob.getCascadingProjectName())) {
                CopyOnWriteListProjectProperty childProperty = getCopyOnWriteListProjectProperty(childJob, key);
                //If child value is equal to parent - mark this value as unmodified.
                if (!childProperty.allowOverrideValue(childProperty.getValue(), pdProperties)) {
                    childProperty.setOverridden(false);
                } else if (!childProperty.isOverridden()) {
                    //If child property was not overridden, update this property and cascading children if any.
                    setParameterDefinitionProperties(childJob, key, parameterDefinitionProperties);
                }
            }
        }
    }
}
Also used : ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) CopyOnWriteListProjectProperty(org.hudsonci.model.project.property.CopyOnWriteListProjectProperty) ICascadingJob(org.hudsonci.api.model.ICascadingJob) Job(hudson.model.Job) ParameterDefinition(hudson.model.ParameterDefinition)

Example 2 with CopyOnWriteListProjectProperty

use of org.hudsonci.model.project.property.CopyOnWriteListProjectProperty in project hudson-2.x by hudson.

the class LegacyProjectTest method testConvertLegacyParameterDefinitionProperties.

@Test
@SuppressWarnings("unchecked")
public void testConvertLegacyParameterDefinitionProperties() throws IOException {
    Job project = (Job) Items.getConfigFile(config).read();
    project.setAllowSave(false);
    project.initProjectProperties();
    assertNull(project.getProperty(Job.PARAMETERS_DEFINITION_JOB_PROPERTY_PROPERTY_NAME));
    project.convertJobProperties();
    //Properties should be initialized
    assertNotNull(project.properties);
    CopyOnWriteListProjectProperty property = (CopyOnWriteListProjectProperty) project.getProperty(Job.PARAMETERS_DEFINITION_JOB_PROPERTY_PROPERTY_NAME);
    assertNotNull(property);
    CopyOnWriteList<ParametersDefinitionProperty> propertyValue = property.getValue();
    assertFalse(propertyValue.isEmpty());
    assertEquals(1, propertyValue.size());
    ParametersDefinitionProperty pdp = propertyValue.get(0);
    List<ParameterDefinition> parameterDefinitions = pdp.getParameterDefinitions();
    assertNotNull(parameterDefinitions);
    assertFalse(parameterDefinitions.isEmpty());
    String parameterName1 = "test1";
    String parameterName2 = "test2";
    assertTrue(pdp.getParameterDefinitionNames().contains(parameterName1));
    assertTrue(pdp.getParameterDefinitionNames().contains(parameterName2));
    ParameterDefinition parameterDefinition = pdp.getParameterDefinition(parameterName1);
    assertNotNull(parameterDefinition);
    assertTrue(parameterDefinition instanceof StringParameterDefinition);
    assertEquals(parameterName1, parameterDefinition.getName());
    assertEquals("df1", ((StringParameterDefinition) parameterDefinition).getDefaultValue());
    assertEquals("string value description", parameterDefinition.getDescription());
    parameterDefinition = pdp.getParameterDefinition(parameterName2);
    assertNotNull(parameterDefinition);
    assertTrue(parameterDefinition instanceof BooleanParameterDefinition);
    assertEquals(parameterName2, parameterDefinition.getName());
    assertEquals(true, ((BooleanParameterDefinition) parameterDefinition).isDefaultValue());
    assertEquals("boolean value description", parameterDefinition.getDescription());
}
Also used : CopyOnWriteListProjectProperty(org.hudsonci.model.project.property.CopyOnWriteListProjectProperty) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

CopyOnWriteListProjectProperty (org.hudsonci.model.project.property.CopyOnWriteListProjectProperty)2 Job (hudson.model.Job)1 ParameterDefinition (hudson.model.ParameterDefinition)1 ParametersDefinitionProperty (hudson.model.ParametersDefinitionProperty)1 ICascadingJob (org.hudsonci.api.model.ICascadingJob)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1