CourseCompareByDeptAndCatalog implements Comparator<Course> { @Override public int compare(Course c1, Course c2) { if((null == c1) && (null == c2)){ return 0;} if(null == c1){ return -1;} if(null == c2){ return +1;} if(c1.getCourseDepartment() == c2.getCourseDepartment()) { if(c1.getCode() == c2.getCode()) return 0; if(null == c1.getCode()) return -1; if(null == c2.getCode()) return +1; return (c1.getCode().compareTo(c2.getCode())); } if(null == c1.getCourseDepartment()) return -1; if(null == c2.getCourseDepartment()) return +1; if(c1.getCourseDepartment().getName() == c2.getCourseDepartment().getName()) { if(c1.getCode() == c2.getCode()) return 0; if(null == c1.getCode()) return -1; if(null == c2.getCode()) return +1; return (c1.getCode().compareTo(c2.getCode())); } if(null == c1.getCourseDepartment().getName()) return -1; if(null == c2.getCourseDepartment().getName()) return +1; if(! ( c1.getCourseDepartment().getName().equals((c2.getCourseDepartment().getName())) ) ) { return (c1.getCourseDepartment().getName().compareTo(c2.getCourseDepartment().getName())); } else { if(c1.getCode() == c2.getCode()) return 0; if(null == c1.getCode()) return -1; if(null == c2.getCode()) return +1; return (c1.getCode().compareTo(c2.getCode())); } }  @Override int compare(Course c1, Course c2);  }
@Test public void testCompareDepartmentCodeSameFirstNameNull() { Department dept = new Department(); dept.setCode("195"); Course mikeCourse = new Course(); mikeCourse.setCourseDepartment(dept); Department dept2 = new Department(); dept2.setCode("195"); dept2.setName("E ASIAN"); Course anotherCourse = new Course(); anotherCourse.setCourseDepartment(dept2); assertEquals("They are not equal",-1, new CourseCompareByDeptAndCatalog().compare(mikeCourse,anotherCourse)); }
@Test public void testCompareDepartmentCodeSameSecondNameNull() { Department dept = new Department(); dept.setCode("195"); dept.setName("E ASIAN"); Course mikeCourse = new Course(); mikeCourse.setCourseDepartment(dept); Department dept2 = new Department(); dept2.setCode("195"); Course anotherCourse = new Course(); anotherCourse.setCourseDepartment(dept2); assertEquals("They are not equal",+1, new CourseCompareByDeptAndCatalog().compare(mikeCourse,anotherCourse)); }
@Test public void testCompareDepartmentCodeSameBothNamesNull() { Department dept = new Department(); dept.setCode("195"); Course mikeCourse = new Course(); mikeCourse.setCourseDepartment(dept); Department dept2 = new Department(); dept2.setCode("195"); Course anotherCourse = new Course(); anotherCourse.setCourseDepartment(dept2); assertEquals("They are not equal",0, new CourseCompareByDeptAndCatalog().compare(mikeCourse,anotherCourse)); }
@Test public void testCompareDepartmentCodeSameBothNamesNullFirstCourseCodeNull() { Department dept = new Department(); dept.setCode("195"); Course mikeCourse = new Course(); mikeCourse.setCourseDepartment(dept); Department dept2 = new Department(); dept2.setCode("195"); Course anotherCourse = new Course(); anotherCourse.setCourseDepartment(dept2); anotherCourse.setCode("399"); assertEquals("They are not equal",-1, new CourseCompareByDeptAndCatalog().compare(mikeCourse,anotherCourse)); }
@Test public void testCompareDepartmentCodeSameBothNamesNullSecondCourseCodeNull() { Department dept = new Department(); dept.setCode("195"); Course mikeCourse = new Course(); mikeCourse.setCourseDepartment(dept); mikeCourse.setCode("399"); Department dept2 = new Department(); dept2.setCode("195"); Course anotherCourse = new Course(); anotherCourse.setCourseDepartment(dept2); assertEquals("They are not equal",+1, new CourseCompareByDeptAndCatalog().compare(mikeCourse,anotherCourse)); }
@Test public void testCompare() { Department dept = new Department(); dept.setCode("195"); dept.setName("E ASIAN"); Course senCourse = null; Course colinCourse = null; Course mikeCourse = new Course(); mikeCourse.setCode("404"); mikeCourse.setCourseDepartment(dept); Course anotherCourse = new Course(); anotherCourse.setCode("301"); anotherCourse.setCourseDepartment(dept); Course yetAnotherCourse = new Course(); yetAnotherCourse.setCode("301"); yetAnotherCourse.setCourseDepartment(dept); Course yetAnotherCourse2 = new Course(); assertEquals("They are not equal",0, new CourseCompareByDeptAndCatalog().compare(senCourse,colinCourse)); assertEquals("They are not equal",-1, new CourseCompareByDeptAndCatalog().compare(senCourse,mikeCourse)); assertEquals("They are not equal",+1, new CourseCompareByDeptAndCatalog().compare(mikeCourse,senCourse)); assertEquals("They are not equal",0, new CourseCompareByDeptAndCatalog().compare(anotherCourse,yetAnotherCourse)); assertEquals("They are not equal",+1, new CourseCompareByDeptAndCatalog().compare(mikeCourse,yetAnotherCourse)); assertEquals("They are not equal",-1, new CourseCompareByDeptAndCatalog().compare(yetAnotherCourse,mikeCourse)); assertEquals("They are not equal",+1, new CourseCompareByDeptAndCatalog().compare(yetAnotherCourse,yetAnotherCourse2)); assertEquals("They are not equal",-1, new CourseCompareByDeptAndCatalog().compare(yetAnotherCourse2,yetAnotherCourse)); }
@Test public void testCompareDepartmentSameCourseCodeDifferentSecondNull() { Department dept = new Department(); dept.setCode("195"); dept.setName("E ASIAN"); Course mikeCourse = new Course(); mikeCourse.setCode("404"); mikeCourse.setCourseDepartment(dept); Course anotherCourse = new Course(); anotherCourse.setCourseDepartment(dept); assertEquals("They are not equal",+1, new CourseCompareByDeptAndCatalog().compare(mikeCourse,anotherCourse)); }
@Test public void testCompareDepartmentSameCourseCodeDifferentFirstNull() { Department dept = new Department(); dept.setCode("195"); dept.setName("E ASIAN"); Course mikeCourse = new Course(); mikeCourse.setCourseDepartment(dept); Course anotherCourse = new Course(); anotherCourse.setCode("404"); anotherCourse.setCourseDepartment(dept); assertEquals("They are not equal",-1, new CourseCompareByDeptAndCatalog().compare(mikeCourse,anotherCourse)); }
@Test public void testCompareDepartmentSameCourseCodeNullForBoth() { Department dept = new Department(); dept.setCode("195"); dept.setName("E ASIAN"); Course mikeCourse = new Course(); mikeCourse.setCourseDepartment(dept); Course anotherCourse = new Course(); anotherCourse.setCourseDepartment(dept); assertEquals("They are not equal",0, new CourseCompareByDeptAndCatalog().compare(mikeCourse,anotherCourse)); }