Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ce11c448fe00646bd51e80896e42aba97b8283c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*******************************************************************************
 * Copyright (c) 2008, 2012 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
 *     EclipseSource - ongoing development
 *     Rapicorp, Inc (Pascal Rapicault) - Bug 394156 - Add support for updates from one namespace to another
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.ui.query;

import org.eclipse.equinox.p2.metadata.IInstallableUnit;

import java.net.URI;
import java.util.*;
import junit.framework.Assert;
import org.eclipse.equinox.internal.p2.ui.model.*;
import org.eclipse.equinox.internal.p2.ui.query.AvailableIUWrapper;
import org.eclipse.equinox.p2.engine.IProfile;
import org.eclipse.equinox.p2.metadata.*;
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription;
import org.eclipse.equinox.p2.metadata.expression.*;
import org.eclipse.equinox.p2.query.*;
import org.eclipse.equinox.p2.tests.MockQueryable;

/**
 * Tests for {@link AvailableIUWrapper}.
 */
public class AvailableIUWrapperTest extends AbstractQueryTest {
	protected AvailableIUWrapper createWrapper() {
		return createWrapper(true);
	}

	protected AvailableIUWrapper createWrapper(boolean makeCategories) {
		return new AvailableIUWrapper(new MockQueryable(), null, makeCategories, true);
	}

	/**
	 * Returns the IU corresponding to the collected element.
	 */
	protected IInstallableUnit getIU(Object collected) {
		return ((IIUElement) collected).getIU();
	}

	/**
	 * Tests collecting items AvailableIUCollector doesn't care about.
	 */
	public void testCollectObject() {
		AvailableIUWrapper wrapper = createWrapper();
		Collector<Object> collector = new Collector<Object>();
		Object object = new Object();
		collector.accept(object);
		Collection results = wrapper.getElements(collector);
		assertEquals("1.0", 1, results.size());
		assertEquals("1.1", object, results.iterator().next());
	}

	/**
	 * Tests collecting an IU.
	 */
	public void testCollectIU() {
		AvailableIUWrapper wrapper = createWrapper();
		Collector<IInstallableUnit> collector = new Collector<IInstallableUnit>();
		IInstallableUnit unit = createIU("f1");
		collector.accept(unit);
		Collection results = wrapper.getElements(collector);
		assertEquals("1.0", 1, results.size());
		IInstallableUnit collectedIU = getIU(results.iterator().next());
		assertEquals("1.1", unit, collectedIU);
	}

	/**
	 * Tests collecting a category when makeCategory=true.
	 */
	public void testMakeCategory() {
		AvailableIUWrapper wrapper = createWrapper(true);
		Collector<IInstallableUnit> collector = new Collector<IInstallableUnit>();
		Map<String, String> properties = new HashMap<String, String>();
		properties.put(InstallableUnitDescription.PROP_TYPE_CATEGORY, "true");
		IInstallableUnit category = createIU("category", Version.createOSGi(1, 0, 0), NO_REQUIRES, properties, false);
		IInstallableUnit unit = createIU("basicIU");
		collector.accept(category);
		collector.accept(unit);

		Collection results = wrapper.getElements(collector);
		assertEquals("1.0", 2, collector.size());
		boolean categoryFound = false;
		for (Iterator it = results.iterator(); it.hasNext();) {
			Object element = it.next();
			IInstallableUnit collected = getIU(element);
			if (collected.equals(category)) {
				categoryFound = true;
				assertTrue("1.1", element instanceof CategoryElement);
			} else {
				assertEquals("1.2", unit, collected);
			}
		}
		assertTrue("1.3", categoryFound);
	}

	/**
	 * Tests collecting a category when makeCategory=false
	 */
	public void testNoMakeCategory() {
		AvailableIUWrapper wrapper = createWrapper(false);
		Collector<IInstallableUnit> collector = new Collector<IInstallableUnit>();
		Map<String, String> properties = new HashMap<String, String>();
		properties.put(InstallableUnitDescription.PROP_TYPE_CATEGORY, "true");
		IInstallableUnit category = createIU("category", Version.createOSGi(1, 0, 0), NO_REQUIRES, properties, false);
		IInstallableUnit unit = createIU("basicIU");
		collector.accept(category);
		collector.accept(unit);

		Collection results = wrapper.getElements(collector);
		assertEquals("1.0", 2, results.size());
		boolean categoryFound = false;
		for (Iterator it = results.iterator(); it.hasNext();) {
			Object element = it.next();
			IInstallableUnit collected = getIU(element);
			if (collected.equals(category)) {
				categoryFound = true;
				assertFalse("1.1", element instanceof CategoryElement);
			} else {
				assertEquals("1.2", unit, collected);
			}
		}
		assertTrue("1.3", categoryFound);
	}

	/**
	 * Tests hiding installed IUs.
	 */
	public void testHideInstalled() {
		IProfile profile = createProfile("TestProfile");
		AvailableIUWrapper wrapper = createWrapper(true);
		Collector<IInstallableUnit> collector = new Collector<IInstallableUnit>();
		IInstallableUnit installed = createIU("installed");
		IInstallableUnit notInstalled = createIU("notInstalled");
		install(profile, new IInstallableUnit[] {installed}, true, createPlanner(), createEngine());
		wrapper.markInstalledIUs(profile, true);

		//now feed in the installed and non-installed units, and the installed unit should be ignored.
		collector.accept(installed);
		collector.accept(notInstalled);

		Collection results = wrapper.getElements(collector);

		assertEquals("1.1", 1, results.size());
		Object iuElement = results.iterator().next();
		assertEquals("1.2", notInstalled, getIU(iuElement));
	}

	public void testUpdate() {
		//Create the IU that will be detected as an update
		InstallableUnitDescription iud = new MetadataFactory.InstallableUnitDescription();
		iud.setId("NewB");
		iud.setVersion(Version.create("1.0.0"));

		String orExpression = "providedCapabilities.exists(pc | pc.namespace == 'org.eclipse.equinox.p2.iu' && pc.name == 'B')";
		IExpression expr = ExpressionUtil.parse(orExpression);
		IMatchExpression<IInstallableUnit> matchExpression = ExpressionUtil.getFactory().matchExpression(expr);

		Collection<IMatchExpression<IInstallableUnit>> updateExpression = new ArrayList<IMatchExpression<IInstallableUnit>>();
		updateExpression.add(matchExpression);
		iud.setUpdateDescriptor(MetadataFactory.createUpdateDescriptor(updateExpression, IUpdateDescriptor.HIGH, (String) null, (URI) null));
		IInstallableUnit newIUB = MetadataFactory.createInstallableUnit(iud);

		//create the IU being updated
		IInstallableUnit installed = createIU("B");

		//Setup the profile
		IProfile profile = createProfile("TestProfile");
		AvailableIUWrapper wrapper = createWrapper(true);
		Collector<IInstallableUnit> collector = new Collector<IInstallableUnit>();
		installAsRoots(profile, new IInstallableUnit[] {installed}, true, createPlanner(), createEngine());
		wrapper.markInstalledIUs(profile, true);

		//now feed in the installed and non-installed units, and the installed unit should be ignored.
		collector.accept(newIUB);

		Collection results = wrapper.getElements(collector);

		//Verify 
		assertEquals("1.1", 1, results.size());
		Object iuElement = results.iterator().next();
		assertEquals("1.2", newIUB, getIU(iuElement));
		Assert.assertTrue(((AvailableIUElement) iuElement).isUpdate());
	}

	protected IQuery getMockQuery() {
		return QueryUtil.createIUPropertyQuery("key", "value");
	}
}

Back to the top