1 /******************************************************************************* 2 * Copyright (c) 2009, 2017 EclipseSource and others. 3 * 4 * This 5 * program and the accompanying materials are made available under the terms of 6 * the Eclipse Public License 2.0 which accompanies this distribution, and is 7 * available at 8 * https://www.eclipse.org/legal/epl-2.0/ 9 * 10 * SPDX-License-Identifier: EPL-2.0 11 * 12 * Contributors: 13 * EclipseSource - initial API and implementation 14 ******************************************************************************/ 15 package org.eclipse.equinox.p2.tests.ui.query; 16 17 import static org.junit.Assert.assertEquals; 18 import static org.junit.Assert.assertTrue; 19 20 import java.util.*; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.equinox.internal.p2.ui.ElementQueryDescriptor; 23 import org.eclipse.equinox.internal.p2.ui.ElementWrapper; 24 import org.eclipse.equinox.p2.query.*; 25 import org.junit.Test; 26 27 /** 28 * Tests the Query Descriptor 29 */ 30 public class QueryDescriptorTest { 31 32 class SimpleQueryable implements IQueryable<String> { 33 List<String> elements = Arrays.asList(new String[] { "a", "b", "c", "d", "e" }); 34 35 @Override query(IQuery<String> query, IProgressMonitor monitor)36 public IQueryResult<String> query(IQuery<String> query, IProgressMonitor monitor) { 37 return query.perform(elements.iterator()); 38 } 39 } 40 41 class WrappedString { 42 String string; 43 WrappedString(String string)44 WrappedString(String string) { 45 this.string = string; 46 } 47 48 @Override equals(Object obj)49 public boolean equals(Object obj) { 50 if (this == obj) 51 return true; 52 if (obj == null) 53 return false; 54 if (!(obj instanceof WrappedString)) 55 return false; 56 WrappedString other = (WrappedString) obj; 57 return this.string.equals(other.string); 58 } 59 60 @Override hashCode()61 public int hashCode() { 62 return string.hashCode(); 63 } 64 } 65 66 class StringWrapper extends ElementWrapper { 67 @Override wrap(Object item)68 protected Object wrap(Object item) { 69 return new WrappedString((String) item); 70 } 71 } 72 73 class SimpleMatchQuery extends MatchQuery<Object> { 74 75 @Override 76 @Deprecated isMatch(Object candidate)77 public boolean isMatch(Object candidate) { 78 if (candidate == "a" || candidate == "b") 79 return true; 80 return false; 81 } 82 } 83 84 class SimpleMatchQuery2 extends MatchQuery<Object> { 85 @Override 86 @Deprecated isMatch(Object candidate)87 public boolean isMatch(Object candidate) { 88 if (candidate == "b" || candidate == "c") 89 return true; 90 return false; 91 } 92 } 93 94 @Test testSimpleDescriptorWithWrapper()95 public void testSimpleDescriptorWithWrapper() { 96 ElementQueryDescriptor eqDescriptor = new ElementQueryDescriptor(new SimpleQueryable(), new SimpleMatchQuery(), 97 new Collector<>(), new StringWrapper()); 98 Collection<?> collection = eqDescriptor.performQuery(null); 99 assertEquals("1.0", 2, collection.size()); 100 assertTrue("1.1", collection.contains(new WrappedString("a"))); 101 assertTrue("1.1", collection.contains(new WrappedString("b"))); 102 } 103 104 @Test testSimpleDescriptorWithoutWrapper()105 public void testSimpleDescriptorWithoutWrapper() { 106 ElementQueryDescriptor eqDescriptor = new ElementQueryDescriptor(new SimpleQueryable(), new SimpleMatchQuery(), 107 new Collector<>()); 108 Collection<?> collection = eqDescriptor.performQuery(null); 109 assertEquals("1.0", 2, collection.size()); 110 assertTrue("1.1", collection.contains("a")); 111 assertTrue("1.1", collection.contains("b")); 112 } 113 114 @Test testCompoundDescriptorAND()115 public void testCompoundDescriptorAND() { 116 IQuery<Object> query = QueryUtil.createCompoundQuery(new SimpleMatchQuery(), new SimpleMatchQuery2(), true); 117 ElementQueryDescriptor eqDescriptor = new ElementQueryDescriptor(new SimpleQueryable(), query, 118 new Collector<>(), new StringWrapper()); 119 Collection<?> collection = eqDescriptor.performQuery(null); 120 assertEquals("1.0", 1, collection.size()); 121 assertTrue("1.1", collection.contains(new WrappedString("b"))); 122 } 123 124 @Test testCompoundDescriptorOR()125 public void testCompoundDescriptorOR() { 126 IQuery<Object> query = QueryUtil.createCompoundQuery(new SimpleMatchQuery(), new SimpleMatchQuery2(), false); 127 ElementQueryDescriptor eqDescriptor = new ElementQueryDescriptor(new SimpleQueryable(), query, 128 new Collector<>(), new StringWrapper()); 129 Collection<?> collection = eqDescriptor.performQuery(null); 130 assertEquals("1.0", 3, collection.size()); 131 assertTrue("1.1", collection.contains(new WrappedString("a"))); 132 assertTrue("1.1", collection.contains(new WrappedString("b"))); 133 assertTrue("1.1", collection.contains(new WrappedString("c"))); 134 } 135 } 136