Bug 349711 - [metatype] Improve metatype implementation in equinox to
allow better use of schema extensions

Initial tests.
diff --git a/bundles/org.eclipse.equinox.compendium.tests/.classpath b/bundles/org.eclipse.equinox.compendium.tests/.classpath
index 7682c48..d08a0ac 100644
--- a/bundles/org.eclipse.equinox.compendium.tests/.classpath
+++ b/bundles/org.eclipse.equinox.compendium.tests/.classpath
@@ -10,5 +10,6 @@
 	<classpathentry kind="src" output="bundle_tests/metatype/tb5" path="bundles_src/metatype/tb5"/>
 	<classpathentry kind="src" output="bundle_tests/metatype/tb6" path="bundles_src/metatype/tb6"/>
 	<classpathentry kind="src" output="bundle_tests/metatype/tb7" path="bundles_src/metatype/tb7"/>
+	<classpathentry kind="src" output="bundle_tests/metatype/extendable.tb1" path="bundles_src/metatype/extendable.tb1"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>
diff --git a/bundles/org.eclipse.equinox.compendium.tests/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.compendium.tests/META-INF/MANIFEST.MF
index f61130e..dae6e24 100644
--- a/bundles/org.eclipse.equinox.compendium.tests/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.equinox.compendium.tests/META-INF/MANIFEST.MF
@@ -2,11 +2,12 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %bundleName
 Bundle-SymbolicName: org.eclipse.equinox.compendium.tests
-Bundle-Version: 1.1.0
+Bundle-Version: 1.1.100
 Bundle-Activator: org.eclipse.equinox.compendium.tests.Activator
 Require-Bundle: org.eclipse.core.runtime
 Eclipse-LazyStart: true
 Import-Package: junit.framework;version="3.8.2",
+ org.eclipse.equinox.metatype;version="1.2.0",
  org.eclipse.osgi.tests.bundles,
  org.osgi.framework;version="1.3.0",
  org.osgi.service.event;version="1.1.0",
diff --git a/bundles/org.eclipse.equinox.compendium.tests/build.properties b/bundles/org.eclipse.equinox.compendium.tests/build.properties
index 5b0de9d..74bbb3f 100644
--- a/bundles/org.eclipse.equinox.compendium.tests/build.properties
+++ b/bundles/org.eclipse.equinox.compendium.tests/build.properties
@@ -29,6 +29,8 @@
 manifest.bundle_tests/metatype.tb6.jar = META-INF/MANIFEST.MF
 source.bundle_tests/metatype.tb7.jar = bundles_src/metatype/tb7/
 manifest.bundle_tests/metatype.tb7.jar = META-INF/MANIFEST.MF
+source.bundle_tests/metatype.extendable.tb1.jar = bundles_src/metatype/extendable.tb1/
+manifest.bundle_tests/metatype.extendable.tb1.jar = META-INF/MANIFEST.MF
 
 jars.compile.order = bundle_tests/metatype/metatype.tb1.jar,\
                      bundle_tests/metatype/metatype.tb2.jar,\
@@ -36,4 +38,5 @@
                      bundle_tests/metatype/metatype.tb4.jar,\
                      bundle_tests/metatype/metatype.tb5.jar,\
                      bundle_tests/metatype/metatype.tb6.jar,\
-                     bundle_tests/metatype/metatype.tb7.jar
+                     bundle_tests/metatype/metatype.tb7.jar,\
+                     bundle_tests/metatype/metatype.extendable.tb1.jar
diff --git a/bundles/org.eclipse.equinox.compendium.tests/bundles_src/metatype/extendable.tb1/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.compendium.tests/bundles_src/metatype/extendable.tb1/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..117e03b
--- /dev/null
+++ b/bundles/org.eclipse.equinox.compendium.tests/bundles_src/metatype/extendable.tb1/META-INF/MANIFEST.MF
@@ -0,0 +1,8 @@
+Manifest-Version: 1.0

+Bundle-ManifestVersion: 2

+Bundle-SymbolicName: metatype.extendable.tb1

+Bundle-Version: 1.0.0

+Import-Package: org.osgi.framework;version="1.3.0",

+ org.osgi.service.metatype;version="1.2"

+Bundle-RequiredExecutionEnvironment: J2SE-1.4

+

diff --git a/bundles/org.eclipse.equinox.compendium.tests/bundles_src/metatype/extendable.tb1/OSGI-INF/metatype/metadata.xml b/bundles/org.eclipse.equinox.compendium.tests/bundles_src/metatype/extendable.tb1/OSGI-INF/metatype/metadata.xml
new file mode 100644
index 0000000..0d89346
--- /dev/null
+++ b/bundles/org.eclipse.equinox.compendium.tests/bundles_src/metatype/extendable.tb1/OSGI-INF/metatype/metadata.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>

+<md:MetaData 

+		xmlns:md="http://www.org.osgi/xmlns/metatype/v1.2.0/md" 

+		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

+		xsi:schemaLocation="http://www.org.osgi/xmlns/metatype/v1.2.0/md metatype.xsd"

+		xmlns:validation="urn:xmlns:validation"

+		xmlns:foo="urn:xmlns:foo">

+	<OCD id="ocd1" name="ocd1" foo:foo="bar" validation:enabled="true">

+		<AD name="ad1" id="ad1" type="String" validation:regexp="[a-zA-Z0-9]" validation:validation="validation" foo:bar="foo"/>

+	</OCD>

+	<Designate pid="metatype.extendable.tb1.1">

+		<Object ocdref="ocd1"/>

+	</Designate>

+</md:MetaData>

diff --git a/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/compendium/tests/AllTests.java b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/compendium/tests/AllTests.java
index 8e5a36b..3e61216 100644
--- a/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/compendium/tests/AllTests.java
+++ b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/compendium/tests/AllTests.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2008, 2010 IBM Corporation and others
+ * Copyright (c) 2008, 2011 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
@@ -18,6 +18,8 @@
 	public static Test suite() {
 		TestSuite suite = new TestSuite("Tests for Equinox Compendium"); //$NON-NLS-1$
 		suite.addTest(org.eclipse.equinox.metatype.tests.AllTests.suite());
+		// Second run for EquinoxMetaTypeService.
+		suite.addTest(org.eclipse.equinox.metatype.tests.AllTests.suite());
 		suite.addTest(org.eclipse.equinox.useradmin.tests.AllTests.suite());
 		suite.addTest(org.eclipse.equinox.event.tests.AllTests.suite());
 		return suite;
diff --git a/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/AbstractTest.java b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/AbstractTest.java
index 4490c02..79a6b7c 100644
--- a/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/AbstractTest.java
+++ b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/AbstractTest.java
@@ -10,18 +10,18 @@
  *******************************************************************************/
 package org.eclipse.equinox.metatype.tests;
 
+import org.eclipse.equinox.metatype.EquinoxMetaTypeService;
+
 import junit.framework.TestCase;
 import org.eclipse.equinox.compendium.tests.Activator;
 import org.eclipse.osgi.tests.bundles.BundleInstaller;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.metatype.AttributeDefinition;
-import org.osgi.service.metatype.MetaTypeService;
 
 public abstract class AbstractTest extends TestCase {
 	protected BundleInstaller bundleInstaller;
-	protected MetaTypeService metatype;
-
-	private ServiceReference metaTypeReference;
+	protected EquinoxMetaTypeService metatype;
+	protected ServiceReference metaTypeReference;
 
 	protected void assertValidationFail(String value, AttributeDefinition ad) {
 		String result = assertValidationPresent(value, ad);
@@ -75,9 +75,9 @@
 
 	protected void setUp() throws Exception {
 		Activator.getBundle(Activator.BUNDLE_METATYPE).start();
-		metaTypeReference = Activator.getBundleContext().getServiceReference(MetaTypeService.class.getName());
+		metaTypeReference = Activator.getBundleContext().getServiceReference(EquinoxMetaTypeService.class.getName());
 		assertNotNull("Metatype service reference not found", metaTypeReference); //$NON-NLS-1$
-		metatype = (MetaTypeService) Activator.getBundleContext().getService(metaTypeReference);
+		metatype = (EquinoxMetaTypeService) Activator.getBundleContext().getService(metaTypeReference);
 		assertNotNull("Metatype service not found", metatype); //$NON-NLS-1$
 		bundleInstaller = new BundleInstaller("bundle_tests/metatype", Activator.getBundleContext()); //$NON-NLS-1$
 	}
diff --git a/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/AllTests.java b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/AllTests.java
index 8a2ab04..f61467f 100644
--- a/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/AllTests.java
+++ b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/AllTests.java
@@ -21,6 +21,7 @@
 		suite.addTestSuite(Bug340899Test.class);
 		suite.addTestSuite(BugTests.class);
 		suite.addTestSuite(SameOcdPidFactoryPidTest.class);
+		suite.addTestSuite(ExtendableTest.class);
 		return suite;
 	}
 }
diff --git a/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/ExtendableTest.java b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/ExtendableTest.java
new file mode 100644
index 0000000..fe8b0be
--- /dev/null
+++ b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/ExtendableTest.java
@@ -0,0 +1,65 @@
+/*******************************************************************************

+ * Copyright (c) 2011 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.equinox.metatype.tests;

+

+import org.eclipse.equinox.metatype.*;

+

+import java.util.Map;

+import java.util.Set;

+import org.osgi.framework.Bundle;

+import org.osgi.service.metatype.ObjectClassDefinition;

+

+public class ExtendableTest extends AbstractTest {

+	private Bundle bundle;

+

+	protected void setUp() throws Exception {

+		super.setUp();

+		bundle = bundleInstaller.installBundle("extendable.tb1"); //$NON-NLS-1$

+		bundle.start();

+	}

+

+	public void testExtensions() {

+		EquinoxMetaTypeInformation mti = metatype.getMetaTypeInformation(bundle);

+		EquinoxObjectClassDefinition ocd = mti.getObjectClassDefinition("metatype.extendable.tb1.1", null); //$NON-NLS-1$

+		Set schemas = ocd.getExtensionUris();

+		assertNotNull("Null extension schemas", schemas); //$NON-NLS-1$

+		assertEquals("Wrong schemas size", 2, schemas.size()); //$NON-NLS-1$

+		assertTrue("Missing schema", schemas.contains("urn:xmlns:foo")); //$NON-NLS-1$ //$NON-NLS-2$

+		assertTrue("Missing schema", schemas.contains("urn:xmlns:validation")); //$NON-NLS-1$ //$NON-NLS-2$

+		Map attributes = ocd.getExtensionAttributes("urn:xmlns:foo"); //$NON-NLS-1$

+		assertNotNull("Null attributes", attributes); //$NON-NLS-1$

+		assertEquals("Wrong attributes size", 1, attributes.size()); //$NON-NLS-1$

+		assertEquals("Wrong value", "bar", attributes.get("foo")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

+		attributes = ocd.getExtensionAttributes("urn:xmlns:validation"); //$NON-NLS-1$

+		assertNotNull("Null attributes", attributes); //$NON-NLS-1$

+		assertEquals("Wrong attributes size", 1, attributes.size()); //$NON-NLS-1$

+		assertEquals("Wrong value", "true", attributes.get("enabled")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

+		EquinoxAttributeDefinition[] ads = ocd.getAttributeDefinitions(ObjectClassDefinition.ALL);

+		for (int i = 0; i < ads.length; i++) {

+			if (ads[i].getID().equals("ad1")) { //$NON-NLS-1$

+				schemas = ads[i].getExtensionUris();

+				assertNotNull("Null extension schemas", schemas); //$NON-NLS-1$

+				assertEquals("Wrong schemas size", 2, schemas.size()); //$NON-NLS-1$

+				assertTrue("Missing schema", schemas.contains("urn:xmlns:foo")); //$NON-NLS-1$ //$NON-NLS-2$

+				assertTrue("Missing schema", schemas.contains("urn:xmlns:validation")); //$NON-NLS-1$ //$NON-NLS-2$

+				attributes = ads[i].getExtensionAttributes("urn:xmlns:foo"); //$NON-NLS-1$

+				assertNotNull("Null attributes", attributes); //$NON-NLS-1$

+				assertEquals("Wrong attributes size", 1, attributes.size()); //$NON-NLS-1$

+				assertEquals("Wrong value", "foo", attributes.get("bar")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

+				attributes = ads[i].getExtensionAttributes("urn:xmlns:validation"); //$NON-NLS-1$

+				assertNotNull("Null attributes", attributes); //$NON-NLS-1$

+				assertEquals("Wrong attributes size", 2, attributes.size()); //$NON-NLS-1$

+				assertEquals("Wrong value", "[a-zA-Z0-9]", attributes.get("regexp")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

+				assertEquals("Wrong value", "validation", attributes.get("validation")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

+			}

+		}

+	}

+}