Skip to content

Commit bc3de10

Browse files
committed
Polishing
1 parent 826edae commit bc3de10

File tree

9 files changed

+95
-71
lines changed

9 files changed

+95
-71
lines changed

spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/SpringValidatorAdapterTests.java

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
import javax.validation.Constraint;
3535
import javax.validation.ConstraintValidator;
3636
import javax.validation.ConstraintValidatorContext;
37+
import javax.validation.ConstraintViolation;
3738
import javax.validation.Payload;
3839
import javax.validation.Valid;
3940
import javax.validation.Validation;
@@ -50,6 +51,7 @@
5051
import org.springframework.context.support.StaticMessageSource;
5152
import org.springframework.util.ObjectUtils;
5253
import org.springframework.validation.BeanPropertyBindingResult;
54+
import org.springframework.validation.FieldError;
5355
import org.springframework.validation.beanvalidation.SpringValidatorAdapter;
5456

5557
import static java.lang.annotation.ElementType.*;
@@ -73,7 +75,7 @@ public class SpringValidatorAdapterTests {
7375
@Before
7476
public void setupSpringValidatorAdapter() {
7577
messageSource.addMessage("Size", Locale.ENGLISH, "Size of {0} is must be between {2} and {1}");
76-
messageSource.addMessage("Same", Locale.ENGLISH, "{2} must be same value with {1}");
78+
messageSource.addMessage("Same", Locale.ENGLISH, "{2} must be same value as {1}");
7779
messageSource.addMessage("password", Locale.ENGLISH, "Password");
7880
messageSource.addMessage("confirmPassword", Locale.ENGLISH, "Password(Confirm)");
7981
}
@@ -96,8 +98,11 @@ public void testNoStringArgumentValue() {
9698

9799
assertThat(errors.getFieldErrorCount("password"), is(1));
98100
assertThat(errors.getFieldValue("password"), is("pass"));
99-
assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH),
100-
is("Size of Password is must be between 8 and 128"));
101+
FieldError error = errors.getFieldError("password");
102+
assertNotNull(error);
103+
assertThat(messageSource.getMessage(error, Locale.ENGLISH), is("Size of Password is must be between 8 and 128"));
104+
assertTrue(error.contains(ConstraintViolation.class));
105+
assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("password"));
101106
}
102107

103108
@Test // SPR-13406
@@ -111,8 +116,11 @@ public void testApplyMessageSourceResolvableToStringArgumentValueWithResolvedLog
111116

112117
assertThat(errors.getFieldErrorCount("password"), is(1));
113118
assertThat(errors.getFieldValue("password"), is("password"));
114-
assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH),
115-
is("Password must be same value with Password(Confirm)"));
119+
FieldError error = errors.getFieldError("password");
120+
assertNotNull(error);
121+
assertThat(messageSource.getMessage(error, Locale.ENGLISH), is("Password must be same value as Password(Confirm)"));
122+
assertTrue(error.contains(ConstraintViolation.class));
123+
assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("password"));
116124
}
117125

118126
@Test // SPR-13406
@@ -127,10 +135,16 @@ public void testApplyMessageSourceResolvableToStringArgumentValueWithUnresolvedL
127135
assertThat(errors.getFieldErrorCount("email"), is(1));
128136
assertThat(errors.getFieldValue("email"), is("[email protected]"));
129137
assertThat(errors.getFieldErrorCount("confirmEmail"), is(1));
130-
assertThat(messageSource.getMessage(errors.getFieldError("email"), Locale.ENGLISH),
131-
is("email must be same value with confirmEmail"));
132-
assertThat(messageSource.getMessage(errors.getFieldError("confirmEmail"), Locale.ENGLISH),
133-
is("Email required"));
138+
FieldError error1 = errors.getFieldError("email");
139+
FieldError error2 = errors.getFieldError("confirmEmail");
140+
assertNotNull(error1);
141+
assertNotNull(error2);
142+
assertThat(messageSource.getMessage(error1, Locale.ENGLISH), is("email must be same value as confirmEmail"));
143+
assertThat(messageSource.getMessage(error2, Locale.ENGLISH), is("Email required"));
144+
assertTrue(error1.contains(ConstraintViolation.class));
145+
assertThat(error1.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("email"));
146+
assertTrue(error2.contains(ConstraintViolation.class));
147+
assertThat(error2.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("confirmEmail"));
134148
}
135149

136150
@Test // SPR-15123
@@ -147,10 +161,16 @@ public void testApplyMessageSourceResolvableToStringArgumentValueWithAlwaysUseMe
147161
assertThat(errors.getFieldErrorCount("email"), is(1));
148162
assertThat(errors.getFieldValue("email"), is("[email protected]"));
149163
assertThat(errors.getFieldErrorCount("confirmEmail"), is(1));
150-
assertThat(messageSource.getMessage(errors.getFieldError("email"), Locale.ENGLISH),
151-
is("email must be same value with confirmEmail"));
152-
assertThat(messageSource.getMessage(errors.getFieldError("confirmEmail"), Locale.ENGLISH),
153-
is("Email required"));
164+
FieldError error1 = errors.getFieldError("email");
165+
FieldError error2 = errors.getFieldError("confirmEmail");
166+
assertNotNull(error1);
167+
assertNotNull(error2);
168+
assertThat(messageSource.getMessage(error1, Locale.ENGLISH), is("email must be same value as confirmEmail"));
169+
assertThat(messageSource.getMessage(error2, Locale.ENGLISH), is("Email required"));
170+
assertTrue(error1.contains(ConstraintViolation.class));
171+
assertThat(error1.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("email"));
172+
assertTrue(error2.contains(ConstraintViolation.class));
173+
assertThat(error2.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("confirmEmail"));
154174
}
155175

156176
@Test // SPR-16177
@@ -403,13 +423,13 @@ public static class Child {
403423

404424
private Integer id;
405425

406-
@javax.validation.constraints.NotNull
426+
@NotNull
407427
private String name;
408428

409-
@javax.validation.constraints.NotNull
429+
@NotNull
410430
private Integer age;
411431

412-
@javax.validation.constraints.NotNull
432+
@NotNull
413433
private Parent parent;
414434

415435
public Integer getId() {

spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/ValidatorFactoryTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -285,6 +285,8 @@ public void testListValidation() {
285285
validator.validate(listContainer, errors);
286286

287287
FieldError fieldError = errors.getFieldError("list[1]");
288+
assertNotNull(fieldError);
289+
assertEquals("X", fieldError.getRejectedValue());
288290
assertEquals("X", errors.getFieldValue("list[1]"));
289291
}
290292

spring-context/src/main/java/org/springframework/context/annotation/AutoProxyRegistrar.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,9 +57,9 @@ public class AutoProxyRegistrar implements ImportBeanDefinitionRegistrar {
5757
@Override
5858
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
5959
boolean candidateFound = false;
60-
Set<String> annoTypes = importingClassMetadata.getAnnotationTypes();
61-
for (String annoType : annoTypes) {
62-
AnnotationAttributes candidate = AnnotationConfigUtils.attributesFor(importingClassMetadata, annoType);
60+
Set<String> annTypes = importingClassMetadata.getAnnotationTypes();
61+
for (String annType : annTypes) {
62+
AnnotationAttributes candidate = AnnotationConfigUtils.attributesFor(importingClassMetadata, annType);
6363
if (candidate == null) {
6464
continue;
6565
}

spring-context/src/main/java/org/springframework/context/support/DefaultMessageSourceResolvable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -135,7 +135,7 @@ public String getDefaultMessage() {
135135
* including codes, arguments, and default message.
136136
*/
137137
protected final String resolvableToString() {
138-
StringBuilder result = new StringBuilder();
138+
StringBuilder result = new StringBuilder(64);
139139
result.append("codes [").append(StringUtils.arrayToDelimitedString(this.codes, ","));
140140
result.append("]; arguments [").append(StringUtils.arrayToDelimitedString(this.arguments, ","));
141141
result.append("]; default message [").append(this.defaultMessage).append(']');

spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -358,9 +358,9 @@ public boolean isAlias(String beanName) {
358358

359359
/**
360360
* Register a bean from the given bean class, optionally customizing its
361-
* bean definition metadata (typically declared as a lambda expression
362-
* or method reference).
363-
* @param beanClass the class of the bean
361+
* bean definition metadata (typically declared as a lambda expression).
362+
* @param beanClass the class of the bean (resolving a public constructor
363+
* to be autowired, possibly simply the default constructor)
364364
* @param customizers one or more callbacks for customizing the factory's
365365
* {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
366366
* @since 5.0
@@ -371,53 +371,57 @@ public final <T> void registerBean(Class<T> beanClass, BeanDefinitionCustomizer.
371371
}
372372

373373
/**
374-
* Register a bean from the given bean class, using the given supplier for
375-
* obtaining a new instance (typically declared as a lambda expression or
376-
* method reference), optionally customizing its bean definition metadata
377-
* (again typically declared as a lambda expression or method reference).
374+
* Register a bean from the given bean class, optionally customizing its
375+
* bean definition metadata (typically declared as a lambda expression).
378376
* @param beanName the name of the bean (may be {@code null})
379-
* @param beanClass the class of the bean
377+
* @param beanClass the class of the bean (resolving a public constructor
378+
* to be autowired, possibly simply the default constructor)
380379
* @param customizers one or more callbacks for customizing the factory's
381380
* {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
382381
* @since 5.0
383382
* @see #registerBean(String, Class, Supplier, BeanDefinitionCustomizer...)
384383
*/
385-
public final <T> void registerBean(@Nullable String beanName, Class<T> beanClass, BeanDefinitionCustomizer... customizers) {
384+
public final <T> void registerBean(
385+
@Nullable String beanName, Class<T> beanClass, BeanDefinitionCustomizer... customizers) {
386+
386387
registerBean(beanName, beanClass, null, customizers);
387388
}
388389

389390
/**
390391
* Register a bean from the given bean class, using the given supplier for
391392
* obtaining a new instance (typically declared as a lambda expression or
392393
* method reference), optionally customizing its bean definition metadata
393-
* (again typically declared as a lambda expression or method reference).
394+
* (again typically declared as a lambda expression).
394395
* @param beanClass the class of the bean
395396
* @param supplier a callback for creating an instance of the bean
396397
* @param customizers one or more callbacks for customizing the factory's
397398
* {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
398399
* @since 5.0
399400
* @see #registerBean(String, Class, Supplier, BeanDefinitionCustomizer...)
400401
*/
401-
public final <T> void registerBean(Class<T> beanClass, Supplier<T> supplier, BeanDefinitionCustomizer... customizers) {
402+
public final <T> void registerBean(
403+
Class<T> beanClass, Supplier<T> supplier, BeanDefinitionCustomizer... customizers) {
404+
402405
registerBean(null, beanClass, supplier, customizers);
403406
}
404407

405408
/**
406409
* Register a bean from the given bean class, using the given supplier for
407410
* obtaining a new instance (typically declared as a lambda expression or
408411
* method reference), optionally customizing its bean definition metadata
409-
* (again typically declared as a lambda expression or method reference).
412+
* (again typically declared as a lambda expression).
410413
* <p>This method can be overridden to adapt the registration mechanism for
411414
* all {@code registerBean} methods (since they all delegate to this one).
412415
* @param beanName the name of the bean (may be {@code null})
413416
* @param beanClass the class of the bean
414-
* @param supplier a callback for creating an instance of the bean
417+
* @param supplier a callback for creating an instance of the bean (in case
418+
* of {@code null}, resolving a public constructor to be autowired instead)
415419
* @param customizers one or more callbacks for customizing the factory's
416420
* {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
417421
* @since 5.0
418422
*/
419-
public <T> void registerBean(@Nullable String beanName, Class<T> beanClass, @Nullable Supplier<T> supplier,
420-
BeanDefinitionCustomizer... customizers) {
423+
public <T> void registerBean(@Nullable String beanName, Class<T> beanClass,
424+
@Nullable Supplier<T> supplier, BeanDefinitionCustomizer... customizers) {
421425

422426
BeanDefinitionBuilder builder = (supplier != null ?
423427
BeanDefinitionBuilder.genericBeanDefinition(beanClass, supplier) :

spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,13 +50,17 @@
5050
* while also exposing the original JSR-303 Validator interface itself.
5151
*
5252
* <p>Can be used as a programmatic wrapper. Also serves as base class for
53-
* {@link CustomValidatorBean} and {@link LocalValidatorFactoryBean}.
53+
* {@link CustomValidatorBean} and {@link LocalValidatorFactoryBean},
54+
* and as the primary implementation of the {@link SmartValidator} interface.
5455
*
5556
* <p>As of Spring Framework 5.0, this adapter is fully compatible with
5657
* Bean Validation 1.1 as well as 2.0.
5758
*
5859
* @author Juergen Hoeller
5960
* @since 3.0
61+
* @see SmartValidator
62+
* @see CustomValidatorBean
63+
* @see LocalValidatorFactoryBean
6064
*/
6165
public class SpringValidatorAdapter implements SmartValidator, javax.validation.Validator {
6266

@@ -141,7 +145,7 @@ protected void processConstraintViolations(Set<ConstraintViolation<Object>> viol
141145
// as necessary for Hibernate Validator compatibility (non-indexed set path in field)
142146
BindingResult bindingResult = (BindingResult) errors;
143147
String nestedField = bindingResult.getNestedPath() + field;
144-
if ("".equals(nestedField)) {
148+
if (nestedField.isEmpty()) {
145149
String[] errorCodes = bindingResult.resolveMessageCodes(errorCode);
146150
ObjectError error = new ObjectError(
147151
errors.getObjectName(), errorCodes, errorArgs, violation.getMessage());

spring-context/src/test/java/org/springframework/validation/beanvalidation/ValidatorFactoryTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -284,6 +284,8 @@ public void testListValidation() {
284284
validator.validate(listContainer, errors);
285285

286286
FieldError fieldError = errors.getFieldError("list[1]");
287+
assertNotNull(fieldError);
288+
assertEquals("X", fieldError.getRejectedValue());
287289
assertEquals("X", errors.getFieldValue("list[1]"));
288290
}
289291

0 commit comments

Comments
 (0)