- release resources and clear device handle

- tests for invalid parameters

git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@227 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
schlaepfer
2006-12-06 11:20:45 +00:00
parent f41c33f20d
commit 07eab220ce
2 changed files with 81 additions and 11 deletions

View File

@@ -241,6 +241,7 @@ public class Device {
if (usbDevHandle <= 0) {
throw new USBException("invalid device handle");
}
release_interface(usbDevHandle, dev_interface);
if (LibusbJava.usb_reset(usbDevHandle) < 0) {
usbDevHandle = 0;
throw new USBException("LibusbWin.usb_reset: "
@@ -511,15 +512,23 @@ public class Device {
private void claim_interface(int usb_dev_handle, int configuration,
int interface_, int altinterface) throws USBException {
if (LibusbJava.usb_set_configuration(usb_dev_handle, configuration) < 0) {
usbDevHandle = 0;
throw new USBException("LibusbWin.usb_set_configuration: "
+ LibusbJava.usb_strerror());
}
if (LibusbJava.usb_claim_interface(usb_dev_handle, interface_) < 0) {
usbDevHandle = 0;
throw new USBException("LibusbWin.usb_claim_interface: "
+ LibusbJava.usb_strerror());
}
if (altinterface >= 0) {
if (LibusbJava.usb_set_altinterface(usb_dev_handle, altinterface) < 0) {
try {
release_interface(usb_dev_handle, interface_);
} catch (USBException e) {
// ignore
}
usbDevHandle = 0;
throw new USBException("LibusbWin.usb_set_altinterface: "
+ LibusbJava.usb_strerror());
}

View File

@@ -1,6 +1,6 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <libusb@drip.ch>
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <libusb@drip.ch>
*
* This library is covered by the LGPL, read LGPL.txt for details.
*/
@@ -8,6 +8,7 @@ package ch.ntb.usb.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.FileInputStream;
import java.io.InputStream;
@@ -88,8 +89,14 @@ public class DeviceTest {
@Test(expected = USBException.class)
public void testClose() throws Exception {
doOpen();
doClose();
try {
// this calls must not throw an exception
doOpen();
doClose();
} catch (USBException e) {
e.printStackTrace();
fail(e.getMessage());
}
// this call must throw an exception, because the device is closed
dev.writeBulk(devinfo.getOutEPBulk(), testData, testData.length,
devinfo.getTimeout(), false);
@@ -97,9 +104,15 @@ public class DeviceTest {
@Test(expected = USBException.class)
public void testReset1() throws Exception {
doOpen();
dev.reset();
timeout();
try {
// this calls must not throw an exception
doOpen();
dev.reset();
timeout();
} catch (USBException e) {
e.printStackTrace();
fail(e.getMessage());
}
// this call must throw an exception, because the device is closed
dev.writeBulk(devinfo.getOutEPBulk(), testData, testData.length,
devinfo.getTimeout(), false);
@@ -107,9 +120,15 @@ public class DeviceTest {
@Test(expected = USBException.class)
public void testReset2() throws Exception {
doOpen();
dev.reset();
timeout();
try {
// this calls must not throw an exception
doOpen();
dev.reset();
timeout();
} catch (USBException e) {
e.printStackTrace();
fail(e.getMessage());
}
// this call must throw an exception, because the device can't be closed
doClose();
}
@@ -182,6 +201,48 @@ public class DeviceTest {
}
}
@Test
public void invalidConfig() throws Exception {
try {
dev.open(devinfo.getConfiguration() + 5, devinfo.getInterface(),
devinfo.getAltinterface());
fail("USBException expected");
} catch (USBException e) {
System.err.println("INFO: " + getClass()
+ ": error expected: could not set config "
+ (devinfo.getConfiguration() + 5));
}
doOpenWriteReadClose();
}
@Test
public void invalidInterface() throws Exception {
try {
dev.open(devinfo.getConfiguration(), devinfo.getInterface() + 5,
devinfo.getAltinterface());
fail("USBException expected");
} catch (USBException e) {
System.err.println("INFO: " + getClass()
+ ": error expected: could not claim interface "
+ (devinfo.getInterface() + 5));
}
doOpenWriteReadClose();
}
@Test
public void invalidAltinterface() throws Exception {
try {
dev.open(devinfo.getConfiguration(), devinfo.getInterface(),
devinfo.getAltinterface() + 5);
fail("USBException expected");
} catch (USBException e) {
System.err.println("INFO: " + getClass()
+ ": error expected: could not set alt interface "
+ (devinfo.getAltinterface() + 5));
}
doOpenWriteReadClose();
}
@Test
public void testGetIdProduct() {
Assert.assertEquals(dev.getIdProduct(), devinfo.getIdProduct());
@@ -247,8 +308,8 @@ public class DeviceTest {
} else if (devinfo.getMode().equals(TransferMode.Interrupt)) {
dev.writeInterrupt(devinfo.getOutEPInt(), testData,
testData.length, devinfo.getTimeout(), false);
dev.readInterrupt(devinfo.getInEPInt(), readData,
readData.length, devinfo.getTimeout(), false);
dev.readInterrupt(devinfo.getInEPInt(), readData, readData.length,
devinfo.getTimeout(), false);
}
compare(testData, readData);
}