Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d7bffd9c549dbff83a0dae17959f3ce1719813d6 (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
/*******************************************************************************
 *  Copyright (c) 2009, 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.webapp;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import org.eclipse.help.internal.webapp.servlet.PluginsRootResolvingStream;
import org.eclipse.ua.tests.plugin.UserAssistanceTestPlugin;
import org.eclipse.ua.tests.util.ResourceFinder;
import org.junit.Test;

/**
 * Test for replacing PLUGINS_ROOT with a relative path
 */

public class PluginsRootReplacement {
	@Test
	public void testEmpty() {
	    final String input = "";
		checkFilter(input, input);
	}

	@Test
	public void testNoMatch() {
	    final String input = "<HEAD><HEAD/>";
		checkFilter(input, input);
	}

	@Test
	public void testPartialMatch() {
	    final String input = "<A href = \"PLUGINS\">";
		checkFilter(input, input);
	}

	@Test
	public void testEndsUnmatched() {
	    final String input = "<A href = \"PLUGIN";
		checkFilter(input, input);
	}

	@Test
	public void testNotAtStart() {
	    final String input = "<A href = \"../PLUGINS_ROOT/plugin/a.html\">";
		checkFilter(input, input);
	}

	@Test
	public void testAtStart() {
	    final String input = "<A href = \"PLUGINS_ROOT/plugin/a.html\">";
	    final String expected = "<A href = \"../plugin/a.html\">";
		checkFilter(input, expected);
	}

	@Test
	public void testSecondArg() {
	    final String input = "<A alt=\"alt\" href = \"PLUGINS_ROOT/plugin/a.html\">";
	    final String expected = "<A alt=\"alt\" href = \"../plugin/a.html\">";
		checkFilter(input, expected);
	}

	@Test
	public void testMultipleMatches() {
	    final String input = "<A href = \"PLUGINS_ROOT/plugin/a.html\"><A href = \"PLUGINS_ROOT/plugin/b.html\">";
	    final String expected = "<A href = \"../plugin/a.html\"><A href = \"../plugin/b.html\">";
		checkFilter(input, expected);
	}

	private void checkFilter(final String input, final String expected) {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		OutputStream filteredOutput = new PluginsRootResolvingStream(output, null, "../");
		try {
			filteredOutput.write(input.getBytes());
			filteredOutput.close();
		} catch (IOException e) {
			fail("IO Exception");
		}
		assertEquals(expected, output.toString());
	}

	@Test
	public void testHelpContentActiveAction() throws IOException {
		String filename = "ua_help_content_active_action.htm";
		checkFileContentsPreserved(filename);
	}

	@Test
	public void testHelpContentActiveDebug() throws IOException {
		String filename = "ua_help_content_active_debug.htm";
		checkFileContentsPreserved(filename);
	}

	@Test
	public void testHelpContentActiveInvoke() throws IOException {
		String filename = "ua_help_content_active_invoke.htm";
		checkFileContentsPreserved(filename);
	}

	@Test
	public void testHelpContentActive() throws IOException {
		String filename = "ua_help_content_active.htm";
		checkFileContentsPreserved(filename);
	}

	@Test
	public void testHelpContentManifest() throws IOException {
		String filename = "ua_help_content_manifest.htm";
		checkFileContentsPreserved(filename);
	}

	@Test
	public void testHelpContentProcess() throws IOException {
		String filename = "ua_help_content_process.htm";
		checkFileContentsPreserved(filename);
	}

	@Test
	public void testHelpContentNested() throws IOException {
		String filename = "ua_help_content_nested.htm";
		checkFileContentsPreserved(filename);
	}

	@Test
	public void testHelpContentToc() throws IOException {
		String filename = "ua_help_content_toc.htm";
		checkFileContentsPreserved(filename);
	}

	@Test
	public void testHelpContentXhtml() throws IOException {
		String filename = "ua_help_content_xhtml.htm";
		checkFileContentsPreserved(filename);
	}

	@Test
	public void testHelpContent() throws IOException {
		String filename = "ua_help_content.htm";
		checkFileContentsPreserved(filename);
	}

	/*
	 * Test a pages from the help system to make sure there is no corruption
	 * when it is transformed.
	 */
	private void checkFileContentsPreserved(String filename) throws IOException {
		URL testURL = ResourceFinder.findFile(UserAssistanceTestPlugin.getDefault(),
		"/data/help/performance/search/" + filename);
		assertNotNull(testURL);
		InputStream input = testURL.openStream();
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		while (input.available() > 0) {
			int next = input.read();
			output.write(next);
		}
		String data = output.toString();
		checkFilter(data, data);
		input.close();
		output.close();
	}

}

Back to the top