2 * Sone - CssClassNameFilterTest.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.template;
20 import static org.hamcrest.Matchers.is;
22 import java.util.Collections;
24 import org.hamcrest.MatcherAssert;
25 import org.junit.Test;
28 * Unit test for {@link CssClassNameFilter}.
30 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32 public class CssClassNameFilterTest {
34 private final CssClassNameFilter cssClassNameFilter = new CssClassNameFilter();
37 public void stringIsSanitized() {
38 String filtered = (String) cssClassNameFilter.format(null, createString(), Collections.<String, Object>emptyMap());
39 MatcherAssert.assertThat(filtered, is(filterString(createString())));
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);
48 filteredString.append('_');
51 return filteredString.toString();
54 private static boolean isASmallLetter(char c) {
55 return ((c >= 'a') && (c <= 'z'));
58 private static boolean isACapitalLetter(char c) {
59 return ((c >= 'A') && (c <= 'Z'));
62 private static boolean isADigit(char c) {
63 return ((c >= '0') && (c <= '9'));
66 private static String createString() {
67 StringBuilder string = new StringBuilder();
68 for (int i = 0; i < 256; i++) {
69 string.append((char) i);
71 return string.toString();