1 /* 2 * Copyright (c) 2017 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito.internal.util.reflection; 6 7 import static org.assertj.core.api.Assertions.assertThat; 8 import static org.mockito.internal.util.reflection.SuperTypesLastSorter.sortSuperTypesLast; 9 10 import java.lang.reflect.Field; 11 import java.util.*; 12 13 import org.junit.Test; 14 15 @SuppressWarnings("unused") 16 public class SuperTypesLastSorterTest { 17 /** 18 * A Comparator that behaves like the old one, so the existing tests 19 * continue to work. 20 */ 21 private static Comparator<Field> cmp = 22 new Comparator<Field>() { 23 public int compare(Field o1, Field o2) { 24 if (o1.equals(o2)) { 25 return 0; 26 } 27 28 List<Field> l = sortSuperTypesLast(Arrays.asList(o1, o2)); 29 30 if (l.get(0) == o1) { 31 return -1; 32 } else { 33 return 1; 34 } 35 } 36 }; 37 38 private Object objectA; 39 private Object objectB; 40 41 private Number numberA; 42 private Number numberB; 43 44 private Integer integerA; 45 private Integer integerB; 46 47 private Iterable<?> iterableA; 48 49 private Number xNumber; 50 private Iterable<?> yIterable; 51 private Integer zInteger; 52 53 @Test when_same_type_the_order_is_based_on_field_name()54 public void when_same_type_the_order_is_based_on_field_name() throws Exception { 55 assertThat(cmp.compare(field("objectA"), field("objectB"))).isEqualTo(-1); 56 assertThat(cmp.compare(field("objectB"), field("objectA"))).isEqualTo(1); 57 assertThat(cmp.compare(field("objectB"), field("objectB"))).isEqualTo(0); 58 } 59 60 @Test when_type_is_different_the_supertype_comes_last()61 public void when_type_is_different_the_supertype_comes_last() throws Exception { 62 assertThat(cmp.compare(field("numberA"), field("objectB"))).isEqualTo(-1); 63 assertThat(cmp.compare(field("objectB"), field("numberA"))).isEqualTo(1); 64 } 65 66 @Test using_Collections_dot_sort()67 public void using_Collections_dot_sort() throws Exception { 68 List<Field> unsortedFields = 69 Arrays.asList( 70 field("objectB"), 71 field("integerB"), 72 field("numberA"), 73 field("numberB"), 74 field("objectA"), 75 field("integerA")); 76 77 List<Field> sortedFields = sortSuperTypesLast(unsortedFields); 78 79 assertThat(sortedFields) 80 .containsSequence( 81 field("integerA"), 82 field("integerB"), 83 field("numberA"), 84 field("numberB"), 85 field("objectA"), 86 field("objectB")); 87 } 88 89 @Test issue_352_order_was_different_between_JDK6_and_JDK7()90 public void issue_352_order_was_different_between_JDK6_and_JDK7() throws Exception { 91 List<Field> unsortedFields = Arrays.asList(field("objectB"), field("objectA")); 92 93 Collections.sort(unsortedFields, cmp); 94 95 assertThat(unsortedFields).containsSequence(field("objectA"), field("objectB")); 96 } 97 98 @Test fields_sort_consistently_when_interfaces_are_included()99 public void fields_sort_consistently_when_interfaces_are_included() 100 throws NoSuchFieldException { 101 assertSortConsistently(field("iterableA"), field("numberA"), field("integerA")); 102 } 103 104 @Test fields_sort_consistently_when_names_and_type_indicate_different_order()105 public void fields_sort_consistently_when_names_and_type_indicate_different_order() 106 throws NoSuchFieldException { 107 assertSortConsistently(field("xNumber"), field("yIterable"), field("zInteger")); 108 } 109 110 /** 111 * Assert that these fields sort in the same order no matter which order 112 * they start in. 113 */ assertSortConsistently(Field a, Field b, Field c)114 private static void assertSortConsistently(Field a, Field b, Field c) { 115 Field[][] initialOrderings = { 116 {a, b, c}, 117 {a, c, b}, 118 {b, a, c}, 119 {b, c, a}, 120 {c, a, b}, 121 {c, b, a} 122 }; 123 124 Set<List<Field>> results = new HashSet<List<Field>>(); 125 126 for (Field[] o : initialOrderings) { 127 results.add(sortSuperTypesLast(Arrays.asList(o))); 128 } 129 130 assertThat(results).hasSize(1); 131 } 132 field(String field)133 private Field field(String field) throws NoSuchFieldException { 134 return getClass().getDeclaredField(field); 135 } 136 } 137