Add unit test for Interface and catch uninitialized usage of it.

This commit is contained in:
Klaus Reimer 2013-07-13 20:01:18 +02:00
parent 60d8c7c904
commit e234b9a75f
3 changed files with 75 additions and 7 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -0,0 +1,65 @@
/*
* Copyright (C) 2013 Klaus Reimer <k@ailis.de>
* 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();
}
}