Skip to content

Commit 725388d

Browse files
committed
Add more tests for Version class and removed Comparable stuff.
There can be only one Version state (The current libusb version) so there is never any other version to compare it to.
1 parent a0d8b16 commit 725388d

2 files changed

Lines changed: 89 additions & 33 deletions

File tree

src/main/java/org/usb4java/Version.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
package org.usb4java;
2020

21-
import org.apache.commons.lang3.builder.CompareToBuilder;
2221
import org.apache.commons.lang3.builder.EqualsBuilder;
2322
import org.apache.commons.lang3.builder.HashCodeBuilder;
2423

@@ -27,7 +26,7 @@
2726
*
2827
* @author Klaus Reimer (k@ailis.de)
2928
*/
30-
public final class Version implements Comparable<Version>
29+
public final class Version
3130
{
3231
/** The native pointer to the version structure. */
3332
private long versionPointer;
@@ -125,27 +124,6 @@ public boolean equals(final Object obj)
125124
.isEquals();
126125
}
127126

128-
@Override
129-
public int compareTo(final Version other)
130-
{
131-
if (this == other)
132-
{
133-
return 0;
134-
}
135-
if (other == null)
136-
{
137-
return 1;
138-
}
139-
140-
return new CompareToBuilder()
141-
.append(this.major(), other.major())
142-
.append(this.minor(), other.minor())
143-
.append(this.micro(), other.micro())
144-
.append(this.nano(), other.nano())
145-
.append(this.rc(), other.rc())
146-
.toComparison();
147-
}
148-
149127
@Override
150128
public String toString()
151129
{

src/test/java/org/usb4java/VersionTest.java

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
package org.usb4java;
77

8+
import static org.junit.Assert.assertFalse;
9+
import static org.junit.Assert.assertNotNull;
10+
import static org.junit.Assert.assertTrue;
811
import static org.usb4java.test.UsbAssume.assumeUsbTestsEnabled;
912
import static org.usb4java.test.UsbAssume.isUsbTestsEnabled;
1013

1114
import org.junit.After;
1215
import org.junit.Before;
1316
import org.junit.Test;
14-
import org.usb4java.LibUsb;
15-
import org.usb4java.Version;
1617

1718
/**
1819
* Tests the {@link Version} class.
@@ -21,9 +22,6 @@
2122
*/
2223
public class VersionTest
2324
{
24-
/** The test subject. */
25-
private Version version;
26-
2725
/**
2826
* Setup test.
2927
*/
@@ -33,7 +31,6 @@ public void setUp()
3331
if (isUsbTestsEnabled())
3432
{
3533
LibUsb.init(null);
36-
this.version = new Version();
3734
}
3835
}
3936

@@ -49,14 +46,34 @@ public void tearDown()
4946
}
5047
}
5148

49+
/**
50+
* Tests the {@link Version#major()} method.
51+
*/
52+
@Test
53+
public void testMajor()
54+
{
55+
assumeUsbTestsEnabled();
56+
assertTrue(LibUsb.getVersion().major() > 0);
57+
}
58+
5259
/**
5360
* Tests uninitialized access to {@link Version#major()}
5461
*/
5562
@Test(expected = IllegalStateException.class)
5663
public void testUninitializedMajor()
5764
{
5865
assumeUsbTestsEnabled();
59-
this.version.major();
66+
new Version().major();
67+
}
68+
69+
/**
70+
* Tests the {@link Version#minor()} method.
71+
*/
72+
@Test
73+
public void testMinor()
74+
{
75+
assumeUsbTestsEnabled();
76+
assertTrue(LibUsb.getVersion().minor() >= 0);
6077
}
6178

6279
/**
@@ -66,7 +83,17 @@ public void testUninitializedMajor()
6683
public void testUninitializedMinor()
6784
{
6885
assumeUsbTestsEnabled();
69-
this.version.minor();
86+
new Version().minor();
87+
}
88+
89+
/**
90+
* Tests the {@link Version#micro()} method.
91+
*/
92+
@Test
93+
public void testMicro()
94+
{
95+
assumeUsbTestsEnabled();
96+
assertTrue(LibUsb.getVersion().micro() >= 0);
7097
}
7198

7299
/**
@@ -76,7 +103,17 @@ public void testUninitializedMinor()
76103
public void testUninitializedMicro()
77104
{
78105
assumeUsbTestsEnabled();
79-
this.version.micro();
106+
new Version().micro();
107+
}
108+
109+
/**
110+
* Tests the {@link Version#micro()} method.
111+
*/
112+
@Test
113+
public void testRc()
114+
{
115+
assumeUsbTestsEnabled();
116+
assertNotNull(LibUsb.getVersion().rc());
80117
}
81118

82119
/**
@@ -86,6 +123,47 @@ public void testUninitializedMicro()
86123
public void testUninitializedRc()
87124
{
88125
assumeUsbTestsEnabled();
89-
this.version.rc();
126+
new Version().rc();
127+
}
128+
129+
/**
130+
* Tests the {@link Version#equals(Object)} method. This equals test
131+
* is not complete because we can't generate a version object with a
132+
* different LibUSB version.
133+
*/
134+
@Test
135+
public void testEquals()
136+
{
137+
assumeUsbTestsEnabled();
138+
Version version = LibUsb.getVersion();
139+
assertTrue(version.equals(version));
140+
assertTrue(version.equals(LibUsb.getVersion()));
141+
assertFalse(version.equals(null));
142+
assertFalse(version.equals(""));
143+
}
144+
145+
/**
146+
* Tests the {@link Version#hashCode()} method. This test is not complete
147+
* because we can't generate a version object with a different LibUSB
148+
* version. So we just check that it doesn't crash.
149+
*/
150+
@Test
151+
public void testHashCode()
152+
{
153+
assumeUsbTestsEnabled();
154+
Version version = LibUsb.getVersion();
155+
version.hashCode();
156+
}
157+
158+
/**
159+
* Tests the {@link Version#toString()} method
160+
*/
161+
@Test
162+
public void testToString()
163+
{
164+
assumeUsbTestsEnabled();
165+
Version version = LibUsb.getVersion();
166+
assertNotNull(version.toString());
167+
assertTrue(version.toString().length() > 0);
90168
}
91169
}

0 commit comments

Comments
 (0)