Skip to content

Commit 17c4d42

Browse files
committed
Introduce and use @CatalogNumbers validation constraint.
Inspired by PMD warnings about too many string usage. No functional changes.
1 parent 2eabb30 commit 17c4d42

File tree

5 files changed

+55
-11
lines changed

5 files changed

+55
-11
lines changed

src/main/java/ru/mystamps/web/model/AddSeriesForm.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@
2121
import javax.validation.constraints.Max;
2222
import javax.validation.constraints.Min;
2323
import javax.validation.constraints.NotNull;
24-
import javax.validation.constraints.Pattern;
2524
import javax.validation.constraints.Size;
2625

2726
import org.springframework.web.multipart.MultipartFile;
2827

28+
import ru.mystamps.web.validation.jsr303.CatalogNumbers;
2929
import ru.mystamps.web.validation.jsr303.NotEmptyFilename;
3030

3131
import static ru.mystamps.web.validation.ValidationRules.MAX_SERIES_COMMENT_LENGTH;
3232
import static ru.mystamps.web.validation.ValidationRules.MAX_STAMPS_IN_SERIES;
3333
import static ru.mystamps.web.validation.ValidationRules.MIN_STAMPS_IN_SERIES;
34-
import static ru.mystamps.web.validation.ValidationRules.CATALOG_NUMBERS_REGEXP;
3534

3635
import lombok.Getter;
3736
import lombok.Setter;
@@ -50,16 +49,16 @@ public class AddSeriesForm {
5049
@NotNull
5150
private Boolean perforated;
5251

53-
@Pattern(regexp = CATALOG_NUMBERS_REGEXP, message = "{catalog-numbers.invalid}")
52+
@CatalogNumbers
5453
private String michelNumbers;
5554

56-
@Pattern(regexp = CATALOG_NUMBERS_REGEXP, message = "{catalog-numbers.invalid}")
55+
@CatalogNumbers
5756
private String scottNumbers;
5857

59-
@Pattern(regexp = CATALOG_NUMBERS_REGEXP, message = "{catalog-numbers.invalid}")
58+
@CatalogNumbers
6059
private String yvertNumbers;
6160

62-
@Pattern(regexp = CATALOG_NUMBERS_REGEXP, message = "{catalog-numbers.invalid}")
61+
@CatalogNumbers
6362
private String gibbonsNumbers;
6463

6564
@Size(max = MAX_SERIES_COMMENT_LENGTH, message = "{value.too-long}")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (C) 2009-2012 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.validation.jsr303;
19+
20+
import java.lang.annotation.Documented;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.Target;
23+
24+
import javax.validation.Constraint;
25+
import javax.validation.Payload;
26+
import javax.validation.ReportAsSingleViolation;
27+
import javax.validation.constraints.Pattern;
28+
29+
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
30+
import static java.lang.annotation.ElementType.FIELD;
31+
import static java.lang.annotation.ElementType.METHOD;
32+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
33+
34+
import ru.mystamps.web.validation.ValidationRules;
35+
36+
@Pattern(regexp = ValidationRules.CATALOG_NUMBERS_REGEXP)
37+
@ReportAsSingleViolation
38+
@Target({ METHOD, FIELD, ANNOTATION_TYPE })
39+
@Retention(RUNTIME)
40+
@Constraint(validatedBy = {})
41+
@Documented
42+
public @interface CatalogNumbers {
43+
String message() default "{ru.mystamps.web.validation.jsr303.CatalogNumbers.message}";
44+
Class<?>[] groups() default {};
45+
Class<? extends Payload>[] payload() default {};
46+
}

src/main/resources/ru/mystamps/i18n/ValidationMessages.properties

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ ru.mystamps.web.validation.jsr303.UniqueCountryName.message = Country already ex
1111
ru.mystamps.web.validation.jsr303.ExistingActivationKey.message = Wrong activation key
1212
ru.mystamps.web.validation.jsr303.Email.message = Wrong e-mail address
1313
ru.mystamps.web.validation.jsr303.NotEmptyFilename.message = Value must not be empty
14+
ru.mystamps.web.validation.jsr303.CatalogNumbers.message = Value must be comma delimited numbers
1415

1516
value.too-short = Value is less than allowable minimum of {min} characters
1617
value.too-long = Value is greater than allowable maximum of {max} characters
1718
value.invalid-length = Value length must be equals to {max} characters
1819

19-
catalog-numbers.invalid = Value must be comma delimited numbers
20-
2120
country-name.invalid = Country name must consist only latin letters, hyphen or spaces
2221
country-name.hyphen = Country name must not start or end with hyphen
2322

src/main/resources/ru/mystamps/i18n/ValidationMessages_ru.properties

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ ru.mystamps.web.validation.jsr303.UniqueCountryName.message = Страна уж
1111
ru.mystamps.web.validation.jsr303.ExistingActivationKey.message = Неправильный код активации
1212
ru.mystamps.web.validation.jsr303.Email.message = Неправильный адрес электронной почты
1313
ru.mystamps.web.validation.jsr303.NotEmptyFilename.message = Поле обязательно для заполнения
14+
ru.mystamps.web.validation.jsr303.CatalogNumbers.message = Значение должно быть списком чисел через запятую
1415

1516
value.too-short = Значение должно быть не менее {min} символов
1617
value.too-long = Значение должно быть не более {max} символов
1718
value.invalid-length = Значение должно быть длинной {max} символов
1819

19-
catalog-numbers.invalid = Значение должно быть списком чисел через запятую
20-
2120
country-name.invalid = Название страны может содержать только латинские буквы, дефис или пробел
2221
country-name.hyphen = Название страны не должно начинаться или заканчиваться знаком дефиса
2322

src/test/java/ru/mystamps/web/tests/cases/WhenUserAddSeries.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ public Object[][] getValidCatalogNumbers() {
389389

390390
@DataProvider(name = "invalidCatalogNumbers")
391391
public Object[][] getInvalidCatalogNumbers() {
392-
final String expectedErrorMessage = tr("catalog-numbers.invalid");
392+
final String expectedErrorMessage =
393+
tr("ru.mystamps.web.validation.jsr303.CatalogNumbers.message");
393394

394395
return new Object[][] {
395396
{"t", expectedErrorMessage},

0 commit comments

Comments
 (0)