@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(); }
@SuppressWarnings("unchecked") public static <T extends Describable<T>> void buildExternalProperties(StaplerRequest req, JSONObject json, List<Descriptor<T>> descriptors, Job owner) throws Descriptor.FormException { for (Descriptor d : descriptors) { String name = d.getJsonSafeClassName(); ExternalProjectProperty<Describable> baseProperty = getExternalProjectProperty(owner, name); Describable describable = null; if (json.has(name)) { describable = d.newInstance(req, json.getJSONObject(name)); } baseProperty.setValue(describable); } }
CascadingUtil { @SuppressWarnings("unchecked") public static <T extends Describable<T>> void buildExternalProperties(StaplerRequest req, JSONObject json, List<Descriptor<T>> descriptors, Job owner) throws Descriptor.FormException { for (Descriptor d : descriptors) { String name = d.getJsonSafeClassName(); ExternalProjectProperty<Describable> baseProperty = getExternalProjectProperty(owner, name); Describable describable = null; if (json.has(name)) { describable = d.newInstance(req, json.getJSONObject(name)); } baseProperty.setValue(describable); } } }
CascadingUtil { @SuppressWarnings("unchecked") public static <T extends Describable<T>> void buildExternalProperties(StaplerRequest req, JSONObject json, List<Descriptor<T>> descriptors, Job owner) throws Descriptor.FormException { for (Descriptor d : descriptors) { String name = d.getJsonSafeClassName(); ExternalProjectProperty<Describable> baseProperty = getExternalProjectProperty(owner, name); Describable describable = null; if (json.has(name)) { describable = d.newInstance(req, json.getJSONObject(name)); } baseProperty.setValue(describable); } }  }
CascadingUtil { @SuppressWarnings("unchecked") public static <T extends Describable<T>> void buildExternalProperties(StaplerRequest req, JSONObject json, List<Descriptor<T>> descriptors, Job owner) throws Descriptor.FormException { for (Descriptor d : descriptors) { String name = d.getJsonSafeClassName(); ExternalProjectProperty<Describable> baseProperty = getExternalProjectProperty(owner, name); Describable describable = null; if (json.has(name)) { describable = d.newInstance(req, json.getJSONObject(name)); } baseProperty.setValue(describable); } }  static IProjectProperty getProjectProperty(Job currentJob, String key); static StringProjectProperty getStringProjectProperty(Job currentJob, String key); static BaseProjectProperty getBaseProjectProperty(Job currentJob, String key); static ExternalProjectProperty getExternalProjectProperty(Job currentJob, String key); static CopyOnWriteListProjectProperty getCopyOnWriteListProjectProperty(Job currentJob, String key); static ResultProjectProperty getResultProjectProperty(Job currentJob, String key); static BooleanProjectProperty getBooleanProjectProperty(Job currentJob, String key); static IntegerProjectProperty getIntegerProjectProperty(Job currentJob, String key); static LogRotatorProjectProperty getLogRotatorProjectProperty(Job currentJob, String key); static DescribableListProjectProperty getDescribableListProjectProperty(Job currentJob, String key); static AxisListProjectProperty getAxesListProjectProperty(Job currentJob, String key); static SCMProjectProperty getScmProjectProperty(Job currentJob, String key); static TriggerProjectProperty getTriggerProjectProperty(Job currentJob, String key); @SuppressWarnings("unchecked") static T getProjectProperty(ICascadingJob currentJob, String key, Class<T> clazz); @SuppressWarnings("unchecked") static boolean hasCyclicCascadingLink(Job cascadingCandidate, Set<String> cascadingChildren); static boolean unlinkProjectFromCascadingParents(ICascadingJob cascadingProject, String projectToUnlink); static void linkCascadingProjectsToChild(ICascadingJob cascadingProject, String childProjectName); static void renameCascadingChildLinks(ICascadingJob cascadingProject, String oldName, String newName); static void renameCascadingParentLinks(final String oldName, final String newName); @SuppressWarnings("unchecked") static List<Job> getCascadingParents(Class<T> type, Job currentJob); @SuppressWarnings("unchecked") static void buildExternalProperties(StaplerRequest req, JSONObject json,
                                                                          List<Descriptor<T>> descriptors, Job owner); @SuppressWarnings("unchecked") static void setChildrenTrigger(Job job, TriggerDescriptor descriptor, String key, StaplerRequest req,
                                          JSONObject json); @SuppressWarnings("unchecked") static void setParameterDefinitionProperties(Job job,
                                                        String key,
                                                        CopyOnWriteList<ParametersDefinitionProperty> parameterDefinitionProperties); static boolean isCascadableJobProperty(JobPropertyDescriptor d); }
CascadingUtil { @SuppressWarnings("unchecked") public static <T extends Describable<T>> void buildExternalProperties(StaplerRequest req, JSONObject json, List<Descriptor<T>> descriptors, Job owner) throws Descriptor.FormException { for (Descriptor d : descriptors) { String name = d.getJsonSafeClassName(); ExternalProjectProperty<Describable> baseProperty = getExternalProjectProperty(owner, name); Describable describable = null; if (json.has(name)) { describable = d.newInstance(req, json.getJSONObject(name)); } baseProperty.setValue(describable); } }  static IProjectProperty getProjectProperty(Job currentJob, String key); static StringProjectProperty getStringProjectProperty(Job currentJob, String key); static BaseProjectProperty getBaseProjectProperty(Job currentJob, String key); static ExternalProjectProperty getExternalProjectProperty(Job currentJob, String key); static CopyOnWriteListProjectProperty getCopyOnWriteListProjectProperty(Job currentJob, String key); static ResultProjectProperty getResultProjectProperty(Job currentJob, String key); static BooleanProjectProperty getBooleanProjectProperty(Job currentJob, String key); static IntegerProjectProperty getIntegerProjectProperty(Job currentJob, String key); static LogRotatorProjectProperty getLogRotatorProjectProperty(Job currentJob, String key); static DescribableListProjectProperty getDescribableListProjectProperty(Job currentJob, String key); static AxisListProjectProperty getAxesListProjectProperty(Job currentJob, String key); static SCMProjectProperty getScmProjectProperty(Job currentJob, String key); static TriggerProjectProperty getTriggerProjectProperty(Job currentJob, String key); @SuppressWarnings("unchecked") static T getProjectProperty(ICascadingJob currentJob, String key, Class<T> clazz); @SuppressWarnings("unchecked") static boolean hasCyclicCascadingLink(Job cascadingCandidate, Set<String> cascadingChildren); static boolean unlinkProjectFromCascadingParents(ICascadingJob cascadingProject, String projectToUnlink); static void linkCascadingProjectsToChild(ICascadingJob cascadingProject, String childProjectName); static void renameCascadingChildLinks(ICascadingJob cascadingProject, String oldName, String newName); static void renameCascadingParentLinks(final String oldName, final String newName); @SuppressWarnings("unchecked") static List<Job> getCascadingParents(Class<T> type, Job currentJob); @SuppressWarnings("unchecked") static void buildExternalProperties(StaplerRequest req, JSONObject json,
                                                                          List<Descriptor<T>> descriptors, Job owner); @SuppressWarnings("unchecked") static void setChildrenTrigger(Job job, TriggerDescriptor descriptor, String key, StaplerRequest req,
                                          JSONObject json); @SuppressWarnings("unchecked") static void setParameterDefinitionProperties(Job job,
                                                        String key,
                                                        CopyOnWriteList<ParametersDefinitionProperty> parameterDefinitionProperties); static boolean isCascadableJobProperty(JobPropertyDescriptor d);  }