11package ch .jalu .typeresolver ;
22
3- import ch .jalu .typeresolver .JavaVersionHelper .ConstableAndConstantDescTypes ;
43import ch .jalu .typeresolver .array .ArrayTypeUtils ;
54import ch .jalu .typeresolver .reference .TypeReference ;
5+ import ch .jalu .typeresolver .typeimpl .ParameterizedTypeImpl ;
6+ import ch .jalu .typeresolver .typeimpl .WildcardTypeImpl ;
7+ import org .hamcrest .Matcher ;
68import org .junit .jupiter .api .Test ;
79
810import java .io .Serializable ;
1517import java .util .AbstractList ;
1618import java .util .AbstractMap ;
1719import java .util .ArrayList ;
20+ import java .util .Arrays ;
1821import java .util .Collection ;
1922import java .util .HashMap ;
2023import java .util .HashSet ;
2124import java .util .LinkedHashMap ;
2225import java .util .List ;
2326import java .util .Map ;
24- import java .util .Optional ;
2527import java .util .RandomAccess ;
2628import java .util .Set ;
2729import java .util .concurrent .TimeUnit ;
2830import java .util .function .Consumer ;
2931
3032import static ch .jalu .typeresolver .TypeInfo .of ;
33+ import static ch .jalu .typeresolver .typeimpl .ParameterizedTypeBuilder .parameterizedTypeBuilder ;
3134import static org .hamcrest .MatcherAssert .assertThat ;
3235import static org .hamcrest .Matchers .contains ;
3336import static org .hamcrest .Matchers .containsInAnyOrder ;
@@ -57,24 +60,35 @@ void shouldReturnParentsAndInterfaces() {
5760 Set <TypeInfo > arrayListAll = arrayList .getAllTypeInfos ();
5861
5962 // then
63+ // Verify stringAll
6064 TypeReference <Comparable <String >> comparableString = new TypeReference <Comparable <String >>() { };
61- Optional <ConstableAndConstantDescTypes > constableTypes = JavaVersionHelper .getConstableClassIfApplicable ();
62- if (constableTypes .isPresent ()) {
63- Class <?> constable = constableTypes .get ().getConstableClass ();
64- Class <?> constantDesc = constableTypes .get ().getConstantDescClass ();
65- assertThat (stringAll , containsInAnyOrder (String .class , CharSequence .class , comparableString .getType (), Serializable .class , constable , constantDesc , Object .class ));
66- } else {
67- assertThat (stringAll , containsInAnyOrder (String .class , CharSequence .class , comparableString .getType (), Serializable .class , Object .class ));
68- }
69-
70- assertThat (arrayListAll , containsInAnyOrder (
65+ List <Type > expectedStringTypes = new ArrayList <>(Arrays .asList (
66+ String .class , CharSequence .class , comparableString .getType (), Serializable .class , Object .class ));
67+
68+ JavaVersionHelper .getConstableClassIfApplicable ().ifPresent (constableTypes -> {
69+ expectedStringTypes .add (constableTypes .getConstableClass ());
70+ expectedStringTypes .add (constableTypes .getConstantDescClass ());
71+ });
72+ assertThat (stringAll , containsTypesInAnyOrder (expectedStringTypes ));
73+
74+ // Verify arrayListAll
75+ List <TypeInfo > expectedArrayListTypes = new ArrayList <>(Arrays .asList (
7176 new TypeReference <ArrayList <String >>() { },
7277 new TypeReference <AbstractList <String >>() { },
7378 new TypeReference <AbstractCollection <String >>() { },
7479 new TypeReference <List <String >>() { },
7580 new TypeReference <Collection <String >>() { },
7681 new TypeReference <Iterable <String >>() { },
7782 of (Cloneable .class ), of (Serializable .class ), of (RandomAccess .class ), of (Object .class )));
83+
84+ JavaVersionHelper .getSequencedCollectionTypesIfApplicable ().ifPresent (sequencedCollTypes -> {
85+ ParameterizedTypeImpl sequencedStringCollection =
86+ parameterizedTypeBuilder (sequencedCollTypes .getSequencedCollectionClass ())
87+ .withTypeArg (0 , String .class )
88+ .build ();
89+ expectedArrayListTypes .add (TypeInfo .of (sequencedStringCollection ));
90+ });
91+ assertThat (arrayListAll , containsTypeInfosInAnyOrder (expectedArrayListTypes ));
7892 }
7993
8094 @ Test
@@ -88,20 +102,17 @@ void shouldReturnAllTypesForArrays() {
88102 Set <Type > primitiveIntArrAll = primitiveIntArr .getAllTypes ();
89103
90104 // then
91- Optional <ConstableAndConstantDescTypes > constableTypes = JavaVersionHelper .getConstableClassIfApplicable ();
92- if (constableTypes .isPresent ()) {
93- Class <?> constableArray = ArrayTypeUtils .createArrayClass (constableTypes .get ().getConstableClass ());
94- Class <?> constantDescArray = ArrayTypeUtils .createArrayClass (constableTypes .get ().getConstantDescClass ());
95-
96- assertThat (stringArrayAll , containsInAnyOrder (
97- String [].class , CharSequence [].class , Serializable [].class , new TypeReference <Comparable <String >[]>() { }.getType (), constableArray , constantDescArray , Object [].class ,
98- Object .class , Cloneable .class , Serializable .class ));
99- } else {
100- assertThat (stringArrayAll , containsInAnyOrder (
101- String [].class , CharSequence [].class , Serializable [].class , new TypeReference <Comparable <String >[]>() { }.getType (), Object [].class ,
102- Object .class , Cloneable .class , Serializable .class ));
103- }
104-
105+ // Verify stringArrayAll
106+ List <Type > expectedStringArrayTypes = new ArrayList <>(Arrays .asList (
107+ String [].class , CharSequence [].class , Serializable [].class , new TypeReference <Comparable <String >[]>() { }.getType (), Object [].class ,
108+ Object .class , Cloneable .class , Serializable .class ));
109+ JavaVersionHelper .getConstableClassIfApplicable ().ifPresent (constableTypes -> {
110+ expectedStringArrayTypes .add (ArrayTypeUtils .createArrayType (constableTypes .getConstableClass ()));
111+ expectedStringArrayTypes .add (ArrayTypeUtils .createArrayType (constableTypes .getConstantDescClass ()));
112+ });
113+ assertThat (stringArrayAll , containsTypesInAnyOrder (expectedStringArrayTypes ));
114+
115+ // Verify primitiveIntArrAll
105116 assertThat (primitiveIntArrAll , containsInAnyOrder (int [].class , Object .class , Cloneable .class , Serializable .class ));
106117 }
107118
@@ -116,14 +127,27 @@ void shouldHandleMultiDimensionalArrays() {
116127 Set <Type > float3dArrayAll = float3dArray .getAllTypes ();
117128
118129 // then
119- assertThat (list2dArrayAll , containsInAnyOrder (
130+ // Verify list2dArrayAll
131+ List <TypeInfo > expectedListTypeInfos = new ArrayList <>(Arrays .asList (
120132 new TypeReference <List <String >[][]>() { },
121133 new TypeReference <Collection <String >[][]>() { },
122134 new TypeReference <Iterable <String >[][]>() { },
123135 new TypeReference <Object [][]>() { },
124136 of (Serializable [].class ), of (Cloneable [].class ), of (Object [].class ),
125137 of (Serializable .class ), of (Cloneable .class ), of (Object .class )));
126138
139+ JavaVersionHelper .getSequencedCollectionTypesIfApplicable ().ifPresent (sequencedCollTypes -> {
140+ // java.util.SequencedCollection<java.lang.String>[][]
141+ ParameterizedTypeImpl sequencedStringCollection =
142+ parameterizedTypeBuilder (sequencedCollTypes .getSequencedCollectionClass ())
143+ .withTypeArg (0 , String .class )
144+ .build ();
145+ Type sequencedColl2dType = ArrayTypeUtils .createArrayType (sequencedStringCollection , 2 );
146+ expectedListTypeInfos .add (new TypeInfo (sequencedColl2dType ));
147+ });
148+ assertThat (list2dArrayAll , containsTypeInfosInAnyOrder (expectedListTypeInfos ));
149+
150+ // Verify float3dArray
127151 assertThat (float3dArrayAll , containsInAnyOrder (
128152 float [][][].class ,
129153 Serializable [][].class , Cloneable [][].class , Object [][].class ,
@@ -142,22 +166,33 @@ void shouldReturnAllTypes2() {
142166 Set <TypeInfo > linkedHashMapAll = linkedHashMap .getAllTypeInfos ();
143167
144168 // then
145- Optional <ConstableAndConstantDescTypes > constableTypes = JavaVersionHelper .getConstableClassIfApplicable ();
146- if (constableTypes .isPresent ()) {
147- Class <?> constable = constableTypes .get ().getConstableClass ();
148- assertThat (timeUnitAll , containsInAnyOrder (TimeUnit .class , Serializable .class , constable , Object .class ,
149- new TypeReference <Enum <TimeUnit >>() { }.getType (), new TypeReference <Comparable <TimeUnit >>() { }.getType ()));
150- } else {
151- assertThat (timeUnitAll , containsInAnyOrder (TimeUnit .class , Serializable .class , Object .class ,
152- new TypeReference <Enum <TimeUnit >>() { }.getType (), new TypeReference <Comparable <TimeUnit >>() { }.getType ()));
153- }
154-
155- assertThat (linkedHashMapAll , containsInAnyOrder (
169+ // Verify timeUnitAll
170+ List <Type > expectedTimeUnitTypes = new ArrayList <>(Arrays .asList (
171+ TimeUnit .class , Serializable .class , Object .class ,
172+ new TypeReference <Enum <TimeUnit >>() { }.getType (), new TypeReference <Comparable <TimeUnit >>() { }.getType ()));
173+
174+ JavaVersionHelper .getConstableClassIfApplicable ().ifPresent (constableTypes -> {
175+ expectedTimeUnitTypes .add (constableTypes .getConstableClass ());
176+ });
177+ assertThat (timeUnitAll , containsTypesInAnyOrder (expectedTimeUnitTypes ));
178+
179+ // Verify linkedHashMapAll
180+ List <TypeInfo > expectedLinkedHashMapTypes = new ArrayList <>(Arrays .asList (
156181 new TypeReference <LinkedHashMap <String , ? extends Integer >>() { },
157182 new TypeReference <HashMap <String , ? extends Integer >>() { },
158183 new TypeReference <AbstractMap <String , ? extends Integer >>() { },
159184 new TypeReference <Map <String , ? extends Integer >>() { },
160185 of (Cloneable .class ), of (Serializable .class ), of (Object .class )));
186+
187+ JavaVersionHelper .getSequencedCollectionTypesIfApplicable ().ifPresent (seqCollectionTypes -> {
188+ ParameterizedTypeImpl sequencedMapTypeEquivalent =
189+ parameterizedTypeBuilder (seqCollectionTypes .getSequencedMapClass ())
190+ .withTypeArg (0 , String .class )
191+ .withTypeArg (1 , WildcardTypeImpl .newWildcardExtends (Integer .class ))
192+ .build ();
193+ expectedLinkedHashMapTypes .add (TypeInfo .of (sequencedMapTypeEquivalent ));
194+ });
195+ assertThat (linkedHashMapAll , containsTypeInfosInAnyOrder (expectedLinkedHashMapTypes ));
161196 }
162197
163198 @ Test
@@ -189,4 +224,12 @@ void shouldReturnTypesWithGivenCollectionAndFunction() {
189224 // then
190225 assertThat (typesOfBigDecimal , contains ("BigDecimal" , "Number" , "Object" , "Serializable" , "Comparable" ));
191226 }
227+
228+ private Matcher <Iterable <? extends Type >> containsTypesInAnyOrder (List <Type > items ) {
229+ return containsInAnyOrder (items .toArray (new Type [0 ]));
230+ }
231+
232+ private Matcher <Iterable <? extends TypeInfo >> containsTypeInfosInAnyOrder (List <TypeInfo > items ) {
233+ return containsInAnyOrder (items .toArray (new TypeInfo [0 ]));
234+ }
192235}
0 commit comments