public Map<JobPropertyDescriptor, JobProperty<? super InheritanceProject>> getProperties(IMode mode) { List<JobProperty<? super InheritanceProject>> lst = this.getAllProperties(mode); if (lst == null || lst.isEmpty()) { return Collections.emptyMap(); } HashMap<JobPropertyDescriptor, JobProperty<? super InheritanceProject>> map = new HashMap<JobPropertyDescriptor, JobProperty<? super InheritanceProject>>(); for (JobProperty<? super InheritanceProject> prop : lst) { map.put(prop.getDescriptor(), prop); } return map; }
/** * Test persisting jobs with parameters. * * @throws Exception in Jenkins rule */ @Test public void testSaveParameters() throws Exception { FreeStyleProject project = j.createFreeStyleProject(); GroovyScript scriptParam001 = new GroovyScript(new SecureGroovyScript(SCRIPT_PARAM001, false, null), new SecureGroovyScript(SCRIPT_FALLBACK_PARAM001, false, null)); ChoiceParameter param001 = new ChoiceParameter("param001", "param001 description", "random-name", scriptParam001, AbstractUnoChoiceParameter.PARAMETER_TYPE_SINGLE_SELECT, true, 1); GroovyScript scriptParam002 = new GroovyScript(new SecureGroovyScript(SCRIPT_PARAM002, false, null), new SecureGroovyScript(SCRIPT_FALLBACK_PARAM002, false, null)); CascadeChoiceParameter param002 = new CascadeChoiceParameter("param002", "param002 description", "random-name", scriptParam002, AbstractUnoChoiceParameter.PARAMETER_TYPE_SINGLE_SELECT, "param001", true, 1); ParametersDefinitionProperty param001Def = new ParametersDefinitionProperty( Arrays.<ParameterDefinition>asList(param001, param002)); project.addProperty(param001Def); QueueTaskFuture<FreeStyleBuild> future = project.scheduleBuild2(0); FreeStyleBuild build = future.get(); // even though the cascaded parameter will fail to evaluate, we should // still get a success here. assertEquals(Result.SUCCESS, build.getResult()); XmlFile configXml = project.getConfigFile(); FreeStyleProject reReadProject = (FreeStyleProject) configXml.read(); int found = 0; for (Entry<JobPropertyDescriptor, JobProperty<? super FreeStyleProject>> entry : reReadProject.getProperties() .entrySet()) { JobProperty<? super FreeStyleProject> jobProperty = entry.getValue(); if (jobProperty instanceof ParametersDefinitionProperty) { ParametersDefinitionProperty paramDef = (ParametersDefinitionProperty) jobProperty; List<ParameterDefinition> parameters = paramDef.getParameterDefinitions(); for (ParameterDefinition parameter : parameters) { if (parameter instanceof AbstractScriptableParameter) { found++; AbstractScriptableParameter choiceParam = (AbstractScriptableParameter) parameter; String scriptText = ((GroovyScript) choiceParam.getScript()).getScript().getScript(); String fallbackScriptText = ((GroovyScript) choiceParam.getScript()).getFallbackScript() .getScript(); assertTrue("Found an empty script!", StringUtils.isNotBlank(scriptText)); assertTrue("Found an empty fallback script!", StringUtils.isNotBlank(fallbackScriptText)); if (parameter.getName().equals("param001")) { assertEquals(SCRIPT_PARAM001, scriptText); assertEquals(SCRIPT_FALLBACK_PARAM001, fallbackScriptText); } else { assertEquals(SCRIPT_PARAM002, scriptText); assertEquals(SCRIPT_FALLBACK_PARAM002, fallbackScriptText); } } } } } // We have two parameters before saving. We must have two now. assertEquals("Didn't find all parameters after persisting xml", 2, found); }
@Override public Map<JobPropertyDescriptor, JobProperty<? super InheritanceProject>> getProperties() { return this.getProperties(IMode.AUTO); }
public static List<JobPropertyDescriptor> getJobPropertyDescriptors( Class<? extends Job> clazz, boolean filterIsExcluding, String... filters) { List<JobPropertyDescriptor> out = new ArrayList<JobPropertyDescriptor>(); //JobPropertyDescriptor.getPropertyDescriptors(clazz); List<JobPropertyDescriptor> allDesc = Functions.getJobPropertyDescriptors(clazz); for (JobPropertyDescriptor desc : allDesc) { String dName = desc.getClass().getName(); if (filters.length > 0) { boolean matched = false; if (filters != null) { for (String filter : filters) { if (dName.contains(filter)) { matched = true; break; } } } if (filterIsExcluding && matched) { continue; } else if (!filterIsExcluding && !matched) { continue; } } //The class has survived the filter out.add(desc); } //At last, we make sure to sort the fields by full name; to ensure //that properties from the same package/plugin are next to each other Collections.sort(out, new Comparator<JobPropertyDescriptor>() { @Override public int compare(JobPropertyDescriptor o1, JobPropertyDescriptor o2) { String c1 = o1.getClass().getName(); String c2 = o2.getClass().getName(); return c1.compareTo(c2); } }); return out; }
@Override public JobPropertyDescriptor getDescriptor() { return DESCRIPTOR; }
/** * We need to override this method do prevent Jenkins from trying to * register this class as a "real" property worthy of inclusion in the * configuration view. * <p> * This is necessary, because this class is only a pure wrapper around * {@link ParametersDefinitionProperty} and does not own any properties * that need to be stored separately. * <p> * Unfortunately; not defining a Descriptor at all would lead to this * class not being able to completely wrap the * {@link ParametersDefinitionProperty} class. */ @Override public JobPropertyDescriptor getDescriptor() { //return super.getDescriptor(); return (JobPropertyDescriptor) Jenkins.getInstance().getDescriptorOrDie( ParametersDefinitionProperty.class ); }