ComparableOrToStringComparator implements Comparator<Object>, Serializable { @Override public int compare(final Object vertex1, final Object vertex2) { if (null == vertex1) { if (null == vertex2) { return 0; } return 1; } if (null == vertex2) { return -1; } if (vertex1 instanceof Comparable && vertex2.getClass().equals(vertex1.getClass())) { return ((Comparable) vertex1).compareTo(vertex2); } final String toString1; if (vertex1.getClass().isArray()) { toString1 = Arrays.toString((Object[]) vertex1); } else { toString1 = vertex1.toString(); } final String toString2; if (vertex2.getClass().isArray()) { toString2 = Arrays.toString((Object[]) vertex2); } else { toString2 = vertex2.toString(); } return toString1.compareTo(toString2); }  @Override int compare(final Object vertex1, final Object vertex2);  }
@Test public void shouldMatchStringComparison() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare("1", "2"); assertTrue(result < 0, "Both should be less than 0"); }
@Test public void shouldMatchStringComparisonInReverse() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare("2", "1"); assertTrue(result > 0, "Both should be less than 0"); }
@Test public void shouldCompareEqualStrings() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare("1", "1"); assertEquals(0, result); }
@Test public void shouldCompareNulls() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare(null, null); assertEquals(0, result); }
@Test public void shouldCompareNullWithValue() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare(null, 1); assertTrue(result > 0, "Should be more than 0"); }
@Test public void shouldCompareValueWithNull() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare(1, null); assertTrue(result < 0, "Should be less than 0"); }
@Test public void shouldCompareIntegers() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare(1, 2); assertTrue(result < 0, "Should be less than 0"); }
@Test public void shouldCompareIntegersReversed() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare(2, 1); assertTrue(result > 0, "Should be more than 0"); }
@Test public void shouldCompareEqualIntegers() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare(1, 1); assertEquals(0, result); }
@Test public void shouldCompareEqualArrayWithString() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare(new Integer[] {1, 1}, "[1, 1]"); assertEquals(0, result); }
@Test public void shouldCompareIntegerArrays() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare(new Integer[] {1, 1}, new Integer[] {1, 2}); assertTrue(result < 0, "Should be less than 0"); }
@Test public void shouldCompareIntegerArraysReversed() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare(new Integer[] {1, 2}, new Integer[] {1, 1}); assertTrue(result > 0, "Should be more than 0"); }
@Test public void shouldCompareEqualIntegerArrays() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare(new Integer[] {1, 2}, new Integer[] {1, 2}); assertEquals(0, result); }
@Test public void shouldCompareCustomObjUsingToString() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare(new IntegerWrapper(1), new IntegerWrapper(2)); assertTrue(result < 0, "Should be less than 0"); }
@Test public void shouldCompareCustomObjUsingToStringReversed() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare(new IntegerWrapper(2), new IntegerWrapper(1)); assertTrue(result > 0, "Should be more than 0"); }
@Test public void shouldCompareEqualCustomObjUsingToString() { final ComparableOrToStringComparator comparator = new ComparableOrToStringComparator(); final int result = comparator.compare(new IntegerWrapper(1), new IntegerWrapper(1)); assertEquals(0, result); }