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
|
/*******************************************************************************
* Copyright (c) 2010, 2016 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.dynamic;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.eclipse.help.internal.xhtml.DynamicXHTMLProcessor;
import org.eclipse.help.ui.internal.HelpUIPlugin;
import org.eclipse.ua.tests.plugin.UserAssistanceTestPlugin;
import org.eclipse.ua.tests.util.XMLUtil;
import org.junit.Before;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.xml.sax.SAXException;
public class DynamicXHTMLProcessorTest {
@Before
public void setUp() throws Exception {
// activate the UI plug-in for UI filtering ability
HelpUIPlugin.getDefault();
}
private String process(String path) throws Exception {
Bundle bundle = UserAssistanceTestPlugin.getDefault().getBundle();
try (InputStream is = getProcessedInput(path, bundle)) {
return readStream(is);
}
}
protected InputStream getProcessedInput(String path, Bundle bundle)
throws IOException, SAXException, ParserConfigurationException,
TransformerException, TransformerConfigurationException {
try (InputStream in = bundle.getEntry(path).openStream()) {
String href = '/' + bundle.getBundleId() + path;
return DynamicXHTMLProcessor.process(href, in, "en", true);
}
}
private String readStream(InputStream is) throws Exception {
try (InputStreamReader inputStreamReader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
StringBuilder buffer = new StringBuilder();
char[] cbuf = new char[256];
int len;
do {
len = inputStreamReader.read(cbuf);
if (len > 0) {
buffer.append(cbuf, 0, len);
}
} while (len >= 0);
return buffer.toString();
}
}
@Test
public void testXhtmlNoCollapseAnchor() throws Exception {
String processed = process("data/help/dynamic/xhtml/emptyAnchor.xhtml");
assertTrue("Anchor collapsed in " + processed, processed.indexOf("</a>") > 0);
}
@Test
public void testXhtmlNoCollapseParagraph() throws Exception {
String processed = process("data/help/dynamic/xhtml/emptyAnchor.xhtml");
assertTrue("Paragraph collapsed in " + processed, processed.indexOf("</p>") > 0);
}
@Test
public void testXhtmlNoCollapseAnchorIC() throws Exception {
String processed = process("data/help/dynamic/xhtml/emptyAnchorWithComment.xhtml");
assertTrue("Anchor collapsed in " + processed, processed.indexOf("</a>") > 0);
}
@Test
public void testXhtmlNoCollapseParagraphIC() throws Exception {
String processed = process("data/help/dynamic/xhtml/emptyAnchorWithComment.xhtml");
assertTrue("Paragraph collapsed in " + processed, processed.indexOf("</p>") > 0);
}
@Test
public void testXhtmlNoCollapseDiv() throws Exception {
String processed = process("data/help/dynamic/xhtml/emptyDiv.xhtml");
assertTrue("Div collapsed in " + processed, processed.indexOf("</div>") > 0);
}
@Test
public void testXhtmlNoCollapseScript() throws Exception {
String processed = process("data/help/dynamic/xhtml/emptyAnchor.xhtml");
assertTrue("Div collapsed in " + processed, processed.indexOf("</script>") > 0);
}
@Test
public void testValidXML() throws Exception {
String processed = process("data/help/dynamic/xhtml/emptyAnchor.xhtml");
XMLUtil.assertParseableXML(processed);
}
}
|