diff options
author | Angel Avila | 2013-02-06 01:32:27 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2013-02-06 20:07:34 +0000 |
commit | 8de7e64ca22b06d979984612ba54183144a97fc5 (patch) | |
tree | 8676c725c64f28d48d326f2c8fa29757a00a464a /plugins/org.eclipse.osee.ote.help.ui.test/src | |
parent | a5c3eb24e1d06175e24ba11a4fa9afcf4ac4a652 (diff) | |
download | org.eclipse.osee-8de7e64ca22b06d979984612ba54183144a97fc5.tar.gz org.eclipse.osee-8de7e64ca22b06d979984612ba54183144a97fc5.tar.xz org.eclipse.osee-8de7e64ca22b06d979984612ba54183144a97fc5.zip |
refinement[ats_PX4HJ]: Create Help System Unit Test
Change-Id: I04d175e4f5dad3b0e55dc8db481711523ad2e8de
Diffstat (limited to 'plugins/org.eclipse.osee.ote.help.ui.test/src')
7 files changed, 574 insertions, 0 deletions
diff --git a/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/HelpContextTest.java b/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/HelpContextTest.java new file mode 100644 index 00000000000..86eb58062a6 --- /dev/null +++ b/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/HelpContextTest.java @@ -0,0 +1,85 @@ +/******************************************************************************* + * Copyright (c) 2013 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.help.ui; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import java.lang.reflect.Field; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import org.eclipse.osee.framework.jdk.core.util.Compare; +import org.eclipse.osee.framework.ui.plugin.util.HelpContext; +import org.eclipse.osee.ote.help.ui.util.ContextParser; +import org.eclipse.osee.ote.help.ui.util.ContextParser.ContextEntry; +import org.eclipse.osee.ote.help.ui.util.HelpTestUtil; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * @author Angel Avila + */ +public class HelpContextTest { + + private static final String PLUGIN_ID = "org.eclipse.osee.ote.help.ui"; + + private static Map<String, HelpContext> contexts; + private static ContextParser parser; + + @BeforeClass + public static void setUp() throws Exception { + contexts = getContexts(OteHelpContext.class); + parser = new ContextParser("contexts/contexts.xml"); + parser.parse(); + } + + @Test + public void testAdd() throws Exception { + assertEquals(12, contexts.size()); + } + + @Test + public void testContextsXml() throws Exception { + assertEquals(contexts.size(), parser.getEntries().size()); + + Assert.assertFalse(Compare.isDifferent(contexts.keySet(), parser.getIds())); + for (HelpContext context : contexts.values()) { + assertEquals(PLUGIN_ID, context.getPluginId()); + + String id = context.getName(); + ContextEntry entry = parser.getEntry(id); + assertNotNull(entry); + } + } + + @Test + public void testHrefFiles() throws Exception { + for (ContextEntry entry : parser.getEntries()) { + for (String reference : entry.getReferences()) { + URL url = HelpTestUtil.getResource(reference); + assertNotNull(String.format("[%s] was not valid", reference), url); + } + } + } + + private static Map<String, HelpContext> getContexts(Class<?> clazz) throws IllegalArgumentException, IllegalAccessException { + Map<String, HelpContext> contexts = new HashMap<String, HelpContext>(); + for (Field field : clazz.getFields()) { + Object object = field.get(null); + if (object instanceof HelpContext) { + HelpContext context = (HelpContext) object; + contexts.put(context.getName(), context); + } + } + return contexts; + } +} diff --git a/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/HelpTableOfContentTest.java b/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/HelpTableOfContentTest.java new file mode 100644 index 00000000000..6a1ff5fe6ff --- /dev/null +++ b/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/HelpTableOfContentTest.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2013 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.help.ui; + +import static org.junit.Assert.assertNotNull; +import java.net.URL; +import java.util.Set; +import org.eclipse.osee.ote.help.ui.util.HelpTestUtil; +import org.eclipse.osee.ote.help.ui.util.HtmlParser; +import org.eclipse.osee.ote.help.ui.util.TocParser; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * @author Angel Avila + */ +public class HelpTableOfContentTest { + + private static final String PLUGIN_ID = "org.eclipse.osee.ote.help.ui"; + + private static TocParser parser; + + @BeforeClass + public static void setUp() throws Exception { + parser = new TocParser("toc.xml"); + parser.parse(); + } + + @Test + public void testAllTocReferencesExist() throws Exception { + for (String reference : parser.getEntries()) { + URL url = HelpTestUtil.getResource(reference); + assertNotNull(String.format("[%s] was not valid", reference), url); + } + } + + @Test + public void testTocReferencesValid() throws Exception { + HtmlParser htmlParser = new HtmlParser(PLUGIN_ID); + + for (String reference : parser.getEntries()) { + URL url = HelpTestUtil.getResource(reference); + + Set<String> entries = htmlParser.parse(url); + for (String resource : entries) { + URL referencedUrl = HelpTestUtil.getResource(reference); + assertNotNull(String.format("[%s] was not valid", resource), referencedUrl); + } + + } + } +} diff --git a/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/OteHelpUiTestSuite.java b/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/OteHelpUiTestSuite.java new file mode 100644 index 00000000000..e01b751cd37 --- /dev/null +++ b/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/OteHelpUiTestSuite.java @@ -0,0 +1,23 @@ +/******************************************************************************* + * Copyright (c) 2013 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.help.ui; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * @author Angel Avila + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({HelpContextTest.class, HelpTableOfContentTest.class}) +public class OteHelpUiTestSuite { + // Test Suite +} diff --git a/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/util/ContextParser.java b/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/util/ContextParser.java new file mode 100644 index 00000000000..e9d2c3ea926 --- /dev/null +++ b/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/util/ContextParser.java @@ -0,0 +1,141 @@ +/******************************************************************************* + * Copyright (c) 2013 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.help.ui.util; +import java.io.BufferedInputStream; +import java.io.InputStream; +import java.net.URL; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamReader; +import org.eclipse.osee.framework.jdk.core.util.Lib; +import org.eclipse.osee.framework.jdk.core.util.Strings; + + +/** + * @author Angel Avila + */ +public class ContextParser { + + private static final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory(); + + private static final String CONTEXT = "context"; + private static final String TOPIC = "topic"; + private static final String HREF = "href"; + private static final String ID_TAG = "id"; + + public Map<String, ContextEntry> entries = new HashMap<String, ContextEntry>(); + public String path; + + private String localName; + private String uri; + private ContextEntry currentEntry; + + public ContextParser(String path) { + this.path = path; + } + + public void parse() throws Exception { + entries.clear(); + URL url = HelpTestUtil.getResource(path); + + InputStream inputStream = null; + try { + inputStream = new BufferedInputStream(url.openStream()); + XMLStreamReader streamReader = xmlInputFactory.createXMLStreamReader(inputStream); + while (streamReader.hasNext()) { + process(streamReader); + streamReader.next(); + } + + } finally { + Lib.close(inputStream); + } + } + + private void process(XMLStreamReader reader) { + int eventType = reader.getEventType(); + switch (eventType) { + case XMLStreamConstants.START_ELEMENT: + localName = reader.getLocalName(); + uri = reader.getNamespaceURI(); + if (CONTEXT.equals(localName)) { + + String id = reader.getAttributeValue(uri, ID_TAG); + if (Strings.isValid(id)) { + currentEntry = new ContextEntry(id); + } + + } else if (TOPIC.equals(localName) && currentEntry != null) { + String reference = reader.getAttributeValue(uri, HREF); + if (Strings.isValid(reference)) { + String path = normalizePath(reference); + currentEntry.getReferences().add(path); + } + } + break; + case XMLStreamConstants.END_ELEMENT: + localName = reader.getLocalName(); + uri = reader.getNamespaceURI(); + if (CONTEXT.equals(localName) && currentEntry != null) { + entries.put(currentEntry.getId(), currentEntry); + reset(); + } + break; + } + } + + private String normalizePath(String reference) { + return reference.replaceAll("\\.html#.*", ".html"); + } + + private void reset() { + localName = null; + uri = null; + currentEntry = null; + } + + public Collection<ContextEntry> getEntries() { + return entries.values(); + } + + public ContextEntry getEntry(String id) { + return entries.get(id); + } + + public Set<String> getIds() { + return entries.keySet(); + } + + public final class ContextEntry { + private final String id; + private final Set<String> references = new LinkedHashSet<String>(); + + public ContextEntry(String id) { + super(); + this.id = id; + } + + public String getId() { + return id; + } + + public Set<String> getReferences() { + return references; + } + + } + +} diff --git a/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/util/HelpTestUtil.java b/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/util/HelpTestUtil.java new file mode 100644 index 00000000000..157b5fd57db --- /dev/null +++ b/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/util/HelpTestUtil.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2013 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.help.ui.util; + +import static junit.framework.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import java.io.File; +import java.net.URL; +import org.eclipse.core.runtime.FileLocator; +import org.osgi.framework.Bundle; +import org.osgi.framework.FrameworkUtil; + +/** + * @author Angel Avila + */ +public final class HelpTestUtil { + + private HelpTestUtil() { + // Utility Class + } + + public static URL getResource(String resource) throws Exception { + Bundle bundle = FrameworkUtil.getBundle(HelpTestUtil.class); + URL url = bundle.getResource(resource); + + assertNotNull(String.format("Resource not found: [%s]", resource), url); + + url = FileLocator.toFileURL(url); + File file = new File(url.toURI()); + assertEquals(String.format("[%s] does not exist", resource), true, file.exists()); + assertEquals(String.format("[%s] unreadable", resource), true, file.canRead()); + + return url; + } +} diff --git a/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/util/HtmlParser.java b/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/util/HtmlParser.java new file mode 100644 index 00000000000..f9e0c42a171 --- /dev/null +++ b/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/util/HtmlParser.java @@ -0,0 +1,119 @@ +/******************************************************************************* + * Copyright (c) 2013 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.help.ui.util; + +import java.io.BufferedInputStream; +import java.io.InputStream; +import java.net.URL; +import java.util.HashSet; +import java.util.Set; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamReader; +import org.eclipse.osee.framework.jdk.core.util.Lib; +import org.eclipse.osee.framework.jdk.core.util.Strings; + +; + +/** + * @author Angel Avila + */ +public class HtmlParser { + + private static final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory(); + + private static final String LINK_NODE = "link"; + private static final String HREF_TAG = "href"; + private static final String SRC_TAG = "src"; + + private final String pathHint; + + public HtmlParser(String pathHint) { + this.pathHint = pathHint; + } + + private String getPath(String fullPath) { + StringBuilder builder = new StringBuilder(); + + String[] parts = fullPath.split("/"); + boolean found = false; + for (String part : parts) { + if (found && !part.endsWith(".html")) { + builder.append(part); + builder.append("/"); + } + if (pathHint.equals(part)) { + found = true; + } + } + return builder.toString(); + } + + public Set<String> parse(URL url) throws Exception { + Set<String> entries = new HashSet<String>(); + entries.clear(); + + String pathPrefix = getPath(url.toString()); + + InputStream inputStream = null; + try { + inputStream = new BufferedInputStream(url.openStream()); + XMLStreamReader streamReader = xmlInputFactory.createXMLStreamReader(inputStream); + while (streamReader.hasNext()) { + process(streamReader, pathPrefix, entries); + streamReader.next(); + } + + } finally { + Lib.close(inputStream); + } + + return entries; + } + + private void process(XMLStreamReader reader, String pathPrefix, Set<String> entries) { + int eventType = reader.getEventType(); + switch (eventType) { + case XMLStreamConstants.START_ELEMENT: + String localName = reader.getLocalName(); + for (int index = 0; index < reader.getAttributeCount(); index++) { + + String attributeName = reader.getAttributeLocalName(index); + String value = reader.getAttributeValue(index); + + if (Strings.isValid(value)) { + if (!LINK_NODE.equals(localName)) { + if (HREF_TAG.equals(attributeName) || SRC_TAG.equals(attributeName)) { + processResource(pathPrefix, entries, value); + } + } + } + } + break; + } + } + + private void processResource(String pathPrefix, Set<String> references, String value) { + if (!isExternalLink(value)) { + String reference = normalizePath(pathPrefix, value); + references.add(reference); + } + } + + private String normalizePath(String pathPrefix, String reference) { + String path = reference.replaceAll("\\.html#.*", ".html"); + return String.format("%s%s", pathPrefix, path); + } + + private boolean isExternalLink(String resource) { + return resource.contains("://"); + } +} diff --git a/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/util/TocParser.java b/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/util/TocParser.java new file mode 100644 index 00000000000..db79a07fd48 --- /dev/null +++ b/plugins/org.eclipse.osee.ote.help.ui.test/src/org/eclipse/osee/ote/help/ui/util/TocParser.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * Copyright (c) 2013 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.help.ui.util; +import java.io.BufferedInputStream; +import java.io.InputStream; +import java.net.URL; +import java.util.HashSet; +import java.util.Set; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamReader; +import org.eclipse.osee.framework.jdk.core.util.Lib; +import org.eclipse.osee.framework.jdk.core.util.Strings; + + +/** + * @author Angel Avila + */ +public class TocParser { + + private static final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory(); + + private static final String TOC = "toc"; + private static final String TOPIC = "topic"; + private static final String HREF = "href"; + + public Set<String> entries = new HashSet<String>(); + public String path; + + private String localName; + private String uri; + + public TocParser(String path) { + this.path = path; + } + + public void parse() throws Exception { + entries.clear(); + URL url = HelpTestUtil.getResource(path); + + InputStream inputStream = null; + try { + inputStream = new BufferedInputStream(url.openStream()); + XMLStreamReader streamReader = xmlInputFactory.createXMLStreamReader(inputStream); + while (streamReader.hasNext()) { + process(streamReader); + streamReader.next(); + } + + } finally { + Lib.close(inputStream); + } + } + + private void process(XMLStreamReader reader) { + int eventType = reader.getEventType(); + switch (eventType) { + case XMLStreamConstants.START_ELEMENT: + localName = reader.getLocalName(); + uri = reader.getNamespaceURI(); + if (TOC.equals(localName)) { + processReference(reader, TOPIC); + } else if (TOPIC.equals(localName)) { + processReference(reader, HREF); + } + break; + case XMLStreamConstants.END_ELEMENT: + localName = reader.getLocalName(); + uri = reader.getNamespaceURI(); + reset(); + break; + } + } + + private void processReference(XMLStreamReader reader, String tag) { + String reference = reader.getAttributeValue(uri, tag); + if (Strings.isValid(reference)) { + String path = normalizePath(reference); + entries.add(path); + } + } + + private String normalizePath(String reference) { + return reference.replaceAll("\\.html#.*", ".html"); + } + + private void reset() { + localName = null; + uri = null; + } + + public Set<String> getEntries() { + return entries; + } +} |