diff options
author | Chris Goldthorpe | 2009-10-16 20:39:07 +0000 |
---|---|---|
committer | Chris Goldthorpe | 2009-10-16 20:39:07 +0000 |
commit | e1d8a5e5c04581f9ada67b3829ef9841669b2d0b (patch) | |
tree | 88dbe7c16454fbc64c53410cbc7efa63b18a08a1 | |
parent | aecd39c80aad760f7cbdca60cbf4edc324dce1dd (diff) | |
download | eclipse.platform.ua-e1d8a5e5c04581f9ada67b3829ef9841669b2d0b.tar.gz eclipse.platform.ua-e1d8a5e5c04581f9ada67b3829ef9841669b2d0b.tar.xz eclipse.platform.ua-e1d8a5e5c04581f9ada67b3829ef9841669b2d0b.zip |
Add test for reading remote indexv20091019
7 files changed, 232 insertions, 29 deletions
diff --git a/org.eclipse.help.base/src/org/eclipse/help/internal/server/JettyHelpServer.java b/org.eclipse.help.base/src/org/eclipse/help/internal/server/JettyHelpServer.java index 6ca5a454d..af5540ac2 100644 --- a/org.eclipse.help.base/src/org/eclipse/help/internal/server/JettyHelpServer.java +++ b/org.eclipse.help.base/src/org/eclipse/help/internal/server/JettyHelpServer.java @@ -94,7 +94,7 @@ public class JettyHelpServer extends HelpServer { private String host; - private int port = -1; + protected int port = -1; protected static final int AUTO_SELECT_JETTY_PORT = 0; public void start(final String webappName) throws Exception { @@ -107,7 +107,7 @@ public class JettyHelpServer extends HelpServer { * Ensures that the bundle with the specified name and the highest available * version is started and reads the port number */ - private void checkBundle() throws InvalidSyntaxException, BundleException { + protected void checkBundle() throws InvalidSyntaxException, BundleException { Bundle bundle = Platform.getBundle("org.eclipse.equinox.http.registry"); //$NON-NLS-1$if (bundle != null) { if (bundle.getState() == Bundle.RESOLVED) { bundle.start(Bundle.START_TRANSIENT); diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/other/UserIndex.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/other/UserIndex.java new file mode 100644 index 000000000..51f28af2d --- /dev/null +++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/other/UserIndex.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * 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.other; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.expressions.IEvaluationContext; +import org.eclipse.help.IIndex; +import org.eclipse.help.IIndexEntry; +import org.eclipse.help.IUAElement; + +/** + * This class is used to test topics created using the IIndex API + */ + +public class UserIndex implements IIndex { + + private List entries = new ArrayList(); + private boolean enabled; + + public UserIndex(boolean enabled) { + this.enabled = true; + } + + public IUAElement[] getChildren() { + return getEntries(); + } + + public void addEntry(IIndexEntry child) { + entries.add(child); + } + + public IIndexEntry[] getEntries() { + return (IIndexEntry[])entries.toArray(new IIndexEntry[0]); + } + + public boolean isEnabled(IEvaluationContext context) { + return enabled; + } + +} diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/AllRemoteTests.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/AllRemoteTests.java index 786f35cc6..f78b669ba 100755 --- a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/AllRemoteTests.java +++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/AllRemoteTests.java @@ -34,10 +34,11 @@ public class AllRemoteTests extends TestSuite { addTestSuite(SearchServletTest.class); addTestSuite(IndexServletTest.class); addTestSuite(ContentServletTest.class); - addTestSuite(ContextServletTest.class); - addTestSuite(SearchUsingRemoteHelp.class); + addTestSuite(ContextServletTest.class); addTestSuite(LoadTocUsingRemoteHelp.class); - addTestSuite(GetContextUsingRemoteHelp.class); + addTestSuite(SearchUsingRemoteHelp.class); + addTestSuite(LoadIndexUsingRemoteHelp.class); addTestSuite(GetContentUsingRemoteHelp.class); + addTestSuite(GetContextUsingRemoteHelp.class); } } diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/JettyTestServer.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/JettyTestServer.java index 532dcba4b..e09bbeb21 100644 --- a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/JettyTestServer.java +++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/JettyTestServer.java @@ -11,16 +11,37 @@ package org.eclipse.ua.tests.help.remote; +import org.eclipse.core.runtime.Platform; import org.eclipse.help.internal.server.JettyHelpServer; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleException; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; public class JettyTestServer extends JettyHelpServer { protected String getOtherInfo() { - return //super.getOtherInfo(); - "org.eclipse.ua.tests"; + return "org.eclipse.ua.tests"; } protected int getPortParameter() { return AUTO_SELECT_JETTY_PORT; } + + /* + * Ensures that the bundle with the specified name and the highest available + * version is started and reads the port number + */ + protected void checkBundle() throws InvalidSyntaxException, BundleException { + Bundle bundle = Platform.getBundle("org.eclipse.equinox.http.registry"); //$NON-NLS-1$if (bundle != null) { + if (bundle.getState() == Bundle.RESOLVED) { + bundle.start(Bundle.START_TRANSIENT); + } + if (port == -1) { + // Jetty selected a port number for us + ServiceReference[] reference = bundle.getBundleContext().getServiceReferences("org.osgi.service.http.HttpService", "(other.info=" + getOtherInfo() + ')'); //$NON-NLS-1$ //$NON-NLS-2$ + Object assignedPort = reference[reference.length - 1].getProperty("http.port"); //$NON-NLS-1$ + port = Integer.parseInt((String)assignedPort); + } + } } diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/LoadIndexUsingRemoteHelp.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/LoadIndexUsingRemoteHelp.java new file mode 100644 index 000000000..51c6cba14 --- /dev/null +++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/LoadIndexUsingRemoteHelp.java @@ -0,0 +1,83 @@ +/******************************************************************************* + * 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.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +import org.eclipse.help.IIndex; +import org.eclipse.help.IIndexEntry; +import org.eclipse.help.internal.HelpPlugin; +import org.eclipse.help.internal.base.BaseHelpSystem; + +public class LoadIndexUsingRemoteHelp extends TestCase { + + private int mode; + + protected void setUp() throws Exception { + RemotePreferenceStore.savePreferences(); + mode = BaseHelpSystem.getMode(); + BaseHelpSystem.setMode(BaseHelpSystem.MODE_INFOCENTER); + } + + protected void tearDown() throws Exception { + RemotePreferenceStore.restorePreferences(); + BaseHelpSystem.setMode(mode); + } + + public void testIndexContribution() throws Exception { + String locale = "en"; + HelpPlugin.getIndexManager().clearCache(); + IIndex index = HelpPlugin.getIndexManager().getIndex(locale); + assertEquals(0, matchingEntries(index, "entry1_" + locale).length); + assertEquals(0, matchingEntries(index, "entry2_" + locale).length); + RemotePreferenceStore.setMockRemoteServer(); + HelpPlugin.getIndexManager().clearCache(); + index = HelpPlugin.getIndexManager().getIndex(locale); + assertEquals(1, matchingEntries(index, "entry1_" + locale).length); + assertEquals(1, matchingEntries(index, "entry2_" + locale).length); + } + + public void testIndexWithTwoRemoteServers() throws Exception { + String locale = "en"; + HelpPlugin.getIndexManager().clearCache(); + IIndex index = HelpPlugin.getIndexManager().getIndex(locale); + IIndexEntry[] entry1 = matchingEntries(index, "entry1_" + locale); + assertEquals(0, entry1.length); + IIndexEntry[] entry2 = matchingEntries(index, "entry2_" + locale); + assertEquals(0, entry2.length); + RemotePreferenceStore.setTwoMockRemoteServers(); + HelpPlugin.getIndexManager().clearCache(); + index = HelpPlugin.getIndexManager().getIndex(locale); + // Entry 1 has the same child on each remote server, Entry 2 has different children + entry1 = matchingEntries(index, "entry1_" + locale); + entry2 = matchingEntries(index, "entry2_" + locale); + assertEquals(1, entry1.length); + assertEquals(1, entry1[0].getTopics().length); + assertEquals(1, entry2.length); + assertEquals(2, entry2[0].getTopics().length); + } + + private IIndexEntry[] matchingEntries(IIndex index, String keyword) { + List matches = new ArrayList(); + IIndexEntry[] entries = index.getEntries(); + for (int i = 0; i < entries.length; i++) { + if (keyword.equals(entries[i].getKeyword())) { + matches.add(entries[i]); + } + } + return (IIndexEntry[]) matches.toArray(new IIndexEntry[matches.size()]); + } + +} diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/MockIndexServlet.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/MockIndexServlet.java new file mode 100644 index 000000000..6f7d67605 --- /dev/null +++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/MockIndexServlet.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * 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 javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.xml.transform.TransformerException; + +import org.eclipse.help.internal.index.Index; +import org.eclipse.help.internal.index.IndexContribution; +import org.eclipse.help.internal.webapp.data.UrlUtil; +import org.eclipse.help.internal.webapp.servlet.IndexServlet; +import org.eclipse.ua.tests.help.other.UserIndex; +import org.eclipse.ua.tests.help.other.UserIndexEntry; +import org.eclipse.ua.tests.help.other.UserTopic; + +public class MockIndexServlet extends IndexServlet { + + private static final long serialVersionUID = -930969620357059313L; + + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + String locale = UrlUtil.getLocale(req, resp); + String response; + req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$ + resp.setContentType("application/xml; charset=UTF-8"); //$NON-NLS-1$ + + IndexContribution[] contributions = getIndex(req, locale); + try { + response = serialize(contributions, locale); + } catch (TransformerException e) { + throw new ServletException(e); + } + resp.getWriter().write(response); + } + + private IndexContribution[] getIndex(HttpServletRequest req, String locale) { + int port = req.getLocalPort(); + UserIndex index = new UserIndex(true); + UserIndexEntry entry1 = new UserIndexEntry("entry1_" + locale, false); + UserTopic topic1 = new UserTopic("topic1_", "href.html", false); + index.addEntry(entry1); + entry1.addTopic(topic1); + UserIndexEntry entry2 = new UserIndexEntry("entry2_" + locale, false); + UserTopic topic2 = new UserTopic("topic2_" + port, "href" + port + ".html", false); + index.addEntry(entry2); + entry2.addTopic(topic2); + IndexContribution contribution = new IndexContribution(); + contribution.setId("mock.index"); + contribution.setIndex(new Index(index)); + contribution.setLocale(locale); + return new IndexContribution[] { contribution }; + } + +} + + diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/MockIndexSevlet.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/MockIndexSevlet.java deleted file mode 100644 index ae1e3a5df..000000000 --- a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/MockIndexSevlet.java +++ /dev/null @@ -1,22 +0,0 @@ -/******************************************************************************* - * 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 org.eclipse.help.internal.webapp.servlet.IndexServlet; - -public class MockIndexSevlet extends IndexServlet { - - private static final long serialVersionUID = -930969620357059313L; - - // TODO implement servlet - -} |