diff --git a/src/main/c/src/Interface.c b/src/main/c/src/Interface.c index b68d191..7598150 100644 --- a/src/main/c/src/Interface.c +++ b/src/main/c/src/Interface.c @@ -8,7 +8,7 @@ jobject wrapInterface(JNIEnv *env, const struct libusb_interface *iface) { - WRAP_POINTER(env, iface, "Interface", "pointer"); + WRAP_POINTER(env, iface, "Interface", "interfacePointer"); } jobjectArray wrapInterfaces(JNIEnv *env, int count, @@ -27,7 +27,7 @@ jobjectArray wrapInterfaces(JNIEnv *env, int count, struct libusb_interface *unwrapInterface(JNIEnv *env, jobject obj) { - UNWRAP_POINTER(env, obj, struct libusb_interface*, "pointer"); + UNWRAP_POINTER(env, obj, struct libusb_interface*, "interfacePointer"); } JNIEXPORT jshort JNICALL METHOD_NAME(Interface, numAltsetting) @@ -35,7 +35,9 @@ JNIEXPORT jshort JNICALL METHOD_NAME(Interface, numAltsetting) JNIEnv *env, jobject this ) { - return (jshort) unwrapInterface(env, this)->num_altsetting; + struct libusb_interface* interface = unwrapInterface(env, this); + if (!interface) return 0; + return (jshort) interface->num_altsetting; } JNIEXPORT jobjectArray JNICALL METHOD_NAME(Interface, altsetting) @@ -44,6 +46,7 @@ JNIEXPORT jobjectArray JNICALL METHOD_NAME(Interface, altsetting) ) { struct libusb_interface* interface = unwrapInterface(env, this); + if (!interface) return NULL; return wrapInterfaceDescriptors(env, interface->num_altsetting, interface->altsetting); } diff --git a/src/main/java/de/ailis/usb4java/libusb/Interface.java b/src/main/java/de/ailis/usb4java/libusb/Interface.java index b86bb56..71753a7 100644 --- a/src/main/java/de/ailis/usb4java/libusb/Interface.java +++ b/src/main/java/de/ailis/usb4java/libusb/Interface.java @@ -21,7 +21,7 @@ import org.apache.commons.lang3.builder.HashCodeBuilder; public final class Interface { /** The native pointer to the descriptor structure. */ - private long pointer; + private long interfacePointer; /** * Package-private constructor to prevent manual instantiation. Interfaces @@ -39,7 +39,7 @@ public final class Interface */ public long getPointer() { - return this.pointer; + return this.interfacePointer; } /** @@ -88,7 +88,7 @@ public final class Interface @Override public int hashCode() { - return new HashCodeBuilder().append(this.pointer).toHashCode(); + return new HashCodeBuilder().append(this.interfacePointer).toHashCode(); } @Override @@ -97,7 +97,7 @@ public final class Interface if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; final Interface other = (Interface) obj; - return this.pointer == other.pointer; + return this.interfacePointer == other.interfacePointer; } @Override diff --git a/src/test/java/de/ailis/usb4java/libusb/InterfaceTest.java b/src/test/java/de/ailis/usb4java/libusb/InterfaceTest.java new file mode 100644 index 0000000..9fa5116 --- /dev/null +++ b/src/test/java/de/ailis/usb4java/libusb/InterfaceTest.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2013 Klaus Reimer + * See LICENSE.md for licensing information. + */ + +package de.ailis.usb4java.libusb; + +import static de.ailis.usb4java.test.UsbAssume.assumeUsbTestsEnabled; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests the {@link Interface} class. + * + * @author Klaus Reimer (k@ailis.de) + */ +public class InterfaceTest +{ + /** The test subject. */ + private Interface descriptor; + + /** + * Setup test. + */ + @Before + public void setUp() + { + assumeUsbTestsEnabled(); + LibUsb.init(null); + this.descriptor = new Interface(); + } + + /** + * Tear down test. + */ + @After + public void tearDown() + { + LibUsb.exit(null); + } + + /** + * Tests uninitialized access to + * {@link Interface#altsetting()} + */ + @Test(expected = IllegalStateException.class) + public void testUninitializedAltsetting() + { + assumeUsbTestsEnabled(); + this.descriptor.altsetting(); + } + + /** + * Tests uninitialized access to + * {@link Interface#numAltsetting()} + */ + @Test(expected = IllegalStateException.class) + public void testUninitializedDescriptorType() + { + assumeUsbTestsEnabled(); + this.descriptor.numAltsetting(); + } +}