|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2011 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
* Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services |
| 11 |
*******************************************************************************/ |
| 12 |
package org.eclipse.ua.tests.help.webapp.service; |
| 13 |
|
| 14 |
import java.io.InputStream; |
| 15 |
import java.net.URL; |
| 16 |
import java.util.ArrayList; |
| 17 |
import java.util.List; |
| 18 |
|
| 19 |
import javax.xml.parsers.DocumentBuilder; |
| 20 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 21 |
|
| 22 |
import junit.framework.TestCase; |
| 23 |
|
| 24 |
import org.eclipse.help.internal.base.BaseHelpSystem; |
| 25 |
import org.eclipse.help.internal.entityresolver.LocalEntityResolver; |
| 26 |
import org.eclipse.help.internal.server.WebappManager; |
| 27 |
import org.w3c.dom.Document; |
| 28 |
import org.w3c.dom.Element; |
| 29 |
import org.w3c.dom.Node; |
| 30 |
import org.w3c.dom.NodeList; |
| 31 |
import org.xml.sax.InputSource; |
| 32 |
|
| 33 |
public class TocFragmentServiceTest extends TestCase { |
| 34 |
|
| 35 |
private int mode; |
| 36 |
|
| 37 |
protected void setUp() throws Exception { |
| 38 |
BaseHelpSystem.ensureWebappRunning(); |
| 39 |
mode = BaseHelpSystem.getMode(); |
| 40 |
BaseHelpSystem.setMode(BaseHelpSystem.MODE_INFOCENTER); |
| 41 |
} |
| 42 |
|
| 43 |
protected void tearDown() throws Exception { |
| 44 |
BaseHelpSystem.setMode(mode); |
| 45 |
} |
| 46 |
|
| 47 |
public void testTocFragmentServiceContainsUAToc() throws Exception { |
| 48 |
int port = WebappManager.getPort(); |
| 49 |
URL url = new URL("http", "localhost", port, |
| 50 |
"/help/ss/service/tocfragment?lang=en"); |
| 51 |
Node root = getTreeData(url); |
| 52 |
Element[] UARoot = findNodeById(root, |
| 53 |
"/org.eclipse.ua.tests/data/help/toc/root.xml"); |
| 54 |
assertEquals(1, UARoot.length); |
| 55 |
} |
| 56 |
|
| 57 |
public void testTocFragmentServiceContainsFilteredToc() throws Exception { |
| 58 |
int port = WebappManager.getPort(); |
| 59 |
URL url = new URL("http", "localhost", port, |
| 60 |
"/help/ss/service/tocfragment?lang=en&toc=/org.eclipse.ua.tests/data/help/toc/root.xml&path=2"); |
| 61 |
Node root = getTreeData(url); |
| 62 |
Element[] UARoot = findNodeById(root, |
| 63 |
"/org.eclipse.ua.tests/data/help/toc/root.xml"); |
| 64 |
assertEquals(1, UARoot.length); |
| 65 |
Element[] filterNode = findNodeById(UARoot[0], "2"); |
| 66 |
assertEquals(1, filterNode.length); |
| 67 |
Element[] results = findChildren(filterNode[0], "node", "href", |
| 68 |
"../topic/org.eclipse.ua.tests/data/help/toc/filteredToc/simple_page.html"); |
| 69 |
assertEquals(24, results.length); |
| 70 |
|
| 71 |
results = findChildren(filterNode[0], "node", "href", |
| 72 |
"../topic/org.eclipse.ua.tests/data/help/toc/filteredToc/helpInstalled.html"); |
| 73 |
assertEquals(1, results.length); |
| 74 |
} |
| 75 |
|
| 76 |
public void testReadEnToc() throws Exception { |
| 77 |
int port = WebappManager.getPort(); |
| 78 |
URL url = new URL("http", "localhost", port, |
| 79 |
"/help/ss/service/tocfragment?lang=en&toc=/org.eclipse.ua.tests/data/help/toc/root.xml&path=7"); |
| 80 |
Node root = getTreeData(url); |
| 81 |
Element[] UARoot = findNodeById(root, |
| 82 |
"/org.eclipse.ua.tests/data/help/toc/root.xml"); |
| 83 |
assertEquals(1, UARoot.length); |
| 84 |
Element[] searchNode = findNodeById(UARoot[0], "7"); |
| 85 |
assertEquals(1, searchNode.length); |
| 86 |
Element[] topicEn = findChildren(searchNode[0], "node", "href", |
| 87 |
"../topic/org.eclipse.ua.tests/data/help/search/test_en.html"); |
| 88 |
assertEquals(1, topicEn.length); |
| 89 |
Element[] topicDe = findChildren(searchNode[0], "node", "href", |
| 90 |
"../topic/org.eclipse.ua.tests/data/help/search/test_de.html"); |
| 91 |
assertEquals(0, topicDe.length); |
| 92 |
} |
| 93 |
|
| 94 |
public void testReadDeToc() throws Exception { |
| 95 |
int port = WebappManager.getPort(); |
| 96 |
URL url = new URL("http", "localhost", port, |
| 97 |
"/help/ss/service/tocfragment?lang=de&toc=/org.eclipse.ua.tests/data/help/toc/root.xml&path=7"); |
| 98 |
Node root = getTreeData(url); |
| 99 |
Element[] UARoot = findNodeById(root, |
| 100 |
"/org.eclipse.ua.tests/data/help/toc/root.xml"); |
| 101 |
assertEquals(1, UARoot.length); |
| 102 |
Element[] searchNode = findNodeById(UARoot[0], "7"); |
| 103 |
Element[] topicEn = findChildren(searchNode[0], "node", "href", |
| 104 |
"../topic/org.eclipse.ua.tests/data/help/search/test_en.html"); |
| 105 |
assertEquals(0, topicEn.length); |
| 106 |
Element[] topicDe = findChildren(searchNode[0], "node", "href", |
| 107 |
"../topic/org.eclipse.ua.tests/data/help/search/test_de.html"); |
| 108 |
assertEquals(1, topicDe.length); |
| 109 |
} |
| 110 |
|
| 111 |
private Element[] findNodeById(Node root, String id) { |
| 112 |
return findChildren(root, "node", "id", id); |
| 113 |
} |
| 114 |
|
| 115 |
private Element[] findChildren(Node parent, String childKind, |
| 116 |
String attributeName, String attributeValue) { |
| 117 |
NodeList nodes = parent.getChildNodes(); |
| 118 |
List<Node> results = new ArrayList<Node>(); |
| 119 |
for (int i = 0; i < nodes.getLength(); i++) { |
| 120 |
Node next = nodes.item(i); |
| 121 |
if (next instanceof Element) { |
| 122 |
Element nextElement = (Element)next; |
| 123 |
if ( childKind.equals(nextElement.getTagName()) |
| 124 |
&& attributeValue.equals(nextElement.getAttribute(attributeName))) { |
| 125 |
|
| 126 |
results.add(next); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
return (Element[]) results.toArray(new Element[results.size()]); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
private Node getTreeData(URL url) |
| 135 |
throws Exception { |
| 136 |
InputStream is = url.openStream(); |
| 137 |
InputSource inputSource = new InputSource(is); |
| 138 |
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 139 |
documentBuilder.setEntityResolver(new LocalEntityResolver()); |
| 140 |
Document document = documentBuilder.parse(inputSource); |
| 141 |
Node root = document.getFirstChild(); |
| 142 |
is.close(); |
| 143 |
assertEquals("tree_data", root.getNodeName()); |
| 144 |
return root; |
| 145 |
} |
| 146 |
|
| 147 |
public void testTocFragmentServiceXMLSchema() |
| 148 |
throws Exception { |
| 149 |
int port = WebappManager.getPort(); |
| 150 |
URL url = new URL("http", "localhost", port, "/help/ss/service/tocfragment?lang=en"); |
| 151 |
URL schemaUrl = new URL("http", "localhost", port, "/help/test/schema/xml/tocfragment.xsd"); |
| 152 |
String schema = schemaUrl.toString(); |
| 153 |
String uri = url.toString(); |
| 154 |
String result = SchemaValidator.testXMLSchema(uri, schema); |
| 155 |
|
| 156 |
assertEquals("URL: \"" + uri + "\" is ", "valid", result); |
| 157 |
} |
| 158 |
|
| 159 |
public void testTocFragmentServiceJSONSchema() |
| 160 |
throws Exception { |
| 161 |
// fail("Not yet implemented."); |
| 162 |
} |
| 163 |
|
| 164 |
} |