1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
// Simple test that we can use xapian from java
//
// Copyright (C) 2005,2006,2007,2008,2011,2016,2017,2019 Olly Betts
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
// USA
import org.xapian.*;
// FIXME: need to sort out throwing wrapped Xapian::Error subclasses
//import org.xapian.errors.*;
// FIXME: "implements" not "extends" in JNI Java API
class MyMatchDecider extends MatchDecider {
public boolean accept(Document d) {
// NB It's not normally appropriate to call getData() in a MatchDecider
// but we do it here to make sure we don't get an empty document.
/* try { */
return d.getData().length() == 0;
/*
} catch (XapianError e) {
return true;
}
*/
}
}
// FIXME: "implements" not "extends" in JNI Java API
class MyExpandDecider extends ExpandDecider {
public boolean accept(String s) { return s.charAt(0) != 'a'; }
}
class MyFieldProcessor extends FieldProcessor {
public Query apply(String str) {
if (str.equals("spam"))
return new Query("eggs");
return new Query("spam");
}
}
public class SmokeTest {
public static void main(String[] args) throws Exception {
TermGenerator termGenerator = new TermGenerator();
termGenerator.setFlags(TermGenerator.FLAG_SPELLING);
try {
// Test the version number reporting functions give plausible
// results.
String v = "";
v += Version.major();
v += ".";
v += Version.minor();
v += ".";
v += Version.revision();
String v2 = Version.string();
if (!v.equals(v2)) {
System.err.println("Unexpected version output (" + v + " != " + v2 + ")");
System.exit(1);
}
Stem stem = new Stem("english");
if (!stem.toString().equals("Xapian::Stem(english)")) {
System.err.println("Unexpected stem.toString()");
System.exit(1);
}
Document doc = new Document();
doc.setData("a\000b");
String s = doc.getData();
if (s.equals("a")) {
System.err.println("getData+setData truncates at a zero byte");
System.exit(1);
}
if (!s.equals("a\000b")) {
System.err.println("getData+setData doesn't transparently handle a zero byte");
System.exit(1);
}
doc.setData("is there anybody out there?");
doc.addTerm("XYzzy");
// apply was stemWord() in the JNI bindings
doc.addPosting(stem.apply("is"), 1);
doc.addPosting(stem.apply("there"), 2);
doc.addPosting(stem.apply("anybody"), 3);
doc.addPosting(stem.apply("out"), 4);
doc.addPosting(stem.apply("there"), 5);
WritableDatabase db = new WritableDatabase("", Xapian.DB_BACKEND_INMEMORY);
db.addDocument(doc);
if (db.getDocCount() != 1) {
System.err.println("Unexpected db.getDocCount()");
System.exit(1);
}
QueryParser qp = new QueryParser();
// Test wrapping of null-able grouping parameter.
qp.addBooleanPrefix("colour", "XC");
qp.addBooleanPrefix("color", "XC");
qp.addBooleanPrefix("foo", "XFOO", null);
qp.addBooleanPrefix("bar", "XBAR", "XBA*");
qp.addBooleanPrefix("baa", "XBAA", "XBA*");
DateRangeProcessor rpdate = new DateRangeProcessor(1, Xapian.RP_DATE_PREFER_MDY, 1960);
qp.addRangeprocessor(rpdate);
qp.addRangeprocessor(rpdate, null);
qp.addRangeprocessor(rpdate, "foo");
if (!Query.MatchAll.toString().equals("Query(<alldocuments>)")) {
System.err.println("Unexpected Query.MatchAll.toString()");
System.exit(1);
}
if (!Query.MatchNothing.toString().equals("Query()")) {
System.err.println("Unexpected Query.MatchNothing.toString()");
System.exit(1);
}
String[] terms = { "smoke", "test", "terms" };
Query query = new Query(Query.OP_OR, terms);
if (!query.toString().equals("Query((smoke OR test OR terms))")) {
System.err.println("Unexpected query.toString()");
System.exit(1);
}
Query[] queries = { new Query("smoke"), query, new Query("string") };
Query query2 = new Query(Query.OP_XOR, queries);
if (!query2.toString().equals("Query((smoke XOR (smoke OR test OR terms) XOR string))")) {
System.err.println("Unexpected query2.toString()");
System.exit(1);
}
String[] subqs = { "a", "b" };
Query query3 = new Query(Query.OP_OR, subqs);
if (!query3.toString().equals("Query((a OR b))")) {
System.err.println("Unexpected query3.toString()");
System.exit(1);
}
Enquire enq = new Enquire(db);
// Check Xapian::BAD_VALUENO is wrapped suitably.
enq.setCollapseKey(Xapian.BAD_VALUENO);
// Test that the non-constant wrapping prior to 1.4.10 still works.
enq.setCollapseKey(Xapian.getBAD_VALUENO());
enq.setQuery(new Query(Query.OP_OR, "there", "is"));
MSet mset = enq.getMSet(0, 10);
if (mset.size() != 1) {
System.err.println("Unexpected mset.size()");
System.exit(1);
}
MSetIterator m_itor = mset.begin();
Document m_doc = null;
long m_id;
while(m_itor.hasNext()) {
m_id = m_itor.next();
if(m_itor.hasNext()) {
m_doc = mset.getDocument(m_id);
}
}
// Only one doc exists in this mset
if(m_doc != null && m_doc.getDocId() != 0) {
System.err.println("Unexpected docid");
System.exit(1);
}
String term_str = "";
TermIterator itor = enq.getMatchingTermsBegin(mset.getElement(0));
while (itor.hasNext()) {
term_str += itor.next();
if (itor.hasNext())
term_str += ' ';
}
if (!term_str.equals("is there")) {
System.err.println("Unexpected term_str");
System.exit(1);
}
/* FIXME:dc: Fails since Xapian::Error is still unmapped
boolean ok = false;
try {
Database db_fail = new Database("NOsuChdaTabASe");
// Ignore the return value.
db_fail.getDocCount();
} catch (DatabaseOpeningError e) {
ok = true;
}
if (!ok) {
System.err.println("Managed to open non-existent database");
System.exit(1);
}
*/
/*
if (Query.OP_ELITE_SET != 10) {
System.err.println("OP_ELITE_SET is " + Query.OP_ELITE_SET + " not 10");
System.exit(1);
}
*/
RSet rset = new RSet();
rset.addDocument(1);
ESet eset = enq.getESet(10, rset, new MyExpandDecider());
// FIXME: temporary simple check
if (0 == eset.size()) {
System.err.println("ESet.size() was 0");
System.exit(1);
}
int count = 0;
for(ESetIterator eit = eset.begin(); eit.hasNext(); ) {
// for (int i = 0; i < eset.size(); i++) {
if (eit.getTerm().charAt(0) == 'a') {
System.err.println("MyExpandDecider wasn't used");
System.exit(1);
}
++count;
eit.next();
}
if (count != eset.size()) {
System.err.println("ESet.size() mismatched number of terms returned by ESetIterator");
System.err.println(count + " " + eset.size());
System.exit(1);
}
/*
MSet mset2 = enq.getMSet(0, 10, null, new MyMatchDecider());
if (mset2.size() > 0) {
System.err.println("MyMatchDecider wasn't used");
System.exit(1);
}
*/
if (!enq.getQuery().toString().equals("Query((there OR is))")) {
System.err.println("Enquire::getQuery() returned the wrong query: " + enq.getQuery().toString());
System.exit(1);
}
{
qp.addPrefix("food", new MyFieldProcessor());
if (!qp.parseQuery("food:spam").toString().equals("Query(eggs)")) {
System.err.println("FieldProcessor subclass doesn't work as expected");
System.exit(1);
}
}
{
// Wrapped functions which take/return byte[] for std::string.
// Check that serialisation returns byte[], that
// unserialisation takes byte[], and round-tripping works.
byte[] res = Xapian.sortableSerialise(1.675);
if (Xapian.sortableUnserialise(res) != 1.675) {
System.err.println("sortableSerialise() and/or sortableUnserialise() don't work as expected");
System.exit(1);
}
// Check that serialisation returns byte[], that
// unserialisation takes byte[], and round-tripping works.
Query q = new Query("foo");
res = q.serialise();
Query q_out = Query.unserialise(res);
if (!q.toString().equals(q_out.toString())) {
System.err.println("Query serialisation doesn't work as expected");
System.exit(1);
}
// Check Document.addValue() takes byte[], that getValue()
// returns byte[], that serialisation returns byte[], that
// unserialisation takes byte[], and round-tripping works.
Document d = new Document();
d.setData("xyzzy");
d.addValue(7, res);
byte[] res2 = d.getValue(7);
if (!java.util.Arrays.equals(res, res2)) {
System.err.println("Document.getValue() returns a different byte[] to the one set with addValue()");
System.exit(1);
}
res = d.serialise();
Document d_out = Document.unserialise(res);
// Make sure the "terms_here" flag is set so the descriptions match.
d_out.termListCount();
if (!d.toString().equals(d_out.toString())) {
System.err.println("Document serialisation doesn't work as expected");
System.err.println(d.toString());
System.err.println(d_out.toString());
System.exit(1);
}
// Check that serialisation returns byte[], that
// unserialisation takes byte[], and round-tripping works.
LatLongCoord llc = new LatLongCoord(10.5, 45.25);
res = llc.serialise();
LatLongCoord llc_out = new LatLongCoord();
llc_out.unserialise(res);
if (!llc.toString().equals(llc_out.toString())) {
System.err.println("LatLongCoord serialisation doesn't work as expected");
System.exit(1);
}
// Check that serialisation returns byte[], that
// unserialisation takes byte[], and round-tripping works.
LatLongCoords llcs = new LatLongCoords();
llcs.append(llc);
res = llcs.serialise();
LatLongCoords llcs_out = new LatLongCoords();
llcs_out.unserialise(res);
if (!llcs.toString().equals(llcs_out.toString())) {
System.err.println("LatLongCoords serialisation doesn't work as expected");
System.exit(1);
}
// Check `range_limit` mapped to byte[].
q = new Query(Query.op.OP_VALUE_GE, 0, res);
if (q.toString().length() == 0) {
// Mostly just a way to actually use the constructed object.
System.err.println("Query description shouldn't be empty");
System.exit(1);
}
// Check `range_limit` mapped to byte[].
q = new Query(Query.op.OP_VALUE_LE, 1, res);
if (q.toString().length() == 0) {
// Mostly just a way to actually use the constructed object.
System.err.println("Query description shouldn't be empty");
System.exit(1);
}
// Check `range_lower` and `range_upper` mapped to byte[].
q = new Query(Query.op.OP_VALUE_RANGE, 2, res, res);
if (q.toString().length() == 0) {
// Mostly just a way to actually use the constructed object.
System.err.println("Query description shouldn't be empty");
System.exit(1);
}
// Check ValueSetMatchDecider.addValue() and removeValue() take
// byte[].
ValueSetMatchDecider vsmd = new ValueSetMatchDecider(1, false);
vsmd.addValue(res);
vsmd.removeValue(res);
// Check Database.getValueLowerBound() and getValueUpperBound()
// return byte[].
byte[] lo = "abba".getBytes();
byte[] hi = "xyzzy".getBytes();
{
WritableDatabase wdb = new WritableDatabase("", Xapian.DB_BACKEND_INMEMORY);
Document document = new Document();
document.addValue(42, hi);
wdb.addDocument(document);
document.addValue(42, lo);
wdb.addDocument(document);
db = wdb;
}
if (!java.util.Arrays.equals(db.getValueLowerBound(42), lo)) {
System.err.println("Database.getValueLowerBound() doesn't work as expected");
System.exit(1);
}
if (!java.util.Arrays.equals(db.getValueUpperBound(42), hi)) {
System.err.println("Database.getValueUpperBound() doesn't work as expected");
System.exit(1);
}
ValueIterator it = db.valuestreamBegin(42);
if (!java.util.Arrays.equals(it.getValue(), hi)) {
System.err.println("ValueIterator.getValue() doesn't work as expected");
System.exit(1);
}
}
} catch (Exception e) {
System.err.println("Caught unexpected exception " + e.toString());
System.exit(1);
}
}
}
|