Skip to content

Commit 626a7c3

Browse files
committed
Fix toString method of SimpleUsbDeviceDescriptor class
1 parent c3dfa0e commit 626a7c3

File tree

2 files changed

+46
-121
lines changed

2 files changed

+46
-121
lines changed

src/main/java/org/usb4java/javax/descriptors/SimpleUsbDeviceDescriptor.java

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -257,18 +257,6 @@ public boolean equals(final Object obj)
257257

258258
@Override
259259
public String toString()
260-
{
261-
return dump(this);
262-
}
263-
264-
/**
265-
* Dumps the specified USB device descriptor into a string and returns it.
266-
*
267-
* @param descriptor
268-
* The USB device descriptor to dump.
269-
* @return The descriptor dump.
270-
*/
271-
public static String dump(final UsbDeviceDescriptor descriptor)
272260
{
273261
return String.format(
274262
"Device Descriptor:%n" +
@@ -286,20 +274,20 @@ public static String dump(final UsbDeviceDescriptor descriptor)
286274
" iProduct %17d%n" +
287275
" iSerial %18d%n" +
288276
" bNumConfigurations %7d%n",
289-
descriptor.bLength(),
290-
descriptor.bDescriptorType(),
291-
DescriptorUtils.decodeBCD(descriptor.bcdUSB()),
292-
descriptor.bDeviceClass() & 0xff,
293-
DescriptorUtils.getUSBClassName(descriptor.bDeviceClass()),
294-
descriptor.bDeviceSubClass() & 0xff,
295-
descriptor.bDeviceProtocol() & 0xff,
296-
descriptor.bMaxPacketSize0() & 0xff,
297-
String.format("0x%04x", descriptor.idVendor() & 0xffff),
298-
String.format("0x%04x", descriptor.idProduct() & 0xffff),
299-
DescriptorUtils.decodeBCD(descriptor.bcdDevice()),
300-
descriptor.iManufacturer() & 0xff,
301-
descriptor.iProduct() & 0xff,
302-
descriptor.iSerialNumber() & 0xff,
303-
descriptor.bNumConfigurations() & 0xff);
277+
bLength() & 0xff,
278+
bDescriptorType() & 0xff,
279+
DescriptorUtils.decodeBCD(bcdUSB()),
280+
bDeviceClass() & 0xff,
281+
DescriptorUtils.getUSBClassName(bDeviceClass()),
282+
bDeviceSubClass() & 0xff,
283+
bDeviceProtocol() & 0xff,
284+
bMaxPacketSize0() & 0xff,
285+
String.format("0x%04x", idVendor() & 0xffff),
286+
String.format("0x%04x", idProduct() & 0xffff),
287+
DescriptorUtils.decodeBCD(bcdDevice()),
288+
iManufacturer() & 0xff,
289+
iProduct() & 0xff,
290+
iSerialNumber() & 0xff,
291+
bNumConfigurations() & 0xff);
304292
}
305293
}

src/test/java/org/usb4java/javax/descriptors/SimpleUsbDeviceDescriptorTest.java

Lines changed: 31 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77

88
import static org.junit.Assert.assertEquals;
99
import static org.junit.Assert.assertFalse;
10-
import static org.junit.Assert.assertNotEquals;
1110
import static org.junit.Assert.assertTrue;
1211

1312
import org.junit.BeforeClass;
1413
import org.junit.Test;
15-
import org.usb4java.javax.descriptors.SimpleUsbDeviceDescriptor;
1614

1715
/**
1816
* Tests the {@link SimpleUsbDeviceDescriptor}.
@@ -25,46 +23,46 @@ public class SimpleUsbDeviceDescriptorTest
2523
private static SimpleUsbDeviceDescriptor descriptor;
2624

2725
/** Value for {@link SimpleUsbDeviceDescriptor#bLength()}. */
28-
private static final byte LENGTH = 1;
26+
private static final byte LENGTH = (byte) 0xff;
2927

3028
/** Value for {@link SimpleUsbDeviceDescriptor#bDescriptorType()}. */
31-
private static final byte DESCRIPTOR_TYPE = 2;
29+
private static final byte DESCRIPTOR_TYPE = (byte) 0xfe;
3230

3331
/** Value for {@link SimpleUsbDeviceDescriptor#bcdUSB()}. */
34-
private static final short USB = 3;
32+
private static final short USB = (short) 0xffff;
3533

3634
/** Value for {@link SimpleUsbDeviceDescriptor#bDeviceClass()}. */
37-
private static final byte DEVICE_CLASS = 4;
35+
private static final byte DEVICE_CLASS = (byte) 0xfd;
3836

3937
/** Value for {@link SimpleUsbDeviceDescriptor#bDeviceSubClass()}. */
40-
private static final byte DEVICE_SUB_CLASS = 5;
38+
private static final byte DEVICE_SUB_CLASS = (byte) 0xfc;
4139

4240
/** Value for {@link SimpleUsbDeviceDescriptor#bDeviceProtocol()}. */
43-
private static final byte DEVICE_PROTOCOL = 6;
41+
private static final byte DEVICE_PROTOCOL = (byte) 0xfb;
4442

4543
/** Value for {@link SimpleUsbDeviceDescriptor#bMaxPacketSize0()}. */
46-
private static final byte MAX_PACKET_SIZE0 = 7;
44+
private static final byte MAX_PACKET_SIZE0 = (byte) 0xfa;
4745

4846
/** Value for {@link SimpleUsbDeviceDescriptor#idVendor()}. */
49-
private static final short ID_VENDOR = 8;
47+
private static final short ID_VENDOR = (short) 0xfffe;
5048

5149
/** Value for {@link SimpleUsbDeviceDescriptor#idProduct()}. */
52-
private static final short ID_PRODUCT = 9;
50+
private static final short ID_PRODUCT = (short) 0xfffd;
5351

5452
/** Value for {@link SimpleUsbDeviceDescriptor#bcdDevice()}. */
55-
private static final short DEVICE = 10;
53+
private static final short DEVICE = (short) 0xfffc;
5654

5755
/** Value for {@link SimpleUsbDeviceDescriptor#iManufacturer()}. */
58-
private static final byte MANUFACTURER = 11;
56+
private static final byte MANUFACTURER = (byte) 0xf9;
5957

6058
/** Value for {@link SimpleUsbDeviceDescriptor#iProduct()}. */
61-
private static final byte PRODUCT = 12;
59+
private static final byte PRODUCT = (byte) 0xf8;
6260

6361
/** Value for {@link SimpleUsbDeviceDescriptor#iSerialNumber()}. */
64-
private static final byte SERIAL_NUMBER = 13;
62+
private static final byte SERIAL_NUMBER = (byte) 0xf7;
6563

6664
/** Value for {@link SimpleUsbDeviceDescriptor#bNumConfigurations()}. */
67-
private static final byte NUM_CONFIGURATIONS = 14;
65+
private static final byte NUM_CONFIGURATIONS = (byte) 0xf6;
6866

6967
/** A wrong value for equality test. */
7068
private static final byte WRONG = 0;
@@ -206,10 +204,10 @@ public void testHashCode()
206204
{
207205
final int code = descriptor.hashCode();
208206
assertEquals(code, descriptor.hashCode());
209-
assertEquals(code,new SimpleUsbDeviceDescriptor(
207+
assertEquals(code, new SimpleUsbDeviceDescriptor(
210208
LENGTH, DESCRIPTOR_TYPE, USB, DEVICE_CLASS, DEVICE_SUB_CLASS,
211209
DEVICE_PROTOCOL, MAX_PACKET_SIZE0, ID_VENDOR, ID_PRODUCT, DEVICE,
212-
MANUFACTURER, PRODUCT, SERIAL_NUMBER,
210+
MANUFACTURER, PRODUCT, SERIAL_NUMBER,
213211
NUM_CONFIGURATIONS).hashCode());
214212
}
215213

@@ -290,81 +288,20 @@ public void testEquals()
290288
@Test
291289
public void testToString()
292290
{
293-
assertEquals(descriptor.toString(), descriptor.toString());
294-
assertEquals(descriptor.toString(), new SimpleUsbDeviceDescriptor(
295-
LENGTH, DESCRIPTOR_TYPE, USB, DEVICE_CLASS, DEVICE_SUB_CLASS,
296-
DEVICE_PROTOCOL, MAX_PACKET_SIZE0, ID_VENDOR, ID_PRODUCT, DEVICE,
297-
MANUFACTURER, PRODUCT, SERIAL_NUMBER,
298-
NUM_CONFIGURATIONS).toString());
299-
assertNotEquals(descriptor.toString(), new SimpleUsbDeviceDescriptor(
300-
WRONG, DESCRIPTOR_TYPE, USB, DEVICE_CLASS, DEVICE_SUB_CLASS,
301-
DEVICE_PROTOCOL, MAX_PACKET_SIZE0, ID_VENDOR, ID_PRODUCT, DEVICE,
302-
MANUFACTURER, PRODUCT, SERIAL_NUMBER, NUM_CONFIGURATIONS)
303-
.toString());
304-
assertNotEquals(descriptor.toString(), new SimpleUsbDeviceDescriptor(
305-
LENGTH, WRONG, USB, DEVICE_CLASS, DEVICE_SUB_CLASS,
306-
DEVICE_PROTOCOL, MAX_PACKET_SIZE0, ID_VENDOR, ID_PRODUCT, DEVICE,
307-
MANUFACTURER, PRODUCT, SERIAL_NUMBER, NUM_CONFIGURATIONS)
308-
.toString());
309-
assertNotEquals(descriptor.toString(), new SimpleUsbDeviceDescriptor(
310-
LENGTH, DESCRIPTOR_TYPE, WRONG, DEVICE_CLASS, DEVICE_SUB_CLASS,
311-
DEVICE_PROTOCOL, MAX_PACKET_SIZE0, ID_VENDOR, ID_PRODUCT, DEVICE,
312-
MANUFACTURER, PRODUCT, SERIAL_NUMBER, NUM_CONFIGURATIONS)
313-
.toString());
314-
assertNotEquals(descriptor.toString(), new SimpleUsbDeviceDescriptor(
315-
LENGTH, DESCRIPTOR_TYPE, USB, WRONG, DEVICE_SUB_CLASS,
316-
DEVICE_PROTOCOL, MAX_PACKET_SIZE0, ID_VENDOR, ID_PRODUCT, DEVICE,
317-
MANUFACTURER, PRODUCT, SERIAL_NUMBER, NUM_CONFIGURATIONS)
318-
.toString());
319-
assertNotEquals(descriptor.toString(), new SimpleUsbDeviceDescriptor(
320-
LENGTH, DESCRIPTOR_TYPE, USB, DEVICE_CLASS, WRONG,
321-
DEVICE_PROTOCOL, MAX_PACKET_SIZE0, ID_VENDOR, ID_PRODUCT, DEVICE,
322-
MANUFACTURER, PRODUCT, SERIAL_NUMBER, NUM_CONFIGURATIONS)
323-
.toString());
324-
assertNotEquals(descriptor.toString(), new SimpleUsbDeviceDescriptor(
325-
LENGTH, DESCRIPTOR_TYPE, USB, DEVICE_CLASS, DEVICE_SUB_CLASS,
326-
WRONG, MAX_PACKET_SIZE0, ID_VENDOR, ID_PRODUCT, DEVICE,
327-
MANUFACTURER, PRODUCT, SERIAL_NUMBER, NUM_CONFIGURATIONS)
328-
.toString());
329-
assertNotEquals(descriptor.toString(), new SimpleUsbDeviceDescriptor(
330-
LENGTH, DESCRIPTOR_TYPE, USB, DEVICE_CLASS, DEVICE_SUB_CLASS,
331-
DEVICE_PROTOCOL, WRONG, ID_VENDOR, ID_PRODUCT, DEVICE,
332-
MANUFACTURER, PRODUCT, SERIAL_NUMBER, NUM_CONFIGURATIONS)
333-
.toString());
334-
assertNotEquals(descriptor.toString(), new SimpleUsbDeviceDescriptor(
335-
LENGTH, DESCRIPTOR_TYPE, USB, DEVICE_CLASS, DEVICE_SUB_CLASS,
336-
DEVICE_PROTOCOL, MAX_PACKET_SIZE0, WRONG, ID_PRODUCT, DEVICE,
337-
MANUFACTURER, PRODUCT, SERIAL_NUMBER, NUM_CONFIGURATIONS)
338-
.toString());
339-
assertNotEquals(descriptor.toString(), new SimpleUsbDeviceDescriptor(
340-
LENGTH, DESCRIPTOR_TYPE, USB, DEVICE_CLASS, DEVICE_SUB_CLASS,
341-
DEVICE_PROTOCOL, MAX_PACKET_SIZE0, ID_VENDOR, WRONG, DEVICE,
342-
MANUFACTURER, PRODUCT, SERIAL_NUMBER, NUM_CONFIGURATIONS)
343-
.toString());
344-
assertNotEquals(descriptor.toString(), new SimpleUsbDeviceDescriptor(
345-
LENGTH, DESCRIPTOR_TYPE, USB, DEVICE_CLASS, DEVICE_SUB_CLASS,
346-
DEVICE_PROTOCOL, MAX_PACKET_SIZE0, ID_VENDOR, ID_PRODUCT, WRONG,
347-
MANUFACTURER, PRODUCT, SERIAL_NUMBER, NUM_CONFIGURATIONS)
348-
.toString());
349-
assertNotEquals(descriptor.toString(), new SimpleUsbDeviceDescriptor(
350-
LENGTH, DESCRIPTOR_TYPE, USB, DEVICE_CLASS, DEVICE_SUB_CLASS,
351-
DEVICE_PROTOCOL, MAX_PACKET_SIZE0, ID_VENDOR, ID_PRODUCT, DEVICE,
352-
WRONG, PRODUCT, SERIAL_NUMBER, NUM_CONFIGURATIONS)
353-
.toString());
354-
assertNotEquals(descriptor.toString(), new SimpleUsbDeviceDescriptor(
355-
LENGTH, DESCRIPTOR_TYPE, USB, DEVICE_CLASS, DEVICE_SUB_CLASS,
356-
DEVICE_PROTOCOL, MAX_PACKET_SIZE0, ID_VENDOR, ID_PRODUCT, DEVICE,
357-
MANUFACTURER, WRONG, SERIAL_NUMBER, NUM_CONFIGURATIONS)
358-
.toString());
359-
assertNotEquals(descriptor.toString(), new SimpleUsbDeviceDescriptor(
360-
LENGTH, DESCRIPTOR_TYPE, USB, DEVICE_CLASS, DEVICE_SUB_CLASS,
361-
DEVICE_PROTOCOL, MAX_PACKET_SIZE0, ID_VENDOR, ID_PRODUCT, DEVICE,
362-
MANUFACTURER, PRODUCT, WRONG, NUM_CONFIGURATIONS)
363-
.toString());
364-
assertNotEquals(descriptor.toString(), new SimpleUsbDeviceDescriptor(
365-
LENGTH, DESCRIPTOR_TYPE, USB, DEVICE_CLASS, DEVICE_SUB_CLASS,
366-
DEVICE_PROTOCOL, MAX_PACKET_SIZE0, ID_VENDOR, ID_PRODUCT, DEVICE,
367-
MANUFACTURER, PRODUCT, SERIAL_NUMBER, WRONG)
368-
.toString());
291+
assertEquals(String.format("Device Descriptor:%n"
292+
+ " bLength 255%n"
293+
+ " bDescriptorType 254%n"
294+
+ " bcdUSB ff.ff%n"
295+
+ " bDeviceClass 253 Unknown%n"
296+
+ " bDeviceSubClass 252%n"
297+
+ " bDeviceProtocol 251%n"
298+
+ " bMaxPacketSize0 250%n"
299+
+ " idVendor 0xfffe%n"
300+
+ " idProduct 0xfffd%n"
301+
+ " bcdDevice ff.fc%n"
302+
+ " iManufacturer 249%n"
303+
+ " iProduct 248%n"
304+
+ " iSerial 247%n"
305+
+ " bNumConfigurations 246%n"), descriptor.toString());
369306
}
370307
}

0 commit comments

Comments
 (0)