Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f435c6cf9a4287906ebd9b8894460ed7c513a047 (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
/*******************************************************************************
 *  Copyright (c) 2008, 2010 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.p2.tests.ui.query;

import java.util.Collection;
import java.util.Iterator;
import org.eclipse.equinox.internal.p2.ui.model.CategoryElement;
import org.eclipse.equinox.internal.p2.ui.model.EmptyElementExplanation;
import org.eclipse.equinox.internal.p2.ui.query.CategoryElementWrapper;
import org.eclipse.equinox.p2.metadata.*;
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription;
import org.eclipse.equinox.p2.query.Collector;
import org.eclipse.equinox.p2.tests.MockQueryable;

/**
 * Tests for {@link CategoryElementWrapper}.
 */
public class CategoryElementWrapperTest extends AbstractQueryTest {
	private CategoryElementWrapper createWrapper() {
		IInstallableUnit category = createIU("default category");
		return new CategoryElementWrapper(new MockQueryable(category), null);
	}

	private IInstallableUnit createNamedCategory(String id, String name, Version version) {
		InstallableUnitDescription iu = new MetadataFactory.InstallableUnitDescription();
		iu.setId(id);
		iu.setVersion(version);
		iu.setProperty(IInstallableUnit.PROP_NAME, name);
		iu.setProperty(InstallableUnitDescription.PROP_TYPE_CATEGORY, Boolean.toString(true));
		return MetadataFactory.createInstallableUnit(iu);
	}

	public void testCollectObject() {
		CategoryElementWrapper wrapper = createWrapper();
		Collector collector = new Collector();
		collector.accept("AnObjectThatIsNotAnIU");
		Iterator results = wrapper.getElements(collector).iterator();
		// Collection should either be empty or explain its emptiness.
		while (results.hasNext())
			assertTrue("1.0", results.next() instanceof EmptyElementExplanation);
	}

	/**
	 * Tests for the {@link Collector#isEmpty()} method.
	 */
	public void testIsEmpty() {
		CategoryElementWrapper wrapper = createWrapper();
		Collector collector = new Collector();
		assertTrue("1.1", collector.isEmpty());

		IInstallableUnit category1 = createIU("category1");
		collector.accept(category1);
		Collection results = wrapper.getElements(collector);
		assertTrue("1.2", !results.isEmpty());
	}

	/**
	 * Tests for the {@link Collector#size()} method.
	 */
	public void testSize() {
		CategoryElementWrapper wrapper = createWrapper();
		Collector collector = new Collector();
		assertEquals("1.1", 0, collector.size());

		IInstallableUnit category1 = createIU("category1");
		collector.accept(category1);
		Collection results = wrapper.getElements(collector);
		assertEquals("1.2", 1, collector.size());
		assertEquals("1.3", category1, ((CategoryElement) results.iterator().next()).getIU());

		//adding the same category twice shouldn't affect size
		collector.accept(category1);
		results = wrapper.getElements(collector);
		assertEquals("1.6", 1, results.size());

		//adding a nested category shouldn't affected size
		IRequirement[] required = createRequiredCapabilities(IInstallableUnit.NAMESPACE_IU_ID, "category1");
		IInstallableUnit nested = createIU("Nested", required);
		collector.accept(nested);
		results = wrapper.getElements(collector);
		assertEquals("1.7", 1, results.size());
	}

	public void testCategoryMerging() {
		CategoryElementWrapper wrapper = createWrapper();
		Collector collector = new Collector();
		assertEquals("1.1", 0, collector.size());

		IInstallableUnit category1 = createNamedCategory("qualifier1.foo", "Foo", DEFAULT_VERSION);
		collector.accept(category1);
		Collection results = wrapper.getElements(collector);
		assertEquals("1.2", 1, collector.size());
		assertEquals("1.3", category1, ((CategoryElement) results.iterator().next()).getIU());

		//add a second category with different id and different name
		IInstallableUnit category2 = createNamedCategory("qualifier2.foo", "Foo", DEFAULT_VERSION);
		collector.accept(category2);
		results = wrapper.getElements(collector);
		assertEquals("1.4", 1, results.size());
	}
}

Back to the top