KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > TestQueryTermVector


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import junit.framework.TestCase;
20 import org.apache.lucene.analysis.WhitespaceAnalyzer;
21
22 public class TestQueryTermVector extends TestCase {
23
24
25   public TestQueryTermVector(String JavaDoc s) {
26     super(s);
27   }
28
29   protected void setUp() {
30   }
31
32   protected void tearDown() {
33
34   }
35
36   public void testConstructor() {
37     String JavaDoc [] queryTerm = {"foo", "bar", "foo", "again", "foo", "bar", "go", "go", "go"};
38     //Items are sorted lexicographically
39
String JavaDoc [] gold = {"again", "bar", "foo", "go"};
40     int [] goldFreqs = {1, 2, 3, 3};
41     QueryTermVector result = new QueryTermVector(queryTerm);
42     assertTrue(result != null);
43     String JavaDoc [] terms = result.getTerms();
44     assertTrue(terms.length == 4);
45     int [] freq = result.getTermFrequencies();
46     assertTrue(freq.length == 4);
47     checkGold(terms, gold, freq, goldFreqs);
48     result = new QueryTermVector(null);
49     assertTrue(result.getTerms().length == 0);
50     
51     result = new QueryTermVector("foo bar foo again foo bar go go go", new WhitespaceAnalyzer());
52     assertTrue(result != null);
53     terms = result.getTerms();
54     assertTrue(terms.length == 4);
55     freq = result.getTermFrequencies();
56     assertTrue(freq.length == 4);
57     checkGold(terms, gold, freq, goldFreqs);
58   }
59
60   private void checkGold(String JavaDoc[] terms, String JavaDoc[] gold, int[] freq, int[] goldFreqs) {
61     for (int i = 0; i < terms.length; i++) {
62       assertTrue(terms[i].equals(gold[i]));
63       assertTrue(freq[i] == goldFreqs[i]);
64     }
65   }
66 }
67
Popular Tags