Skip to main content
summaryrefslogtreecommitdiffstats
blob: f806b9326b874e9669fee52ddf3c8264bc8420d9 (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
/*******************************************************************************
 * Copyright (c) 2009, 2012 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.remote;

import java.net.URL;

import junit.framework.Test;
import junit.framework.TestCase;

import org.eclipse.help.internal.HelpPlugin;
import org.eclipse.help.internal.base.BaseHelpSystem;
import org.eclipse.help.internal.server.WebappManager;
import org.eclipse.test.OrderedTestSuite;

public class GetContentUsingRemoteHelp extends TestCase {
	

	public static Test suite() {
		return new OrderedTestSuite(GetContentUsingRemoteHelp.class, new String[] {
			"testContentNotFound",
			"testContentFound",
			"testContentFoundDe",
			"testLocalBeatsRemote",
			"testRemoteHelpPreferredPreference",
			"testRemoteOrdering",
			"testRemoteOrderingReversed",
			"testRemoteUsedIfLocalUnavaliable"
		});
	}


	private int mode;

	protected void setUp() throws Exception {
		BaseHelpSystem.ensureWebappRunning();
        mode = BaseHelpSystem.getMode();
        RemotePreferenceStore.savePreferences();
		RemotePreferenceStore.setMockRemoteServer();
		RemotePreferenceStore.disableErrorPage();
		BaseHelpSystem.setMode(BaseHelpSystem.MODE_INFOCENTER);
		HelpPlugin.getTocManager().getTocs("en");
	}
	
	protected void tearDown() throws Exception {
		RemotePreferenceStore.restorePreferences();
		BaseHelpSystem.setMode(mode);
	}

	public void testContentNotFound()  {
		try {
			getHelpContent("mock.toc", "/invalid/nosuchfile.html", "en");
			fail("No exception thrown");
		} catch (Exception e) {
			// Exception thrown as expected
		}
	}

	public void testContentFound() throws Exception  {
        final String path = "/data/help/index/topic1.html";
        String remoteContent = getHelpContent("mock.toc", path, "en");
		int port = TestServerManager.getPort(0);
		String expectedContent = RemoteTestUtils.createMockContent("mock.toc", path, "en", port);
		assertEquals(expectedContent, remoteContent);
	}

	public void testContentFoundDe() throws Exception  {
        final String path = "/data/help/index/topic2.html";
        String remoteContent = getHelpContent("mock.toc", path, "de");
		int port = TestServerManager.getPort(0);
		String expectedContent = RemoteTestUtils.createMockContent("mock.toc", path, "de", port);
		assertEquals(expectedContent, remoteContent);
	}

	public void testLocalBeatsRemote() throws Exception  {
        final String path = "/doc/help_home.html";
        String plugin = "org.eclipse.help.base";
		String helpContent = getHelpContent(plugin, path, "en");
		String localContent = RemoteTestUtils.getLocalContent(plugin, path);
		assertEquals(localContent, helpContent);
	}
	
	public void testRemoteHelpPreferredPreference() throws Exception  {
		RemotePreferenceStore.setMockRemotePriority();
		HelpPlugin.getTocManager().clearCache();
		HelpPlugin.getTocManager().getTocs("en");
        final String path = "/doc/help_home.html";
        String plugin = "org.eclipse.help.base";
		String helpContent = getHelpContent(plugin, path, "en");

		int port = TestServerManager.getPort(0);
		String remoteContent = RemoteTestUtils.createMockContent(plugin, path, "en", port);
		assertEquals(remoteContent, helpContent);
	}

	public void testRemoteOrdering() throws Exception {
		RemotePreferenceStore.setTwoMockRemoteServers();
		RemotePreferenceStore.setMockRemotePriority();
		HelpPlugin.getTocManager().clearCache();
		HelpPlugin.getTocManager().getTocs("en");
		//Verify help coming from first one
		 final String path = "/doc/help_home.html";
	     String plugin = "org.eclipse.help.base";
		 String helpContent = GetContentUsingRemoteHelp.getHelpContent(plugin, path, "en");
		 
		 //Get remote content from first one in prefs

		 int port0 = TestServerManager.getPort(0);
		 String remoteContent0 = RemoteTestUtils.createMockContent(plugin, path, "en", port0);
		 
		 int port1 = TestServerManager.getPort(1);
		 String remoteContent1 = RemoteTestUtils.createMockContent(plugin, path, "en", port1);

		 assertEquals(remoteContent0, helpContent);
		 assertFalse(remoteContent1.equals(helpContent));

	}
	
	public void testRemoteOrderingReversed() throws Exception {
		RemotePreferenceStore.setTwoMockRemoteServersReversePriority();
		RemotePreferenceStore.setMockRemotePriority();
		HelpPlugin.getTocManager().clearCache();
		HelpPlugin.getTocManager().getTocs("en");
		//Verify help coming from first one
		 final String path = "/doc/help_home.html";
	     String plugin = "org.eclipse.help.base";
		 String helpContent = GetContentUsingRemoteHelp.getHelpContent(plugin, path, "en");
		 
		 //Get remote content from second in prefs

		 int port0 = TestServerManager.getPort(0);
		 String remoteContent0 = RemoteTestUtils.createMockContent(plugin, path, "en", port0);
		 
		 int port1 = TestServerManager.getPort(1);
		 String remoteContent1 = RemoteTestUtils.createMockContent(plugin, path, "en", port1);
		 
		 
		 assertEquals(remoteContent1, helpContent);
		 assertFalse(remoteContent0.equals(helpContent));

	}

	
	public void testRemoteUsedIfLocalUnavaliable() throws Exception  {
		RemotePreferenceStore.setMockRemoteServer();
		HelpPlugin.getTocManager().clearCache();
		HelpPlugin.getTocManager().getTocs("en");
		final String path = "/data/help/nonlocal.html";
        String plugin = "org.eclipse.help.base";
		String remoteContent = getHelpContent(plugin, path, "en");		
		int port = TestServerManager.getPort(0);
		String expectedContent = RemoteTestUtils.createMockContent(plugin, path, "en", port);
		assertEquals(expectedContent, remoteContent);
	}
	
	public static String getHelpContent(String plugin, String path, String locale)
			throws Exception {
		int port = WebappManager.getPort();
		URL url = new URL("http", "localhost", port, "/help/nftopic/" + plugin + path + "?lang=" + locale);
		return RemoteTestUtils.readFromURL(url);
	}

}

Back to the top