Add unit test for CssClassNameFilter.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 9 Nov 2013 21:14:53 +0000 (22:14 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:58 +0000 (22:25 +0100)
src/test/java/net/pterodactylus/sone/template/CssClassNameFilterTest.java [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/sone/template/CssClassNameFilterTest.java b/src/test/java/net/pterodactylus/sone/template/CssClassNameFilterTest.java
new file mode 100644 (file)
index 0000000..801a59d
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Sone - CssClassNameFilterTest.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.template;
+
+import static org.hamcrest.Matchers.is;
+
+import java.util.Collections;
+
+import org.hamcrest.MatcherAssert;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link CssClassNameFilter}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class CssClassNameFilterTest {
+
+       private final CssClassNameFilter cssClassNameFilter = new CssClassNameFilter();
+
+       @Test
+       public void stringIsSanitized() {
+               String filtered = (String) cssClassNameFilter.format(null, createString(), Collections.<String, Object>emptyMap());
+               MatcherAssert.assertThat(filtered, is(filterString(createString())));
+       }
+
+       private static String filterString(String string) {
+               StringBuffer filteredString = new StringBuffer(string.length());
+               for (char c : string.toCharArray()) {
+                       if (isADigit(c) || isACapitalLetter(c) || isASmallLetter(c) || (c == '-') || (c == '_')) {
+                               filteredString.append(c);
+                       } else {
+                               filteredString.append('_');
+                       }
+               }
+               return filteredString.toString();
+       }
+
+       private static boolean isASmallLetter(char c) {
+               return ((c >= 'a') && (c <= 'z'));
+       }
+
+       private static boolean isACapitalLetter(char c) {
+               return ((c >= 'A') && (c <= 'Z'));
+       }
+
+       private static boolean isADigit(char c) {
+               return ((c >= '0') && (c <= '9'));
+       }
+
+       private static String createString() {
+               StringBuilder string = new StringBuilder();
+               for (int i = 0; i < 256; i++) {
+                       string.append((char) i);
+               }
+               return string.toString();
+       }
+
+}