aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Ross2011-11-18 17:15:33 (EST)
committerJohn Ross2011-11-18 17:15:33 (EST)
commite76b83ae31eb5092188f16117981690f0d4872d6 (patch)
treea4d71aec9608f75affc4b22126d5b7bd90fec192
parenteeb130d28cd0fdaa277aee16b737d515c36ac742 (diff)
downloadrt.equinox.bundles-e76b83ae31eb5092188f16117981690f0d4872d6.zip
rt.equinox.bundles-e76b83ae31eb5092188f16117981690f0d4872d6.tar.gz
rt.equinox.bundles-e76b83ae31eb5092188f16117981690f0d4872d6.tar.bz2
Bug 349189: New tests for AttributeDefinition.getDefaultValue.
-rw-r--r--bundles/org.eclipse.equinox.compendium.tests/bundles_src/metatype/tb1/OSGI-INF/metatype/testfile.xml44
-rw-r--r--bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/AbstractTest.java26
-rw-r--r--bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/AllTests.java1
-rw-r--r--bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/GetDefaultValueTest.java123
4 files changed, 192 insertions, 2 deletions
diff --git a/bundles/org.eclipse.equinox.compendium.tests/bundles_src/metatype/tb1/OSGI-INF/metatype/testfile.xml b/bundles/org.eclipse.equinox.compendium.tests/bundles_src/metatype/tb1/OSGI-INF/metatype/testfile.xml
index 5f29988..e4d907e 100644
--- a/bundles/org.eclipse.equinox.compendium.tests/bundles_src/metatype/tb1/OSGI-INF/metatype/testfile.xml
+++ b/bundles/org.eclipse.equinox.compendium.tests/bundles_src/metatype/tb1/OSGI-INF/metatype/testfile.xml
@@ -12,7 +12,51 @@
<AD default="password" id="password4" max="5" type="Password"/>
<AD default="password" id="password5" max="6" min="3" type="Password"/>
</OCD>
+ <OCD id="ocd2" name="getDefaultValues">
+ <!-- Unspecified default. Default value must be null. -->
+ <AD id="1" cardinality="0" type="Integer"/>
+ <AD id="2" cardinality="1" type="Double"/>
+ <AD id="3" cardinality="-1" type="Short"/>
+ <AD id="4" cardinality="5" type="Byte"/>
+ <AD id="5" cardinality="-10" type="Integer"/>
+ <AD id="6" cardinality="0" type="String"/>
+ <AD id="7" cardinality="1" type="Password"/>
+ <AD id="8" cardinality="-1" type="String"/>
+ <AD id="9" cardinality="20" type="Password"/>
+ <AD id="10" cardinality="-9" type="String"/>
+ <!--
+ Specified default of one or more empty strings with incompatible type.
+ Equinox will log a warning and ignore the default if invalid (i.e. same as unspecified).
+ -->
+ <AD id="11" cardinality="0" default="" type="Byte"/>
+ <AD id="12" cardinality="1" default="" type="Short"/>
+ <AD id="13" cardinality="-1" default="" type="Integer"/>
+ <AD id="14" cardinality="7" default=",,,," type="Double"/>
+ <AD id="15" cardinality="-3" default="," type="Double"/>
+ <!--
+ Specified default of empty string with compatible type.
+ For cardinality = 0, the default value must be String[1]{""}.
+ For cardinality != 0, the default value must be String[0].
+ -->
+ <AD id="16" cardinality="0" default="" type="String"/>
+ <AD id="17" cardinality="1" default="" type="Password"/>
+ <AD id="18" cardinality="-1" default="" type="String"/>
+ <AD id="19" cardinality="42" default="" type="Password"/>
+ <AD id="20" cardinality="-210" default="" type="String"/>
+ <!--
+ Specified default of more than one empty string with compatible type.
+ Let n = the number of empty strings in the default value, then the
+ default value must be String[n]{"","",...,""}, where n <= abs(cardinality).
+ -->
+ <AD id="21" cardinality="2" default="," type="String"/> <!-- String[2]{"",""} -->
+ <AD id="22" cardinality="-2" default="," type="Password"/> <!-- String[2]{"",""} -->
+ <AD id="23" cardinality="4" default=",," type="String"/> <!-- String[3]{"","",""} -->
+ <AD id="24" cardinality="-8" default=",,,," type="Password"/> <!-- String[5]{"","","","",""} -->
+ </OCD>
<Designate pid="org.eclipse.equinox.metatype.tests.tb1">
<Object ocdref="ocd1"/>
</Designate>
+ <Designate pid="org.eclipse.equinox.metatype.tests.tb1.getDefaultValues">
+ <Object ocdref="ocd2"/>
+ </Designate>
</md:MetaData>
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 79a6b7c..99b0806 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,10 +10,9 @@
*******************************************************************************/
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.equinox.metatype.EquinoxMetaTypeService;
import org.eclipse.osgi.tests.bundles.BundleInstaller;
import org.osgi.framework.ServiceReference;
import org.osgi.service.metatype.AttributeDefinition;
@@ -23,6 +22,29 @@ public abstract class AbstractTest extends TestCase {
protected EquinoxMetaTypeService metatype;
protected ServiceReference metaTypeReference;
+ protected void assertAttributeDefinition(AttributeDefinition ad, int cardinality, String[] defaultValue, String description, String id, String name, String[] optionLabels, String[] optionValues, int type) {
+ assertEquals("Wrong cardinality", cardinality, ad.getCardinality()); //$NON-NLS-1$
+ assertEquals("Wrong default value", defaultValue, ad.getDefaultValue()); //$NON-NLS-1$
+ assertEquals("Wrong description", description, ad.getDescription()); //$NON-NLS-1$
+ assertEquals("Wrong id", id, ad.getID()); //$NON-NLS-1$
+ assertEquals("Wrong name", name, ad.getName()); //$NON-NLS-1$
+ assertEquals("Wrong option labels", optionLabels, ad.getOptionLabels()); //$NON-NLS-1$
+ assertEquals("Wrong option values", optionValues, ad.getOptionValues()); //$NON-NLS-1$
+ assertEquals("Wrong type", type, ad.getType()); //$NON-NLS-1$
+ }
+
+ protected void assertEquals(String message, String[] s1, String[] s2) {
+ if (s1 == s2)
+ return;
+ // We know that at least one is not null from the above check.
+ if (s1 == null || s2 == null)
+ fail(message + " (one of the arrays was null)"); //$NON-NLS-1$
+ if (s1.length != s2.length)
+ fail(message + " (array lengths weren't equal)"); //$NON-NLS-1$
+ for (int i = 0; i < s1.length; i++)
+ assertEquals(message, s1[i], s2[i]);
+ }
+
protected void assertValidationFail(String value, AttributeDefinition ad) {
String result = assertValidationPresent(value, ad);
assertTrue("Validation passed", result.length() > 0); //$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 f530f08..c741b07 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
@@ -24,6 +24,7 @@ public class AllTests {
suite.addTestSuite(ExtendableTest.class);
suite.addTestSuite(Bug358969Test.class);
suite.addTestSuite(UnresolvedBundleTest.class);
+ suite.addTestSuite(GetDefaultValueTest.class);
return suite;
}
}
diff --git a/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/GetDefaultValueTest.java b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/GetDefaultValueTest.java
new file mode 100644
index 0000000..fd5d533
--- /dev/null
+++ b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/metatype/tests/GetDefaultValueTest.java
@@ -0,0 +1,123 @@
+/*******************************************************************************
+ * 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.osgi.framework.Bundle;
+import org.osgi.service.metatype.*;
+
+/*
+ * Ensure default values on attribute definitions are correct when the default
+ * value is either unspecified or specified as an empty string.
+ *
+ * See https://www.osgi.org/members/bugzilla/show_bug.cgi?id=2182.
+ * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=349189.
+ */
+public class GetDefaultValueTest extends AbstractTest {
+ private Bundle bundle;
+
+ public void testGetDefaultValue() {
+ MetaTypeInformation mti = metatype.getMetaTypeInformation(bundle);
+ assertNotNull("Metatype information was null", mti); //$NON-NLS-1$
+ ObjectClassDefinition ocd = mti.getObjectClassDefinition("org.eclipse.equinox.metatype.tests.tb1.getDefaultValues", null); //$NON-NLS-1$
+ assertNotNull("Object class definition was null", ocd); //$NON-NLS-1$
+ assertEquals("Wrong object class definition ID", "ocd2", ocd.getID()); //$NON-NLS-1$ //$NON-NLS-2$
+ assertEquals("Wrong object class definition name", "getDefaultValues", ocd.getName()); //$NON-NLS-1$ //$NON-NLS-2$
+ AttributeDefinition[] ads = ocd.getAttributeDefinitions(ObjectClassDefinition.ALL);
+ assertEquals("Wrong number of attribute definitions", 24, ads.length); //$NON-NLS-1$
+ for (int i = 0; i < ads.length; i++) {
+ switch (Integer.parseInt(ads[i].getID())) {
+ case 1 :
+ assertAttributeDefinition(ads[i], 0, null, null, "1", null, null, null, AttributeDefinition.INTEGER); //$NON-NLS-1$
+ break;
+ case 2 :
+ assertAttributeDefinition(ads[i], 1, null, null, "2", null, null, null, AttributeDefinition.DOUBLE); //$NON-NLS-1$
+ break;
+ case 3 :
+ assertAttributeDefinition(ads[i], -1, null, null, "3", null, null, null, AttributeDefinition.SHORT); //$NON-NLS-1$
+ break;
+ case 4 :
+ assertAttributeDefinition(ads[i], 5, null, null, "4", null, null, null, AttributeDefinition.BYTE); //$NON-NLS-1$
+ break;
+ case 5 :
+ assertAttributeDefinition(ads[i], -10, null, null, "5", null, null, null, AttributeDefinition.INTEGER); //$NON-NLS-1$
+ break;
+ case 6 :
+ assertAttributeDefinition(ads[i], 0, null, null, "6", null, null, null, AttributeDefinition.STRING); //$NON-NLS-1$
+ break;
+ case 7 :
+ assertAttributeDefinition(ads[i], 1, null, null, "7", null, null, null, AttributeDefinition.PASSWORD); //$NON-NLS-1$
+ break;
+ case 8 :
+ assertAttributeDefinition(ads[i], -1, null, null, "8", null, null, null, AttributeDefinition.STRING); //$NON-NLS-1$
+ break;
+ case 9 :
+ assertAttributeDefinition(ads[i], 20, null, null, "9", null, null, null, AttributeDefinition.PASSWORD); //$NON-NLS-1$
+ break;
+ case 10 :
+ assertAttributeDefinition(ads[i], -9, null, null, "10", null, null, null, AttributeDefinition.STRING); //$NON-NLS-1$
+ break;
+ case 11 :
+ assertAttributeDefinition(ads[i], 0, null, null, "11", null, null, null, AttributeDefinition.BYTE); //$NON-NLS-1$
+ break;
+ case 12 :
+ assertAttributeDefinition(ads[i], 1, null, null, "12", null, null, null, AttributeDefinition.SHORT); //$NON-NLS-1$
+ break;
+ case 13 :
+ assertAttributeDefinition(ads[i], -1, null, null, "13", null, null, null, AttributeDefinition.INTEGER); //$NON-NLS-1$
+ break;
+ case 14 :
+ assertAttributeDefinition(ads[i], 7, null, null, "14", null, null, null, AttributeDefinition.DOUBLE); //$NON-NLS-1$
+ break;
+ case 15 :
+ assertAttributeDefinition(ads[i], -3, null, null, "15", null, null, null, AttributeDefinition.DOUBLE); //$NON-NLS-1$
+ break;
+ case 16 :
+ assertAttributeDefinition(ads[i], 0, new String[] {""}, null, "16", null, null, null, AttributeDefinition.STRING); //$NON-NLS-1$ //$NON-NLS-2$
+ break;
+ case 17 :
+ assertAttributeDefinition(ads[i], 1, new String[0], null, "17", null, null, null, AttributeDefinition.PASSWORD); //$NON-NLS-1$
+ break;
+ case 18 :
+ assertAttributeDefinition(ads[i], -1, new String[0], null, "18", null, null, null, AttributeDefinition.STRING); //$NON-NLS-1$
+ break;
+ case 19 :
+ assertAttributeDefinition(ads[i], 42, new String[0], null, "19", null, null, null, AttributeDefinition.PASSWORD); //$NON-NLS-1$
+ break;
+ case 20 :
+ assertAttributeDefinition(ads[i], -210, new String[0], null, "20", null, null, null, AttributeDefinition.STRING); //$NON-NLS-1$
+ break;
+ case 21 :
+ assertAttributeDefinition(ads[i], 2, new String[] {"", ""}, null, "21", null, null, null, AttributeDefinition.STRING); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ break;
+ case 22 :
+ assertAttributeDefinition(ads[i], -2, new String[] {"", ""}, null, "22", null, null, null, AttributeDefinition.PASSWORD); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ break;
+ case 23 :
+ assertAttributeDefinition(ads[i], 4, new String[] {"", "", ""}, null, "23", null, null, null, AttributeDefinition.STRING); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ break;
+ case 24 :
+ assertAttributeDefinition(ads[i], -8, new String[] {"", "", "", "", ""}, null, "24", null, null, null, AttributeDefinition.PASSWORD); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
+ break;
+ default :
+ fail("Unexpected attribute definition ID: " + ads[i].getID()); //$NON-NLS-1$
+ }
+ }
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ bundle = bundleInstaller.installBundle("tb1"); //$NON-NLS-1$
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+}