Skip to content

Commit c3dfa0e

Browse files
committed
Fix toString method of SimpleUsbConfigurationDescriptor class
1 parent e23e40e commit c3dfa0e

File tree

2 files changed

+40
-75
lines changed

2 files changed

+40
-75
lines changed

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

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,6 @@ public boolean equals(final Object obj)
165165

166166
@Override
167167
public String toString()
168-
{
169-
return dump(this);
170-
}
171-
172-
/**
173-
* Dumps the specified USB configuration descriptor into a string and
174-
* returns it.
175-
*
176-
* @param descriptor
177-
* The USB configuration descriptor to dump.
178-
* @return The descriptor dump.
179-
*/
180-
public static String dump(final UsbConfigurationDescriptor descriptor)
181168
{
182169
return String.format(
183170
"Configuration Descriptor:%n" +
@@ -191,17 +178,17 @@ public static String dump(final UsbConfigurationDescriptor descriptor)
191178
" %s%n" +
192179
"%s" +
193180
" bMaxPower %16smA%n",
194-
descriptor.bLength(),
195-
descriptor.bDescriptorType(),
196-
descriptor.wTotalLength() & 0xffff,
197-
descriptor.bNumInterfaces() & 0xff,
198-
descriptor.bConfigurationValue() & 0xff,
199-
descriptor.iConfiguration() & 0xff,
200-
String.format("0x%02x", descriptor.bmAttributes() & 0xff),
201-
((descriptor.bmAttributes() & 64) == 0) ? "(Bus Powered)"
181+
bLength() & 0xff,
182+
bDescriptorType() & 0xff,
183+
wTotalLength() & 0xffff,
184+
bNumInterfaces() & 0xff,
185+
bConfigurationValue() & 0xff,
186+
iConfiguration() & 0xff,
187+
String.format("0x%02x", bmAttributes() & 0xff),
188+
((bmAttributes() & 64) == 0) ? "(Bus Powered)"
202189
: "Self Powered",
203-
((descriptor.bmAttributes() & 32) == 0) ? ""
190+
((bmAttributes() & 32) == 0) ? ""
204191
: String.format(" Remote Wakeup%n"),
205-
(descriptor.bMaxPower() & 0xff) * 2);
192+
(bMaxPower() & 0xff) * 2);
206193
}
207194
}

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

Lines changed: 30 additions & 52 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.SimpleUsbConfigurationDescriptor;
1614

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

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

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

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

3634
/** Value for {@link SimpleUsbConfigurationDescriptor#bNumInterfaces()}. */
37-
private static final byte NUM_INTERFACES = 4;
35+
private static final byte NUM_INTERFACES = (byte) 0xfc;
3836

3937
/**
4038
* Value for {@link SimpleUsbConfigurationDescriptor#bConfigurationValue()}.
4139
*/
42-
private static final byte CONFIGURATION_VALUE = 5;
40+
private static final byte CONFIGURATION_VALUE = (byte) 0xfb;
4341

4442
/** Value for {@link SimpleUsbConfigurationDescriptor#iConfiguration()}. */
45-
private static final byte CONFIGURATION = 6;
43+
private static final byte CONFIGURATION = (byte) 0xfa;
4644

4745
/** Value for {@link SimpleUsbConfigurationDescriptor#bmAttributes()}. */
48-
private static final byte ATTRIBUTES = 7;
46+
private static final byte ATTRIBUTES = (byte) 0xf9;
4947

5048
/** Value for {@link SimpleUsbConfigurationDescriptor#bMaxPower()}. */
51-
private static final byte MAX_POWER = 8;
49+
private static final byte MAX_POWER = (byte) 0xf8;
5250

5351
/** A wrong value for equality test. */
5452
private static final byte WRONG = 0;
@@ -150,7 +148,7 @@ public void testHashCode()
150148
assertEquals(code, descriptor.hashCode());
151149
assertEquals(code, new SimpleUsbConfigurationDescriptor(
152150
LENGTH, DESCRIPTOR_TYPE, TOTAL_LENGTH, NUM_INTERFACES,
153-
CONFIGURATION_VALUE, CONFIGURATION, ATTRIBUTES,
151+
CONFIGURATION_VALUE, CONFIGURATION, ATTRIBUTES,
154152
MAX_POWER).hashCode());
155153
}
156154

@@ -198,50 +196,30 @@ public void testEquals()
198196
@Test
199197
public void testToString()
200198
{
201-
assertEquals(descriptor.toString(), descriptor.toString());
202-
assertEquals(descriptor.toString(),
203-
new SimpleUsbConfigurationDescriptor(
204-
LENGTH, DESCRIPTOR_TYPE, TOTAL_LENGTH, NUM_INTERFACES,
205-
CONFIGURATION_VALUE, CONFIGURATION, ATTRIBUTES,
206-
MAX_POWER).toString());
207-
assertNotEquals(descriptor.toString(),
208-
new SimpleUsbConfigurationDescriptor(
209-
WRONG, DESCRIPTOR_TYPE, TOTAL_LENGTH, NUM_INTERFACES,
210-
CONFIGURATION_VALUE, CONFIGURATION, ATTRIBUTES, MAX_POWER)
211-
.toString());
212-
assertNotEquals(descriptor.toString(),
213-
new SimpleUsbConfigurationDescriptor(
214-
LENGTH, WRONG, TOTAL_LENGTH, NUM_INTERFACES,
215-
CONFIGURATION_VALUE, CONFIGURATION, ATTRIBUTES, MAX_POWER)
216-
.toString());
217-
assertNotEquals(descriptor.toString(),
218-
new SimpleUsbConfigurationDescriptor(
219-
LENGTH, DESCRIPTOR_TYPE, WRONG, NUM_INTERFACES,
220-
CONFIGURATION_VALUE, CONFIGURATION, ATTRIBUTES, MAX_POWER)
221-
.toString());
222-
assertNotEquals(descriptor.toString(),
223-
new SimpleUsbConfigurationDescriptor(
224-
LENGTH, DESCRIPTOR_TYPE, TOTAL_LENGTH, WRONG,
225-
CONFIGURATION_VALUE, CONFIGURATION, ATTRIBUTES, MAX_POWER)
226-
.toString());
227-
assertNotEquals(descriptor.toString(),
228-
new SimpleUsbConfigurationDescriptor(
229-
LENGTH, DESCRIPTOR_TYPE, TOTAL_LENGTH, NUM_INTERFACES,
230-
WRONG, CONFIGURATION, ATTRIBUTES, MAX_POWER).toString());
231-
assertNotEquals(descriptor.toString(),
232-
new SimpleUsbConfigurationDescriptor(
233-
LENGTH, DESCRIPTOR_TYPE, TOTAL_LENGTH, NUM_INTERFACES,
234-
CONFIGURATION_VALUE, WRONG, ATTRIBUTES, MAX_POWER)
235-
.toString());
236-
assertNotEquals(descriptor.toString(),
199+
assertEquals(String.format("Configuration Descriptor:%n"
200+
+ " bLength 255%n"
201+
+ " bDescriptorType 254%n"
202+
+ " wTotalLength 65535%n"
203+
+ " bNumInterfaces 252%n"
204+
+ " bConfigurationValue 251%n"
205+
+ " iConfiguration 250%n"
206+
+ " bmAttributes 0xf9%n"
207+
+ " Self Powered%n"
208+
+ " Remote Wakeup%n"
209+
+ " bMaxPower 496mA%n"), descriptor.toString());
210+
assertEquals(String.format("Configuration Descriptor:%n"
211+
+ " bLength 255%n"
212+
+ " bDescriptorType 254%n"
213+
+ " wTotalLength 65535%n"
214+
+ " bNumInterfaces 252%n"
215+
+ " bConfigurationValue 251%n"
216+
+ " iConfiguration 250%n"
217+
+ " bmAttributes 0x00%n"
218+
+ " (Bus Powered)%n"
219+
+ " bMaxPower 496mA%n"),
237220
new SimpleUsbConfigurationDescriptor(
238221
LENGTH, DESCRIPTOR_TYPE, TOTAL_LENGTH, NUM_INTERFACES,
239222
CONFIGURATION_VALUE, CONFIGURATION, WRONG, MAX_POWER)
240223
.toString());
241-
assertNotEquals(descriptor.toString(),
242-
new SimpleUsbConfigurationDescriptor(
243-
LENGTH, DESCRIPTOR_TYPE, TOTAL_LENGTH, NUM_INTERFACES,
244-
CONFIGURATION_VALUE, CONFIGURATION, ATTRIBUTES, WRONG)
245-
.toString());
246224
}
247225
}

0 commit comments

Comments
 (0)