Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 94960af3b6a2e7c6edd66960e0ee4d541a878fc7 (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
/*******************************************************************************
 *  Copyright (c) 2007, 2008 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;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import junit.framework.TestCase;

import org.eclipse.help.internal.search.HTMLDocParser;
import org.eclipse.help.internal.webapp.servlet.FilterHTMLHeadAndBodyOutputStream;
import org.eclipse.help.internal.webapp.servlet.FilterHTMLHeadOutputStream;

/**
 * Test for functions which decode a topic string
 */

public class FilterTest extends TestCase {
	private final String HTML40 =  "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
	private final String HEAD1 =  "<HEAD>";
	private final String HEAD2 = "</HEAD>";
	private final String HEADLC1 =  "<head>";
	private final String HEADLC2 = "</head>";
	private final String CONTENT_TYPE_ISO_8859_1 =    "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">";
	private final String CONTENT_TYPE_ISO_8859_1_UC = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />";
	private final String CONTENT_TYPE_UTF8 = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
	private final String CONTENT_TYPE_UTF8UC = "<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">";
	private final String BODY1 = "<BODY>";
	private final String BODY2 = "</BODY></html>";
	private final String BODYLC1 = "<body>";
	private final String BODYLC2 = "</body></html>";
	private final String CSS1 = "<LINK REL=\"STYLESHEET\" HREF=\"book1.css\" TYPE=\"text/css\">";
	private final String CSS2 = "<LINK REL=\"STYLESHEET\" HREF=\"book1.css\" TYPE=\"text/css\">";
	private final String CONTENT1 = "<p>Content1</p>";
	private final String CONTENT2 = "<p>Content2</p>";
	private String CHINESE_CONTENT = "<p>" + (char)24320 + (char)21457 + (char)29932 + "</p>";
	private String CHINESE_ENTITY_CONTENT = "<p>&#24320;&#21457;&#29932;</p>";

	public void testHeadOutputFilter() {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		OutputStream filteredOutput = new FilterHTMLHeadOutputStream(output, CSS2.getBytes());
		try {
			filteredOutput.write(HTML40.getBytes());
			filteredOutput.write(HEAD1.getBytes());
			filteredOutput.write(CSS1.getBytes());
			filteredOutput.write(HEAD2.getBytes());
			filteredOutput.write(BODY1.getBytes());
			filteredOutput.write(CONTENT1.getBytes());
			filteredOutput.write(BODY2.getBytes());
		} catch (IOException e) {
			fail("IO Exception");
		}
		final String expected = HTML40 + HEAD1 + CSS1 + CSS2 + '\n' + HEAD2 + BODY1 + CONTENT1 + BODY2;
		assertEquals(expected, output.toString());
	}

	public void testHeadAndBodyOutputFilter() {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		FilterHTMLHeadAndBodyOutputStream filteredOutput = new FilterHTMLHeadAndBodyOutputStream(output, CSS2.getBytes(), CONTENT2);
		try {
			filteredOutput.write(HTML40.getBytes());
			filteredOutput.write(HEAD1.getBytes());
			filteredOutput.write(CONTENT_TYPE_ISO_8859_1.getBytes());
			filteredOutput.write(CSS1.getBytes());
			filteredOutput.write(HEAD2.getBytes());
			filteredOutput.write(BODY1.getBytes());
			filteredOutput.write(CONTENT1.getBytes());
			filteredOutput.write(BODY2.getBytes());
		} catch (IOException e) {
			fail("IO Exception");
		}
		final String expected = HTML40 + HEAD1 + CONTENT_TYPE_ISO_8859_1 + CSS1 + CSS2 + '\n' + HEAD2 + BODY1 + '\n' + CONTENT2 + '\n' + CONTENT1 + BODY2;
		assertEquals(expected, output.toString());
	}
	
	public void testLowerCaseTags() {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		OutputStream filteredOutput = new FilterHTMLHeadAndBodyOutputStream(output, CSS1.getBytes(), CONTENT2);
		try {
			filteredOutput.write(HTML40.getBytes());
			filteredOutput.write(HEADLC1.getBytes());
			filteredOutput.write(HEADLC2.getBytes());
			filteredOutput.write(BODYLC1.getBytes());
			filteredOutput.write(CONTENT1.getBytes());
			filteredOutput.write(BODYLC2.getBytes());
		} catch (IOException e) {
			fail("IO Exception");
		}
		final String expected = HTML40 + HEADLC1 + CSS1 + '\n' + HEADLC2 + BODYLC1 + '\n' + CONTENT2 + '\n' + CONTENT1 + BODYLC2;
		assertEquals(expected, output.toString());
	}	
	
	public void testFilterHeadlessDocument() {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		OutputStream filteredOutput = new FilterHTMLHeadAndBodyOutputStream(output, CSS1.getBytes(), CONTENT2);
		try {
			filteredOutput.write(HTML40.getBytes());
			filteredOutput.write(BODY1.getBytes());
			filteredOutput.write(CONTENT1.getBytes());
			filteredOutput.write(BODY2.getBytes());
		} catch (IOException e) {
			fail("IO Exception");
		}
		final String expected = HTML40 + BODY1 + '\n' + CONTENT2 + '\n' + CONTENT1 + BODY2;
		assertEquals(expected, output.toString());
	}

	public void testInsertChineseUtf8() {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		FilterHTMLHeadAndBodyOutputStream filteredOutput = new FilterHTMLHeadAndBodyOutputStream(output, null, CHINESE_CONTENT);
		try {
			filteredOutput.write(HTML40.getBytes());
			filteredOutput.write(HEAD1.getBytes());
			filteredOutput.write(CONTENT_TYPE_UTF8.getBytes());
			filteredOutput.write(HEAD2.getBytes());
			filteredOutput.write(BODY1.getBytes());
			filteredOutput.write(BODY2.getBytes());
			final String expected = HTML40 + HEAD1 + CONTENT_TYPE_UTF8 + HEAD2 + BODY1 + '\n' + CHINESE_CONTENT + '\n' + BODY2;
			assertEquals(expected, output.toString("UTF-8"));
		} catch (IOException e) {
			fail("IO Exception");
		}
	}

	public void testInsertChineseISO8859() {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		FilterHTMLHeadAndBodyOutputStream filteredOutput = new FilterHTMLHeadAndBodyOutputStream(output, null, CHINESE_CONTENT);
		try {
			filteredOutput.write(HTML40.getBytes());
			filteredOutput.write(HEAD1.getBytes());
			filteredOutput.write(CONTENT_TYPE_ISO_8859_1.getBytes());
			filteredOutput.write(HEAD2.getBytes());
			filteredOutput.write(BODY1.getBytes());
			filteredOutput.write(BODY2.getBytes());
			final String expected = HTML40 + HEAD1 + CONTENT_TYPE_ISO_8859_1 + HEAD2 + BODY1 + '\n' + CHINESE_ENTITY_CONTENT + '\n' + BODY2;
			assertEquals(expected, output.toString());
		} catch (IOException e) {
			fail("IO Exception");
		}
	}
	
	public void testInsertChineseNoCharsetSpecified() {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		FilterHTMLHeadAndBodyOutputStream filteredOutput = new FilterHTMLHeadAndBodyOutputStream(output, null, CHINESE_CONTENT);
		try {
			filteredOutput.write(HTML40.getBytes());
			filteredOutput.write(HEAD1.getBytes());
			filteredOutput.write(HEAD2.getBytes());
			filteredOutput.write(BODY1.getBytes());
			filteredOutput.write(BODY2.getBytes());
			final String expected = HTML40 + HEAD1 + HEAD2 + BODY1 + '\n' + CHINESE_ENTITY_CONTENT + '\n' + BODY2;
			assertEquals(expected, output.toString());
		} catch (IOException e) {
			fail("IO Exception");
		}
	}

	public void testCharsetUtf8Upper() {
		InputStream is = new ByteArrayInputStream(CONTENT_TYPE_UTF8UC.getBytes());
	    assertEquals("UTF-8", HTMLDocParser.getCharsetFromHTML(is));
	}
	
	public void testCharsetISO_8859_UCUpper() {
		InputStream is = new ByteArrayInputStream(CONTENT_TYPE_ISO_8859_1_UC.getBytes());
	    assertEquals("ISO-8859-1", HTMLDocParser.getCharsetFromHTML(is));
	}
	
}

Back to the top