null pointer exception when calling setManufacturer with filename fixed

java doc updates
new test for many read/write operations

git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@271 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
spandi
2008-12-20 09:25:05 +00:00
parent fae4615b79
commit 712b5205fe
8 changed files with 252 additions and 23 deletions

View File

@@ -60,7 +60,7 @@
<target name="javadoc" description="--> Create Javadoc">
<javadoc destdir="${project.dir.doc}" encoding="iso-8859-1">
<fileset dir="${project.dir.src}" defaultexcludes="yes" />
<fileset dir="${project.dir.src}" includes="**/*.java" />
</javadoc>
</target>
</project>

View File

@@ -170,12 +170,15 @@ public class Device {
* endpoints are found in the descriptors an exception is thrown.
*
* @param configuration
* the configuration
* the configuration, see
* {@link Usb_Config_Descriptor#getBConfigurationValue()}
* @param interface_
* the interface
* the interface, see
* {@link Usb_Interface_Descriptor#getBInterfaceNumber()}
* @param altinterface
* the alternate interface. If no alternate interface must be set
* <i>-1</i> can be used.
* the alternate interface, see
* {@link Usb_Interface_Descriptor#getBAlternateSetting()}. If
* no alternate interface must be set <i>-1</i> can be used.
* @throws USBException
*/
public void open(int configuration, int interface_, int altinterface)

View File

@@ -203,7 +203,7 @@ public class USB {
*
* @param idVendor
* @param idProduct
* @return
* @return a newly created device or an already registered device
*/
public static Device getDevice(short idVendor, short idProduct) {
return getDevice(idVendor, idProduct, null);
@@ -225,7 +225,8 @@ public class USB {
String filename) {
for (Iterator<Device> iter = devices.iterator(); iter.hasNext();) {
Device dev = iter.next();
if (filename != null && filename.compareTo(dev.getFilename()) == 0
if (filename != null && dev.getFilename() != null
&& filename.compareTo(dev.getFilename()) == 0
&& dev.getIdVendor() == idVendor
&& dev.getIdProduct() == idProduct) {
return dev;

View File

@@ -21,13 +21,13 @@ public abstract class AbstractDeviceInfo {
private int altinterface;
private int outEPBulk;
private int outEPBulk = -1;
private int inEPBulk;
private int inEPBulk = -1;
private int outEPInt;
private int outEPInt = -1;
private int inEPInt;
private int inEPInt = -1;
private int sleepTimeout;
@@ -37,6 +37,12 @@ public abstract class AbstractDeviceInfo {
private boolean compareData = true;
private String manufacturer = null;
private String product = null;
private String serialVersion = null;
public static enum TransferMode {
Bulk, Interrupt
}
@@ -158,4 +164,28 @@ public abstract class AbstractDeviceInfo {
public void setDoCompareData(boolean compareData) {
this.compareData = compareData;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public String getSerialVersion() {
return serialVersion;
}
public void setSerialVersion(String serialVersion) {
this.serialVersion = serialVersion;
}
}

View File

@@ -27,5 +27,8 @@ public class AT90USB1287 extends AbstractDeviceInfo {
setSleepTimeout(2000);
setMaxDataSize(USB.FULLSPEED_MAX_BULK_PACKET_SIZE);
setMode(TransferMode.Bulk);
setManufacturer("inf.ntb.ch");
setProduct("JUnit Test Board");
setSerialVersion("00.10.00");
}
}

View File

@@ -42,10 +42,6 @@ public class DeviceTest {
private static final String testdevicePropertiesFile = "testdevice.properties";
private static final String deviceInfoKey = "testdeviceInfo";
private static final String Manufacturer = "inf.ntb.ch";
private static final String Product = "JUnit Test Board";
private static final String SerialVersion = "00.10.00";
private static AbstractDeviceInfo devinfo;
private static byte[] testData;
@@ -56,7 +52,6 @@ public class DeviceTest {
private static Logger log = Logger.getLogger(DeviceTest.class.getName());
@SuppressWarnings("unchecked")
@BeforeClass
public static void setUp() throws Exception {
// load the device info class with the key
@@ -70,7 +65,7 @@ public class DeviceTest {
throw new Exception("property " + deviceInfoKey
+ " not found in file " + testdevicePropertiesFile);
}
Class devInfoClazz = Class.forName(devInfoClazzName);
Class<?> devInfoClazz = Class.forName(devInfoClazzName);
devinfo = (AbstractDeviceInfo) devInfoClazz.newInstance();
// devinfo = new CY7C68013A();
// setup test data
@@ -92,7 +87,6 @@ public class DeviceTest {
log.info(baos.toString());
}
@SuppressWarnings("null")
@Test
public void getDescriptors() throws Exception {
dev.updateDescriptors();
@@ -318,7 +312,7 @@ public class DeviceTest {
USB.REQ_GET_DESCRIPTOR, (3 << 8) + 1, 0, data, data.length,
devinfo.getTimeout(), false);
String s = getString(data, length);
assertEquals(s, Manufacturer);
assertEquals(s, devinfo.getManufacturer());
// GET DESCRIPTOR (string descriptor (2))
data = getTestData(128);
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
@@ -326,7 +320,7 @@ public class DeviceTest {
USB.REQ_GET_DESCRIPTOR, (3 << 8) + 2, 0, data, data.length,
devinfo.getTimeout(), false);
s = getString(data, length);
assertEquals(s, Product);
assertEquals(s, devinfo.getProduct());
// GET DESCRIPTOR (string descriptor (3))
data = getTestData(128);
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
@@ -334,7 +328,7 @@ public class DeviceTest {
USB.REQ_GET_DESCRIPTOR, (3 << 8) + 3, 0, data, data.length,
devinfo.getTimeout(), false);
s = getString(data, length);
assertEquals(s, SerialVersion);
assertEquals(s, devinfo.getSerialVersion());
// close the device
dev.close();
} catch (Exception e) {
@@ -404,7 +398,6 @@ public class DeviceTest {
return b;
}
@SuppressWarnings("unused")
private void logData(byte[] data, int length) {
if (length > 0) {
log.info("length: " + length);

View File

@@ -0,0 +1,199 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2008 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.usb.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Properties;
import java.util.logging.Logger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ch.ntb.usb.Device;
import ch.ntb.usb.LibusbJava;
import ch.ntb.usb.USB;
import ch.ntb.usb.USBException;
import ch.ntb.usb.Usb_Bus;
import ch.ntb.usb.Utils;
import ch.ntb.usb.testApp.AbstractDeviceInfo;
import ch.ntb.usb.testApp.AbstractDeviceInfo.TransferMode;
public class MemoryLeakTest {
private static final String testdevicePropertiesFile = "testdevice.properties";
private static final String deviceInfoKey = "testdeviceInfo";
private static AbstractDeviceInfo devinfo;
private static byte[] testData;
private static byte[] readData;
private static Device dev;
private static Logger log = Logger
.getLogger(MemoryLeakTest.class.getName());
@BeforeClass
public static void setUp() throws Exception {
// load the device info class with the key
// from 'testdevice.properties'
InputStream propInputStream = new FileInputStream(
testdevicePropertiesFile);
Properties devInfoProp = new Properties();
devInfoProp.load(propInputStream);
String devInfoClazzName = devInfoProp.getProperty(deviceInfoKey);
if (devInfoClazzName == null) {
throw new Exception("property " + deviceInfoKey
+ " not found in file " + testdevicePropertiesFile);
}
Class<?> devInfoClazz = Class.forName(devInfoClazzName);
devinfo = (AbstractDeviceInfo) devInfoClazz.newInstance();
// setup test data
testData = new byte[devinfo.getMaxDataSize()];
readData = new byte[testData.length];
// initialise the device
LibusbJava.usb_set_debug(255);
dev = USB.getDevice(devinfo.getIdVendor(), devinfo.getIdProduct());
assertNotNull(dev);
// print the devices
LibusbJava.usb_init();
LibusbJava.usb_find_busses();
LibusbJava.usb_find_devices();
Usb_Bus bus = LibusbJava.usb_get_busses();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
Utils.logBus(bus, ps);
log.info(baos.toString());
}
@Test
public void initalReset() throws Exception {
doOpen();
dev.reset();
timeout();
}
@Test
public void bulkWriteReadMultiple() throws Exception {
final int NumberOfIterations = 30000;
devinfo.setMode(TransferMode.Bulk);
doOpen();
for (int i = 0; i < NumberOfIterations; i++) {
doWriteRead();
if (i % 1000 == 0) {
System.out.print(".");
}
}
doClose();
}
@Test
public void interruptWriteReadMultiple() throws Exception {
final int NumberOfIterations = 30000;
devinfo.setMode(TransferMode.Interrupt);
doOpen();
for (int i = 0; i < NumberOfIterations; i++) {
doWriteRead();
if (i % 1000 == 0) {
System.out.print(".");
}
}
doClose();
}
private void closeOnException() {
try {
dev.close();
} catch (USBException e1) {
// ignore exceptions
}
}
@AfterClass
public static void tearDown() throws Exception {
if (dev != null && dev.isOpen()) {
dev.close();
}
}
private void doOpen() throws USBException {
dev.open(devinfo.getConfiguration(), devinfo.getInterface(), devinfo
.getAltinterface());
}
private void doClose() throws USBException {
dev.close();
}
private void doWriteRead() throws Exception {
initTestData();
try {
if (devinfo.getMode().equals(TransferMode.Bulk)) {
if (devinfo.getOutEPBulk() != -1) {
dev.writeBulk(devinfo.getOutEPBulk(), testData,
testData.length, devinfo.getTimeout(), false);
}
if (devinfo.getInEPBulk() != -1) {
dev.readBulk(devinfo.getInEPBulk(), readData,
readData.length, devinfo.getTimeout(), false);
}
} else if (devinfo.getMode().equals(TransferMode.Interrupt)) {
if (devinfo.getOutEPInt() != -1) {
dev.writeInterrupt(devinfo.getOutEPInt(), testData,
testData.length, devinfo.getTimeout(), false);
}
if (devinfo.getInEPInt() != -1) {
dev.readInterrupt(devinfo.getInEPInt(), readData,
readData.length, devinfo.getTimeout(), false);
}
}
if (devinfo.doCompareData()) {
compare(testData, readData);
}
} catch (AssertionError e) {
closeOnException();
throw e;
} catch (Exception e) {
closeOnException();
throw e;
}
}
private static void compare(byte[] d1, byte[] d2) {
final int minLength = Math.min(d1.length, d2.length);
for (int i = 0; i < minLength; i++) {
assertEquals(d1[i], d2[i]);
}
}
private static void timeout() {
try {
Thread.sleep(devinfo.getSleepTimeout());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void initTestData() {
for (int i = 0; i < testData.length; i++) {
testData[i] = (byte) (Math.random() * 0xff);
readData[i] = 0;
}
}
}

View File

@@ -1,4 +1,4 @@
#Thu Aug 24 14:28:28 CEST 2006
version.major=0
version.minor=5
version.release=7
version.release=8