18
18
19
19
import java .lang .annotation .Annotation ;
20
20
import java .lang .reflect .Type ;
21
- import java .util .ArrayList ;
22
- import java .util .Collections ;
23
21
import java .util .HashSet ;
24
22
import java .util .Set ;
25
23
import javax .enterprise .inject .Default ;
26
24
import javax .enterprise .inject .spi .AfterBeanDiscovery ;
27
- import javax .enterprise .inject .spi .AnnotatedCallable ;
28
- import javax .enterprise .inject .spi .AnnotatedConstructor ;
29
- import javax .enterprise .inject .spi .AnnotatedMethod ;
30
- import javax .enterprise .inject .spi .AnnotatedParameter ;
31
- import javax .enterprise .inject .spi .AnnotatedType ;
32
25
import javax .enterprise .inject .spi .Bean ;
33
26
import javax .enterprise .inject .spi .BeanManager ;
34
- import javax .enterprise .inject .spi .BeforeBeanDiscovery ;
35
- import javax .enterprise .inject .spi .ProcessAnnotatedType ;
36
27
import javax .enterprise .inject .spi .ProcessBean ;
37
28
import javax .enterprise .util .AnnotationLiteral ;
38
29
import javax .validation .Validator ;
39
30
import javax .validation .ValidatorFactory ;
40
- import javax .validation .constraints .NotNull ;
41
31
42
- import org .easymock .Capture ;
43
32
import org .testng .annotations .BeforeMethod ;
44
33
import org .testng .annotations .Test ;
45
34
46
35
import org .hibernate .validator .cdi .HibernateValidator ;
47
36
import org .hibernate .validator .internal .cdi .ValidationExtension ;
48
37
import org .hibernate .validator .internal .cdi .ValidatorBean ;
49
38
import org .hibernate .validator .internal .cdi .ValidatorFactoryBean ;
50
- import org .hibernate .validator .internal .cdi .interceptor .MethodValidated ;
51
- import org .hibernate .validator .internal .util .annotationfactory .AnnotationDescriptor ;
52
- import org .hibernate .validator .internal .util .annotationfactory .AnnotationFactory ;
53
39
54
- import static org .easymock .EasyMock .capture ;
55
40
import static org .easymock .EasyMock .createMock ;
56
41
import static org .easymock .EasyMock .expect ;
57
42
import static org .easymock .EasyMock .isA ;
58
43
import static org .easymock .EasyMock .replay ;
59
44
import static org .easymock .EasyMock .verify ;
60
- import static org .hibernate .validator .internal .util .CollectionHelper .newHashSet ;
61
- import static org .testng .Assert .assertTrue ;
62
45
63
46
/**
64
47
* @author Hardy Ferentschik
65
48
*/
66
49
@ Test (singleThreaded = true ) // needs to run single threaded, because the mocks are shared across test methods
67
- public class ValidationExtensionTest < T > {
50
+ public class ValidationExtensionTest {
68
51
private ValidationExtension extension ;
69
52
private AfterBeanDiscovery afterBeanDiscoveryMock ;
70
53
private ProcessBean processBeanMock ;
71
- private BeforeBeanDiscovery beforeBeanDiscoveryMock ;
72
- private ProcessAnnotatedType <T > processAnnotatedTypeMock ;
73
- private AnnotatedType <T > annotatedTypeMock ;
74
- private AnnotatedMethod <T > annotatedMethodMock ;
75
- private AnnotatedConstructor <T > annotatedConstructorMock ;
76
54
private Bean <ValidatorFactory > validatorFactoryBeanMock ;
77
55
private Bean <Validator > validatorBeanMock ;
78
56
private BeanManager beanManagerMock ;
@@ -83,11 +61,6 @@ public void setUp() {
83
61
extension = new ValidationExtension ();
84
62
afterBeanDiscoveryMock = createMock ( AfterBeanDiscovery .class );
85
63
processBeanMock = createMock ( ProcessBean .class );
86
- beforeBeanDiscoveryMock = createMock ( BeforeBeanDiscovery .class );
87
- processAnnotatedTypeMock = createMock ( ProcessAnnotatedType .class );
88
- annotatedTypeMock = createMock ( AnnotatedType .class );
89
- annotatedMethodMock = createMock ( AnnotatedMethod .class );
90
- annotatedConstructorMock = createMock ( AnnotatedConstructor .class );
91
64
beanManagerMock = createMock ( BeanManager .class );
92
65
validatorFactoryBeanMock = createMock ( Bean .class );
93
66
validatorBeanMock = createMock ( Bean .class );
@@ -194,113 +167,6 @@ public void testNoRegistrationRequired() {
194
167
public void testProcessAnnotatedTypeNullParameter () {
195
168
extension .processAnnotatedType ( null );
196
169
}
197
-
198
- @ Test
199
- public void testConstrainedMethodGetsInterceptorBidingAdded () {
200
- AnnotationDescriptor <NotNull > descriptor = new AnnotationDescriptor <NotNull >( NotNull .class );
201
- Annotation notNull = AnnotationFactory .create ( descriptor );
202
- setupMocks ( annotatedMethodMock , notNull );
203
-
204
- Capture <AnnotatedType <T >> capturedType = new Capture <AnnotatedType <T >>();
205
- processAnnotatedTypeMock .setAnnotatedType ( capture ( capturedType ) );
206
-
207
- // get the mocks ready
208
- replay ( processAnnotatedTypeMock , annotatedTypeMock , annotatedMethodMock , annotatedConstructorMock );
209
-
210
- // run the code
211
- extension .processAnnotatedType ( processAnnotatedTypeMock );
212
-
213
- // verify the mocks
214
- verify ( processAnnotatedTypeMock , annotatedTypeMock , annotatedMethodMock , annotatedConstructorMock );
215
-
216
- // check the captured type has @MethodValidated added
217
- Set <AnnotatedMethod <? super T >> methods = capturedType .getValue ().getMethods ();
218
- assertTrue ( methods .size () == 1 , "We still should only have a single method" );
219
- AnnotatedMethod <?> method = methods .iterator ().next ();
220
- assertTrue (
221
- method .isAnnotationPresent ( MethodValidated .class ),
222
- "The @MethodValidated annotation method should have been added"
223
- );
224
- }
225
-
226
- @ Test
227
- public void testConstrainedConstructorGetsInterceptorBidingAdded () {
228
- AnnotationDescriptor <NotNull > descriptor = new AnnotationDescriptor <NotNull >( NotNull .class );
229
- Annotation notNull = AnnotationFactory .create ( descriptor );
230
- setupMocks ( annotatedConstructorMock , notNull );
231
-
232
- Capture <AnnotatedType <T >> capturedType = new Capture <AnnotatedType <T >>();
233
- processAnnotatedTypeMock .setAnnotatedType ( capture ( capturedType ) );
234
-
235
- // get the mocks ready
236
- replay ( processAnnotatedTypeMock , annotatedTypeMock , annotatedMethodMock , annotatedConstructorMock );
237
-
238
- // run the code
239
- extension .processAnnotatedType ( processAnnotatedTypeMock );
240
-
241
- // verify the mocks
242
- verify ( processAnnotatedTypeMock , annotatedTypeMock , annotatedMethodMock , annotatedConstructorMock );
243
-
244
- // check the captured type has @MethodValidated added
245
- Set <AnnotatedConstructor <T >> constructors = capturedType .getValue ().getConstructors ();
246
- assertTrue ( constructors .size () == 1 , "We still should only have a single constructor" );
247
- AnnotatedConstructor <?> constructor = constructors .iterator ().next ();
248
- assertTrue (
249
- constructor .isAnnotationPresent ( MethodValidated .class ),
250
- "The @MethodValidated annotation method should have been added"
251
- );
252
- }
253
-
254
- @ Test
255
- public void testUnConstrainedMethodDoesNotGetInterceptorBidingAdded () {
256
- setupMocks ( annotatedMethodMock );
257
-
258
- // get the mocks ready
259
- replay ( processAnnotatedTypeMock , annotatedTypeMock , annotatedMethodMock );
260
-
261
- // run the code
262
- extension .processAnnotatedType ( processAnnotatedTypeMock );
263
-
264
- // verify the mocks
265
- verify ( processAnnotatedTypeMock , annotatedTypeMock , annotatedMethodMock );
266
- }
267
-
268
- private void setupMocks (AnnotatedCallable <T > callable , Annotation ... constraintAnnotations ) {
269
- expect ( processAnnotatedTypeMock .getAnnotatedType () ).andReturn ( annotatedTypeMock );
270
-
271
- Set <AnnotatedConstructor <T >> constructors = newHashSet ();
272
- Set <Annotation > constructorAnnotations = newHashSet ();
273
- if ( callable instanceof AnnotatedConstructor ) {
274
- constructors .add ( (AnnotatedConstructor <T >) callable );
275
- Collections .addAll ( constructorAnnotations , constraintAnnotations );
276
- }
277
- expect ( annotatedTypeMock .getConstructors () ).andReturn ( constructors );
278
-
279
- Set <AnnotatedMethod <? super T >> methods = newHashSet ();
280
- Set <Annotation > methodAnnotations = newHashSet ();
281
- if ( callable instanceof AnnotatedMethod ) {
282
- methods .add ( (AnnotatedMethod <T >) callable );
283
- Collections .addAll ( methodAnnotations , constraintAnnotations );
284
- }
285
- expect ( annotatedTypeMock .getMethods () ).andReturn ( methods );
286
-
287
- if ( callable instanceof AnnotatedConstructor ) {
288
- expect ( annotatedConstructorMock .getAnnotations () ).andReturn ( constructorAnnotations );
289
- }
290
- else {
291
- expect ( annotatedMethodMock .getAnnotations () ).andReturn ( methodAnnotations );
292
- }
293
-
294
- if ( constraintAnnotations .length == 0 ) {
295
- // if there is no constraint annotation on the method the parameters get checked
296
- expect ( annotatedMethodMock .getParameters () ).andReturn ( new ArrayList <AnnotatedParameter <T >>() );
297
- }
298
- else {
299
- // if we have found a constraint annotation we expect another call to getConstructors and getMethods when the wrapped type gets build
300
- expect ( annotatedTypeMock .getConstructors () ).andReturn ( constructors );
301
- expect ( annotatedTypeMock .getMethods () ).andReturn ( methods );
302
- }
303
- }
304
170
}
305
171
306
172
0 commit comments