Add unit test for CssClassNameFilter.
[Sone.git] / src / test / java / net / pterodactylus / sone / template / CssClassNameFilterTest.java
1 /*
2  * Sone - CssClassNameFilterTest.java - Copyright © 2013 David Roden
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 3 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, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.template;
19
20 import static org.hamcrest.Matchers.is;
21
22 import java.util.Collections;
23
24 import org.hamcrest.MatcherAssert;
25 import org.junit.Test;
26
27 /**
28  * Unit test for {@link CssClassNameFilter}.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class CssClassNameFilterTest {
33
34         private final CssClassNameFilter cssClassNameFilter = new CssClassNameFilter();
35
36         @Test
37         public void stringIsSanitized() {
38                 String filtered = (String) cssClassNameFilter.format(null, createString(), Collections.<String, Object>emptyMap());
39                 MatcherAssert.assertThat(filtered, is(filterString(createString())));
40         }
41
42         private static String filterString(String string) {
43                 StringBuffer filteredString = new StringBuffer(string.length());
44                 for (char c : string.toCharArray()) {
45                         if (isADigit(c) || isACapitalLetter(c) || isASmallLetter(c) || (c == '-') || (c == '_')) {
46                                 filteredString.append(c);
47                         } else {
48                                 filteredString.append('_');
49                         }
50                 }
51                 return filteredString.toString();
52         }
53
54         private static boolean isASmallLetter(char c) {
55                 return ((c >= 'a') && (c <= 'z'));
56         }
57
58         private static boolean isACapitalLetter(char c) {
59                 return ((c >= 'A') && (c <= 'Z'));
60         }
61
62         private static boolean isADigit(char c) {
63                 return ((c >= '0') && (c <= '9'));
64         }
65
66         private static String createString() {
67                 StringBuilder string = new StringBuilder();
68                 for (int i = 0; i < 256; i++) {
69                         string.append((char) i);
70                 }
71                 return string.toString();
72         }
73
74 }