- Introduction of real Unit-Tests
- First steps of modularizing the code by extracting the Usb_Device part into a single, testable module. - Integration of Unit-Tests for the LibusbJava methods git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@305 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -481,11 +481,19 @@ JNIEXPORT jstring JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_libusb_1strerro
|
||||
|
||||
/*
|
||||
* Class: ch_ntb_inf_libusbJava_LibusbJava1
|
||||
* Method: libusb_exceptionTest
|
||||
* Signature: (I)V
|
||||
* Method: setup
|
||||
* Signature: ()I
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_libusb_1exceptionTest
|
||||
(JNIEnv *, jclass, jint);
|
||||
JNIEXPORT jint JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_setup
|
||||
(JNIEnv *, jclass);
|
||||
|
||||
/*
|
||||
* Class: ch_ntb_inf_libusbJava_LibusbJava1
|
||||
* Method: teardown
|
||||
* Signature: ()V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_teardown
|
||||
(JNIEnv *, jclass);
|
||||
|
||||
/*
|
||||
* Class: ch_ntb_inf_libusbJava_LibusbJava1
|
||||
|
||||
@@ -19,6 +19,11 @@
|
||||
<property name="out_dir" value="${basedir}/out" />
|
||||
<property name="ver_info" value="${out_dir}/LibusbJava.res" />
|
||||
<property name="Unit-Test Executable" value="${out_dir}/LibusbJava-UnitTest.exe" />
|
||||
<property name="Source Files" value=" ${basedir}/LibusbJava.cpp
|
||||
${basedir}/objects/Usb_Device.cpp"
|
||||
/>
|
||||
|
||||
|
||||
|
||||
<property name="LIB_CCFLAGS_WIN" value="-I"C:/Program Files/Java/jdk${java.version}/include"
|
||||
-I"C:/Program Files/Java/jdk${java.version}/include/win32"
|
||||
@@ -33,12 +38,12 @@
|
||||
-m64
|
||||
-static
|
||||
-shared
|
||||
${basedir}/LibusbJava.cpp
|
||||
${ver_info}
|
||||
${Source Files}
|
||||
-o ${out_dir}/LibusbJava-1_0.dll
|
||||
-L${basedir}/libusb/MinGW64/static
|
||||
-L${basedir}/libusb/MinGW32/static
|
||||
-lusb-1.0" />
|
||||
-lusb-1.0"
|
||||
/>
|
||||
<property name="APP_CCFLAGS_WIN" value="-Wall
|
||||
-Wno-write-strings
|
||||
-D_JNI_IMPLEMENTATION_
|
||||
@@ -51,9 +56,8 @@
|
||||
-static
|
||||
-I"C:/Program Files/Java/jdk${java.version}/include"
|
||||
-I"C:/Program Files/Java/jdk${java.version}/include/win32"
|
||||
-I${basedir}
|
||||
${basedir}/test/CuTest.c
|
||||
${basedir}/test/LibusbJavaTest.cpp
|
||||
${basedir}/test/CuTest.c
|
||||
-o
|
||||
${Unit-Test Executable}
|
||||
-L"C:/Program Files/Java/jdk${java.version}/lib"" />
|
||||
@@ -64,9 +68,7 @@
|
||||
-Wl,-soname,libusbJava-1.0.so
|
||||
-I/usr/lib
|
||||
-I/usr/lib/jvm/java-6-openjdk/include/
|
||||
-I${basedir}
|
||||
-I${basedir}/libusb/include
|
||||
${basedir}/LibusbJava.cpp
|
||||
${Source Files}
|
||||
-o${out_dir}/libusbJava-1.0.so.${version}
|
||||
/usr/lib/libusb-1.0.so" />
|
||||
<property name="APP_CCFLAGS_LIN" value="-Wall
|
||||
|
||||
140
LibusbJava/objects/Usb_Device.cpp
Normal file
140
LibusbJava/objects/Usb_Device.cpp
Normal file
@@ -0,0 +1,140 @@
|
||||
/*! \file
|
||||
*
|
||||
* C++ Stub for the java class of a java class ch.ntb.inf.libusbJava.Usb_Device.
|
||||
*
|
||||
* \author Ueli Niederer <ueli.niederer @ ntb.ch>
|
||||
*/
|
||||
#include "Usb_Device.h"
|
||||
|
||||
#ifdef DO_UNIT_TEST
|
||||
# include <test/CuTest.h>
|
||||
|
||||
# define TEST_CASE(name) static void name(CuTest *tc)
|
||||
#endif
|
||||
|
||||
typedef struct JField {
|
||||
const char *type;
|
||||
const char *name;
|
||||
jfieldID id;
|
||||
}tJField;
|
||||
|
||||
typedef struct Usb_Device {
|
||||
int loaded;
|
||||
|
||||
jclass clazz;
|
||||
|
||||
jmethodID constructor;
|
||||
|
||||
jfieldID next;
|
||||
jfieldID prev;
|
||||
jfieldID filename;
|
||||
jfieldID bus;
|
||||
jfieldID descriptor;
|
||||
jfieldID config;
|
||||
jfieldID devnum;
|
||||
jfieldID num_children;
|
||||
jfieldID children;
|
||||
jfieldID devStructAddr;
|
||||
}tUsb_Device;
|
||||
|
||||
#define LOAD_FIELD(env, me, type, name) me->name = env->GetFieldID(me->clazz, #name, (type))
|
||||
#if 0
|
||||
#define SANITY_CHECK() { if (me->loaded == 0) Usb_Device_connect(env); }
|
||||
#else
|
||||
#define SANITY_CHECK()
|
||||
#endif
|
||||
|
||||
static tUsb_Device instance = { 0 };
|
||||
static tUsb_Device *me = &instance;
|
||||
|
||||
int Usb_Device_connect(JNIEnv *env)
|
||||
{
|
||||
jobject theClass = env->FindClass("ch/ntb/inf/libusbJava/Usb_Device");
|
||||
if (theClass == NULL) goto no_class;
|
||||
|
||||
me->clazz = (jclass) env->NewGlobalRef(theClass); // make it global to avoid class unloading and therefore
|
||||
// invalidating the references obtained.
|
||||
if (me->clazz == NULL) goto no_memory;
|
||||
|
||||
me->constructor = env->GetMethodID(me->clazz, "<init>", "()V");
|
||||
if (me->constructor == NULL) goto no_constructor;
|
||||
|
||||
LOAD_FIELD(env, me, "Lch/ntb/inf/libusbJava/Usb_Device;", next);
|
||||
LOAD_FIELD(env, me, "Lch/ntb/inf/libusbJava/Usb_Device;", prev);
|
||||
LOAD_FIELD(env, me, "Ljava/lang/String;", filename);
|
||||
LOAD_FIELD(env, me, "Lch/ntb/inf/libusbJava/Usb_Bus;", bus);
|
||||
LOAD_FIELD(env, me, "Lch/ntb/inf/libusbJava/Usb_Device_Descriptor;", descriptor);
|
||||
LOAD_FIELD(env, me, "[Lch/ntb/inf/libusbJava/Usb_Config_Descriptor;", config);
|
||||
LOAD_FIELD(env, me, "B", devnum);
|
||||
LOAD_FIELD(env, me, "B", num_children);
|
||||
LOAD_FIELD(env, me, "Lch/ntb/inf/libusbJava/Usb_Device;", children);
|
||||
LOAD_FIELD(env, me, "J", devStructAddr);
|
||||
|
||||
me->loaded = 1;
|
||||
return 0;
|
||||
|
||||
no_constructor:
|
||||
env->DeleteGlobalRef(me->clazz);
|
||||
no_memory:
|
||||
no_class:
|
||||
return -1;
|
||||
}
|
||||
|
||||
void Usb_Device_disconnect(JNIEnv *env)
|
||||
{
|
||||
if (me->loaded == 0) return;
|
||||
|
||||
env->DeleteGlobalRef(me->clazz);
|
||||
memset(me, 0, sizeof(tUsb_Device));
|
||||
}
|
||||
|
||||
jobject Usb_Device_create(JNIEnv *env, libusb_device *handle, uint8_t address){
|
||||
if (me->loaded == 0) return NULL;
|
||||
|
||||
jobject result = env->NewObject(me->clazz, me->constructor);
|
||||
|
||||
if (result != NULL) {
|
||||
char filename[4];
|
||||
|
||||
snprintf(filename, 4, "%03d", address);
|
||||
|
||||
env->SetLongField(result, me->devStructAddr, (jlong)handle);
|
||||
env->SetByteField(result, me->devnum, address);
|
||||
env->SetObjectField(result, me->filename, env->NewStringUTF(filename));
|
||||
|
||||
env->SetByteField(result, me->num_children, 0); /*Hardcoded to 0, like the compatibility Layer*/
|
||||
env->SetObjectField(result, me->children, NULL); /*Hardcoded to NULL, like the compatibility Layer*/
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
libusb_device * Usb_Device_HandleGet(JNIEnv *env, jobject dev)
|
||||
{
|
||||
if (me->loaded == 0) return NULL;
|
||||
|
||||
return (libusb_device*)env->GetLongField(dev, me->devStructAddr);
|
||||
}
|
||||
|
||||
void Usb_Device_HandleSet(JNIEnv *env, jobject dev, libusb_device *handle)
|
||||
{
|
||||
env->SetLongField(dev, me->devStructAddr, (jlong)handle);
|
||||
}
|
||||
|
||||
void Usb_Device_ConfigSet(JNIEnv *env, jobject dev, jobjectArray config)
|
||||
{
|
||||
env->SetObjectField(dev, me->config, config);
|
||||
}
|
||||
|
||||
void Usb_Device_DescriptorSet(JNIEnv *env, jobject dev, jobject desc)
|
||||
{
|
||||
env->SetObjectField(dev, me->descriptor, desc);
|
||||
}
|
||||
|
||||
void Usb_Device_NextSet(JNIEnv *env, jobject dev, jobject next) {
|
||||
env->SetObjectField(dev, me->next, next);
|
||||
}
|
||||
|
||||
void Usb_Device_PrevSet(JNIEnv *env, jobject dev, jobject prev) {
|
||||
env->SetObjectField(dev, me->prev, prev);
|
||||
}
|
||||
18
LibusbJava/objects/Usb_Device.h
Normal file
18
LibusbJava/objects/Usb_Device.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef USBDEVICE_H_
|
||||
#define USBDEVICE_H_
|
||||
|
||||
#include <jni.h>
|
||||
#include <libusb.h>
|
||||
|
||||
int Usb_Device_connect(JNIEnv *env);
|
||||
void Usb_Device_disconnect(JNIEnv *env);
|
||||
|
||||
jobject Usb_Device_create(JNIEnv *env, libusb_device *handle, uint8_t address);
|
||||
libusb_device * Usb_Device_HandleGet(JNIEnv *env, jobject dev);
|
||||
void Usb_Device_HandleSet(JNIEnv *env, jobject dev, libusb_device *handle);
|
||||
void Usb_Device_ConfigSet(JNIEnv *env, jobject dev, jobjectArray config);
|
||||
void Usb_Device_DescriptorSet(JNIEnv *env, jobject dev, jobject desc);
|
||||
void Usb_Device_NextSet(JNIEnv *env, jobject dev, jobject next);
|
||||
void Usb_Device_PrevSet(JNIEnv *env, jobject dev, jobject prev);
|
||||
|
||||
#endif /* USBDEVICE_H_ */
|
||||
Reference in New Issue
Block a user