Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6c432a4870d86a1686cceff7b78b870e503e0dcb (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 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.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;

import junit.framework.TestCase;

import org.eclipse.help.internal.webapp.servlet.PluginsRootResolvingStream;

/**
 * Test for text matching when inserting links
 */

public class ChildLinkInsertion extends TestCase {
	
	private class TestableReplacementStream extends PluginsRootResolvingStream {
		public TestableReplacementStream(OutputStream out, HttpServletRequest req, String prefix) {
			super(out, req, prefix);
		}

		@Override
		protected void insertBasedOnKeyword(int index) throws IOException {
			if (index == 0 ) {
				out.write("<LINKS>".getBytes()); //$NON-NLS-1$
			} else if (index == 1) {
				out.write("<STYLE>".getBytes()); //$NON-NLS-1$
			} else {
				out.write("<UNKNOWN>".getBytes()); //$NON-NLS-1$
			}
		}
	}

	public void testEmpty() {
	    final String input = "";
		checkFilter(input, input);
	}

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

	public void testPartialMatch1() {
	    final String input = "<A href = \"PLUGINS\"><!--INSTRUCT-->";
		checkFilter(input, input);
	}

	public void testPartialMatch2() {
	    final String input = "<A href = \"PLUGINS\"><!A -->";
		checkFilter(input, input);
	}

	public void testPartialMatch3() {
	    final String input = "<A href = \"PLUGINS\"><!-A -->";
		checkFilter(input, input);
	}
	
	public void testPartialMatch4() {
	    final String input = "<A href = \"PLUGINS\"><!--A-->";
		checkFilter(input, input);
	}
	
	public void testEndsUnmatched() {
	    final String input = "<A><!--INSTR";
		checkFilter(input, input);
	}

	public void testNotAtStart() {
	    final String input = "<A><!-- INSERT_CHILD_LINKS-->";
		checkFilter(input, input);
	}

	public void testSpaceBeforeEnd() {
	    final String input = "<A><!-- INSERT_CHILD_LINKS -->";
		checkFilter(input, input);
	}

	public void testTooManyCharacters_1() {
	    final String input = "<A><!--INSERT_CHILD_LINKSS-->";
		checkFilter(input, input);
	}
	
	public void testTooManyCharacters_2() {
	    final String input = "<A><!--INSERT_CHILD_LINKS_STYLES-->";
		checkFilter(input, input);
	}

	public void testAtStart() {
	    final String input = "<!--INSERT_CHILD_LINKS--><A>";
	    final String expected = "<LINKS><A>";
		checkFilter(input, expected);
	}

	public void testChildStyle() {
	    final String input = "<!--INSERT_CHILD_LINK_STYLE--><A>";
	    final String expected = "<STYLE><A>";
		checkFilter(input, expected);
	}

	public void testDefaultEncoding() {
	    final String input = "";
		checkEncoding(input, null);
	}

	public void testEncodingUtf8() {
	    final String input = 
	    	"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
		checkEncoding(input, "utf-8");
	}
	
	public void testMetaNoEncoding() {
	    final String input = 
	    	"<meta http-equiv=\"Content-Type\" content=\"text/html\">";
		checkEncoding(input, null);
	}

	public void testMultiMeta() {
	    final String input = 
	    	"<meta name=\"test\" content=\"test\">" +
	    	"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" +
	    	"<meta name=\"test\" content=\"test\">";
		checkEncoding(input, "utf-8");
	}
	
	public void testMetaAndInsert() {
	    final String metaInfo = "<meta name=\"test\" content=\"test\">" +
			    	"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
		final String input = 
	    	metaInfo + "<!--INSERT_CHILD_LINK_STYLE--><A>";
		    final String expected = metaInfo + "<STYLE><A>";
		checkFilter(input, expected);
		checkEncoding(input, "utf-8");
	}

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

Back to the top