39
39
* @author Roman Rodov
40
40
* @author Mark Paluch
41
41
* @author Christoph Strobl
42
+ * @author Pavel Anisimov
42
43
*/
43
44
class PreferredConstructorDiscovererUnitTests <P extends PersistentProperty <P >> {
44
45
45
46
@ Test // DATACMNS-1126
46
47
void findsNoArgConstructorForClassWithoutExplicitConstructor () {
47
48
48
- assertThat ( PreferredConstructorDiscoverer .discover (EntityWithoutConstructor .class )). satisfies ( constructor -> {
49
+ var constructor = PreferredConstructorDiscoverer .discover (EntityWithoutConstructor .class );
49
50
50
- assertThat (constructor ).isNotNull ();
51
- assertThat (constructor .isNoArgConstructor ()).isTrue ();
52
- assertThat (constructor .isExplicitlyAnnotated ()).isFalse ();
53
- });
51
+ assertThat (constructor ).isNotNull ();
52
+ assertThat (constructor .isNoArgConstructor ()).isTrue ();
53
+ assertThat (constructor .isExplicitlyAnnotated ()).isFalse ();
54
54
}
55
55
56
56
@ Test // DATACMNS-1126
57
57
void findsNoArgConstructorForClassWithMultipleConstructorsAndNoArgOne () {
58
58
59
- assertThat ( PreferredConstructorDiscoverer .discover (ClassWithEmptyConstructor .class )). satisfies ( constructor -> {
59
+ var constructor = PreferredConstructorDiscoverer .discover (ClassWithEmptyConstructor .class );
60
60
61
- assertThat (constructor ).isNotNull ();
62
- assertThat (constructor .isNoArgConstructor ()).isTrue ();
63
- assertThat (constructor .isExplicitlyAnnotated ()).isFalse ();
64
- });
61
+ assertThat (constructor ).isNotNull ();
62
+ assertThat (constructor .isNoArgConstructor ()).isTrue ();
63
+ assertThat (constructor .isExplicitlyAnnotated ()).isFalse ();
65
64
}
66
65
67
66
@ Test // DATACMNS-1126
68
67
void doesNotThrowExceptionForMultipleConstructorsAndNoNoArgConstructorWithoutAnnotation () {
69
68
70
- assertThat (PreferredConstructorDiscoverer .discover (ClassWithMultipleConstructorsWithoutEmptyOne .class )).isNull ();
69
+ var constructor = PreferredConstructorDiscoverer .discover (ClassWithMultipleConstructorsWithoutEmptyOne .class );
70
+
71
+ assertThat (constructor ).isNull ();
71
72
}
72
73
73
74
@ Test // DATACMNS-1126
74
- @ SuppressWarnings ({ "unchecked" , "rawtypes" })
75
75
void usesConstructorWithAnnotationOverEveryOther () {
76
76
77
- assertThat ( PreferredConstructorDiscoverer . discover ( ClassWithMultipleConstructorsAndAnnotation . class ))
78
- .satisfies ( constructor -> {
77
+ PreferredConstructor < ClassWithMultipleConstructorsAndAnnotation , P > constructor = PreferredConstructorDiscoverer
78
+ .discover ( ClassWithMultipleConstructorsAndAnnotation . class );
79
79
80
- assertThat (constructor ).isNotNull ();
81
- assertThat (constructor .isNoArgConstructor ()).isFalse ();
82
- assertThat (constructor .isExplicitlyAnnotated ()).isTrue ();
80
+ assertThat (constructor ).isNotNull ();
81
+ assertThat (constructor .isNoArgConstructor ()).isFalse ();
82
+ assertThat (constructor .isExplicitlyAnnotated ()).isTrue ();
83
83
84
- assertThat (constructor .hasParameters ()).isTrue ();
84
+ assertThat (constructor .hasParameters ()).isTrue ();
85
85
86
- Iterator <Parameter <Object , P >> parameters = ( Iterator ) constructor .getParameters ().iterator ();
86
+ Iterator <Parameter <Object , P >> parameters = constructor .getParameters ().iterator ();
87
87
88
- Parameter <?, P > parameter = parameters .next ();
89
- assertThat (parameter .getType ().getType ()).isEqualTo (Long .class );
90
- assertThat (parameters .hasNext ()).isFalse ();
91
- });
88
+ Parameter <?, P > parameter = parameters .next ();
89
+ assertThat (parameter .getType ().getType ()).isEqualTo (Long .class );
90
+ assertThat (parameters .hasNext ()).isFalse ();
92
91
}
93
92
94
93
@ Test // DATACMNS-134, DATACMNS-1126
95
94
void discoversInnerClassConstructorCorrectly () {
96
95
97
96
PersistentEntity <Inner , P > entity = new BasicPersistentEntity <>(TypeInformation .of (Inner .class ));
98
97
99
- assertThat (PreferredConstructorDiscoverer .discover (entity )).satisfies (constructor -> {
98
+ var constructor = PreferredConstructorDiscoverer .discover (entity );
99
+
100
+ assertThat (constructor ).isNotNull ();
100
101
101
- Parameter <?, P > parameter = constructor .getParameters ().iterator ().next ();
102
- assertThat (constructor .isParentParameter (parameter )).isTrue ();
103
- });
102
+ Parameter <?, P > parameter = constructor .getParameters ().iterator ().next ();
103
+ assertThat (constructor .isParentParameter (parameter )).isTrue ();
104
104
}
105
105
106
106
@ Test // DATACMNS-1082, DATACMNS-1126
@@ -109,73 +109,76 @@ void skipsSyntheticConstructor() {
109
109
PersistentEntity <SyntheticConstructor , P > entity = new BasicPersistentEntity <>(
110
110
TypeInformation .of (SyntheticConstructor .class ));
111
111
112
- assertThat (PreferredConstructorDiscoverer .discover (entity )).satisfies (constructor -> {
112
+ var constructor = PreferredConstructorDiscoverer .discover (entity );
113
+ assertThat (constructor ).isNotNull ();
113
114
114
- var annotation = constructor .getConstructor ().getAnnotation (PersistenceConstructor .class );
115
- assertThat (annotation ).isNotNull ();
116
- assertThat (constructor .getConstructor ().isSynthetic ()).isFalse ();
117
- });
115
+ var annotation = constructor .getConstructor ().getAnnotation (PersistenceConstructor .class );
116
+ assertThat (annotation ).isNotNull ();
117
+ assertThat (constructor .getConstructor ().isSynthetic ()).isFalse ();
118
118
}
119
119
120
120
@ Test // GH-2313
121
121
void capturesEnclosingTypeParameterOfNonStaticInnerClass () {
122
122
123
- assertThat ( PreferredConstructorDiscoverer .discover (NonStaticWithGenericTypeArgUsedInCtor .class )). satisfies ( ctor -> {
123
+ var constructor = PreferredConstructorDiscoverer .discover (NonStaticWithGenericTypeArgUsedInCtor .class );
124
124
125
- assertThat (ctor . getParameters ()). hasSize ( 2 );
126
- assertThat (ctor .getParameters (). get ( 0 ). getName ()). isEqualTo ( "this$0" );
127
- assertThat (ctor .getParameters ().get (1 ).getName ()).isEqualTo ("value " );
128
- } );
125
+ assertThat (constructor ). isNotNull ( );
126
+ assertThat (constructor .getParameters ()). hasSize ( 2 );
127
+ assertThat (constructor .getParameters ().get (0 ).getName ()).isEqualTo ("this$0 " );
128
+ assertThat ( constructor . getParameters (). get ( 1 ). getName ()). isEqualTo ( "value" );
129
129
}
130
130
131
131
@ Test // GH-2313
132
132
void capturesSuperClassEnclosingTypeParameterOfNonStaticInnerClass () {
133
133
134
- assertThat (PreferredConstructorDiscoverer .discover (NonStaticInnerWithGenericArgUsedInCtor .class ))
135
- .satisfies (ctor -> {
134
+ var constructor = PreferredConstructorDiscoverer .discover (NonStaticInnerWithGenericArgUsedInCtor .class );
136
135
137
- assertThat (ctor . getParameters ()). hasSize ( 2 );
138
- assertThat (ctor .getParameters (). get ( 0 ). getName ()). isEqualTo ( "this$0" );
139
- assertThat (ctor .getParameters ().get (1 ).getName ()).isEqualTo ("value " );
140
- } );
136
+ assertThat (constructor ). isNotNull ( );
137
+ assertThat (constructor .getParameters ()). hasSize ( 2 );
138
+ assertThat (constructor .getParameters ().get (0 ).getName ()).isEqualTo ("this$0 " );
139
+ assertThat ( constructor . getParameters (). get ( 1 ). getName ()). isEqualTo ( "value" );
141
140
}
142
141
143
142
@ Test // GH-2332
144
143
void detectsMetaAnnotatedValueAnnotation () {
145
144
146
- assertThat ( PreferredConstructorDiscoverer .discover (ClassWithMetaAnnotatedParameter .class )). satisfies ( ctor -> {
145
+ var constructor = PreferredConstructorDiscoverer .discover (ClassWithMetaAnnotatedParameter .class );
147
146
148
- assertThat (ctor . getParameters (). get ( 0 ). getSpelExpression ()). isEqualTo ( "${hello-world}" );
149
- assertThat (ctor .getParameters ().get (0 ).getAnnotations ()).isNotNull ( );
150
- } );
147
+ assertThat (constructor ). isNotNull ( );
148
+ assertThat (constructor .getParameters ().get (0 ).getSpelExpression ()).isEqualTo ( "${hello-world}" );
149
+ assertThat ( constructor . getParameters (). get ( 0 ). getAnnotations ()). isNotNull ( );
151
150
}
152
151
153
152
@ Test // GH-2332
154
- void detectsCanonicalRecordConstructor () {
153
+ void detectsCanonicalRecordConstructorWhenRecordHasSingleArgConstructor () {
155
154
156
- assertThat ( PreferredConstructorDiscoverer .discover (RecordWithSingleArgConstructor .class )). satisfies ( ctor -> {
155
+ var constructor = PreferredConstructorDiscoverer .discover (RecordWithSingleArgConstructor .class );
157
156
158
- assertThat (ctor .getParameters ()).hasSize (2 );
159
- assertThat (ctor .getParameters ().get (0 ).getRawType ()).isEqualTo (Long .class );
160
- assertThat (ctor .getParameters ().get (1 ).getRawType ()).isEqualTo (String .class );
161
- });
157
+ assertThat (constructor ).isNotNull ();
158
+ assertThat (constructor .getParameters ()).hasSize (2 );
159
+ assertThat (constructor .getParameters ().get (0 ).getRawType ()).isEqualTo (Long .class );
160
+ assertThat (constructor .getParameters ().get (1 ).getRawType ()).isEqualTo (String .class );
161
+ }
162
+
163
+ @ Test // GH-2332
164
+ void detectsCanonicalRecordConstructorWhenRecordHasNoArgConstructor () {
162
165
163
- assertThat ( PreferredConstructorDiscoverer .discover (RecordWithNoArgConstructor .class )). satisfies ( ctor -> {
166
+ var constructor = PreferredConstructorDiscoverer .discover (RecordWithNoArgConstructor .class );
164
167
165
- assertThat (ctor . getParameters ()). hasSize ( 2 );
166
- assertThat (ctor .getParameters (). get ( 0 ). getRawType ()). isEqualTo ( Long . class );
167
- assertThat (ctor .getParameters ().get (1 ).getRawType ()).isEqualTo (String .class );
168
- } );
168
+ assertThat (constructor ). isNotNull ( );
169
+ assertThat (constructor .getParameters ()). hasSize ( 2 );
170
+ assertThat (constructor .getParameters ().get (0 ).getRawType ()).isEqualTo (Long .class );
171
+ assertThat ( constructor . getParameters (). get ( 1 ). getRawType ()). isEqualTo ( String . class );
169
172
}
170
173
171
174
@ Test // GH-2332
172
175
void detectsAnnotatedRecordConstructor () {
173
176
174
- assertThat ( PreferredConstructorDiscoverer .discover (RecordWithPersistenceCreator .class )). satisfies ( ctor -> {
177
+ var constructor = PreferredConstructorDiscoverer .discover (RecordWithPersistenceCreator .class );
175
178
176
- assertThat (ctor . getParameters ()). hasSize ( 1 );
177
- assertThat (ctor .getParameters (). get ( 0 ). getRawType ()). isEqualTo ( String . class );
178
- } );
179
+ assertThat (constructor ). isNotNull ( );
180
+ assertThat (constructor .getParameters ()). hasSize ( 1 );
181
+ assertThat ( constructor . getParameters (). get ( 0 ). getRawType ()). isEqualTo ( String . class );
179
182
}
180
183
181
184
static class SyntheticConstructor {
0 commit comments