Fix jni tests on osx & with new interface

This commit is contained in:
Jeremy Lakeman 2013-01-23 14:40:19 +10:30
parent 521c9471d4
commit dbd4cb1771
6 changed files with 75 additions and 7 deletions

View File

@ -0,0 +1,37 @@
package org.servalproject.servald;
public abstract class AbstractJniResults implements IJniResults {
@Override
public void startResultSet(int columns) {
putBlob(Integer.toString(columns).getBytes());
}
@Override
public void setColumnName(int i, String name) {
putBlob(name.getBytes());
}
@Override
public void putString(String value) {
putBlob((value != null) ? value.getBytes() : null);
}
@Override
public abstract void putBlob(byte[] value);
@Override
public void putLong(long value) {
putBlob(Long.toString(value).getBytes());
}
@Override
public void putDouble(double value) {
putBlob(Double.toString(value).getBytes());
}
@Override
public void totalRowCount(int rows) {
}
}

View File

@ -0,0 +1,11 @@
package org.servalproject.servald;
public interface IJniResults {
public void startResultSet(int columns);
public void setColumnName(int column, String name);
public void putString(String value);
public void putBlob(byte[] value);
public void putLong(long value);
public void putDouble(double value);
public void totalRowCount(int rows);
}

View File

@ -0,0 +1,17 @@
package org.servalproject.servald;
import java.util.List;
public class JniResultsList extends AbstractJniResults implements IJniResults {
final List<byte[]> list;
private byte[] empty = new byte[0];
JniResultsList(List<byte[]> list) {
this.list = list;
}
@Override
public void putBlob(byte[] value) {
list.add(value == null ? empty : value);
}
}

View File

@ -28,17 +28,20 @@ class ServalD
int status;
List<byte[]> outv;
public ServalD()
static
{
String property = System.getProperty("java.library.path");
System.err.println("Attempting to load libservald.so from "+property);
System.loadLibrary("servald");
}
public native int rawCommand(List<byte[]> outv, String... args);
private static native int rawCommand(IJniResults outv, String[] args);
public void command(String... args)
{
this.outv = new LinkedList<byte[]>();
this.status = this.rawCommand(this.outv, args);
IJniResults results = new JniResultsList(outv);
this.status = this.rawCommand(results, args);
}
public static void main(String[] args)

View File

@ -11,8 +11,7 @@ class ServalDTests
public static void main(String[] args)
{
try {
Class<?> cls = new Object() { }.getClass().getEnclosingClass();
Method m = cls.getMethod(args[0], String[].class);
Method m = ServalDTests.class.getMethod(args[0], String[].class);
m.invoke(null, (Object) Arrays.copyOfRange(args, 1, args.length));
}
catch (Exception e) {

View File

@ -35,6 +35,7 @@ compile_java_classes() {
mkdir classes
assert $JAVAC -Xlint:unchecked -d classes "$servald_source_root"/java/org/servalproject/servald/*.java
assert [ -r classes/org/servalproject/servald/ServalD.class ]
assert [ -r classes/org/servalproject/servald/IJniResults.class ]
assert [ -r classes/org/servalproject/servald/ServalDTests.class ]
}
@ -46,7 +47,7 @@ assert_echo_works() {
doc_Echo="Serval JNI echo Hello world"
test_Echo() {
executeOk java -classpath "$PWD/classes" org.servalproject.servald.ServalD echo -e 'Hello,\ttab' 'world\0!'
executeOk java "-Djava.library.path=$LD_LIBRARY_PATH" -classpath "$PWD/classes" org.servalproject.servald.ServalD echo -e 'Hello,\ttab' 'world\0!'
assertStdoutIs -e 'Hello,\ttab\nworld\0!\n'
}
@ -63,7 +64,7 @@ test_Delim() {
doc_Repeat="Serval JNI repeated calls in same process"
test_Repeat() {
executeOk java -classpath "$PWD/classes" org.servalproject.servald.ServalDTests repeat 50 echo 'Hello,' 'world!'
executeOk java "-Djava.library.path=$LD_LIBRARY_PATH" -classpath "$PWD/classes" org.servalproject.servald.ServalDTests repeat 50 echo 'Hello,' 'world!'
assertStdoutLineCount '==' 50
assertStdoutGrep --matches=50 '^0:Hello,:world!$'
}