From 3d04353d8947710873181679046fa3ab8fa2fc98 Mon Sep 17 00:00:00 2001 From: Pascal Rapicault Date: Sat, 8 Dec 2012 21:59:07 -0500 Subject: Bug 396136 - [publisher] Support for expression in update descriptor and requirements --- .../equinox/p2/publisher/AdviceFileParser.java | 30 +++++++++++++--- .../publisher/actions/AdviceFileParserTest.java | 41 +++++++++++++++++++++- 2 files changed, 65 insertions(+), 6 deletions(-) (limited to 'bundles') diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AdviceFileParser.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AdviceFileParser.java index 27c1fd9dc..b19757761 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AdviceFileParser.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AdviceFileParser.java @@ -31,7 +31,7 @@ public class AdviceFileParser { private static final String UPDATE_SEVERITY = "update.severity"; //$NON-NLS-1$ private static final String UPDATE_RANGE = "update.range"; //$NON-NLS-1$ private static final String UPDATE_ID = "update.id"; //$NON-NLS-1$ - private static final String UPDATE_MATCH = "update.match"; //$NON-NLS-1$ + private static final String UPDATE_MATCH_EXP = "update.matchExp"; //$NON-NLS-1$ private static final String CLASSIFIER = "classifier"; //$NON-NLS-1$ private static final String TOUCHPOINT_VERSION = "touchpoint.version"; //$NON-NLS-1$ private static final String TOUCHPOINT_ID = "touchpoint.id"; //$NON-NLS-1$ @@ -50,6 +50,7 @@ public class AdviceFileParser { private static final String VERSION = "version"; //$NON-NLS-1$ private static final String NAMESPACE = "namespace"; //$NON-NLS-1$ private static final String NAME = "name"; //$NON-NLS-1$ + private static final String MATCH_EXP = "matchExp"; //$NON-NLS-1$ private static final String LOCATION = "location"; //$NON-NLS-1$ private static final String VALUE = "value"; //$NON-NLS-1$ @@ -173,7 +174,7 @@ public class AdviceFileParser { while (current != null && current.startsWith(prefix)) { String token = current; - if (token.equals(UPDATE_MATCH)) { + if (token.equals(UPDATE_MATCH_EXP)) { match = currentValue(); } else if (token.equals(UPDATE_ID)) { name = currentValue(); @@ -249,6 +250,7 @@ public class AdviceFileParser { String namespace = null; String name = null; VersionRange range = null; + String matchExp = null; String filter = null; boolean optional = false; boolean multiple = false; @@ -277,22 +279,40 @@ public class AdviceFileParser { min = Integer.valueOf(currentValue()).intValue(); } else if (token.equals(MAX)) { max = Integer.valueOf(currentValue()).intValue(); + } else if (token.equals(MATCH_EXP)) { + matchExp = currentValue(); } else { // we ignore elements we do not understand } next(); } IRequirement capability = null; - if (min >= 0 && max >= 0) { - capability = createRequirement(namespace, name, range, filter, min, max, greedy); + if (matchExp == null) { + if (min >= 0 && max >= 0) { + capability = createRequirement(namespace, name, range, filter, min, max, greedy); + } else { + capability = createRequirement(namespace, name, range, filter, optional, multiple, greedy); + } } else { - capability = createRequirement(namespace, name, range, filter, optional, multiple, greedy); + //When a match expression is specified, namespace, name and versionRange are ignored + if (optional && min == -1 && max == -1) { + min = 0; + max = 1; + } + capability = createRequirement(matchExp, filter, min, max, greedy, null); } if (capability != null) { requires.add(capability); } } + protected IRequirement createRequirement(String requirement, String filter, int min, int max, boolean greedy, String description) { + IExpression expr = ExpressionUtil.parse(substituteVersionAndQualifier(requirement)); + IMatchExpression requirementExp = ExpressionUtil.getFactory().matchExpression(expr); + IMatchExpression filterExp = InstallableUnit.parseFilter(filter); + return MetadataFactory.createRequirement(requirementExp, filterExp, min, max, greedy, description); + } + protected IRequirement createRequirement(String namespace, String name, VersionRange range, String filter, int min, int max, boolean greedy) { IMatchExpression filterExpression = InstallableUnit.parseFilter(filter); return MetadataFactory.createRequirement(namespace, name, range, filterExpression, min, max, greedy); diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/publisher/actions/AdviceFileParserTest.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/publisher/actions/AdviceFileParserTest.java index 9c752ba85..20a9bc7b2 100644 --- a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/publisher/actions/AdviceFileParserTest.java +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/publisher/actions/AdviceFileParserTest.java @@ -15,6 +15,8 @@ import junit.framework.TestCase; import org.eclipse.equinox.internal.p2.metadata.InstallableUnit; import org.eclipse.equinox.internal.p2.metadata.RequiredCapability; import org.eclipse.equinox.p2.metadata.*; +import org.eclipse.equinox.p2.metadata.expression.ExpressionUtil; +import org.eclipse.equinox.p2.metadata.expression.IMatchExpression; import org.eclipse.equinox.p2.publisher.AdviceFileParser; import org.eclipse.equinox.p2.query.QueryUtil; @@ -87,7 +89,7 @@ public class AdviceFileParserTest extends TestCase { public void testUpdateDescriptorWithMatch() { Map map = new HashMap(); - map.put("update.match", "providedCapabilities.exists(pc | pc.namespace == 'org.eclipse.equinox.p2.iu' && (pc.name == 'B' || pc.name == 'C'))"); + map.put("update.matchExp", "providedCapabilities.exists(pc | pc.namespace == 'org.eclipse.equinox.p2.iu' && (pc.name == 'B' || pc.name == 'C'))"); map.put("update.severity", "10"); map.put("update.description", "Test Description"); @@ -245,6 +247,43 @@ public class AdviceFileParserTest extends TestCase { assertEquals(1, reqs[1].getMin()); } + public void testRequireWithExpression() { + Map map = new HashMap(); + String matchExp = "properties[abc] == 'def'"; + map.put("requires.0.matchExp", matchExp); + map.put("requires.0.greedy", Boolean.TRUE.toString()); + map.put("requires.0.min", "1"); + map.put("requires.0.max", "1"); + + AdviceFileParser parser = new AdviceFileParser("id", Version.emptyVersion, map); + parser.parse(); + IRequirement[] reqs = parser.getRequiredCapabilities(); + reqs = parser.getRequiredCapabilities(); + assertEquals(1, reqs.length); + + IMatchExpression matchExpression = ExpressionUtil.getFactory().matchExpression(ExpressionUtil.parse(matchExp)); + assertEquals(matchExpression, reqs[0].getMatches()); + } + + public void testRequireWithExpressionAndOptional() { + Map map = new HashMap(); + String matchExp = "properties[abc] == 'def'"; + map.put("requires.0.matchExp", matchExp); + map.put("requires.0.greedy", Boolean.TRUE.toString()); + map.put("requires.0.optional", Boolean.TRUE.toString()); + + AdviceFileParser parser = new AdviceFileParser("id", Version.emptyVersion, map); + parser.parse(); + IRequirement[] reqs = parser.getRequiredCapabilities(); + reqs = parser.getRequiredCapabilities(); + assertEquals(1, reqs.length); + + IMatchExpression matchExpression = ExpressionUtil.getFactory().matchExpression(ExpressionUtil.parse(matchExp)); + assertEquals(matchExpression, reqs[0].getMatches()); + assertEquals(0, reqs[0].getMin()); + assertEquals(1, reqs[0].getMax()); + } + public void testMetaRequiresAdvice() { Map map = new HashMap(); map.put("metaRequirements.0.namespace", "testNamespace1"); -- cgit v1.2.3