Skip to main content
summaryrefslogtreecommitdiffstats
blob: 30ac88c737a7bcbb1d3779af9167cce5e27e45e6 (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
/*******************************************************************************
 * 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.remote;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.eclipse.help.internal.server.WebappManager;
import org.osgi.framework.Bundle;

public class RemoteTestUtils {

	public static String createMockContent(String plugin, String path,
			String locale, int port) {
		String result = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">"
				+ "<HTML lang =\"" + locale + "\"><HEAD>"
				+ "<TITLE> Content from: " + plugin + "</TITLE></HEAD>"
				+ "<BODY><P>Path is: " + path + ",Port is: "+port+"</P></BODY></HTML>";
		return result;
	}

	public static String getRemoteContent(String plugin, String path,
			String locale) throws Exception {
		int port = WebappManager.getPort();
		URL url = new URL("http", "localhost", port, "/help/rtopic/" + plugin
				+ path + "?lang=" + locale);
		return readFromURL(url);
	}

	public static String getLocalContent(String plugin, String path)
			throws IOException {
		Bundle bundle = Platform.getBundle(plugin);
		URL url;
		if (bundle != null) {
			url = FileLocator.toFileURL(new URL(bundle.getEntry("/"), path)); //$NON-NLS-1$
		} else {
			throw new IOException("Invalid bundle " + plugin);
		}
		return readFromURL(url);
	}

	public static String readFromURL(URL url) throws IOException,
			UnsupportedEncodingException {
		InputStream is = url.openStream();
		InputStreamReader inputStreamReader = new InputStreamReader(is, "UTF-8");
		StringBuffer buffer = new StringBuffer();
		char[] cbuf = new char[256];
		int len;
		do {
			len = inputStreamReader.read(cbuf);
			if (len > 0) {
				buffer.append(cbuf, 0, len);
			}
		} while (len >= 0);
		return buffer.toString();
	}

}

Back to the top