Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 707c08ee963ff142d903da2efad28cb228cc56ea (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
/*******************************************************************************
 *  Copyright (c) 2008, 2016 IBM Corporation and others.
 *
 *  This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ua.tests.help.webapp;

import static org.junit.Assert.assertEquals;

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

import org.eclipse.help.internal.webapp.servlet.ExtraFilters;
import org.eclipse.help.internal.webapp.servlet.PrioritizedFilter;
import org.junit.After;
import org.junit.Test;

/**
 * Tests for the code which supports the extension point org.eclipse.help.webapp.extraFilter
 */
public class FilterExtensionTest {

	@After
	public void tearDown() throws Exception {
        ExtraFilters.setFilters(new PrioritizedFilter[0]);
	}

	@Test
	public void testFilterExtensions() throws IOException {
		PrioritizedFilter[] filters = new PrioritizedFilter[] {
				new PrioritizedFilter(new CommentFilterTwo(), 2),
				new PrioritizedFilter(new CommentFilterThree(), 3),
				new PrioritizedFilter(new CommentFilterOne(), 1) };
		ExtraFilters.setFilters(filters);
		OutputStream out = new ByteArrayOutputStream(1000);
		MockServletRequest req = new MockServletRequest();
		try (OutputStream filteredOutput = new ExtraFilters().filter(req, out)) {
			filteredOutput.write("<html>".getBytes());
		}
		String result = out.toString();
		String expected = "<!-- pre 3 --><!-- pre 2 --><!-- pre 1 --><html>"
				+ "<!-- post 1 --><!-- post 2 --><!-- post 3 -->";
		assertEquals(expected, result);
	}

	@Test
	public void testRepeatedExtensions() throws IOException {
		PrioritizedFilter[] filters = new PrioritizedFilter[] {
				new PrioritizedFilter(new CommentFilterTwo(), 2),
				new PrioritizedFilter(new CommentFilterThree(), 3),
				new PrioritizedFilter(new CommentFilterTwo(), 1) };
		ExtraFilters.setFilters(filters);
		OutputStream out = new ByteArrayOutputStream(1000);
		MockServletRequest req = new MockServletRequest();
		try (OutputStream filteredOutput = new ExtraFilters().filter(req, out)) {
			filteredOutput.write("<html>".getBytes());
		}
		String result = out.toString();
		String expected = "<!-- pre 3 --><!-- pre 2 --><!-- pre 2 --><html>"
				+ "<!-- post 2 --><!-- post 2 --><!-- post 3 -->";
		assertEquals(expected, result);
	}

	@Test
	public void testNoFilters() throws IOException {
		PrioritizedFilter[] filters = new PrioritizedFilter[0];
		ExtraFilters.setFilters(filters);
		OutputStream out = new ByteArrayOutputStream(1000);
		MockServletRequest req = new MockServletRequest();
		try (OutputStream filteredOutput = new ExtraFilters().filter(req, out)) {
			filteredOutput.write("<html>".getBytes());
		}
		String result = out.toString();
		String expected = "<html>";
		assertEquals(expected, result);
	}

}

Back to the top