AnnotationComparator implements Comparator<Property<T>>, Serializable { @Override public int compare(Property<T> prop1, Property<T> prop2) { int prop1Rank = fieldRanking.get(prop1.getName()); int prop2Rank = fieldRanking.get(prop2.getName()); if (prop1Rank < prop2Rank) { return -1; } else if (prop1Rank == prop2Rank) { return 0; } else { return 1; } } AnnotationComparator(Class<T> instanceClazz); @Override int compare(Property<T> prop1, Property<T> prop2);  }
@Test public void compareJsonPropertyOrderLessThan() { AnnotationComparator<BeanWithJsonPropertyOrder> comparator = new AnnotationComparator<>(BeanWithJsonPropertyOrder.class); BeanWithJsonPropertyOrder model = new BeanWithJsonPropertyOrder(); Property<BeanWithJsonPropertyOrder> first = new CatnapProperty<>(model, ClassUtil.getReadableProperty("first", BeanWithJsonPropertyOrder.class)); Property<BeanWithJsonPropertyOrder> second = new CatnapProperty<>(model, ClassUtil.getReadableProperty("second", BeanWithJsonPropertyOrder.class)); assertEquals(-1, comparator.compare(second, first)); }
@Test public void compareJsonPropertyOrderEquals() { AnnotationComparator<BeanWithJsonPropertyOrder> comparator = new AnnotationComparator<>(BeanWithJsonPropertyOrder.class); BeanWithJsonPropertyOrder model = new BeanWithJsonPropertyOrder(); Property<BeanWithJsonPropertyOrder> first = new CatnapProperty<>(model, ClassUtil.getReadableProperty("first", BeanWithJsonPropertyOrder.class)); assertEquals(0, comparator.compare(first, first)); }
@Test public void compareJsonPropertyOrderGreaterThan() { AnnotationComparator<BeanWithJsonPropertyOrder> comparator = new AnnotationComparator<>(BeanWithJsonPropertyOrder.class); BeanWithJsonPropertyOrder model = new BeanWithJsonPropertyOrder(); Property<BeanWithJsonPropertyOrder> first = new CatnapProperty<>(model, ClassUtil.getReadableProperty("first", BeanWithJsonPropertyOrder.class)); Property<BeanWithJsonPropertyOrder> second = new CatnapProperty<>(model, ClassUtil.getReadableProperty("second", BeanWithJsonPropertyOrder.class)); assertEquals(1, comparator.compare(first, second)); }
@Test(expected = CatnapException.class) public void verifyBeanWithMissingOrderAnnotationThrowsException() { AnnotationComparator<BeanWithoutOrderAnnotation> comparator = new AnnotationComparator<>(BeanWithoutOrderAnnotation.class); BeanWithoutOrderAnnotation model = new BeanWithoutOrderAnnotation(); Property<BeanWithoutOrderAnnotation> first = new CatnapProperty<>(model, ClassUtil.getReadableProperty("first", BeanWithoutOrderAnnotation.class)); Property<BeanWithoutOrderAnnotation> second = new CatnapProperty<>(model, ClassUtil.getReadableProperty("second", BeanWithoutOrderAnnotation.class)); comparator.compare(first, second); }