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 package org.eclipse.ua.tests.help.toc;
15 
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertNull;
18 
19 import java.util.List;
20 
21 import org.eclipse.core.expressions.IEvaluationContext;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
24 import org.eclipse.core.runtime.preferences.InstanceScope;
25 import org.eclipse.help.IToc;
26 import org.eclipse.help.ITocContribution;
27 import org.eclipse.help.ITopic;
28 import org.eclipse.help.IUAElement;
29 import org.eclipse.help.internal.HelpData;
30 import org.eclipse.help.internal.HelpPlugin;
31 import org.eclipse.help.internal.toc.TocSorter;
32 import org.eclipse.help.internal.util.ProductPreferences;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 
37 public class TocSortingTest {
38 
39 	private static final String BASE_TOCS = "baseTOCS";
40 	private static final String ORDERED_XML = "PLUGINS_ROOT/org.eclipse.ua.tests/data/help/toc/toc_data/helpDataOrdered.xml";
41 	private static final String EMPTY_XML = "PLUGINS_ROOT/org.eclipse.ua.tests/data/help/toc/toc_data/helpDataEmpty.xml";
42 	private static final String NO_SORT_XML = "PLUGINS_ROOT/org.eclipse.ua.tests/data/help/toc/toc_data/helpDataOrderedNoSort.xml";
43 	private static final String BAD_PLUGIN_HELP_DATA_XML = "PLUGINS_ROOT/org.eclipse.nosuchplugin/data/help/toc/toc_data/helpData.xml";
44 	private static final String NO_SUCH_FILE_XML = "PLUGINS_ROOT/org.eclipse.ua.tests/data/help/toc/toc_data/noSuchFile.xml";
45 	private static final String ALPHA_SORT_XML = "PLUGINS_ROOT/org.eclipse.ua.tests/data/help/toc/toc_data/helpDataOrderedAlphaSort.xml";
46 	private String helpDataPreference;
47 	private String baseTocsPreference;
48 
49 	private class Toc implements IToc {
50 
51 		private String label;
52 
Toc(String label)53 		public Toc(String label) {
54 			this.label = label;
55 		}
56 
57 		@Override
getTopic(String href)58 		public ITopic getTopic(String href) {
59 			return null;
60 		}
61 
62 		@Override
getTopics()63 		public ITopic[] getTopics() {
64 			return new ITopic[0];
65 		}
66 
67 		@Override
getChildren()68 		public IUAElement[] getChildren() {
69 			return new IUAElement[0];
70 		}
71 
72 		@Override
isEnabled(IEvaluationContext context)73 		public boolean isEnabled(IEvaluationContext context) {
74 			return true;
75 		}
76 
77 		@Override
getHref()78 		public String getHref() {
79 			return null;
80 		}
81 
82 		@Override
getLabel()83 		public String getLabel() {
84 			return label;
85 		}
86 
87 	}
88 
89 	private class TC implements ITocContribution {
90 
91 		private IToc toc;
92 		private String categoryId;
93 		private String id;
94 
TC(String name, String category)95 		public TC(String name, String category) {
96 			this.categoryId = category;
97 			this.id = "/" + name + "/toc.xml";
98 			this.toc = new Toc(name);
99 		}
100 
101 		@Override
getCategoryId()102 		public String getCategoryId() {
103 			return categoryId;
104 		}
105 
106 		@Override
getContributorId()107 		public String getContributorId() {
108 			return "org.eclipse.ua.tests";
109 		}
110 
111 		@Override
getExtraDocuments()112 		public String[] getExtraDocuments() {
113 			return new String[0];
114 		}
115 
116 		@Override
getId()117 		public String getId() {
118 			return id;
119 		}
120 
121 		@Override
getLinkTo()122 		public String getLinkTo() {
123 			return null;
124 		}
125 
126 		@Override
getLocale()127 		public String getLocale() {
128 			return "en";
129 		}
130 
131 		@Override
getToc()132 		public IToc getToc() {
133 			return toc;
134 		}
135 
136 		@Override
isPrimary()137 		public boolean isPrimary() {
138 			return true;
139 		}
140 	}
141 
142 	@Before
setUp()143 	public void setUp() throws Exception {
144 		helpDataPreference = Platform.getPreferencesService().getString
145 			(HelpPlugin.HELP_DATA_KEY, HelpPlugin.HELP_DATA_KEY, "", null);
146 		baseTocsPreference = Platform.getPreferencesService().getString
147 			(HelpPlugin.HELP_DATA_KEY, BASE_TOCS, "", null);
148 		HelpData.clearProductHelpData();
149 		ProductPreferences.resetPrimaryTocOrdering();
150 		setHelpData(EMPTY_XML);
151 		setBaseTocs("");
152 	}
153 
154 	@After
tearDown()155 	public void tearDown() throws Exception {
156 		setHelpData(helpDataPreference);
157 		setBaseTocs(baseTocsPreference);
158 		HelpData.clearProductHelpData();
159 		ProductPreferences.resetPrimaryTocOrdering();
160 	}
161 
setHelpData(String value)162 	private void setHelpData(String value) {
163 		IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(HelpPlugin.PLUGIN_ID);
164 		prefs.put(HelpPlugin.HELP_DATA_KEY, value);
165 	}
166 
setBaseTocs(String value)167 	private void setBaseTocs(String value) {
168 		IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(HelpPlugin.PLUGIN_ID);
169 		prefs.put(BASE_TOCS, value);
170 	}
171 
toString(ITocContribution[] tocs)172 	private String toString(ITocContribution[] tocs) {
173 		String result = "";
174 		for (ITocContribution toc : tocs) {
175 			result += toc.getToc().getLabel();
176 		}
177 		return result;
178 	}
179 
180 	@Test
testNoTocs()181 	public void testNoTocs() {
182 		TocSorter sorter = new TocSorter();
183 		ITocContribution[] result = sorter.orderTocContributions(new TC[0]);
184 		assertEquals(result.length, 0);
185 	}
186 
187 	@Test
testNoCategory()188 	public void testNoCategory() {
189 		TocSorter sorter = new TocSorter();
190 		ITocContribution[] tocs = new ITocContribution[] {
191 				new TC("5", null), new TC("3", null), new TC("8", null)
192 		};
193 		ITocContribution[] result = sorter.orderTocContributions(tocs);
194 		assertEquals("358", toString(result));
195 	}
196 
197 	@Test
testCaseInsensitive()198 	public void testCaseInsensitive() {
199 		TocSorter sorter = new TocSorter();
200 		ITocContribution[] tocs = new ITocContribution[] {
201 				new TC("a", null), new TC("c", null), new TC("B", null), new TC("D", null)
202 		};
203 		ITocContribution[] result = sorter.orderTocContributions(tocs);
204 		assertEquals("aBcD", toString(result));
205 	}
206 
207 	@Test
testCategories()208 	public void testCategories() {
209 		TocSorter sorter = new TocSorter();
210 		ITocContribution[] tocs = new ITocContribution[] {
211 				new TC("4", null), new TC("2", "a"), new TC("5", "b"), new TC("1", null), new TC("8", ""),
212 				new TC("7", "a"), new TC("9", "b"), new TC("3", ""), new TC("6", null)
213 		};
214 		ITocContribution[] result = sorter.orderTocContributions(tocs);
215 		assertEquals("127345968", toString(result));
216 	}
217 
218 	@Test
testTocOrderPreference()219 	public void testTocOrderPreference() {
220 		setHelpData(ORDERED_XML);
221 		TocSorter sorter = new TocSorter();
222 		ITocContribution[] tocs = new ITocContribution[] {
223 				new TC("a", null), new TC("c", null), new TC("b", null), new TC("d", null)
224 		};
225 		ITocContribution[] result = sorter.orderTocContributions(tocs);
226 		assertEquals("dbac", toString(result));
227 	}
228 
229 	@Test
testTocNoSortOthers()230 	public void testTocNoSortOthers() {
231 		setHelpData(NO_SORT_XML);
232 		TocSorter sorter = new TocSorter();
233 		ITocContribution[] tocs = new ITocContribution[] {
234 				new TC("e", null), new TC("c", null), new TC("b", null), new TC("d", null) , new TC("a", null)
235 		};
236 		ITocContribution[] result = sorter.orderTocContributions(tocs);
237 		assertEquals("dbeca", toString(result));
238 	}
239 
240 	@Test
testTocAlphaSortOthers()241 	public void testTocAlphaSortOthers() {
242 		setHelpData(ALPHA_SORT_XML);
243 		TocSorter sorter = new TocSorter();
244 		ITocContribution[] tocs = new ITocContribution[] {
245 				new TC("e", null), new TC("c", null), new TC("b", null), new TC("d", null) , new TC("a", null)
246 		};
247 		ITocContribution[] result = sorter.orderTocContributions(tocs);
248 		assertEquals("dbace", toString(result));
249 	}
250 
251 	@Test
testTocBadHelpDataPlugin()252 	public void testTocBadHelpDataPlugin() {
253 		setHelpData(BAD_PLUGIN_HELP_DATA_XML);
254 		TocSorter sorter = new TocSorter();
255 		ITocContribution[] tocs = new ITocContribution[] {
256 				new TC("e", null), new TC("c", null), new TC("b", null), new TC("d", null) , new TC("a", null)
257 		};
258 		ITocContribution[] result = sorter.orderTocContributions(tocs);
259 		assertEquals("abcde", toString(result));
260 	}
261 
262 	@Test
testTocBadHelpDataPath()263 	public void testTocBadHelpDataPath() {
264 		setHelpData(NO_SUCH_FILE_XML);
265 		TocSorter sorter = new TocSorter();
266 		ITocContribution[] tocs = new ITocContribution[] {
267 				new TC("e", null), new TC("c", null), new TC("b", null), new TC("d", null) , new TC("a", null)
268 		};
269 		ITocContribution[] result = sorter.orderTocContributions(tocs);
270 		assertEquals("abcde", toString(result));
271 	}
272 
273 	@Test
testNoHelpData()274 	public void testNoHelpData() {
275 		setHelpData("");
276 		TocSorter sorter = new TocSorter();
277 		ITocContribution[] tocs = new ITocContribution[] {
278 				new TC("e", null), new TC("c", null), new TC("b", null), new TC("d", null) , new TC("a", null)
279 		};
280 		ITocContribution[] result = sorter.orderTocContributions(tocs);
281 		assertEquals("abcde", toString(result));
282 	}
283 
284 	@Test
testBaseTocs()285 	public void testBaseTocs() {
286 		setHelpData("");
287 		setBaseTocs("/d/toc.xml,/b/toc.xml");
288 		TocSorter sorter = new TocSorter();
289 		ITocContribution[] tocs = new ITocContribution[] {
290 				new TC("e", null), new TC("c", null), new TC("b", null), new TC("d", null) , new TC("a", null)
291 		};
292 		ITocContribution[] result = sorter.orderTocContributions(tocs);
293 		assertEquals("dbace", toString(result));
294 	}
295 
296 	@Test
testNoProductNoHelpData()297 	public void testNoProductNoHelpData() {
298 		List<String> ordering = ProductPreferences.getTocOrdering(null, "", "/a/b.xml,/c/d.xml");
299 		assertEquals(2, ordering.size());
300 		assertEquals("/a/b.xml", ordering.get(0));
301 		assertEquals("/c/d.xml", ordering.get(1));
302 	}
303 
304 	@Test
testNoProductWithHelpData()305 	public void testNoProductWithHelpData() {
306 		List<String> ordering = ProductPreferences.getTocOrdering(null, "helpData.xml", "/a/b.xml,/c/d.xml");
307 		assertNull(ordering);
308 	}
309 
310 	@Test
testNoProductWithPluginsRoot()311 	public void testNoProductWithPluginsRoot() {
312 		List<String> ordering = ProductPreferences.getTocOrdering(null, ORDERED_XML, "/a/b.xml,/c/d.xml");
313 		assertEquals(3, ordering.size());
314 		assertEquals("/x/toc.xml", ordering.get(0));
315 		assertEquals("/d/toc.xml", ordering.get(1));
316 		assertEquals("/b/toc.xml", ordering.get(2));
317 	}
318 
319 }
320