@Test @PrepareForTest(Hudson.class) public void testHasCyclicCascadingLink() throws Exception { FreeStyleProject project1 = new FreeStyleProjectMock("p1"); FreeStyleProjectMock project2 = new FreeStyleProjectMock("p2"); FreeStyleProjectMock project3 = new FreeStyleProjectMock("p3"); FreeStyleProjectMock project4 = new FreeStyleProjectMock("p4"); List<Job> jobs = new ArrayList<Job>(); jobs.add(project1); jobs.add(project2); jobs.add(project3); jobs.add(project4); Hudson hudson = createMock(Hudson.class); mockStatic(Hudson.class); expect(hudson.getAllItems(Job.class)).andReturn(jobs).anyTimes(); expect(Hudson.getInstance()).andReturn(hudson).anyTimes(); replay(Hudson.class, hudson); project2.setCascadingProject(project1); CascadingUtil.linkCascadingProjectsToChild(project1, "p2"); project3.setCascadingProject(project1); CascadingUtil.linkCascadingProjectsToChild(project1, "p3"); project4.setCascadingProject(project3); CascadingUtil.linkCascadingProjectsToChild(project3, "p4"); verifyCyclicCascadingLink(true, project2, project1); verifyCyclicCascadingLink(true, project3, project1); verifyCyclicCascadingLink(true, project4, project3); verifyCyclicCascadingLink(true, project4, project1); verifyCyclicCascadingLink(false, project3, project2); verifyCyclicCascadingLink(false, project1, project2); verifyCyclicCascadingLink(false, project2, project3); verifyCyclicCascadingLink(false, null, project1); verifyCyclicCascadingLink(false, null, project4); verify(Hudson.class, hudson); }
@SuppressWarnings("unchecked") public static boolean hasCyclicCascadingLink(Job cascadingCandidate, Set<String> cascadingChildren) { if (null != cascadingCandidate && CollectionUtils.isNotEmpty(cascadingChildren)) { if (cascadingChildren.contains(cascadingCandidate.getName())) { return true; } for (String childName : cascadingChildren) { Job job = Functions.getItemByName(Hudson.getInstance().getAllItems(Job.class), childName); if (null != job && hasCyclicCascadingLink(cascadingCandidate, job.getCascadingChildrenNames())) { return true; } } } return false; }
CascadingUtil { @SuppressWarnings("unchecked") public static boolean hasCyclicCascadingLink(Job cascadingCandidate, Set<String> cascadingChildren) { if (null != cascadingCandidate && CollectionUtils.isNotEmpty(cascadingChildren)) { if (cascadingChildren.contains(cascadingCandidate.getName())) { return true; } for (String childName : cascadingChildren) { Job job = Functions.getItemByName(Hudson.getInstance().getAllItems(Job.class), childName); if (null != job && hasCyclicCascadingLink(cascadingCandidate, job.getCascadingChildrenNames())) { return true; } } } return false; } }
CascadingUtil { @SuppressWarnings("unchecked") public static boolean hasCyclicCascadingLink(Job cascadingCandidate, Set<String> cascadingChildren) { if (null != cascadingCandidate && CollectionUtils.isNotEmpty(cascadingChildren)) { if (cascadingChildren.contains(cascadingCandidate.getName())) { return true; } for (String childName : cascadingChildren) { Job job = Functions.getItemByName(Hudson.getInstance().getAllItems(Job.class), childName); if (null != job && hasCyclicCascadingLink(cascadingCandidate, job.getCascadingChildrenNames())) { return true; } } } return false; }  }
CascadingUtil { @SuppressWarnings("unchecked") public static boolean hasCyclicCascadingLink(Job cascadingCandidate, Set<String> cascadingChildren) { if (null != cascadingCandidate && CollectionUtils.isNotEmpty(cascadingChildren)) { if (cascadingChildren.contains(cascadingCandidate.getName())) { return true; } for (String childName : cascadingChildren) { Job job = Functions.getItemByName(Hudson.getInstance().getAllItems(Job.class), childName); if (null != job && hasCyclicCascadingLink(cascadingCandidate, job.getCascadingChildrenNames())) { return true; } } } return false; }  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 boolean hasCyclicCascadingLink(Job cascadingCandidate, Set<String> cascadingChildren) { if (null != cascadingCandidate && CollectionUtils.isNotEmpty(cascadingChildren)) { if (cascadingChildren.contains(cascadingCandidate.getName())) { return true; } for (String childName : cascadingChildren) { Job job = Functions.getItemByName(Hudson.getInstance().getAllItems(Job.class), childName); if (null != job && hasCyclicCascadingLink(cascadingCandidate, job.getCascadingChildrenNames())) { return true; } } } return false; }  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);  }