1 // Simple test that we can use xapian from java
3 // Copyright (C) 2005,2006,2007,2008,2011 Olly Betts
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 // FIXME: need to sort out throwing wrapped Xapian::Error subclasses
23 //import org.xapian.errors.*;
25 // FIXME: "implements" not "extends" in JNI Java API
26 class MyMatchDecider
extends MatchDecider
{
27 public boolean accept(Document d
) {
28 // NB It's not normally appropriate to call getData() in a MatchDecider
29 // but we do it here to make sure we don't get an empty document.
31 return d
.getData().length() == 0;
33 } catch (XapianError e) {
40 // FIXME: "implements" not "extends" in JNI Java API
41 class MyExpandDecider
extends ExpandDecider
{
42 public boolean accept(String s
) { return s
.charAt(0) != 'a'; }
45 public class SmokeTest
{
46 public static void main(String
[] args
) throws Exception
{
48 Stem stem
= new Stem("english");
49 if (!stem
.toString().equals("Xapian::Stem(english)")) {
50 System
.err
.println("Unexpected stem.toString()");
53 Document doc
= new Document();
54 doc
.setData("a\000b");
55 String s
= doc
.getData();
57 System
.err
.println("getData+setData truncates at a zero byte");
60 if (!s
.equals("a\000b")) {
61 System
.err
.println("getData+setData doesn't transparently handle a zero byte");
64 doc
.setData("is there anybody out there?");
66 doc
.addPosting(stem
.apply("is"), 1);
67 doc
.addPosting(stem
.apply("there"), 2);
68 doc
.addPosting(stem
.apply("anybody"), 3);
69 doc
.addPosting(stem
.apply("out"), 4);
70 doc
.addPosting(stem
.apply("there"), 5);
71 // FIXME: was WritableDatabase db = Xapian.InMemory.open();
72 WritableDatabase db
= InMemory
.open();
74 if (db
.getDocCount() != 1) {
75 System
.err
.println("Unexpected db.getDocCount()");
79 if (!Query
.MatchAll
.toString().equals("Xapian::Query(<alldocuments>)")) {
80 System
.err
.println("Unexpected Query.MatchAll.toString()");
84 if (!Query
.MatchNothing
.toString().equals("Xapian::Query()")) {
85 System
.err
.println("Unexpected Query.MatchNothing.toString()");
89 String
[] terms
= { "smoke", "test", "terms" };
90 Query query
= new Query(Query
.OP_OR
, terms
);
91 if (!query
.toString().equals("Xapian::Query((smoke OR test OR terms))")) {
92 System
.err
.println("Unexpected query.toString()");
95 Query
[] queries
= { new Query("smoke"), query
, new Query("string") };
96 Query query2
= new Query(Query
.OP_XOR
, queries
);
97 if (!query2
.toString().equals("Xapian::Query((smoke XOR (smoke OR test OR terms) XOR string))")) {
98 System
.err
.println("Unexpected query2.toString()");
101 String
[] subqs
= { "a", "b" };
102 Query query3
= new Query(Query
.OP_OR
, subqs
);
103 if (!query3
.toString().equals("Xapian::Query((a OR b))")) {
104 System
.err
.println("Unexpected query3.toString()");
107 Enquire enq
= new Enquire(db
);
108 enq
.setQuery(new Query(Query
.OP_OR
, "there", "is"));
109 MSet mset
= enq
.getMSet(0, 10);
110 if (mset
.size() != 1) {
111 System
.err
.println("Unexpected mset.size()");
115 String term_str = "";
116 TermIterator itor = enq.getMatchingTerms(mset.getElement(0));
117 while (itor.hasNext()) {
118 term_str += itor.next();
119 if (itor.hasNext()) term_str += ' ';
121 if (!term_str.equals("is there")) {
122 System.err.println("Unexpected term_str");
129 Database db_fail = new Database("NOsuChdaTabASe");
130 // Ignore the return value.
131 db_fail.getDocCount();
132 } catch (DatabaseOpeningError e) {
136 System.err.println("Managed to open non-existent database");
141 if (Query.OP_ELITE_SET != 10) {
142 System.err.println("OP_ELITE_SET is " + Query.OP_ELITE_SET + " not 10");
146 RSet rset
= new RSet();
148 ESet eset
= enq
.getESet(10, rset
, new MyExpandDecider());
149 if (0 == eset
.size()) {
150 System
.err
.println("ESet.size() was 0");
155 ESetIterator eit = eset.iterator();
157 while (eit.hasNext()) {
158 if (eit.getTerm().charAt(0) == 'a') {
159 System.err.println("MyExpandDecider wasn't used");
165 if (count != eset.size()) {
166 System.err.println("ESet.size() mismatched number of terms returned by ESetIterator");
172 MSet mset2 = enq.getMSet(0, 10, null, new MyMatchDecider());
173 if (mset2.size() > 0) {
174 System.err.println("MyMatchDecider wasn't used");
179 if (!enq
.getQuery().toString().equals("Xapian::Query((there OR is))")) {
180 System
.err
.println("Enquire::getQuery() returned the wrong query: " + enq
.getQuery().toString());
183 } catch (Exception e
) {
184 System
.err
.println("Caught unexpected exception " + e
.toString());