1 package org.apache.maven.archiva.indexer.query;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import junit.framework.TestCase;
24 import java.util.Iterator;
27 * @author Brett Porter
29 public class QueryTest
32 private QueryTerm term1 = new QueryTerm( "field1", "value1" );
34 private QueryTerm term2 = new QueryTerm( "field2", "value2" );
36 private QueryTerm term3 = new QueryTerm( "field3", "value3" );
38 public void testQueryTerm()
40 QueryTerm query = new QueryTerm( "Field", "Value" );
41 assertEquals( "check field setting", "Field", query.getField() );
42 assertEquals( "check value setting", "Value", query.getValue() );
45 public void testSingleTermQuery()
47 SingleTermQuery query = new SingleTermQuery( "Field", "Value" );
48 assertEquals( "check field setting", "Field", query.getField() );
49 assertEquals( "check value setting", "Value", query.getValue() );
51 query = new SingleTermQuery( term1 );
52 assertEquals( "check field setting", "field1", query.getField() );
53 assertEquals( "check value setting", "value1", query.getValue() );
56 public void testRangeQueryOpen()
58 RangeQuery rangeQuery = RangeQuery.createOpenRange();
59 assertNull( "Check range has no start", rangeQuery.getBegin() );
60 assertNull( "Check range has no end", rangeQuery.getEnd() );
63 public void testRangeQueryExclusive()
65 RangeQuery rangeQuery = RangeQuery.createExclusiveRange( term1, term2 );
66 assertEquals( "Check range start", term1, rangeQuery.getBegin() );
67 assertEquals( "Check range end", term2, rangeQuery.getEnd() );
68 assertFalse( "Check exclusive", rangeQuery.isInclusive() );
71 public void testRangeQueryInclusive()
73 RangeQuery rangeQuery = RangeQuery.createInclusiveRange( term1, term2 );
74 assertEquals( "Check range start", term1, rangeQuery.getBegin() );
75 assertEquals( "Check range end", term2, rangeQuery.getEnd() );
76 assertTrue( "Check inclusive", rangeQuery.isInclusive() );
79 public void testRangeQueryOpenEnded()
81 RangeQuery rangeQuery = RangeQuery.createGreaterThanOrEqualToRange( term1 );
82 assertEquals( "Check range start", term1, rangeQuery.getBegin() );
83 assertNull( "Check range end", rangeQuery.getEnd() );
84 assertTrue( "Check inclusive", rangeQuery.isInclusive() );
86 rangeQuery = RangeQuery.createGreaterThanRange( term1 );
87 assertEquals( "Check range start", term1, rangeQuery.getBegin() );
88 assertNull( "Check range end", rangeQuery.getEnd() );
89 assertFalse( "Check exclusive", rangeQuery.isInclusive() );
91 rangeQuery = RangeQuery.createLessThanOrEqualToRange( term1 );
92 assertNull( "Check range start", rangeQuery.getBegin() );
93 assertEquals( "Check range end", term1, rangeQuery.getEnd() );
94 assertTrue( "Check inclusive", rangeQuery.isInclusive() );
96 rangeQuery = RangeQuery.createLessThanRange( term1 );
97 assertNull( "Check range start", rangeQuery.getBegin() );
98 assertEquals( "Check range end", term1, rangeQuery.getEnd() );
99 assertFalse( "Check exclusive", rangeQuery.isInclusive() );
102 public void testCompundQuery()
104 CompoundQuery query = new CompoundQuery();
105 assertTrue( "check query is empty", query.getCompoundQueryTerms().isEmpty() );
111 Iterator i = query.getCompoundQueryTerms().iterator();
112 CompoundQueryTerm term = (CompoundQueryTerm) i.next();
113 assertEquals( "Check first term", "field1", getQuery( term ).getField() );
114 assertEquals( "Check first term", "value1", getQuery( term ).getValue() );
115 assertTrue( "Check first term", term.isRequired() );
116 assertFalse( "Check first term", term.isProhibited() );
118 term = (CompoundQueryTerm) i.next();
119 assertEquals( "Check second term", "field2", getQuery( term ).getField() );
120 assertEquals( "Check second term", "value2", getQuery( term ).getValue() );
121 assertFalse( "Check second term", term.isRequired() );
122 assertFalse( "Check second term", term.isProhibited() );
124 term = (CompoundQueryTerm) i.next();
125 assertEquals( "Check third term", "field3", getQuery( term ).getField() );
126 assertEquals( "Check third term", "value3", getQuery( term ).getValue() );
127 assertFalse( "Check third term", term.isRequired() );
128 assertTrue( "Check third term", term.isProhibited() );
130 CompoundQuery query2 = new CompoundQuery();
132 query2.or( new SingleTermQuery( term2 ) );
133 query2.not( new SingleTermQuery( term3 ) );
135 i = query2.getCompoundQueryTerms().iterator();
136 term = (CompoundQueryTerm) i.next();
137 assertEquals( "Check first term", query, term.getQuery() );
138 assertTrue( "Check first term", term.isRequired() );
139 assertFalse( "Check first term", term.isProhibited() );
141 term = (CompoundQueryTerm) i.next();
142 assertEquals( "Check second term", "field2", getQuery( term ).getField() );
143 assertEquals( "Check second term", "value2", getQuery( term ).getValue() );
144 assertFalse( "Check second term", term.isRequired() );
145 assertFalse( "Check second term", term.isProhibited() );
147 term = (CompoundQueryTerm) i.next();
148 assertEquals( "Check third term", "field3", getQuery( term ).getField() );
149 assertEquals( "Check third term", "value3", getQuery( term ).getValue() );
150 assertFalse( "Check third term", term.isRequired() );
151 assertTrue( "Check third term", term.isProhibited() );
154 private static SingleTermQuery getQuery( CompoundQueryTerm term )
156 return (SingleTermQuery) term.getQuery();