1 /*******************************************************************************
2  * Copyright (c) 2007, 2016 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 
15 package org.eclipse.ua.tests.help.toc;
16 
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
22 
23 import org.eclipse.help.IToc;
24 import org.eclipse.help.ITopic;
25 import org.eclipse.help.internal.HelpPlugin;
26 import org.eclipse.help.internal.base.scope.FilterScope;
27 import org.eclipse.help.internal.base.scope.UniversalScope;
28 import org.eclipse.help.internal.webapp.data.TocData;
29 import org.eclipse.help.internal.webapp.data.TopicFinder;
30 import org.eclipse.help.internal.webapp.data.UrlUtil;
31 import org.junit.Test;
32 
33 public class TopicFinderTest {
34 
getTocs()35 	private IToc[] getTocs() {
36 		return HelpPlugin.getTocManager().getTocs("en");
37 	}
38 
39 	@Test
testTocsFound()40 	public void testTocsFound() {
41 		assertTrue(getTocs().length != 0);
42 	}
43 
44 	@Test
testNoTocs()45 	public void testNoTocs() {
46 		TopicFinder finder = new TopicFinder("http:", new IToc[0], new UniversalScope());
47 		assertEquals(-1, finder.getSelectedToc());
48 		assertNull(finder.getTopicPath());
49 	}
50 
51 	@Test
testNoTopic()52 	public void testNoTopic() {
53 		TopicFinder finder = new TopicFinder(null, getTocs(), new UniversalScope());
54 		assertEquals(-1, finder.getSelectedToc());
55 		assertNull(finder.getTopicPath());
56 	}
57 
58 	@Test
testTopicInToc()59 	public void testTopicInToc() {
60 		String topic = "http://localhost:8082/help/topic/org.eclipse.ua.tests/data/help/manual/filter.xhtml";
61 		TopicFinder finder = new TopicFinder(topic, getTocs(), new UniversalScope());
62 		int selectedToc = finder.getSelectedToc();
63 		assertFalse(selectedToc == -1);
64 		ITopic[] path = finder.getTopicPath();
65 		assertNotNull(path);
66 		String tocHref = getTocs()[selectedToc].getHref();
67 		assertEquals("/org.eclipse.ua.tests/data/help/toc/root.xml", tocHref);
68 		assertEquals(2, path.length);
69 		assertEquals("manual", path[0].getLabel());
70 		assertEquals("filter", path[1].getLabel());
71 	}
72 
73 	@Test
testTopicInTocWithAnchor()74 	public void testTopicInTocWithAnchor() {
75 		String topic = "http://localhost:8082/help/topic/org.eclipse.ua.tests/data/help/manual/filter.xhtml#ABC";
76 		TopicFinder finder = new TopicFinder(topic, getTocs(), new UniversalScope());
77 		int selectedToc = finder.getSelectedToc();
78 		assertFalse(selectedToc == -1);
79 		ITopic[] path = finder.getTopicPath();
80 		assertNotNull(path);
81 		String tocHref = getTocs()[selectedToc].getHref();
82 		assertEquals("/org.eclipse.ua.tests/data/help/toc/root.xml", tocHref);
83 		assertEquals(2, path.length);
84 		assertEquals("manual", path[0].getLabel());
85 		assertEquals("filter", path[1].getLabel());
86 	}
87 
88 	@Test
testTopicInFilteredToc()89 	public void testTopicInFilteredToc() {
90 		String topic = "http://localhost:8082/help/topic/org.eclipse.ua.tests/data/help/toc/filteredToc/helpInstalled.html";
91 		TopicFinder finder = new TopicFinder(topic, getTocs(), new FilterScope());
92 		int selectedToc = finder.getSelectedToc();
93 		assertFalse(selectedToc == -1);
94 		ITopic[] path = finder.getTopicPath();
95 		assertNotNull(path);
96 		String tocHref = getTocs()[selectedToc].getHref();
97 		assertEquals("/org.eclipse.ua.tests/data/help/toc/root.xml", tocHref);
98 		assertEquals(2, path.length);
99 		assertEquals("filter", path[0].getLabel());
100 		assertEquals("The plugin org.eclipse.help is installed", path[1].getLabel());
101 		assertEquals("/org.eclipse.ua.tests/data/help/toc/filteredToc/helpInstalled.html", path[1].getHref());
102 	}
103 
104 	@Test
testTopicNotInToc()105 	public void testTopicNotInToc() {
106 		String topic = "http://localhost:8082/help/topic/org.eclipse.ua.tests/data/help/manual/filter25.xhtml";
107 		TopicFinder finder = new TopicFinder(topic, getTocs(), new UniversalScope());
108 		assertEquals(-1, finder.getSelectedToc());
109 		assertNull(finder.getTopicPath());
110 	}
111 
112 	@Test
testLookupFromPath()113 	public void testLookupFromPath() {
114 		String topic = "http://localhost:8082/help/topic/org.eclipse.ua.tests/data/help/toc/filteredToc/helpInstalled.html";
115 		IToc[] tocs = getTocs();
116 		TopicFinder finder = new TopicFinder(topic, tocs, new FilterScope());
117 		String numericPath = finder.getNumericPath();
118 		int tocIndex = finder.getSelectedToc();
119 		String fullPath = "" + tocIndex + "_" + numericPath;
120 		int[] intPath = UrlUtil.splitPath(fullPath);
121 		ITopic[] topics = TocData.decodePath(intPath, tocs[tocIndex], new FilterScope());
122 		assertEquals(2, topics.length);
123 		assertEquals("filter", topics[0].getLabel());
124 		assertEquals("The plugin org.eclipse.help is installed", topics[1].getLabel());
125 		assertEquals("/org.eclipse.ua.tests/data/help/toc/filteredToc/helpInstalled.html", topics[1].getHref());
126 	}
127 
128 	@Test
testTocNavURL()129 	public void testTocNavURL() {
130 		String topic = "http://localhost:8082/help/topic/org.eclipse.ua.tests/data/help/toc/filteredToc/helpInstalled.html";
131 		IToc[] tocs = getTocs();
132 		TopicFinder finder = new TopicFinder(topic, tocs, new UniversalScope());
133 		int selectedToc = finder.getSelectedToc();
134 		String navPath = "http://127.0.0.1:1936/help/nav/" + selectedToc;
135 		TopicFinder finder2 = new TopicFinder(navPath, tocs, new UniversalScope());
136 		assertEquals(selectedToc, finder2.getSelectedToc());
137 		assertEquals(0, finder2.getTopicPath().length);
138 	}
139 
140 	@Test
testTopic_0_0NavURL()141 	public void testTopic_0_0NavURL() {
142 		checkNavTopic(0, 0);
143 	}
144 
145 	@Test
testTopic_0_1NavURL()146 	public void testTopic_0_1NavURL() {
147 		checkNavTopic(0, 1);
148 	}
149 
150 	@Test
testTopic_1_0NavURL()151 	public void testTopic_1_0NavURL() {
152 		checkNavTopic(1, 0);
153 	}
154 
checkNavTopic(int index1, int index2)155 	private void checkNavTopic(int index1, int index2) {
156 		String topic = "http://localhost:8082/help/topic/org.eclipse.ua.tests/data/help/toc/filteredToc/helpInstalled.html";
157 		IToc[] tocs = getTocs();
158 		TopicFinder finder = new TopicFinder(topic, tocs, new UniversalScope());
159 		int selectedToc = finder.getSelectedToc();
160 		String navPath = "http://127.0.0.1:1936/help/nav/" + selectedToc +
161 			'_' + index1 + '_' + index2;
162 		TopicFinder finder2 = new TopicFinder(navPath, tocs, new UniversalScope());
163 		assertEquals(selectedToc, finder2.getSelectedToc());
164 		ITopic[] topicPath = finder2.getTopicPath();
165 		assertEquals(2, topicPath.length);
166 		ITopic[] topLevelTopics = tocs[selectedToc].getTopics();
167 		assertEquals(topLevelTopics[index1], topicPath[0]);
168 		ITopic[] secondLevelTopics = topLevelTopics[index1].getSubtopics();
169 		assertEquals(secondLevelTopics[index2], topicPath[1]);
170 		assertEquals("" + index1 + '_' + index2, finder2.getNumericPath());
171 	}
172 
173 }
174