1 // Simple test that we can use xapian from java
2 //
3 // Copyright (C) 2005,2006,2007,2008,2011 Olly Betts
4 //
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.
9 //
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.
14 //
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
18 // USA
19 
20 import org.xapian.*;
21 
22 // FIXME: need to sort out throwing wrapped Xapian::Error subclasses
23 //import org.xapian.errors.*;
24 
25 // FIXME: "implements" not "extends" in JNI Java API
26 class MyMatchDecider extends MatchDecider {
accept(Document d)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.
30 /*	try { */
31 	    return d.getData().length() == 0;
32 /*
33 	} catch (XapianError e) {
34 	    return true;
35 	}
36 */
37     }
38 }
39 
40 // FIXME: "implements" not "extends" in JNI Java API
41 class MyExpandDecider extends ExpandDecider {
accept(String s)42     public boolean accept(String s) { return s.charAt(0) != 'a'; }
43 }
44 
45 public class SmokeTest {
main(String[] args)46     public static void main(String[] args) throws Exception {
47 	try {
48 	    Stem stem = new Stem("english");
49 	    if (!stem.toString().equals("Xapian::Stem(english)")) {
50 		System.err.println("Unexpected stem.toString()");
51 		System.exit(1);
52 	    }
53 	    Document doc = new Document();
54 	    doc.setData("a\000b");
55 	    String s = doc.getData();
56 	    if (s.equals("a")) {
57 		System.err.println("getData+setData truncates at a zero byte");
58 		System.exit(1);
59 	    }
60 	    if (!s.equals("a\000b")) {
61 		System.err.println("getData+setData doesn't transparently handle a zero byte");
62 		System.exit(1);
63 	    }
64 	    doc.setData("is there anybody out there?");
65 	    doc.addTerm("XYzzy");
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();
73 	    db.addDocument(doc);
74 	    if (db.getDocCount() != 1) {
75 		System.err.println("Unexpected db.getDocCount()");
76 		System.exit(1);
77 	    }
78 
79             if (!Query.MatchAll.toString().equals("Xapian::Query(<alldocuments>)")) {
80 		System.err.println("Unexpected Query.MatchAll.toString()");
81 		System.exit(1);
82             }
83 
84             if (!Query.MatchNothing.toString().equals("Xapian::Query()")) {
85 		System.err.println("Unexpected Query.MatchNothing.toString()");
86 		System.exit(1);
87             }
88 
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()");
93 		System.exit(1);
94 	    }
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()");
99 		System.exit(1);
100 	    }
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()");
105 		System.exit(1);
106 	    }
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()");
112 		System.exit(1);
113 	    }
114 /*
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 += ' ';
120 	    }
121 	    if (!term_str.equals("is there")) {
122 		System.err.println("Unexpected term_str");
123 		System.exit(1);
124 	    }
125 */
126 /*
127 	    boolean ok = false;
128 	    try {
129 		Database db_fail = new Database("NOsuChdaTabASe");
130 		// Ignore the return value.
131 		db_fail.getDocCount();
132 	    } catch (DatabaseOpeningError e) {
133 		ok = true;
134 	    }
135 	    if (!ok) {
136 		System.err.println("Managed to open non-existent database");
137 		System.exit(1);
138 	    }
139 */
140 /*
141 	    if (Query.OP_ELITE_SET != 10) {
142 		System.err.println("OP_ELITE_SET is " + Query.OP_ELITE_SET + " not 10");
143 		System.exit(1);
144 	    }
145 */
146 	    RSet rset = new RSet();
147 	    rset.addDocument(1);
148 	    ESet eset = enq.getESet(10, rset, new MyExpandDecider());
149 	    if (0 == eset.size()) {
150 		System.err.println("ESet.size() was 0");
151 		System.exit(1);
152 	    }
153 
154 /*
155 	    ESetIterator eit = eset.iterator();
156 	    int count = 0;
157 	    while (eit.hasNext()) {
158 		if (eit.getTerm().charAt(0) == 'a') {
159 		    System.err.println("MyExpandDecider wasn't used");
160 		    System.exit(1);
161 		}
162 		++count;
163 		eit.next();
164 	    }
165 	    if (count != eset.size()) {
166 		System.err.println("ESet.size() mismatched number of terms returned by ESetIterator");
167 		System.exit(1);
168 	    }
169 */
170 
171 /*
172 	    MSet mset2 = enq.getMSet(0, 10, null, new MyMatchDecider());
173 	    if (mset2.size() > 0) {
174 		System.err.println("MyMatchDecider wasn't used");
175 		System.exit(1);
176 	    }
177 */
178 
179 	    if (!enq.getQuery().toString().equals("Xapian::Query((there OR is))")) {
180 		System.err.println("Enquire::getQuery() returned the wrong query: " + enq.getQuery().toString());
181 		System.exit(1);
182 	    }
183 	} catch (Exception e) {
184 	    System.err.println("Caught unexpected exception " + e.toString());
185 	    System.exit(1);
186 	}
187     }
188 }
189