Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8fa0bbd7744e72a8004b0500ddee7990981749d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*******************************************************************************
 * Copyright (c) 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ua.tests.help.webapp.service;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import junit.framework.TestCase;

import org.eclipse.help.ITopic;
import org.eclipse.help.internal.HelpPlugin;
import org.eclipse.help.internal.base.BaseHelpSystem;
import org.eclipse.help.internal.entityresolver.LocalEntityResolver;
import org.eclipse.help.internal.server.WebappManager;
import org.eclipse.help.internal.toc.Toc;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class TocFragmentServiceTest extends TestCase {

	private int mode;

	@Override
	protected void setUp() throws Exception {
		BaseHelpSystem.ensureWebappRunning();
		mode = BaseHelpSystem.getMode();
		BaseHelpSystem.setMode(BaseHelpSystem.MODE_INFOCENTER);
	}
	
	@Override
	protected void tearDown() throws Exception {
		BaseHelpSystem.setMode(mode);
	}

	public void testTocFragmentServiceContainsUAToc() throws Exception {
		int port = WebappManager.getPort();
		URL url = new URL("http", "localhost", port, 
				"/help/vs/service/tocfragment?lang=en");
		Node root = getTreeData(url);
		Element[] UARoot = findNodeById(root, 
				"/org.eclipse.ua.tests/data/help/toc/root.xml");
	    assertEquals(1, UARoot.length);
	}

	public void testTocFragmentServiceContainsFilteredToc() throws Exception {
		int port = WebappManager.getPort();
		URL url = new URL("http", "localhost", port, 
				"/help/vs/service/tocfragment?lang=en&toc=/org.eclipse.ua.tests/data/help/toc/root.xml&path=2");
		Node root = getTreeData(url);
		Element[] UARoot = findNodeById(root, 
				"/org.eclipse.ua.tests/data/help/toc/root.xml");
		assertEquals(1, UARoot.length);
		Element[] filterNode = findNodeById(UARoot[0], "2");
		assertEquals(1, filterNode.length);
		Element[] results = findHref(filterNode[0], "node", 
				"../topic/org.eclipse.ua.tests/data/help/toc/filteredToc/simple_page.html");
		assertEquals(24, results.length);
		
		results = findHref(filterNode[0], "node", 
		"../topic/org.eclipse.ua.tests/data/help/toc/filteredToc/helpInstalled.html");
		assertEquals(1, results.length);
	}
	
	public void testTocFragmentServiceReadEnToc() throws Exception {
		int uaSearch = findUATopicIndex("search", "en");
		assertTrue(uaSearch >= 0);
		int port = WebappManager.getPort();
		URL url = new URL("http", "localhost", port, 
				"/help/vs/service/tocfragment?lang=en&toc=/org.eclipse.ua.tests/data/help/toc/root.xml&path=" + uaSearch);
		Node root = getTreeData(url);
		Element[] UARoot = findNodeById(root, 
				"/org.eclipse.ua.tests/data/help/toc/root.xml");
		assertEquals(1, UARoot.length);
		Element[] searchNode = findChildren(UARoot[0], "node", "title", "search");
		assertEquals(1, searchNode.length);
		Element[] topicEn = findHref(searchNode[0], "node", 
				"../topic/org.eclipse.ua.tests/data/help/search/test_en.html");
		assertEquals(1, topicEn.length);
		Element[] topicDe = findHref(searchNode[0], "node", 
				"../topic/org.eclipse.ua.tests/data/help/search/test_de.html");
		assertEquals(0, topicDe.length);
	}

	private int findUATopicIndex(String title, String locale) {
		int index = -1;
		Toc[] tocs = HelpPlugin.getTocManager().getTocs(locale);
		for (Toc toc : tocs) {
			if ("/org.eclipse.ua.tests/data/help/toc/root.xml".equals(toc.getHref())) {
				ITopic[] topics = toc.getTopics();
				for (int j = 0; j < topics.length; j++) {
					if (title.equals(topics[j].getLabel())) {
						index = j;
					}
				}
			}
		}
		return index;
	}	
	
	public void testTocFragmentServiceReadDeToc() throws Exception {
		int helpMode = BaseHelpSystem.getMode();
		BaseHelpSystem.setMode(BaseHelpSystem.MODE_INFOCENTER);
		int uaSearch = findUATopicIndex("search", "de");
		assertTrue(uaSearch >= 0);
		int port = WebappManager.getPort();
		URL url = new URL("http", "localhost", port, 
				"/help/vs/service/tocfragment?lang=de&toc=/org.eclipse.ua.tests/data/help/toc/root.xml&path=" + uaSearch);
		Node root = getTreeData(url);
		Element[] UARoot = findNodeById(root, 
				"/org.eclipse.ua.tests/data/help/toc/root.xml");
		assertEquals(1, UARoot.length);
		Element[] searchNode = findChildren(UARoot[0], "node", "title", "search");
		Element[] topicEn = findHref(searchNode[0], "node", 
				"../topic/org.eclipse.ua.tests/data/help/search/test_en.html");
		assertEquals(0, topicEn.length);
		Element[] topicDe = findHref(searchNode[0], "node", 
				"../topic/org.eclipse.ua.tests/data/help/search/test_de.html");
		assertEquals(1, topicDe.length);
		BaseHelpSystem.setMode(helpMode);
	}
	
	private Element[] findNodeById(Node root, String id) {
		return findChildren(root, "node", "id", id);
	}

	private Element[] findChildren(Node parent, String childKind, 
			String attributeName, String attributeValue) {
		NodeList nodes = parent.getChildNodes();
		List<Node> results = new ArrayList<Node>();
		for (int i = 0; i < nodes.getLength(); i++) {
			Node next = nodes.item(i);
			if (next instanceof Element) {
				Element nextElement = (Element)next;
				if ( childKind.equals(nextElement.getTagName()) 
						&& attributeValue.equals(nextElement.getAttribute(attributeName))) {
		
				    results.add(next);	
				}
			}
		}
		return results.toArray(new Element[results.size()]);
	}
	
	/*
	 * Look for a matching href, the query part of the href is not compared
	 */
	private Element[] findHref(Node parent, String childKind, 
			 String attributeValue) {
		NodeList nodes = parent.getChildNodes();
		List<Node> results = new ArrayList<Node>();
		for (int i = 0; i < nodes.getLength(); i++) {
			Node next = nodes.item(i);
			if (next instanceof Element) {
				Element nextElement = (Element)next;
				if ( childKind.equals(nextElement.getTagName()) ) {
					String href = nextElement.getAttribute("href");
					if (href != null) {
                        int query = href.indexOf('?');
                        if (query >= 0) {
                        	href = href.substring(0, query);
                        }
	                    if (href.equals (attributeValue)) {			
					        results.add(next);	
	                    }
					}
				}
			}
		}
		return results.toArray(new Element[results.size()]);
	}
	 

	private Node getTreeData(URL url)
			throws Exception {
		InputStream is = url.openStream();
		InputSource inputSource = new InputSource(is);
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		documentBuilder.setEntityResolver(new LocalEntityResolver());
		Document document = documentBuilder.parse(inputSource);
		Node root = document.getFirstChild();
		is.close();
		assertEquals("tree_data", root.getNodeName());
		return root;
	}

	public void testTocFragmentServiceXMLSchema() 
			throws Exception {
		int port = WebappManager.getPort();
		URL url = new URL("http", "localhost", port, "/help/vs/service/tocfragment?lang=en");
		URL schemaUrl = new URL("http", "localhost", port, "/help/test/schema/xml/tocfragment.xsd");
		String schema = schemaUrl.toString();
		String uri = url.toString();
		String result = SchemaValidator.testXMLSchema(uri, schema);
		
		assertEquals("URL: \"" + uri + "\" is ", "valid", result);
	}
	
	/*
	public void testTocFragmentServiceJSONSchema() 
			throws Exception {
		fail("Not yet implemented.");
	}
	*/

}

Back to the top