1+ /*
2+ * Copyright © 2020 Ocado (marian.jureczko@ocado.com)
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+ package com .ocadotechnology .gembus .test ;
17+
18+ import static org .assertj .core .api .Assertions .assertThat ;
19+
20+ import org .junit .jupiter .params .ParameterizedTest ;
21+ import org .junit .jupiter .params .provider .Arguments ;
22+ import org .junit .jupiter .params .provider .MethodSource ;
23+
24+ import java .util .stream .Stream ;
25+
26+ class ReflectionHelperTest {
27+
28+ @ ParameterizedTest (name = "{3}" )
29+ @ MethodSource ("allCases" )
30+ void should_detectPrimitiveField (Class <?> clazz , String fieldName , boolean expected , String description ) {
31+ //when
32+ boolean actual = ReflectionHelper .isPrimitiveField (clazz , fieldName );
33+
34+ //then
35+ assertThat (actual ).isEqualTo (expected );
36+ }
37+
38+ static Stream <Arguments > allCases () {
39+ return Stream .of (
40+ Arguments .of (PlainClass .class , "primitive" , true , "SHOULD detect primitive WHEN field is primitive in plain class" ),
41+ Arguments .of (PlainClass .class , "boxed" , false , "SHOULD detect non-primitive WHEN field is boxed in plain class" ),
42+ Arguments .of (PlainRecord .class , "primitive" , true , "SHOULD detect primitive WHEN field is primitive in record" ),
43+ Arguments .of (PlainRecord .class , "boxed" , false , "SHOULD detect non-primitive WHEN field is boxed in record" ),
44+ Arguments .of (ChildWithPrimitiveParent .class , "primitive" , true , "SHOULD detect primitive WHEN field is inherited from parent" ),
45+ Arguments .of (ChildWithBoxedParent .class , "boxed" , false , "SHOULD detect non-primitive WHEN field is inherited from parent" ),
46+ Arguments .of (ChildHidesWithBoxed .class , "value" , false , "SHOULD detect non-primitive WHEN child hides parent field" ),
47+ Arguments .of (ChildHidesWithPrimitive .class , "value" , true , "SHOULD detect primitive WHEN child hides parent field" ),
48+ Arguments .of (PlainClass .class , "missing" , false , "SHOULD return false WHEN field does not exist" ),
49+ Arguments .of (PlainClass .class , "" , false , "SHOULD return false WHEN field name is empty" ),
50+ Arguments .of (PlainClass .class , null , false , "SHOULD return false WHEN field name is null" )
51+ );
52+ }
53+
54+ static class PlainClass {
55+ int primitive ;
56+ Integer boxed ;
57+ }
58+
59+ record PlainRecord (int primitive , Integer boxed ) {
60+ }
61+
62+ static class ParentWithPrimitive {
63+ int primitive ;
64+ }
65+
66+ static class ChildWithPrimitiveParent extends ParentWithPrimitive {
67+ }
68+
69+ static class ParentWithBoxed {
70+ Integer boxed ;
71+ }
72+
73+ static class ChildWithBoxedParent extends ParentWithBoxed {
74+ }
75+
76+ static class ParentPrimitiveValue {
77+ int value ;
78+ }
79+
80+ static class ChildHidesWithBoxed extends ParentPrimitiveValue {
81+ String value ;
82+ }
83+
84+ static class ParentBoxedValue {
85+ String value ;
86+ }
87+
88+ static class ChildHidesWithPrimitive extends ParentBoxedValue {
89+ int value ;
90+ }
91+ }
0 commit comments