Search in sources :

Example 16 with Descriptor

use of hudson.model.Descriptor in project promoted-builds-plugin by jenkinsci.

the class ManualConditionInheritanceTest method testManualPromotionProcessViaWebClient.

@Test
public void testManualPromotionProcessViaWebClient() throws Exception {
    InheritanceProjectsPair inheritanceProjectsPair = j.createInheritanceProjectDerivedWithBase();
    ExtensionList<Descriptor> list = Jenkins.get().getExtensionList(Descriptor.class);
    list.add(new JobPropertyImpl.DescriptorImpl(JobPropertyImpl.class));
    JobPropertyImpl base = new JobPropertyImpl(inheritanceProjectsPair.getBase());
    inheritanceProjectsPair.getDerived().addProperty(base);
    PromotionProcess foo = base.addProcess("foo");
    ManualCondition condition = new ManualCondition();
    condition.getParameterDefinitions().add(new StringParameterDefinition("bogus_string_param_1", "bogus_value_1", "Bog parameter"));
    condition.getParameterDefinitions().add(new StringParameterDefinition("bogus_string_param_2", "bogus_value_2", "Bog parameter"));
    foo.conditions.add(condition);
    InheritanceBuild b1 = j.assertBuildStatusSuccess(inheritanceProjectsPair.getDerived().scheduleBuild2(0));
    assertNull(b1.getAction(ManualApproval.class));
    HtmlPage page = j.createWebClient().getPage(b1, "promotion");
    // Approve Promotion
    List<HtmlForm> forms = getFormsByName(page, "approve");
    assertFalse(forms.isEmpty());
    assertEquals(1, forms.size());
    HtmlForm form = forms.get(0);
    List<HtmlElement> parameters = getFormParameters(form);
    assertEquals(parameters.size(), condition.getParameterDefinitions().size());
    for (HtmlElement param : parameters) {
        HtmlElement v = param.getElementsByAttribute("input", "name", "value").get(0);
        v.setAttribute("value", v.getAttribute("value") + "1");
    }
    j.submit(forms.get(0));
    j.waitUntilNoActivity();
    // We cannot assume that the process will contain builds because the process added to base project is different to the one in derived.
    final JobPropertyImpl jobProperty = inheritanceProjectsPair.getDerived().getProperty(JobPropertyImpl.class, /*Forcing inheritance as temporary hack for inheritance plugin 1.53 
                because that version of the plugin uses inheritance only for certain predefined cases: 
                -specific methods on the call stack
                -url paths.
                This has been changed as pull request https://github.com/i-m-c/jenkins-inheritance-plugin/pull/40
               */
    IMode.INHERIT_FORCED);
    assertNotNull("derived jobProperty is null", jobProperty);
    final PromotionProcess fooDerived = jobProperty.getItem("foo");
    ManualApproval approval = b1.getAction(ManualApproval.class);
    assertNotNull(approval);
    SortedMap<Integer, Promotion> builds = fooDerived.getBuildsAsMap();
    assertNotNull(builds);
    assertEquals(1, builds.size());
    // Re-Execute approved promotion
    page = j.createWebClient().getPage(b1, "promotion");
    forms = getFormsByName(page, "build");
    assertFalse(forms.isEmpty());
    assertEquals(1, forms.size());
    form = forms.get(0);
    parameters = getFormParameters(form);
    assertEquals(parameters.size(), condition.getParameterDefinitions().size());
    for (HtmlElement param : parameters) {
        HtmlElement v = param.getElementsByAttribute("input", "name", "value").get(0);
        v.setAttribute("value", v.getAttribute("value") + "2");
    }
    j.submit(form);
    j.waitUntilNoActivity();
    final JobPropertyImpl jobProperty2 = inheritanceProjectsPair.getDerived().getProperty(JobPropertyImpl.class, /*Forcing inheritance as temporary hack for inheritance plugin 1.53 
                  because that version of the plugin uses inheritance only for certain predefined cases: 
                  -specific methods on the call stack
                  -url paths.
                  This has been changed as pull request https://github.com/i-m-c/jenkins-inheritance-plugin/pull/40
                 */
    IMode.INHERIT_FORCED);
    assertNotNull("derived jobProperty is null", jobProperty2);
    final PromotionProcess fooDerived2 = jobProperty2.getItem("foo");
    builds = fooDerived2.getBuildsAsMap();
    assertEquals(2, builds.size());
    List<ManualApproval> actions = b1.getActions(ManualApproval.class);
    assertEquals(1, actions.size());
    PromotedBuildAction buildActions = b1.getAction(PromotedBuildAction.class);
    int buildIndex = 1;
    String valueSufix = "1";
    List<Promotion> promotions = new ArrayList<Promotion>();
    promotions.addAll(builds.values());
    Collections.reverse(promotions);
    for (Promotion build : promotions) {
        List<ParameterDefinition> values = build.getParameterDefinitionsWithValue();
        assertEquals(values.size(), condition.getParameterDefinitions().size());
        for (ParameterDefinition v : values) {
            assertTrue(v instanceof StringParameterDefinition);
            String pvalue = ((StringParameterDefinition) v).getDefaultValue();
            assertTrue(pvalue.endsWith(valueSufix));
        }
        buildIndex++;
        valueSufix += buildIndex;
    }
}
Also used : StringParameterDefinition(hudson.model.StringParameterDefinition) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) Promotion(hudson.plugins.promoted_builds.Promotion) ManualCondition(hudson.plugins.promoted_builds.conditions.ManualCondition) HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) PromotionProcess(hudson.plugins.promoted_builds.PromotionProcess) InheritanceProjectsPair(hudson.plugins.promoted_builds.inheritance.helpers.InheritanceProjectsPair) ManualApproval(hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval) InheritanceBuild(hudson.plugins.project_inheritance.projects.InheritanceBuild) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) PromotedBuildAction(hudson.plugins.promoted_builds.PromotedBuildAction) JobPropertyImpl(hudson.plugins.promoted_builds.JobPropertyImpl) Descriptor(hudson.model.Descriptor) ParameterDefinition(hudson.model.ParameterDefinition) StringParameterDefinition(hudson.model.StringParameterDefinition) Test(org.junit.Test)

Example 17 with Descriptor

use of hudson.model.Descriptor in project hudson-2.x by hudson.

the class CascadingUtilTest method testBuildExternalProperties.

@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();
}
Also used : FreeStyleProjectMock(hudson.model.FreeStyleProjectMock) Hudson(hudson.model.Hudson) StaplerRequest(org.kohsuke.stapler.StaplerRequest) ArrayList(java.util.ArrayList) Publisher(hudson.tasks.Publisher) JSONObject(net.sf.json.JSONObject) Descriptor(hudson.model.Descriptor) JavadocArchiver(hudson.tasks.JavadocArchiver) Job(hudson.model.Job) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 18 with Descriptor

use of hudson.model.Descriptor in project hudson-2.x by hudson.

the class DescriptorExtensionList method load.

/**
 * Loading the descriptors in this case means filtering the descriptor from the master {@link ExtensionList}.
 */
@Override
protected List<ExtensionComponent<D>> load() {
    List<ExtensionComponent<D>> r = new ArrayList<ExtensionComponent<D>>();
    for (ExtensionComponent<Descriptor> c : hudson.getExtensionList(Descriptor.class).getComponents()) {
        Descriptor d = c.getInstance();
        Type subTyping = Types.getBaseClass(d.getClass(), Descriptor.class);
        if (!(subTyping instanceof ParameterizedType)) {
            LOGGER.severe(d.getClass() + " doesn't extend Descriptor with a type parameter");
            // skip this one
            continue;
        }
        if (Types.erasure(Types.getTypeArgument(subTyping, 0)) == (Class) describableType)
            r.add((ExtensionComponent) c);
    }
    return r;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Descriptor(hudson.model.Descriptor) ViewDescriptor(hudson.model.ViewDescriptor) NodeDescriptor(hudson.slaves.NodeDescriptor)

Example 19 with Descriptor

use of hudson.model.Descriptor in project hudson-2.x by hudson.

the class BuildStepDescriptor method filter.

/**
 * Filters a descriptor for {@link BuildStep}s by using {@link BuildStepDescriptor#isApplicable(Class)}.
 */
public static <T extends BuildStep & Describable<T>> List<Descriptor<T>> filter(List<Descriptor<T>> base, Class<? extends AbstractProject> type) {
    // descriptor of the project
    Descriptor pd = Hudson.getInstance().getDescriptor((Class) type);
    List<Descriptor<T>> r = new ArrayList<Descriptor<T>>(base.size());
    for (Descriptor<T> d : base) {
        if (pd instanceof AbstractProjectDescriptor && !((AbstractProjectDescriptor) pd).isApplicable(d))
            continue;
        if (d instanceof BuildStepDescriptor) {
            BuildStepDescriptor<T> bd = (BuildStepDescriptor<T>) d;
            if (!bd.isApplicable(type))
                continue;
            r.add(bd);
        } else {
            // old plugins built before 1.150 may not implement BuildStepDescriptor
            r.add(d);
        }
    }
    return r;
}
Also used : ArrayList(java.util.ArrayList) Descriptor(hudson.model.Descriptor) AbstractProjectDescriptor(hudson.model.AbstractProject.AbstractProjectDescriptor) AbstractProjectDescriptor(hudson.model.AbstractProject.AbstractProjectDescriptor)

Example 20 with Descriptor

use of hudson.model.Descriptor in project hudson-2.x by hudson.

the class BuildWrappers method getFor.

/**
 * List up all {@link BuildWrapperDescriptor}s that are applicable for the given project.
 *
 * @return
 *      The signature doesn't use {@link BuildWrapperDescriptor} to maintain compatibility
 *      with {@link BuildWrapper} implementations before 1.150.
 */
public static List<Descriptor<BuildWrapper>> getFor(AbstractProject<?, ?> project) {
    List<Descriptor<BuildWrapper>> result = new ArrayList<Descriptor<BuildWrapper>>();
    Descriptor pd = Hudson.getInstance().getDescriptor((Class) project.getClass());
    for (Descriptor<BuildWrapper> w : BuildWrapper.all()) {
        if (pd instanceof AbstractProjectDescriptor && !((AbstractProjectDescriptor) pd).isApplicable(w))
            continue;
        if (w instanceof BuildWrapperDescriptor) {
            BuildWrapperDescriptor bwd = (BuildWrapperDescriptor) w;
            if (bwd.isApplicable(project))
                result.add(bwd);
        } else {
            // old BuildWrapper that doesn't implement BuildWrapperDescriptor
            result.add(w);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Descriptor(hudson.model.Descriptor) AbstractProjectDescriptor(hudson.model.AbstractProject.AbstractProjectDescriptor) AbstractProjectDescriptor(hudson.model.AbstractProject.AbstractProjectDescriptor)

Aggregations

Descriptor (hudson.model.Descriptor)22 Test (org.junit.Test)9 JobPropertyImpl (hudson.plugins.promoted_builds.JobPropertyImpl)8 ArrayList (java.util.ArrayList)8 ManualApproval (hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval)7 StringParameterDefinition (hudson.model.StringParameterDefinition)6 PromotedBuildAction (hudson.plugins.promoted_builds.PromotedBuildAction)6 PromotionProcess (hudson.plugins.promoted_builds.PromotionProcess)6 FreeStyleBuild (hudson.model.FreeStyleBuild)5 FreeStyleProject (hudson.model.FreeStyleProject)5 Promotion (hudson.plugins.promoted_builds.Promotion)4 StepDescriptor (org.jenkinsci.plugins.workflow.steps.StepDescriptor)4 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)3 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)3 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)3 AbstractProjectDescriptor (hudson.model.AbstractProject.AbstractProjectDescriptor)3 ParameterDefinition (hudson.model.ParameterDefinition)3 ParameterValue (hudson.model.ParameterValue)3 Jenkins (jenkins.model.Jenkins)3 Describable (hudson.model.Describable)2