Skip to main content
summaryrefslogtreecommitdiffstats
blob: e20c3097e998f4e5b6f06e7021c24fa7bdaa0b6e (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
/******************************************************************************* 
* Copyright (c) 2009, 2017 EclipseSource 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:
*   EclipseSource - initial API and implementation
******************************************************************************/
package org.eclipse.equinox.p2.tests.ui.query;

import java.util.*;
import junit.framework.TestCase;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.equinox.internal.p2.ui.ElementQueryDescriptor;
import org.eclipse.equinox.internal.p2.ui.ElementWrapper;
import org.eclipse.equinox.p2.query.*;

/**
 * Tests the Query Descriptor
 */
public class QueryDescriptorTest extends TestCase {

	class SimpleQueryable implements IQueryable<String> {
		List<String> elements = Arrays.asList(new String[] {"a", "b", "c", "d", "e"});

		@Override
		public IQueryResult<String> query(IQuery<String> query, IProgressMonitor monitor) {
			return query.perform(elements.iterator());
		}
	}

	class WrappedString {
		String string;

		WrappedString(String string) {
			this.string = string;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (!(obj instanceof WrappedString))
				return false;
			WrappedString other = (WrappedString) obj;
			return this.string.equals(other.string);
		}

		@Override
		public int hashCode() {
			return string.hashCode();
		}
	}

	class StringWrapper extends ElementWrapper {
		@Override
		protected Object wrap(Object item) {
			return new WrappedString((String) item);
		}
	}

	class SimpleMatchQuery extends MatchQuery<Object> {

		@Override
		@Deprecated
		public boolean isMatch(Object candidate) {
			if (candidate == "a" || candidate == "b")
				return true;
			return false;
		}
	}

	class SimpleMatchQuery2 extends MatchQuery<Object> {
		@Override
		@Deprecated
		public boolean isMatch(Object candidate) {
			if (candidate == "b" || candidate == "c")
				return true;
			return false;
		}
	}

	public void testSimpleDescriptorWithWrapper() {
		ElementQueryDescriptor eqDescriptor = new ElementQueryDescriptor(new SimpleQueryable(), new SimpleMatchQuery(), new Collector<>(), new StringWrapper());
		Collection<?> collection = eqDescriptor.performQuery(null);
		assertEquals("1.0", 2, collection.size());
		assertTrue("1.1", collection.contains(new WrappedString("a")));
		assertTrue("1.1", collection.contains(new WrappedString("b")));
	}

	public void testSimpleDescriptorWithoutWrapper() {
		ElementQueryDescriptor eqDescriptor = new ElementQueryDescriptor(new SimpleQueryable(), new SimpleMatchQuery(), new Collector<>());
		Collection<?> collection = eqDescriptor.performQuery(null);
		assertEquals("1.0", 2, collection.size());
		assertTrue("1.1", collection.contains("a"));
		assertTrue("1.1", collection.contains("b"));
	}

	public void testCompoundDescriptorAND() {
		IQuery<Object> query = QueryUtil.createCompoundQuery(new SimpleMatchQuery(), new SimpleMatchQuery2(), true);
		ElementQueryDescriptor eqDescriptor = new ElementQueryDescriptor(new SimpleQueryable(), query, new Collector<>(), new StringWrapper());
		Collection<?> collection = eqDescriptor.performQuery(null);
		assertEquals("1.0", 1, collection.size());
		assertTrue("1.1", collection.contains(new WrappedString("b")));
	}

	public void testCompoundDescriptorOR() {
		IQuery<Object> query = QueryUtil.createCompoundQuery(new SimpleMatchQuery(), new SimpleMatchQuery2(), false);
		ElementQueryDescriptor eqDescriptor = new ElementQueryDescriptor(new SimpleQueryable(), query, new Collector<>(), new StringWrapper());
		Collection<?> collection = eqDescriptor.performQuery(null);
		assertEquals("1.0", 3, collection.size());
		assertTrue("1.1", collection.contains(new WrappedString("a")));
		assertTrue("1.1", collection.contains(new WrappedString("b")));
		assertTrue("1.1", collection.contains(new WrappedString("c")));
	}
}

Back to the top