package org.example;
import javax.xml.xquery.XQConnection;
import javax.xml.xquery.XQDataSource;
import javax.xml.xquery.XQExpression;
import javax.xml.xquery.XQSequence;
import junit.framework.Assert;
import org.junit.Test;
/**
* Unit test to investigate: "Error: [BXDB0007] Database 'factbook' is opened by
* another process."
*
* When the database is somehow in use, the Java code fails silently. Like:
*
*
* - Start the server, using:
*
*
* {@code
* $ basexserver -S
* Server was started.
* }
*
*
*
* - Import Factbook, using:
*
*
* {@code
* $ basex
* BaseX 7.3 [Standalone]
* Try help to get more information.
*
* > create db factbook ../mondial-3.0.xml
* Database 'factbook' created in 592.53 ms.
*
* > show databases
* 1 opened database(s):
* - factbook (1x)
* }
*
*
*
*
* - Leave the standalone tool open, hence make the database unavailable for
* updates by the server.
* - Run this very unit test; though the log in {@code data/.logs} does show
* the BXDB0007 error, the Java code just silently ignores that.
*
*
*/
public class SimpleBaseXTest {
private static final String DRIVER = "net.xqj.basex.BaseXXQDataSource";
private static final String SELECT = "xs:long(collection('factbook')//country[name='Albania']/@population[1]/data(.))";
private static final String UPDATE = "for $c in collection('factbook')//country[name='Albania']"
+ "return("
+ " replace value of node $c/@population with xs:integer($c/@population + 1)"
+ ")";
@Test
public void testUpdate() {
try {
XQDataSource xqd = (XQDataSource) Class.forName(DRIVER)
.newInstance();
XQConnection xqc = xqd.getConnection("admin", "admin");
XQExpression xqe = xqc.createExpression();
XQSequence xqs = xqe.executeQuery(SELECT);
xqs.next();
long before = xqs.getLong();
System.out.println("Initial value: " + before);
System.out.println("Updating...");
xqs = xqe.executeQuery(UPDATE);
xqs = xqe.executeQuery(SELECT);
xqs.next();
long after = xqs.getLong();
System.out.println("Resulting value: " + after);
xqc.close();
Assert.assertEquals("Update should have succeeded or failed",
before + 1, after);
} catch (Exception e) {
e.printStackTrace();
}
}
}