Browse Source

Merge pull request #345 from kw-udon/dev

Add unit tests for helpers
Shinsuke Sugaya 9 years ago
parent
commit
0161dc07f6

+ 12 - 9
src/test/java/org/codelibs/fess/dict/DictionaryManagerTest.java

@@ -17,37 +17,40 @@ package org.codelibs.fess.dict;
 
 import java.io.File;
 
-import org.apache.commons.io.FileUtils;
-import org.codelibs.core.io.FileUtil;
-import org.codelibs.fess.Constants;
 import org.codelibs.fess.unit.UnitFessTestCase;
 
 public class DictionaryManagerTest extends UnitFessTestCase {
     private File testDir;
 
-    private File synonymFile1;
+    private File file1;
 
     @Override
     public void setUp() throws Exception {
+        /*
         super.setUp();
         testDir = File.createTempFile("synonymtest", "_dir");
         testDir.delete();
         testDir.mkdirs();
-        synonymFile1 = new File(testDir, "synonym.txt");
-        FileUtil.writeBytes(synonymFile1.getAbsolutePath(), "abc=>123\nxyz,890".getBytes(Constants.UTF_8));
+        file1 = new File(testDir, "synonym.txt");
+        FileUtil.writeBytes(file1.getAbsolutePath(), "abc=>123\nxyz,890".getBytes(Constants.UTF_8));
+        */
     }
 
     @Override
     public void tearDown() throws Exception {
+        /*
         super.tearDown();
         FileUtils.deleteDirectory(testDir);
+        */
     }
 
-    public void test_getSynonymFiles() throws Exception {
+    public void test_storeSynonymFiles() throws Exception {
+        // TODO
         /*
         final DictionaryManager dictionaryManager = new DictionaryManager();
-        dictionaryManager.addCreator(new SynonymCreator(synonymFile1.getPath()));
-        dictionaryManager.init();
+        final SynonymCreator synonymCreator = new SynonymCreator();
+        final SynonymFile synonymFile = (SynonymFile) synonymCreator.create(file1.getPath(), new Date());
+        dictionaryManager.store(synonymFile, file1);
         final DictionaryFile<? extends DictionaryItem>[] synonymFiles = dictionaryManager.getDictionaryFiles();
         assertEquals(1, synonymFiles.length);
         */

+ 1 - 0
src/test/java/org/codelibs/fess/dict/synonym/SynonymFileTest.java

@@ -38,6 +38,7 @@ public class SynonymFileTest extends UnitFessTestCase {
     }
 
     public void test_create() {
+        // TODO
         /*
         final SynonymCreator synonymCreator = new SynonymCreator();
         final SynonymFile synonymFile = (SynonymFile) synonymCreator.create(file1.getPath(), new Date());

+ 66 - 0
src/test/java/org/codelibs/fess/helper/CrawlingInfoHelperTest.java

@@ -0,0 +1,66 @@
+/*
+ * Copyright 2012-2015 CodeLibs Project and the Others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.codelibs.fess.helper;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.codelibs.fess.unit.UnitFessTestCase;
+
+public class CrawlingInfoHelperTest extends UnitFessTestCase {
+    private CrawlingInfoHelper crawlingInfoHelper;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        crawlingInfoHelper = new CrawlingInfoHelper();
+    }
+
+    public void test_generateId() {
+        final Map<String, Object> dataMap = new HashMap<String, Object>();
+        dataMap.put("url", "http://example.com/");
+
+        assertEquals("http://example.com/", crawlingInfoHelper.generateId(dataMap));
+
+        final List<String> browserTypeList = new ArrayList<String>();
+        dataMap.put("type", browserTypeList);
+        final List<String> roleTypeList = new ArrayList<String>();
+        dataMap.put("role", roleTypeList);
+
+        assertEquals("http://example.com/", crawlingInfoHelper.generateId(dataMap));
+    }
+
+    public void test_generateId_roleType() {
+        final Map<String, Object> dataMap = new HashMap<String, Object>();
+        dataMap.put("url", "http://example.com/");
+        final List<String> roleTypeList = new ArrayList<String>();
+        roleTypeList.add("admin");
+        dataMap.put("role", roleTypeList);
+
+        assertEquals("http://example.com/;role=admin", crawlingInfoHelper.generateId(dataMap));
+
+        roleTypeList.add("guest");
+
+        assertEquals("http://example.com/;role=admin,guest", crawlingInfoHelper.generateId(dataMap));
+
+        final List<String> browserTypeList = new ArrayList<String>();
+        dataMap.put("type", browserTypeList);
+
+        assertEquals("http://example.com/;role=admin,guest", crawlingInfoHelper.generateId(dataMap));
+    }
+}

+ 81 - 0
src/test/java/org/codelibs/fess/helper/DuplicateHostHelperTest.java

@@ -0,0 +1,81 @@
+/*
+ * Copyright 2012-2015 CodeLibs Project and the Others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.codelibs.fess.helper;
+
+import org.codelibs.fess.unit.UnitFessTestCase;
+
+public class DuplicateHostHelperTest extends UnitFessTestCase {
+    private DuplicateHostHelper duplicateHostHelper;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        duplicateHostHelper = new DuplicateHostHelper();
+        // duplicateHostHelper.init(); // TODO cause error
+    }
+
+    public void test_dummy() {
+    }
+
+    /*
+    public void test_convert() {
+        String url;
+        String result;
+
+        url = "http://foo.com";
+        result = "http://www.foo.com";
+        assertEquals(result, duplicateHostHelper.convert(url));
+
+        url = "http://foo.com/";
+        result = "http://www.foo.com/";
+        assertEquals(result, duplicateHostHelper.convert(url));
+
+        url = "http://foo.com:8080/";
+        result = "http://www.foo.com:8080/";
+        assertEquals(result, duplicateHostHelper.convert(url));
+
+        url = "http://mail.bar.com/";
+        result = "http://www.bar.com/";
+        assertEquals(result, duplicateHostHelper.convert(url));
+
+    }
+
+    public void test_convert_skip() {
+        String url;
+        String result;
+
+        url = "http://www.foo.com";
+        result = "http://www.foo.com";
+        assertEquals(result, duplicateHostHelper.convert(url));
+
+        url = "http://www.foo.com/";
+        result = "http://www.foo.com/";
+        assertEquals(result, duplicateHostHelper.convert(url));
+
+        url = "http://www.foo.com:8080/";
+        result = "http://www.foo.com:8080/";
+        assertEquals(result, duplicateHostHelper.convert(url));
+
+        url = "http://www.bar.com/";
+        result = "http://www.bar.com/";
+        assertEquals(result, duplicateHostHelper.convert(url));
+
+        url = "http://www.bar.com:8080/";
+        result = "http://www.bar.com:8080/";
+        assertEquals(result, duplicateHostHelper.convert(url));
+    }
+    */
+}

+ 111 - 0
src/test/java/org/codelibs/fess/helper/IntervalControlHelperTest.java

@@ -0,0 +1,111 @@
+/*
+ * Copyright 2012-2015 CodeLibs Project and the Others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.codelibs.fess.helper;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+
+import org.codelibs.fess.unit.UnitFessTestCase;
+
+public class IntervalControlHelperTest extends UnitFessTestCase {
+
+    private IntervalControlHelper intervalControlHelper;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        intervalControlHelper = new IntervalControlHelper();
+    }
+
+    public void test_noRule() {
+        assertEquals(0, intervalControlHelper.getDelay());
+    }
+
+    public void test_0000() throws ParseException {
+        final IntervalControlHelper intervalControlHelper = createHelper("00:00", 1);
+        intervalControlHelper.addIntervalRule("01:30", "15:15", "*", 1000);
+        assertEquals(0, intervalControlHelper.getDelay());
+
+        intervalControlHelper.addIntervalRule("16:30", "0:15", "*", 2000);
+        assertEquals(2000, intervalControlHelper.getDelay());
+
+        intervalControlHelper.ruleList.clear();
+
+        intervalControlHelper.addIntervalRule("22:15", "2:45", "1", 3000);
+        assertEquals(0, intervalControlHelper.getDelay());
+
+        intervalControlHelper.addIntervalRule("22:15", "2:45", "2", 4000);
+        assertEquals(4000, intervalControlHelper.getDelay());
+    }
+
+    public void test_1215() throws ParseException {
+        final IntervalControlHelper intervalControlHelper = createHelper("12:15", 1);
+        intervalControlHelper.addIntervalRule("01:30", "12:05", "*", 1000);
+        assertEquals(0, intervalControlHelper.getDelay());
+
+        intervalControlHelper.addIntervalRule("12:30", "23:15", "*", 1000);
+        assertEquals(0, intervalControlHelper.getDelay());
+
+        intervalControlHelper.addIntervalRule("12:05", "12:15", "*", 2000);
+        assertEquals(2000, intervalControlHelper.getDelay());
+
+        intervalControlHelper.ruleList.clear();
+
+        intervalControlHelper.addIntervalRule("12:15", "12:15", "*", 3000);
+        assertEquals(3000, intervalControlHelper.getDelay());
+
+        intervalControlHelper.ruleList.clear();
+
+        intervalControlHelper.addIntervalRule("12:15", "12:15", "3,5", 4000);
+        assertEquals(0, intervalControlHelper.getDelay());
+
+        intervalControlHelper.addIntervalRule("12:10", "12:20", "1", 5000);
+        assertEquals(5000, intervalControlHelper.getDelay());
+
+    }
+
+    public void test_2250() throws ParseException {
+        final IntervalControlHelper intervalControlHelper = createHelper("22:50", 1);
+        intervalControlHelper.addIntervalRule("01:30", "15:15", "*", 1000);
+        assertEquals(0, intervalControlHelper.getDelay());
+
+        intervalControlHelper.addIntervalRule("16:30", "0:15", "*", 2000);
+        assertEquals(2000, intervalControlHelper.getDelay());
+
+        intervalControlHelper.ruleList.clear();
+
+        intervalControlHelper.addIntervalRule("22:15", "2:45", "2", 3000);
+        assertEquals(0, intervalControlHelper.getDelay());
+
+        intervalControlHelper.addIntervalRule("22:15", "2:45", "1", 4000);
+        assertEquals(4000, intervalControlHelper.getDelay());
+    }
+
+    private IntervalControlHelper createHelper(final String time, final int day) throws ParseException {
+        final Date date = new SimpleDateFormat("HH:mm").parse(time);
+        return new IntervalControlHelper() {
+            @Override
+            protected Calendar getCurrentCal() {
+                final Calendar cal = Calendar.getInstance();
+                cal.setTimeInMillis(date.getTime());
+                cal.set(Calendar.DAY_OF_WEEK, day);
+                return cal;
+            }
+        };
+    }
+}

+ 85 - 0
src/test/java/org/codelibs/fess/helper/PathMappingHelperTest.java

@@ -0,0 +1,85 @@
+/*
+ * Copyright 2012-2015 CodeLibs Project and the Others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.codelibs.fess.helper;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.codelibs.fess.es.config.exentity.PathMapping;
+import org.codelibs.fess.unit.UnitFessTestCase;
+
+public class PathMappingHelperTest extends UnitFessTestCase {
+
+    public PathMappingHelper pathMappingHelper;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        pathMappingHelper = new PathMappingHelper();
+        pathMappingHelper.init();
+    }
+
+    public void test_setPathMappingList() {
+        final String sessionId = "test";
+        final List<PathMapping> pathMappingList = new ArrayList<PathMapping>();
+
+        assertNull(pathMappingHelper.getPathMappingList(sessionId));
+        assertNull(pathMappingHelper.getPathMappingList(sessionId + "1"));
+        pathMappingHelper.setPathMappingList(sessionId, pathMappingList);
+        assertNotNull(pathMappingHelper.getPathMappingList(sessionId));
+        assertNull(pathMappingHelper.getPathMappingList(sessionId + "1"));
+        pathMappingHelper.removePathMappingList(sessionId);
+        assertNull(pathMappingHelper.getPathMappingList(sessionId));
+        assertNull(pathMappingHelper.getPathMappingList(sessionId + "1"));
+
+    }
+
+    public void test_replaceUrl() {
+        final String sessionId = "test";
+        final List<PathMapping> pathMappingList = new ArrayList<PathMapping>();
+        final PathMapping pathMapping = new PathMapping();
+        pathMapping.setRegex("file:///home/");
+        pathMapping.setReplacement("http://localhost/");
+        pathMappingList.add(pathMapping);
+
+        pathMappingHelper.setPathMappingList(sessionId, pathMappingList);
+
+        final String url = "file:///home/user/";
+        assertEquals("http://localhost/user/", pathMappingHelper.replaceUrl(sessionId, url));
+    }
+
+    public void test_replaceUrls() {
+        final List<PathMapping> pathMappingList = new ArrayList<PathMapping>();
+        final PathMapping pathMapping = new PathMapping();
+        pathMapping.setRegex("file:///home/");
+        pathMapping.setReplacement("http://localhost/");
+        pathMappingList.add(pathMapping);
+
+        pathMappingHelper.cachedPathMappingList = pathMappingList;
+
+        String text = "\"file:///home/\"";
+        assertEquals("\"http://localhost/\"", pathMappingHelper.replaceUrls(text));
+
+        text = "\"file:///home/user/\"";
+        assertEquals("\"http://localhost/user/\"", pathMappingHelper.replaceUrls(text));
+
+        text = "\"aaafile:///home/user/\"";
+        assertEquals("\"aaahttp://localhost/user/\"", pathMappingHelper.replaceUrls(text));
+
+        text = "aaa\"file:///home/user/\"bbb";
+        assertEquals("aaa\"http://localhost/user/\"bbb", pathMappingHelper.replaceUrls(text));
+    }
+}

+ 1 - 1
src/test/java/org/codelibs/fess/helper/QueryHelperTest.java

@@ -36,7 +36,7 @@ public class QueryHelperTest extends UnitFessTestCase {
     }
 
     public void test_dummy() throws ParseException {
-        System.out.println(queryHelper);
+        System.out.println(queryHelper); // TODO
     }
 
     //    public void test_build() {

+ 86 - 0
src/test/java/org/codelibs/fess/helper/SystemHelperTest.java

@@ -0,0 +1,86 @@
+/*
+ * Copyright 2012-2015 CodeLibs Project and the Others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.codelibs.fess.helper;
+
+import org.codelibs.fess.unit.UnitFessTestCase;
+
+public class SystemHelperTest extends UnitFessTestCase {
+
+    public SystemHelper systemHelper;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        systemHelper = new SystemHelper();
+        systemHelper.init();
+    }
+
+    public void test_encodeUrlFilter() {
+        String path = null;
+        assertNull(systemHelper.encodeUrlFilter(path));
+
+        path = "abc";
+        assertEquals(path, systemHelper.encodeUrlFilter(path));
+
+        path = "あいう";
+        assertEquals("%E3%81%82%E3%81%84%E3%81%86", systemHelper.encodeUrlFilter(path));
+
+        path = "[]^$.*+?,{}|%\\";
+        assertEquals(path, systemHelper.encodeUrlFilter(path));
+    }
+
+    public void test_normalizeLang() {
+        String value = null;
+        assertNull(systemHelper.normalizeLang(value));
+
+        value = "";
+        assertNull(systemHelper.normalizeLang(value));
+
+        value = "_ja";
+        assertNull(systemHelper.normalizeLang(value));
+
+        value = "ja";
+        assertEquals("ja", systemHelper.normalizeLang(value));
+
+        value = " ja ";
+        assertEquals("ja", systemHelper.normalizeLang(value));
+
+        value = "ja-JP";
+        assertEquals("ja", systemHelper.normalizeLang(value));
+
+        value = "ja_JP";
+        assertEquals("ja", systemHelper.normalizeLang(value));
+
+        value = "ja_JP_AAA";
+        assertEquals("ja", systemHelper.normalizeLang(value));
+
+        value = "zh";
+        assertEquals("zh", systemHelper.normalizeLang(value));
+
+        value = "zh-cn";
+        assertEquals("zh_CN", systemHelper.normalizeLang(value));
+
+        value = "zh_CN";
+        assertEquals("zh_CN", systemHelper.normalizeLang(value));
+
+        value = "zh-tw";
+        assertEquals("zh_TW", systemHelper.normalizeLang(value));
+
+        value = "zh_TW";
+        assertEquals("zh_TW", systemHelper.normalizeLang(value));
+    }
+
+}

+ 73 - 0
src/test/java/org/codelibs/fess/helper/UserAgentHelperTest.java

@@ -0,0 +1,73 @@
+/*
+ * Copyright 2012-2015 CodeLibs Project and the Others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.codelibs.fess.helper;
+
+import org.codelibs.fess.helper.UserAgentHelper.UserAgentType;
+import org.codelibs.fess.unit.UnitFessTestCase;
+
+public class UserAgentHelperTest extends UnitFessTestCase {
+
+    public UserAgentHelper userAgentHelper;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        userAgentHelper = new UserAgentHelper();
+    }
+
+    public void test_getUserAgentType_IE9() {
+        getMockRequest().addHeader("user-agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)");
+        assertEquals(UserAgentType.IE, userAgentHelper.getUserAgentType());
+    }
+
+    public void test_getUserAgentType_IE10() {
+        getMockRequest().addHeader("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)");
+        assertEquals(UserAgentType.IE, userAgentHelper.getUserAgentType());
+    }
+
+    public void test_getUserAgentType_IE11() {
+        getMockRequest().addHeader("user-agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko)");
+        assertEquals(UserAgentType.IE, userAgentHelper.getUserAgentType());
+    }
+
+    public void test_getUserAgentType_Chrome() {
+        getMockRequest().addHeader("user-agent",
+                "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36");
+        assertEquals(UserAgentType.CHROME, userAgentHelper.getUserAgentType());
+    }
+
+    public void test_getUserAgentType_FireFox() {
+        getMockRequest().addHeader("user-agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:42.0) Gecko/20100101 Firefox/42.0");
+        assertEquals(UserAgentType.FIREFOX, userAgentHelper.getUserAgentType());
+    }
+
+    public void test_getUserAgentType_Safari() {
+        getMockRequest().addHeader("user-agent",
+                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9");
+        assertEquals(UserAgentType.SAFARI, userAgentHelper.getUserAgentType());
+    }
+
+    public void test_getUserAgentType_Opera() {
+        getMockRequest().addHeader("user-agent", "Opera/9.80 (X11; U; Linux x86_64) Presto/2.9.181 Version/12.00");
+        assertEquals(UserAgentType.OPERA, userAgentHelper.getUserAgentType());
+    }
+
+    public void test_getUserAgentType_OTHER() {
+        getMockRequest().addHeader("user-agent", "Dummy");
+        assertEquals(UserAgentType.OTHER, userAgentHelper.getUserAgentType());
+    }
+
+}

+ 136 - 0
src/test/java/org/codelibs/fess/helper/ViewHelperTest.java

@@ -0,0 +1,136 @@
+/*
+ * Copyright 2012-2015 CodeLibs Project and the Others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.codelibs.fess.helper;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.codelibs.fess.unit.UnitFessTestCase;
+
+public class ViewHelperTest extends UnitFessTestCase {
+    public ViewHelper viewHelper;
+
+    public void setUp() throws Exception {
+        super.setUp();
+        viewHelper = new ViewHelper();
+        viewHelper.init();
+    }
+
+    public void test_replaceHighlightQueries() {
+        String text;
+        String[] queries;
+
+        text = "<a>123<b>456<c>";
+        queries = new String[] { "123", "456" };
+        assertEquals("<a><strong>123</strong><b><strong>456</strong><c>", viewHelper.replaceHighlightQueries(text, queries));
+
+        text = "<123><456>";
+        queries = new String[] { "123", "456" };
+        assertEquals("<123><456>", viewHelper.replaceHighlightQueries(text, queries));
+
+        text = "123<aaa 123>456<bbb 456>123";
+        queries = new String[] { "123", "456" };
+        assertEquals("<strong>123</strong><aaa 123><strong>456</strong><bbb 456><strong>123</strong>",
+                viewHelper.replaceHighlightQueries(text, queries));
+
+        text = "abc";
+        queries = new String[] { "123" };
+        assertEquals("abc", viewHelper.replaceHighlightQueries(text, queries));
+
+        text = "123";
+        queries = new String[] { "123" };
+        assertEquals("<strong>123</strong>", viewHelper.replaceHighlightQueries(text, queries));
+
+        text = "abc123efg";
+        queries = new String[] { "123" };
+        assertEquals("abc<strong>123</strong>efg", viewHelper.replaceHighlightQueries(text, queries));
+
+        text = "123";
+        queries = new String[] { "123", "456" };
+        assertEquals("<strong>123</strong>", viewHelper.replaceHighlightQueries(text, queries));
+
+        text = "123456";
+        queries = new String[] { "123", "456" };
+        assertEquals("<strong>123</strong><strong>456</strong>", viewHelper.replaceHighlightQueries(text, queries));
+
+        text = "a123b456c";
+        queries = new String[] { "123", "456" };
+        assertEquals("a<strong>123</strong>b<strong>456</strong>c", viewHelper.replaceHighlightQueries(text, queries));
+
+        text = "abc";
+        queries = new String[] { "abc" };
+        assertEquals("<strong>abc</strong>", viewHelper.replaceHighlightQueries(text, queries));
+
+        text = "1ABC2";
+        queries = new String[] { "abc" };
+        assertEquals("1<strong>abc</strong>2", viewHelper.replaceHighlightQueries(text, queries));
+    }
+
+    public void test_escapeHighlight() {
+        viewHelper = new ViewHelper();
+        viewHelper.useHighlight = true;
+        viewHelper.init();
+
+        String text = "";
+        assertEquals("", viewHelper.escapeHighlight(text));
+
+        text = "aaa";
+        assertEquals("aaa", viewHelper.escapeHighlight(text));
+
+        text = "<em>aaa</em>";
+        assertEquals("<em>aaa</em>", viewHelper.escapeHighlight(text));
+
+        text = "<em>aaa</em><b>bbb</b>";
+        assertEquals("<em>aaa</em>&lt;b&gt;bbb&lt;/b&gt;", viewHelper.escapeHighlight(text));
+
+    }
+
+    public void test_getSitePath() {
+        String urlLink;
+        String sitePath;
+        final Map<String, Object> docMap = new HashMap<>();
+
+        urlLink = "http://www.google.com";
+        sitePath = "www.google.com";
+        docMap.put("urlLink", urlLink);
+        assertEquals(sitePath, viewHelper.getSitePath(docMap));
+
+        urlLink = "https://www.jp.websecurity.symantec.com/";
+        sitePath = "www.jp.websecurity.symantec.com/";
+        docMap.put("urlLink", urlLink);
+        assertEquals(sitePath, viewHelper.getSitePath(docMap));
+
+        urlLink = "http://www.qwerty.jp";
+        sitePath = "www.qwerty.jp";
+        docMap.put("urlLink", urlLink);
+        assertEquals(sitePath, viewHelper.getSitePath(docMap));
+
+        urlLink = "www.google.com";
+        sitePath = "www.google.com";
+        docMap.put("urlLink", urlLink);
+        assertEquals(sitePath, viewHelper.getSitePath(docMap));
+
+        urlLink = "smb://123.45.678.91/share1";
+        sitePath = "123.45.678.91/share1";
+        docMap.put("urlLink", urlLink);
+        assertEquals(sitePath, viewHelper.getSitePath(docMap));
+
+        urlLink = "file://home/user/";
+        sitePath = "home/user/";
+        docMap.put("urlLink", urlLink);
+        assertEquals(sitePath, viewHelper.getSitePath(docMap));
+    }
+}

+ 366 - 0
src/test/java/org/codelibs/fess/helper/impl/RoleQueryHelperImplTest.java

@@ -0,0 +1,366 @@
+/*
+ * Copyright 2012-2015 CodeLibs Project and the Others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.codelibs.fess.helper.impl;
+
+import org.codelibs.core.crypto.CachedCipher;
+import org.codelibs.fess.helper.SystemHelper;
+import org.codelibs.fess.mylasta.direction.FessConfig;
+import org.codelibs.fess.unit.UnitFessTestCase;
+import org.codelibs.fess.util.ComponentUtil;
+
+public class RoleQueryHelperImplTest extends UnitFessTestCase {
+    public RoleQueryHelperImpl roleQueryHelperImpl;
+    public CachedCipher cipher;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        roleQueryHelperImpl = new RoleQueryHelperImpl();
+        cipher = new CachedCipher();
+        final FessConfig fessConfig = ComponentUtil.getFessConfig();
+        registerMockInstance(fessConfig);
+        registerMockInstance(new SystemHelper());
+        inject(roleQueryHelperImpl);
+    }
+
+    public void test_dummy() {
+        // TODO
+    }
+
+    /*
+     * 
+    public void test_buildByParameter() {
+        final RoleQueryHelperImpl roleQueryHelperImpl = new RoleQueryHelperImpl();
+
+        Set<String> roleSet;
+
+        roleSet = roleQueryHelperImpl.buildByParameter(getRequest());
+        assertEquals(0, roleSet.size());
+
+        roleQueryHelperImpl.parameterKey = "fess1";
+
+        getRequest().setParameter("aaa", "bbb");
+        roleSet = roleQueryHelperImpl.buildByParameter(getRequest());
+        assertEquals(0, roleSet.size());
+
+        roleQueryHelperImpl.encryptedParameterValue = false;
+        getRequest().setParameter("fess1", "xxx\nrole1,role2,role3");
+        roleSet = roleQueryHelperImpl.buildByParameter(getRequest());
+        assertEquals(3, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+        assertTrue(roleSet.contains("role2"));
+        assertTrue(roleSet.contains("role3"));
+
+        roleQueryHelperImpl.parameterKey = "fess2";
+
+        roleQueryHelperImpl.cipher = cipher;
+        roleQueryHelperImpl.encryptedParameterValue = true;
+        getRequest().setParameter("fess2",
+                cipher.encryptoText("xxx\nrole1,role2,role3"));
+        roleSet = roleQueryHelperImpl.buildByParameter(getRequest());
+        assertEquals(3, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+        assertTrue(roleSet.contains("role2"));
+        assertTrue(roleSet.contains("role3"));
+
+        getRequest().setParameter("fess2", "fail");
+        try {
+            roleSet = roleQueryHelperImpl.buildByParameter(getRequest());
+            fail();
+        } catch (final IllegalBlockSizeRuntimeException e) {
+            // ok
+        }
+
+        roleQueryHelperImpl.parameterKey = "fess3";
+
+        roleQueryHelperImpl.encryptedParameterValue = false;
+        roleSet = roleQueryHelperImpl.buildByParameter(getRequest());
+        assertEquals(0, roleSet.size());
+
+        roleQueryHelperImpl.parameterKey = "fess4";
+
+        roleQueryHelperImpl.cipher = cipher;
+        roleQueryHelperImpl.encryptedParameterValue = true;
+        roleSet = roleQueryHelperImpl.buildByParameter(getRequest());
+        assertEquals(0, roleSet.size());
+
+    }
+
+    public void test_buildByHeader() {
+        final RoleQueryHelperImpl roleQueryHelperImpl = new RoleQueryHelperImpl();
+
+        Set<String> roleSet;
+
+        try {
+            roleSet = roleQueryHelperImpl.buildByHeader(getRequest());
+            fail();
+        } catch (final NullPointerException e) {
+            //ok
+        }
+
+        roleQueryHelperImpl.headerKey = "fess1";
+
+        getRequest().addHeader("aaa", "bbb");
+        roleSet = roleQueryHelperImpl.buildByHeader(getRequest());
+        assertEquals(0, roleSet.size());
+
+        roleQueryHelperImpl.encryptedHeaderValue = false;
+        getRequest().addHeader("fess1", "xxx\nrole1,role2,role3");
+        roleSet = roleQueryHelperImpl.buildByHeader(getRequest());
+        assertEquals(3, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+        assertTrue(roleSet.contains("role2"));
+        assertTrue(roleSet.contains("role3"));
+
+        roleQueryHelperImpl.headerKey = "fess2";
+
+        roleQueryHelperImpl.cipher = cipher;
+        roleQueryHelperImpl.encryptedHeaderValue = true;
+        getRequest().addHeader("fess2",
+                cipher.encryptoText("xxx\nrole1,role2,role3"));
+        roleSet = roleQueryHelperImpl.buildByHeader(getRequest());
+        assertEquals(3, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+        assertTrue(roleSet.contains("role2"));
+        assertTrue(roleSet.contains("role3"));
+
+        roleQueryHelperImpl.headerKey = "fess2x";
+        getRequest().addHeader("fess2x", "fail");
+        try {
+            roleSet = roleQueryHelperImpl.buildByHeader(getRequest());
+            fail();
+        } catch (final IllegalBlockSizeRuntimeException e) {
+            // ok
+        }
+
+        roleQueryHelperImpl.headerKey = "fess3";
+
+        roleQueryHelperImpl.encryptedHeaderValue = false;
+        roleSet = roleQueryHelperImpl.buildByHeader(getRequest());
+        assertEquals(0, roleSet.size());
+
+        roleQueryHelperImpl.headerKey = "fess4";
+
+        roleQueryHelperImpl.cipher = cipher;
+        roleQueryHelperImpl.encryptedHeaderValue = true;
+        roleSet = roleQueryHelperImpl.buildByHeader(getRequest());
+        assertEquals(0, roleSet.size());
+    }
+
+    public void test_buildByCookie() {
+        final RoleQueryHelperImpl roleQueryHelperImpl = new RoleQueryHelperImpl();
+
+        Set<String> roleSet;
+        Cookie cookie;
+
+        roleSet = roleQueryHelperImpl.buildByCookie(getRequest());
+        assertEquals(0, roleSet.size());
+
+        cookie = new Cookie("aaa", "bbb");
+        getRequest().addCookie(cookie);
+        try {
+            roleSet = roleQueryHelperImpl.buildByCookie(getRequest());
+            fail();
+        } catch (final NullPointerException e) {
+            // ok
+        }
+
+        roleQueryHelperImpl.cookieKey = "fess1";
+
+        roleSet = roleQueryHelperImpl.buildByCookie(getRequest());
+        assertEquals(0, roleSet.size());
+
+        roleQueryHelperImpl.encryptedCookieValue = false;
+        cookie = new Cookie("fess1", "xxx\nrole1,role2,role3");
+        getRequest().addCookie(cookie);
+        roleSet = roleQueryHelperImpl.buildByCookie(getRequest());
+        assertEquals(3, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+        assertTrue(roleSet.contains("role2"));
+        assertTrue(roleSet.contains("role3"));
+
+        roleQueryHelperImpl.cookieKey = "fess2";
+
+        roleQueryHelperImpl.cipher = cipher;
+        roleQueryHelperImpl.encryptedCookieValue = true;
+        cookie = new Cookie("fess2",
+                cipher.encryptoText("xxx\nrole1,role2,role3"));
+        getRequest().addCookie(cookie);
+        roleSet = roleQueryHelperImpl.buildByCookie(getRequest());
+        assertEquals(3, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+        assertTrue(roleSet.contains("role2"));
+        assertTrue(roleSet.contains("role3"));
+
+        roleQueryHelperImpl.cookieKey = "fess2x";
+
+        roleQueryHelperImpl.cipher = cipher;
+        roleQueryHelperImpl.encryptedCookieValue = true;
+        cookie = new Cookie("fess2x", "fail");
+        getRequest().addCookie(cookie);
+        try {
+            roleSet = roleQueryHelperImpl.buildByCookie(getRequest());
+            fail();
+        } catch (final Exception e) {
+            // ok
+        }
+
+        roleQueryHelperImpl.cookieKey = "fess3";
+
+        roleQueryHelperImpl.encryptedCookieValue = false;
+        roleSet = roleQueryHelperImpl.buildByCookie(getRequest());
+        assertEquals(0, roleSet.size());
+
+        roleQueryHelperImpl.cookieKey = "fess4";
+
+        roleQueryHelperImpl.cipher = cipher;
+        roleQueryHelperImpl.encryptedCookieValue = true;
+        roleSet = roleQueryHelperImpl.buildByCookie(getRequest());
+        assertEquals(0, roleSet.size());
+    }
+
+    public void test_decodedRoleList() {
+
+        final RoleQueryHelperImpl roleQueryHelperImpl = new RoleQueryHelperImpl();
+
+        Set<String> roleSet;
+        boolean encrypted;
+        String value;
+
+        encrypted = false;
+        value = "";
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(0, roleSet.size());
+
+        encrypted = false;
+        value = "role1";
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(0, roleSet.size());
+
+        encrypted = false;
+        value = "role1,role2";
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(0, roleSet.size());
+
+        encrypted = false;
+        value = "xxx\nrole1";
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(1, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+
+        encrypted = false;
+        value = "xxx\nrole1,role2";
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(2, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+        assertTrue(roleSet.contains("role2"));
+
+        roleQueryHelperImpl.valueSeparator = "";
+
+        encrypted = false;
+        value = "";
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(0, roleSet.size());
+
+        encrypted = false;
+        value = "role1";
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(1, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+
+        encrypted = false;
+        value = "role1,role2";
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(2, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+        assertTrue(roleSet.contains("role2"));
+
+        encrypted = false;
+        value = "role1,role2,role3";
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(3, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+        assertTrue(roleSet.contains("role2"));
+        assertTrue(roleSet.contains("role3"));
+    }
+
+    public void test_decodedRoleList_withCipher() {
+
+        final RoleQueryHelperImpl roleQueryHelperImpl = new RoleQueryHelperImpl();
+        roleQueryHelperImpl.cipher = cipher;
+
+        Set<String> roleSet;
+        boolean encrypted;
+        String value;
+
+        encrypted = true;
+        value = cipher.encryptoText("");
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(0, roleSet.size());
+
+        encrypted = true;
+        value = cipher.encryptoText("role1");
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(0, roleSet.size());
+
+        encrypted = true;
+        value = cipher.encryptoText("role1,role2");
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(0, roleSet.size());
+
+        encrypted = true;
+        value = cipher.encryptoText("xxx\nrole1");
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(1, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+
+        encrypted = true;
+        value = cipher.encryptoText("xxx\nrole1,role2");
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(2, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+        assertTrue(roleSet.contains("role2"));
+
+        roleQueryHelperImpl.valueSeparator = "";
+
+        encrypted = true;
+        value = cipher.encryptoText("");
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(0, roleSet.size());
+
+        encrypted = true;
+        value = cipher.encryptoText("role1");
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(1, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+
+        encrypted = true;
+        value = cipher.encryptoText("role1,role2");
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(2, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+        assertTrue(roleSet.contains("role2"));
+
+        encrypted = true;
+        value = cipher.encryptoText("role1,role2,role3");
+        roleSet = roleQueryHelperImpl.decodedRoleList(value, encrypted);
+        assertEquals(3, roleSet.size());
+        assertTrue(roleSet.contains("role1"));
+        assertTrue(roleSet.contains("role2"));
+        assertTrue(roleSet.contains("role3"));
+        }
+     */
+}