1 // Simple test that we can use xapian from java
2 //
3 // Copyright (C) 2005,2006,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 import org.xapian.errors.*;
22 
23 class MyMatchDecider implements MatchDecider {
accept(Document d)24     public boolean accept(Document d) {
25 	// NB It's not normally appropriate to call getData() in a MatchDecider
26 	// but we do it here to make sure we don't get an empty document.
27 	try {
28 	    return d.getData().length() == 0;
29 	} catch (XapianError e) {
30 	    return true;
31 	}
32     }
33 }
34 
35 class MyExpandDecider implements ExpandDecider {
accept(String s)36     public boolean accept(String s) { return s.charAt(0) != 'a'; }
37 }
38 
39 public class SmokeTest {
main(String[] args)40     public static void main(String[] args) throws Exception {
41 	try {
42 	    Stem stem = new Stem("english");
43 	    if (!stem.toString().equals("Xapian::Stem(english)")) {
44 		System.err.println("Unexpected stem.toString()");
45 		System.exit(1);
46 	    }
47 	    Document doc = new Document();
48 	    doc.setData("a\000b");
49 	    String s = doc.getData();
50 	    if (s.equals("a")) {
51 		System.err.println("getData+setData truncates at a zero byte");
52 		System.exit(1);
53 	    }
54 	    if (!s.equals("a\000b")) {
55 		System.err.println("getData+setData doesn't transparently handle a zero byte");
56 		System.exit(1);
57 	    }
58 	    doc.setData("is there anybody out there?");
59 	    doc.addTerm("XYzzy");
60 	    doc.addPosting(stem.stemWord("is"), 1);
61 	    doc.addPosting(stem.stemWord("there"), 2);
62 	    doc.addPosting(stem.stemWord("anybody"), 3);
63 	    doc.addPosting(stem.stemWord("out"), 4);
64 	    doc.addPosting(stem.stemWord("there"), 5);
65 	    WritableDatabase db = Xapian.InMemory.open();
66 	    db.addDocument(doc);
67 	    if (db.getDocCount() != 1) {
68 		System.err.println("Unexpected db.getDocCount()");
69 		System.exit(1);
70 	    }
71 
72 	    String[] terms = { "smoke", "test", "terms" };
73 	    Query query = new Query(Query.OP_OR, terms);
74 	    if (!query.toString().equals("Xapian::Query((smoke OR test OR terms))")) {
75 		System.err.println("Unexpected query.toString()");
76 		System.exit(1);
77 	    }
78 	    Query[] queries = { new Query("smoke"), query, new Query("string") };
79 	    Query query2 = new Query(Query.OP_XOR, queries);
80 	    if (!query2.toString().equals("Xapian::Query((smoke XOR (smoke OR test OR terms) XOR string))")) {
81 		System.err.println("Unexpected query2.toString()");
82 		System.exit(1);
83 	    }
84 	    String[] subqs = { "a", "b" };
85 	    Query query3 = new Query(Query.OP_OR, subqs);
86 	    if (!query3.toString().equals("Xapian::Query((a OR b))")) {
87 		System.err.println("Unexpected query3.toString()");
88 		System.exit(1);
89 	    }
90 	    Enquire enq = new Enquire(db);
91 	    enq.setQuery(new Query(Query.OP_OR, "there", "is"));
92 	    MSet mset = enq.getMSet(0, 10);
93 	    if (mset.size() != 1) {
94 		System.err.println("Unexpected mset.size()");
95 		System.exit(1);
96 	    }
97 	    String term_str = "";
98 	    TermIterator itor = enq.getMatchingTerms(mset.getElement(0));
99 	    while (itor.hasNext()) {
100 		term_str += itor.next();
101 		if (itor.hasNext()) term_str += ' ';
102 	    }
103 	    if (!term_str.equals("is there")) {
104 		System.err.println("Unexpected term_str");
105 		System.exit(1);
106 	    }
107 	    boolean ok = false;
108 	    try {
109 		Database db_fail = new Database("NOsuChdaTabASe");
110 		// Ignore the return value.
111 		db_fail.getDocCount();
112 	    } catch (DatabaseOpeningError e) {
113 		ok = true;
114 	    }
115 	    if (!ok) {
116 		System.err.println("Managed to open non-existent database");
117 		System.exit(1);
118 	    }
119 
120 	    if (Query.OP_ELITE_SET != 10) {
121 		System.err.println("OP_ELITE_SET is " + Query.OP_ELITE_SET + " not 10");
122 		System.exit(1);
123 	    }
124 
125 	    RSet rset = new RSet();
126 	    rset.addDocument(1);
127 	    ESet eset = enq.getESet(10, rset, new MyExpandDecider());
128 	    ESetIterator eit = eset.iterator();
129 	    int count = 0;
130 	    while (eit.hasNext()) {
131 		if (eit.getTerm().charAt(0) == 'a') {
132 		    System.err.println("MyExpandDecider wasn't used");
133 		    System.exit(1);
134 		}
135 		++count;
136 		eit.next();
137 	    }
138 	    if (count != eset.size()) {
139 		System.err.println("ESet.size() mismatched number of terms returned by ESetIterator");
140 		System.exit(1);
141 	    }
142 
143 	    MSet mset2 = enq.getMSet(0, 10, null, new MyMatchDecider());
144 	    if (mset2.size() > 0) {
145 		System.err.println("MyMatchDecider wasn't used");
146 		System.exit(1);
147 	    }
148 
149 	    if (!enq.getQuery().toString().equals("Xapian::Query((there OR is))")) {
150 		System.err.println("Enquire::getQuery() returned the wrong query: " + enq.getQuery().toString());
151 		System.exit(1);
152 	    }
153 	} catch (Exception e) {
154 	    System.err.println("Caught unexpected exception " + e.toString());
155 	    System.exit(1);
156 	}
157     }
158 }
159