From 0057eac5b99b1f7fcb91b015d2733f900a7e6a34 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 9 Nov 2013 22:14:53 +0100 Subject: [PATCH] Add unit test for CssClassNameFilter. --- .../sone/template/CssClassNameFilterTest.java | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/template/CssClassNameFilterTest.java 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 index 0000000..801a59d --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/template/CssClassNameFilterTest.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +public class CssClassNameFilterTest { + + private final CssClassNameFilter cssClassNameFilter = new CssClassNameFilter(); + + @Test + public void stringIsSanitized() { + String filtered = (String) cssClassNameFilter.format(null, createString(), Collections.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(); + } + +} -- 2.7.4