From 6bd3d79c4b4fd6dcdbed80d72e34d15a5e068455 Mon Sep 17 00:00:00 2001 From: Chris Goldthorpe Date: Fri, 16 Oct 2009 00:07:36 +0000 Subject: Add tests for remote context servlet --- .../eclipse/ua/tests/help/other/UserContext.java | 66 ++++++++++++ .../ua/tests/help/remote/AllRemoteTests.java | 2 + .../ua/tests/help/remote/ContextServletTest.java | 111 +++++++++++++++++++++ .../help/remote/GetContextUsingRemoteHelp.java | 71 +++++++++++++ .../ua/tests/help/remote/MockContextServlet.java | 37 +++++++ .../nl/de/data/cheatsheet/contexts.xml | 19 ++++ org.eclipse.ua.tests/plugin.xml | 4 + 7 files changed, 310 insertions(+) create mode 100644 org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/other/UserContext.java create mode 100644 org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/ContextServletTest.java create mode 100644 org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/GetContextUsingRemoteHelp.java create mode 100644 org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/MockContextServlet.java create mode 100644 org.eclipse.ua.tests/nl/de/data/cheatsheet/contexts.xml diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/other/UserContext.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/other/UserContext.java new file mode 100644 index 000000000..4a3344adf --- /dev/null +++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/other/UserContext.java @@ -0,0 +1,66 @@ +/******************************************************************************* + * 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.help.ICommandLink; +import org.eclipse.help.IContext3; +import org.eclipse.help.IHelpResource; +import org.eclipse.help.ITopic; + +/** + * This class is used to test contexts created using the IContext API + */ + +public class UserContext implements IContext3 { + + private String title; + private String text; + private List children = new ArrayList(); + + public UserContext(String title, String text) { + this.title = title; + this.text = text; + } + + public ICommandLink[] getRelatedCommands() { + return new ICommandLink[0]; + } + + public String getCategory(IHelpResource topic) { + return null; + } + + public String getStyledText() { + return getText(); + } + + public String getTitle() { + return title; + } + + public IHelpResource[] getRelatedTopics() { + return (IHelpResource[])children.toArray(new IHelpResource[0]); + } + + public String getText() { + return text; + } + + public void addTopic(ITopic child) { + children.add(child); + } + + +} 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 38fd3efc4..b300c4058 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,7 +34,9 @@ public class AllRemoteTests extends TestSuite { addTestSuite(SearchServletTest.class); addTestSuite(IndexServletTest.class); addTestSuite(ContentServletTest.class); + addTestSuite(ContextServletTest.class); addTestSuite(SearchUsingRemoteHelp.class); addTestSuite(LoadTocUsingRemoteHelp.class); + addTestSuite(GetContextUsingRemoteHelp.class); } } diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/ContextServletTest.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/ContextServletTest.java new file mode 100644 index 000000000..2ca6a4baf --- /dev/null +++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/ContextServletTest.java @@ -0,0 +1,111 @@ +/******************************************************************************* + * 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.net.URL; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.FactoryConfigurationError; +import javax.xml.parsers.ParserConfigurationException; + +import junit.framework.TestCase; + +import org.eclipse.help.internal.base.BaseHelpSystem; +import org.eclipse.help.internal.entityresolver.LocalEntityResolver; +import org.eclipse.help.internal.server.WebappManager; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +public class ContextServletTest extends TestCase { + + private int mode; + + protected void setUp() throws Exception { + BaseHelpSystem.ensureWebappRunning(); + mode = BaseHelpSystem.getMode(); + BaseHelpSystem.setMode(BaseHelpSystem.MODE_INFOCENTER); + } + + protected void tearDown() throws Exception { + BaseHelpSystem.setMode(mode); + } + + public void testRemoteContextFound() throws Exception { + Element[] topics = getContextsFromServlet("org.eclipse.ua.tests.test_cheatsheets"); + assertEquals(1, topics.length); + assertEquals("abcdefg", topics[0].getAttribute("label")); + } + + public void testRemoteContextFoundDe() throws Exception { + Element[] topics = getContextsUsingLocale + ("org.eclipse.ua.tests.test_cheatsheets", "de"); + assertEquals(1, topics.length); + assertEquals("German Context", topics[0].getAttribute("label")); + } + + public void testRemoteContextNotFound() throws Exception { + try { + getContextsFromServlet("org.eclipse.ua.tests.no_such_context"); + fail("No exception thrown"); + } catch (IOException e) { + // IO exception caught as expected + } + } + + private Element[] getContextsFromServlet(String phrase) + throws Exception { + int port = WebappManager.getPort(); + URL url = new URL("http", "localhost", port, "/help/context?id=" + URLEncoder.encode(phrase, "UTF-8")); + return makeServletCall(url); + } + + private Element[] getContextsUsingLocale(String phrase, String locale) + throws Exception { + int port = WebappManager.getPort(); + URL url = new URL("http", "localhost", port, "/help/context?id=" + + URLEncoder.encode(phrase, "UTF-8") + "&lang=" + locale); + return makeServletCall(url); + } + + private Element[] makeServletCall(URL url) throws IOException, + ParserConfigurationException, FactoryConfigurationError, + SAXException { + InputStream is = url.openStream(); + InputSource inputSource = new InputSource(is); + DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + documentBuilder.setEntityResolver(new LocalEntityResolver()); + Document document = documentBuilder.parse(inputSource); + Node root = document.getFirstChild(); + is.close(); + assertEquals("context", root.getNodeName()); + NodeList children = root.getChildNodes(); + List topics = new ArrayList(); + int length = children.getLength(); + for (int i = 0; i < length; i++) { + Node next = children.item(i); + if ("topic".equals(next.getNodeName())) { + topics.add(next); + } + } + return (Element[]) topics.toArray(new Element[topics.size()]); + } + +} diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/GetContextUsingRemoteHelp.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/GetContextUsingRemoteHelp.java new file mode 100644 index 000000000..2a6cc2dce --- /dev/null +++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/GetContextUsingRemoteHelp.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * 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 junit.framework.TestCase; + +import org.eclipse.help.IContext; +import org.eclipse.help.IContext3; +import org.eclipse.help.IHelpResource; +import org.eclipse.help.internal.HelpPlugin; +import org.eclipse.help.internal.base.BaseHelpSystem; + +public class GetContextUsingRemoteHelp extends TestCase { + + private int mode; + + protected void setUp() throws Exception { + RemotePreferenceStore.savePreferences(); + mode = BaseHelpSystem.getMode(); + RemotePreferenceStore.setMockRemoteServer(); + BaseHelpSystem.setMode(BaseHelpSystem.MODE_INFOCENTER); + } + + protected void tearDown() throws Exception { + RemotePreferenceStore.restorePreferences(); + BaseHelpSystem.setMode(mode); + } + + public void testContextDefaultLocale() throws Exception { + IContext context = HelpPlugin.getContextManager().getContext("org.eclipse.ua.tests.test_one", "en"); + assertNotNull(context); + IHelpResource[] relatedTopics = context.getRelatedTopics(); + assertEquals(1, relatedTopics.length); + String topicLabel = relatedTopics[0].getLabel(); + assertEquals("context_one_en", topicLabel); + String title = ((IContext3)context).getTitle(); + assertEquals("context_one_en", title); + RemotePreferenceStore.disableRemoteHelp(); + context = HelpPlugin.getContextManager().getContext("org.eclipse.ua.tests.test_one", "en"); + assertNull(context); + } + + public void testLocalContextBeatsRemote() throws Exception { + IContext context = HelpPlugin.getContextManager().getContext("org.eclipse.ua.tests.test_cheatsheets", "en"); + assertNotNull(context); + IHelpResource[] relatedTopics = context.getRelatedTopics(); + assertEquals(1, relatedTopics.length); + String topicLabel = relatedTopics[0].getLabel(); + assertEquals("abcdefg", topicLabel); + } + + public void testContextDeLocale() throws Exception { + IContext context = HelpPlugin.getContextManager().getContext("org.eclipse.ua.tests.test_one", "de"); + assertEquals("context_one_de", ((IContext3)context).getTitle()); + } + + public void testContextNotFound() throws Exception { + IContext context = HelpPlugin.getContextManager().getContext("org.eclipse.ua.tests.no_such_ctx", "en"); + assertNull(context); + } + +} diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/MockContextServlet.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/MockContextServlet.java new file mode 100644 index 000000000..49100a4fb --- /dev/null +++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/remote/MockContextServlet.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * 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.IContext; +import org.eclipse.help.internal.webapp.servlet.ContextServlet; +import org.eclipse.ua.tests.help.other.UserContext; +import org.eclipse.ua.tests.help.other.UserTopic; + +public class MockContextServlet extends ContextServlet { + + private static final String ORG_ECLIPSE_UA_TESTS_TEST = "org.eclipse.ua.tests.test"; + private static final long serialVersionUID = 3615294041471900830L; + + protected IContext getContext(String locale, String id) { + if (id.startsWith(ORG_ECLIPSE_UA_TESTS_TEST)) { + String suffix = id.substring(ORG_ECLIPSE_UA_TESTS_TEST.length()); + String title = "context" + suffix + '_' + locale; + UserContext context = new UserContext(title, "Decriptiion for " + title); + UserTopic topic = new UserTopic(title, "topic.html", true); + context.addTopic(topic); + return context; + } + return null; + } + + +} diff --git a/org.eclipse.ua.tests/nl/de/data/cheatsheet/contexts.xml b/org.eclipse.ua.tests/nl/de/data/cheatsheet/contexts.xml new file mode 100644 index 000000000..c90592f32 --- /dev/null +++ b/org.eclipse.ua.tests/nl/de/data/cheatsheet/contexts.xml @@ -0,0 +1,19 @@ + + + + + + + Infopop test successful! The "contextId" was specified correctly in the cheat sheets content file. + + + diff --git a/org.eclipse.ua.tests/plugin.xml b/org.eclipse.ua.tests/plugin.xml index 62a0fbc44..fc791f15b 100644 --- a/org.eclipse.ua.tests/plugin.xml +++ b/org.eclipse.ua.tests/plugin.xml @@ -581,6 +581,10 @@ class="org.eclipse.ua.tests.help.remote.MockTocServlet" httpcontextId="uaTest"> + + -- cgit v1.2.3