Skip to content

Commit c8f2dca

Browse files
bodom91php-coder
authored andcommitted
/category/add: prohibit repeated special characters.
Fix #465
1 parent 7e4c71c commit c8f2dca

File tree

8 files changed

+104
-7
lines changed

8 files changed

+104
-7
lines changed

src/main/java/ru/mystamps/web/controller/CategoryController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import javax.servlet.http.HttpServletResponse;
2525
import javax.validation.Valid;
2626

27-
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
2827
import org.springframework.http.HttpStatus;
2928
import org.springframework.stereotype.Controller;
3029
import org.springframework.ui.Model;
@@ -43,6 +42,7 @@
4342
import ru.mystamps.web.Url;
4443
import ru.mystamps.web.controller.converter.annotation.Category;
4544
import ru.mystamps.web.controller.converter.annotation.CurrentUser;
45+
import ru.mystamps.web.controller.editor.ReplaceRepeatingSpacesEditor;
4646
import ru.mystamps.web.dao.dto.LinkEntityDto;
4747
import ru.mystamps.web.dao.dto.SeriesInfoDto;
4848
import ru.mystamps.web.model.AddCategoryForm;
@@ -61,7 +61,9 @@ public class CategoryController {
6161

6262
@InitBinder("addCategoryForm")
6363
protected void initBinder(WebDataBinder binder) {
64-
StringTrimmerEditor editor = new StringTrimmerEditor(false);
64+
// CheckStyle: ignore LineLength for next 1 line
65+
// We can't use StringTrimmerEditor here because "only one single registered custom editor per property path is supported".
66+
ReplaceRepeatingSpacesEditor editor = new ReplaceRepeatingSpacesEditor(true);
6567
binder.registerCustomEditor(String.class, "name", editor);
6668
binder.registerCustomEditor(String.class, "nameRu", editor);
6769
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2009-2016 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.controller.editor;
19+
20+
import java.beans.PropertyEditorSupport;
21+
import java.util.regex.Pattern;
22+
23+
import lombok.RequiredArgsConstructor;
24+
25+
/**
26+
* @author Maxim Shestakov
27+
*/
28+
@RequiredArgsConstructor
29+
public class ReplaceRepeatingSpacesEditor extends PropertyEditorSupport {
30+
private static final Pattern REPEATING_SPACES = Pattern.compile("[ ]{2,}");
31+
private final boolean performTrimming;
32+
33+
@Override
34+
public void setAsText(String name) throws IllegalArgumentException {
35+
String text = name;
36+
37+
if (performTrimming) {
38+
text = name.trim();
39+
}
40+
41+
if (text.contains(" ")) {
42+
text = REPEATING_SPACES.matcher(text).replaceAll(" ");
43+
}
44+
45+
setValue(text);
46+
}
47+
48+
}

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static ru.mystamps.web.validation.ValidationRules.CATEGORY_NAME_MAX_LENGTH;
3535
import static ru.mystamps.web.validation.ValidationRules.CATEGORY_NAME_MIN_LENGTH;
3636
import static ru.mystamps.web.validation.ValidationRules.CATEGORY_NAME_NO_HYPHEN_REGEXP;
37+
import static ru.mystamps.web.validation.ValidationRules.CATEGORY_NAME_NO_REPEATING_HYPHENS_REGEXP;
3738
import static ru.mystamps.web.validation.ValidationRules.CATEGORY_NAME_RU_REGEXP;
3839

3940
@Getter
@@ -44,7 +45,8 @@
4445
Group.Level2.class,
4546
Group.Level3.class,
4647
Group.Level4.class,
47-
Group.Level5.class
48+
Group.Level5.class,
49+
Group.Level6.class
4850
})
4951
public class AddCategoryForm implements AddCategoryDto {
5052

@@ -67,13 +69,18 @@ public class AddCategoryForm implements AddCategoryDto {
6769
message = "{category-name-en.invalid}",
6870
groups = Group.Level3.class
6971
),
72+
@Pattern(
73+
regexp = CATEGORY_NAME_NO_REPEATING_HYPHENS_REGEXP,
74+
message = "{value.repeating_hyphen}",
75+
groups = Group.Level4.class
76+
),
7077
@Pattern(
7178
regexp = CATEGORY_NAME_NO_HYPHEN_REGEXP,
7279
message = "{value.hyphen}",
73-
groups = Group.Level4.class
80+
groups = Group.Level5.class
7481
)
7582
})
76-
@UniqueCategoryName(lang = Lang.EN, groups = Group.Level5.class)
83+
@UniqueCategoryName(lang = Lang.EN, groups = Group.Level6.class)
7784
private String name;
7885

7986
@NotEmpty(groups = Group.Level1.class)
@@ -95,13 +102,18 @@ public class AddCategoryForm implements AddCategoryDto {
95102
message = "{category-name-ru.invalid}",
96103
groups = Group.Level3.class
97104
),
105+
@Pattern(
106+
regexp = CATEGORY_NAME_NO_REPEATING_HYPHENS_REGEXP,
107+
message = "{value.repeating_hyphen}",
108+
groups = Group.Level4.class
109+
),
98110
@Pattern(
99111
regexp = CATEGORY_NAME_NO_HYPHEN_REGEXP,
100112
message = "{value.hyphen}",
101-
groups = Group.Level4.class
113+
groups = Group.Level5.class
102114
)
103115
})
104-
@UniqueCategoryName(lang = Lang.RU, groups = Group.Level5.class)
116+
@UniqueCategoryName(lang = Lang.RU, groups = Group.Level6.class)
105117
private String nameRu;
106118

107119
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,8 @@ interface Level4 {
3333

3434
interface Level5 {
3535
}
36+
37+
interface Level6 {
38+
}
3639

3740
}

src/main/java/ru/mystamps/web/validation/ValidationRules.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public final class ValidationRules {
4242
public static final String CATEGORY_NAME_EN_REGEXP = "[- a-zA-Z]+";
4343
public static final String CATEGORY_NAME_RU_REGEXP = "[- а-яёА-ЯЁ]+";
4444
public static final String CATEGORY_NAME_NO_HYPHEN_REGEXP = "[ \\p{L}]([- \\p{L}]+[ \\p{L}])*";
45+
@SuppressWarnings({"PMD.LongVariable", "checkstyle:linelength"})
46+
public static final String CATEGORY_NAME_NO_REPEATING_HYPHENS_REGEXP = "(?!.+[-]{2,}).+";
4547

4648
public static final int COUNTRY_NAME_MIN_LENGTH = 3;
4749
public static final int COUNTRY_NAME_MAX_LENGTH = Db.Country.NAME_LENGTH;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ value.too-short = Value is less than allowable minimum of {min} characters
2424
value.too-long = Value is greater than allowable maximum of {max} characters
2525
value.invalid-length = Value length must be equals to {max} characters
2626
value.hyphen = Value must not start or end with hyphen
27+
value.repeating_hyphen = Value must not contain repetition of hyphen
2728
value.empty = Value must not be empty
2829

2930
category-name-en.invalid = Category name must consist only latin letters, hyphen or spaces

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ value.too-short = Значение должно быть не менее {min}
2424
value.too-long = Значение должно быть не более {max} символов
2525
value.invalid-length = Значение должно быть длинной {max} символов
2626
value.hyphen = Значение не должно начинаться или заканчиваться знаком дефиса
27+
value.repeating_hyphen = Значение не должно содержать повторяющиеся знаки дефиса
2728
value.empty = Значение не должно быть пустым
2829

2930
category-name-en.invalid = Название категории может содержать только латинские буквы, дефис или пробел

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,20 @@ public void categoryNameRuShouldNotEndsWithHyphen() {
184184

185185
assertThat(page).field("nameRu").hasError(tr("value.hyphen"));
186186
}
187+
188+
@Test(groups = "invalid", dependsOnGroups = "std")
189+
public void categoryNameEnShouldNotContainRepeatedHyphens() {
190+
page.addCategory("te--st", TEST_CATEGORY_NAME_RU);
191+
192+
assertThat(page).field("name").hasError(tr("value.repeating_hyphen"));
193+
}
194+
195+
@Test(groups = "invalid", dependsOnGroups = "std")
196+
public void categoryNameRuShouldNotContainRepeatedHyphens() {
197+
page.addCategory(TEST_CATEGORY_NAME_EN, "те--ст");
198+
199+
assertThat(page).field("nameRu").hasError(tr("value.repeating_hyphen"));
200+
}
187201

188202
@Test(groups = "misc", dependsOnGroups = "std")
189203
public void categoryNameEnShouldBeStripedFromLeadingAndTrailingSpaces() {
@@ -198,6 +212,20 @@ public void categoryNameRuShouldBeStripedFromLeadingAndTrailingSpaces() {
198212

199213
assertThat(page).field("nameRu").hasValue("т3ст");
200214
}
215+
216+
@Test(groups = "misc", dependsOnGroups = "std")
217+
public void categoryNameEnShouldReplaceRepeatedSpacesByOne() {
218+
page.addCategory("t3 st", TEST_CATEGORY_NAME_RU);
219+
220+
assertThat(page).field("name").hasValue("t3 st");
221+
}
222+
223+
@Test(groups = "misc", dependsOnGroups = "std")
224+
public void categoryNameRuShouldReplaceRepeatedSpacesByOne() {
225+
page.addCategory(TEST_CATEGORY_NAME_EN, "т3 ст");
226+
227+
assertThat(page).field("nameRu").hasValue("т3 ст");
228+
}
201229

202230
@Test(groups = "logic", dependsOnGroups = { "std", "invalid", "valid", "misc" })
203231
public void shouldBeRedirectedToPageWithInfoAboutCategoryAfterCreation() {

0 commit comments

Comments
 (0)