aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Figueiredo Caetano2013-09-20 15:19:26 (EDT)
committerSamuel Padgett2013-09-23 11:24:27 (EDT)
commita0874250e1c50dad1ea68d13efa4ec96d94d2b32 (patch)
treecdcac6c0e7a6a33a0b29fbfa36031d810376a435
parent91cb773602659dd4aad3bfd8471ea392b1f6bccb (diff)
downloadorg.eclipse.lyo.core-a0874250e1c50dad1ea68d13efa4ec96d94d2b32.zip
org.eclipse.lyo.core-a0874250e1c50dad1ea68d13efa4ec96d94d2b32.tar.gz
org.eclipse.lyo.core-a0874250e1c50dad1ea68d13efa4ec96d94d2b32.tar.bz2
Bug 416659 - Added unit tests for custom and global namespace mappingsrefs/changes/57/16657/2
Change-Id: Idc788b4f629b2aa63559be140918bbb8a28d833a Signed-off-by: Daniel Figueiredo Caetano <dcaetano@br.ibm.com> Also-by: Samuel Padgett <spadgett@us.ibm.com>
-rw-r--r--org.eclipse.lyo.oslc4j.core/src/test/java/org/eclipse/lyo/oslc4j/core/test/OslcGlobalNamespaceProviderTest.java81
-rw-r--r--org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/AnyNamespaceProvider.java50
-rw-r--r--org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/CustomNamespaceConstants.java42
-rw-r--r--org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/CustomNamespaceResource.java37
-rw-r--r--org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/package-info.java41
-rw-r--r--org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/provider/jena/test/JenaCustomNamespaceTest.java123
6 files changed, 374 insertions, 0 deletions
diff --git a/org.eclipse.lyo.oslc4j.core/src/test/java/org/eclipse/lyo/oslc4j/core/test/OslcGlobalNamespaceProviderTest.java b/org.eclipse.lyo.oslc4j.core/src/test/java/org/eclipse/lyo/oslc4j/core/test/OslcGlobalNamespaceProviderTest.java
new file mode 100644
index 0000000..fc4570b
--- /dev/null
+++ b/org.eclipse.lyo.oslc4j.core/src/test/java/org/eclipse/lyo/oslc4j/core/test/OslcGlobalNamespaceProviderTest.java
@@ -0,0 +1,81 @@
+/*******************************************************************************
+ * Copyright (c) 2013 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Daniel Figueiredo Caetano - initial implementation
+ *******************************************************************************/
+package org.eclipse.lyo.oslc4j.core.test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.Assert;
+
+import org.eclipse.lyo.oslc4j.core.OslcGlobalNamespaceProvider;
+import org.junit.Test;
+
+/**
+ * Tests the behavior of {@link OslcGlobalNamespaceProvider}.
+ *
+ * @author Daniel Figueiredo Caetano
+ *
+ */
+public class OslcGlobalNamespaceProviderTest {
+
+ /**
+ * Tests the unique application instance of the {@link OslcGlobalNamespaceProvider}.
+ */
+ @Test
+ public void testSingletonInstance() {
+ OslcGlobalNamespaceProvider globalNamespaceProvider = OslcGlobalNamespaceProvider.getInstance();
+ Assert.assertNotNull(
+ "Global Namespace instance should not be null.",
+ globalNamespaceProvider
+ );
+ OslcGlobalNamespaceProvider secondGlobalNamespaceProvider = OslcGlobalNamespaceProvider.getInstance();
+ Assert.assertSame(
+ "There should be only one instance of the OslcGlobalNamespaceProvider class",
+ globalNamespaceProvider,
+ secondGlobalNamespaceProvider
+ );
+ }
+
+ /**
+ * Tests if the map can be set to another new map or to null.
+ */
+ @Test
+ public void testSetNullMap() {
+ OslcGlobalNamespaceProvider globalNamespaceProvider = OslcGlobalNamespaceProvider.getInstance();
+ Assert.assertNotNull(
+ "Global Namespace Map should not be null when created.",
+ globalNamespaceProvider.getPrefixDefinitionMap()
+ );
+ globalNamespaceProvider.getPrefixDefinitionMap().put("test", "http://anything.com");
+ globalNamespaceProvider.setPrefixDefinitionMap(null);
+ Assert.assertNotNull(
+ "Global Namespace Map should not be null.",
+ globalNamespaceProvider.getPrefixDefinitionMap()
+ );
+ Assert.assertTrue(
+ "Map should be cleared and never set to null.",
+ globalNamespaceProvider.getPrefixDefinitionMap().isEmpty()
+ );
+ Map<String, String> namespaceMappings = new HashMap<String, String>(1);
+ namespaceMappings.put("any", "http://any.test.com#");
+ globalNamespaceProvider.setPrefixDefinitionMap(namespaceMappings);
+ Assert.assertFalse(
+ "Global Namespace Map could not be set.",
+ globalNamespaceProvider.getPrefixDefinitionMap().isEmpty()
+ );
+ }
+
+} \ No newline at end of file
diff --git a/org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/AnyNamespaceProvider.java b/org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/AnyNamespaceProvider.java
new file mode 100644
index 0000000..85bef56
--- /dev/null
+++ b/org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/AnyNamespaceProvider.java
@@ -0,0 +1,50 @@
+/*******************************************************************************
+ * Copyright (c) 2013 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Daniel Figueiredo Caetano - initial implementation
+ *******************************************************************************/
+
+package org.eclipse.lyo.oslc4j.core.test.customnamespace;
+
+import static org.eclipse.lyo.oslc4j.core.test.customnamespace.CustomNamespaceConstants.CUSTOM_PREFIX;
+import static org.eclipse.lyo.oslc4j.core.test.customnamespace.CustomNamespaceConstants.CUSTOM_URL;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.lyo.oslc4j.core.model.IOslcCustomNamespaceProvider;
+
+/**
+ * Used to test the custom namespace mappings feature.
+ * This class just return a custom prefix to ensure that this
+ * prefix will be added when Jena or Json Helper is reading
+ * the default annotation mappings.
+ *
+ * @author Daniel Figueiredo Caetano
+ *
+ */
+public class AnyNamespaceProvider implements IOslcCustomNamespaceProvider {
+
+ /**
+ * Creates a map with a custom prefix, used for tests.
+ *
+ * @return always a new map.
+ */
+ @Override
+ public Map<String, String> getCustomNamespacePrefixes() {
+ Map<String, String> customNamespacePrefixes = new HashMap<String, String>(1);
+ customNamespacePrefixes.put(CUSTOM_PREFIX, CUSTOM_URL);
+ return customNamespacePrefixes;
+ }
+
+} \ No newline at end of file
diff --git a/org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/CustomNamespaceConstants.java b/org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/CustomNamespaceConstants.java
new file mode 100644
index 0000000..4cab92a
--- /dev/null
+++ b/org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/CustomNamespaceConstants.java
@@ -0,0 +1,42 @@
+/*******************************************************************************
+ * Copyright (c) 2013 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Daniel Figueiredo Caetano - initial implementation
+ *******************************************************************************/
+package org.eclipse.lyo.oslc4j.core.test.customnamespace;
+
+/**
+ * Common constants used to the test the global and custom namespace.
+ *
+ * @author Daniel Figueiredo Caetano
+ *
+ */
+public interface CustomNamespaceConstants {
+
+ /*
+ * Test prefixes
+ */
+ String TEST1_PREFIX = "test1";
+ String TEST2_PREFIX = "test2";
+ String CUSTOM_PREFIX = "custom";
+ String GLOBAL_PREFIX = "global";
+
+ /*
+ * Test namespaces
+ */
+ String TEST1_URL = "http://test1.oslc4j.com#";
+ String TEST2_URL = "http://test2.oslc4j.com#";
+ String CUSTOM_URL = "http://custom.oslc4j.com#";
+ String GLOBAL_URL = "http://global.oslc4j.com#";
+
+} \ No newline at end of file
diff --git a/org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/CustomNamespaceResource.java b/org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/CustomNamespaceResource.java
new file mode 100644
index 0000000..ed6bf8a
--- /dev/null
+++ b/org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/CustomNamespaceResource.java
@@ -0,0 +1,37 @@
+/*******************************************************************************
+ * Copyright (c) 2013 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Daniel Figueiredo Caetano - initial implementation
+ *******************************************************************************/
+package org.eclipse.lyo.oslc4j.core.test.customnamespace;
+
+import org.eclipse.lyo.oslc4j.core.annotation.OslcName;
+import org.eclipse.lyo.oslc4j.core.annotation.OslcNamespace;
+import org.eclipse.lyo.oslc4j.core.annotation.OslcResourceShape;
+import org.eclipse.lyo.oslc4j.core.model.AbstractResource;
+
+/**
+ * Resource shape to be used by test classes.
+ *
+ * @author Daniel Figueiredo Caetano
+ *
+ */
+@OslcName("CustomNamespace")
+@OslcNamespace(CustomNamespaceResource.CUSTOM_NAMESPACE)
+@OslcResourceShape(title = "Test Resource", describes = CustomNamespaceResource.CUSTOM_RESOURCE_TYPE)
+public class CustomNamespaceResource extends AbstractResource {
+
+ public final static String CUSTOM_NAMESPACE = "http://customnamespace.oslc.com/ns#";
+ public final static String CUSTOM_RESOURCE_TYPE = CUSTOM_NAMESPACE + "Test";
+
+} \ No newline at end of file
diff --git a/org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/package-info.java b/org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/package-info.java
new file mode 100644
index 0000000..af19236
--- /dev/null
+++ b/org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/core/test/customnamespace/package-info.java
@@ -0,0 +1,41 @@
+/*******************************************************************************
+ * Copyright (c) 2013 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Daniel Figueiredo Caetano - initial implementation
+ *******************************************************************************/
+
+/**
+ * Used to test the custom namespce prefix mappings.
+ * The namespace provider implementation {@link org.eclipse.lyo.oslc4j.core.test.customnamespace.AnyNamespaceProvider}
+ * is passed in the {@link org.eclipse.lyo.oslc4j.core.annotation.OslcSchema#customNamespaceProvider()}
+ * field.
+ *
+ * @author Daniel Figueiredo Caetano
+ *
+ */
+@OslcSchema(
+ customNamespaceProvider= AnyNamespaceProvider.class,
+ value={
+ @OslcNamespaceDefinition(prefix=TEST1_PREFIX,namespaceURI=TEST1_URL),
+ @OslcNamespaceDefinition(prefix=TEST2_PREFIX,namespaceURI=TEST2_URL)
+ }
+)
+package org.eclipse.lyo.oslc4j.core.test.customnamespace;
+
+import static org.eclipse.lyo.oslc4j.core.test.customnamespace.CustomNamespaceConstants.TEST1_PREFIX;
+import static org.eclipse.lyo.oslc4j.core.test.customnamespace.CustomNamespaceConstants.TEST1_URL;
+import static org.eclipse.lyo.oslc4j.core.test.customnamespace.CustomNamespaceConstants.TEST2_PREFIX;
+import static org.eclipse.lyo.oslc4j.core.test.customnamespace.CustomNamespaceConstants.TEST2_URL;
+
+import org.eclipse.lyo.oslc4j.core.annotation.OslcNamespaceDefinition;
+import org.eclipse.lyo.oslc4j.core.annotation.OslcSchema; \ No newline at end of file
diff --git a/org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/provider/jena/test/JenaCustomNamespaceTest.java b/org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/provider/jena/test/JenaCustomNamespaceTest.java
new file mode 100644
index 0000000..5803f9c
--- /dev/null
+++ b/org.eclipse.lyo.oslc4j.provider.jena/src/test/java/org/eclipse/lyo/oslc4j/provider/jena/test/JenaCustomNamespaceTest.java
@@ -0,0 +1,123 @@
+/*******************************************************************************
+ * Copyright (c) 2013 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Daniel Figueiredo Caetano - initial implementation
+ *******************************************************************************/
+package org.eclipse.lyo.oslc4j.provider.jena.test;
+
+import static org.eclipse.lyo.oslc4j.core.test.customnamespace.CustomNamespaceConstants.CUSTOM_PREFIX;
+import static org.eclipse.lyo.oslc4j.core.test.customnamespace.CustomNamespaceConstants.CUSTOM_URL;
+import static org.eclipse.lyo.oslc4j.core.test.customnamespace.CustomNamespaceConstants.GLOBAL_PREFIX;
+import static org.eclipse.lyo.oslc4j.core.test.customnamespace.CustomNamespaceConstants.GLOBAL_URL;
+import static org.eclipse.lyo.oslc4j.core.test.customnamespace.CustomNamespaceConstants.TEST1_PREFIX;
+import static org.eclipse.lyo.oslc4j.core.test.customnamespace.CustomNamespaceConstants.TEST1_URL;
+import static org.eclipse.lyo.oslc4j.core.test.customnamespace.CustomNamespaceConstants.TEST2_PREFIX;
+import static org.eclipse.lyo.oslc4j.core.test.customnamespace.CustomNamespaceConstants.TEST2_URL;
+
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
+import junit.framework.Assert;
+
+import org.eclipse.lyo.oslc4j.core.OslcGlobalNamespaceProvider;
+import org.eclipse.lyo.oslc4j.core.annotation.OslcSchema;
+import org.eclipse.lyo.oslc4j.core.test.customnamespace.CustomNamespaceResource;
+import org.eclipse.lyo.oslc4j.provider.jena.JenaModelHelper;
+import org.junit.Test;
+
+import com.hp.hpl.jena.rdf.model.Model;
+
+/**
+ * Tests the Jena working with custom and global namespace mappings.
+ *
+ * @author Daniel Figueiredo Caetano
+ *
+ */
+public class JenaCustomNamespaceTest {
+
+ private final String NAME_LOCAL_PART = "name";
+ private final String DESCRIPTION_LOCAL_PART = "description";
+
+ /**
+ * Creates an instance of {@link CustomNamespaceResource} and checks if
+ * the custom namespaces returned by the class passed to the
+ * {@link OslcSchema#customNamespaceProvider()} are in the
+ * {@link Model#getNsPrefixMap()} that Jena creates.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testJenaCustomNamespaceProvider() throws Exception {
+ CustomNamespaceResource namespaceResource = createCustomNamespaceResource();
+ Model model = JenaModelHelper.createJenaModel(new Object[]{namespaceResource});
+ assertCustomNamespaces(model, TEST1_PREFIX, TEST1_URL);
+ assertCustomNamespaces(model, TEST2_PREFIX, TEST2_URL);
+ assertCustomNamespaces(model, CUSTOM_PREFIX, CUSTOM_URL);
+ }
+
+ /**
+ * Adds custom prefix to the {@link OslcGlobalNamespaceProvider#getPrefixDefinitionMap()}
+ * and checks if it is in the {@link Model#getNsPrefixMap()} created by Jena.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testJenaGlobalNamespaceProvider() throws Exception {
+ Map<String, String> globalNamespaceMappings = new HashMap<String, String>(1);
+ globalNamespaceMappings.put(GLOBAL_PREFIX, GLOBAL_URL);
+ OslcGlobalNamespaceProvider.getInstance().setPrefixDefinitionMap(globalNamespaceMappings);
+ CustomNamespaceResource namespaceResource = createCustomNamespaceResource();
+ Model model = JenaModelHelper.createJenaModel(new Object[]{namespaceResource});
+ assertCustomNamespaces(model, GLOBAL_PREFIX, GLOBAL_URL);
+ }
+
+ /**
+ * Creates a new instance adding some test values.
+ *
+ * @return new instance
+ */
+ private CustomNamespaceResource createCustomNamespaceResource() {
+ CustomNamespaceResource namespaceResource = new CustomNamespaceResource();
+ URI aboutNamespaceResource = URI.create("http://about.custom.oslc.namespace/001");
+ namespaceResource.setAbout(aboutNamespaceResource);
+ URI typeNamespaceResource = URI.create(CustomNamespaceResource.CUSTOM_RESOURCE_TYPE);
+ namespaceResource.getTypes().add(typeNamespaceResource);
+ QName qName = new QName(TEST1_URL, NAME_LOCAL_PART);
+ namespaceResource.getExtendedProperties().put(qName, "Some Name");
+ qName = new QName(TEST2_URL, DESCRIPTION_LOCAL_PART);
+ namespaceResource.getExtendedProperties().put(qName, "Any Description");
+ return namespaceResource;
+ }
+
+ /**
+ * Checks if the model contains the prefix and its mapped namespace.
+ * @param model to be checked.
+ * @param prefix that model should have.
+ * @param namespaceURI mapped related to the prefix.
+ */
+ private void assertCustomNamespaces(Model model, String prefix, String namespaceURI) {
+ Assert.assertTrue(
+ "The prefix map should contain the custom namespace mapping",
+ model.getNsPrefixMap().containsKey(prefix)
+ );
+ Assert.assertEquals(
+ "The prefix mapped should has a different namespaceURI.",
+ model.getNsPrefixMap().get(prefix),
+ namespaceURI
+ );
+ }
+
+} \ No newline at end of file