From 91e2742830f470dcf0f34d7baca4f27a591c660b Mon Sep 17 00:00:00 2001 From: Klaus Reimer Date: Mon, 7 Feb 2011 19:31:18 +0100 Subject: [PATCH] Added some unit tests. --- pom.xml | 14 +++ .../java/de/ailis/usb4java/ServicesTest.java | 28 ++++++ .../LibUsbEndpointDescriptorTest.java | 94 +++++++++++++++++++ .../jni/USB_Endpoint_Descriptor_Test.java | 68 ++++++++++++++ 4 files changed, 204 insertions(+) create mode 100644 src/test/java/de/ailis/usb4java/ServicesTest.java create mode 100644 src/test/java/de/ailis/usb4java/descriptors/LibUsbEndpointDescriptorTest.java create mode 100644 src/test/java/de/ailis/usb4java/jni/USB_Endpoint_Descriptor_Test.java diff --git a/pom.xml b/pom.xml index bf55855..23af233 100644 --- a/pom.xml +++ b/pom.xml @@ -54,6 +54,7 @@ http://www.ailis.de/nexus/content/repositories/releases/de/ailis/${project.artifactId}/${project.artifactId}/${project.version} http://www.ailis.de/nexus/content/repositories/snapshots/de/ailis/${project.artifactId}/${project.artifactId} + 1.4.7 @@ -138,7 +139,20 @@ junit junit 4.8.2 + test + + org.powermock + powermock-module-junit4 + ${powermock.version} + test + + + org.powermock + powermock-api-mockito + ${powermock.version} + test + javax.usb usb diff --git a/src/test/java/de/ailis/usb4java/ServicesTest.java b/src/test/java/de/ailis/usb4java/ServicesTest.java new file mode 100644 index 0000000..70bd648 --- /dev/null +++ b/src/test/java/de/ailis/usb4java/ServicesTest.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2011 Klaus Reimer + * See LICENSE.txt for licensing information. + */ + +package de.ailis.usb4java; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + + +/** + * Tests the Services class. + * + * @author Klaus Reimer (k@ailis.de) + */ + +@RunWith(PowerMockRunner.class) +@PrepareForTest( { Services.class }) +public class ServicesTest +{ + @Test + public void testConstructor() + { + } +} diff --git a/src/test/java/de/ailis/usb4java/descriptors/LibUsbEndpointDescriptorTest.java b/src/test/java/de/ailis/usb4java/descriptors/LibUsbEndpointDescriptorTest.java new file mode 100644 index 0000000..a416c5b --- /dev/null +++ b/src/test/java/de/ailis/usb4java/descriptors/LibUsbEndpointDescriptorTest.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2011 Klaus Reimer + * See LICENSE.txt for licensing information. + */ + +package de.ailis.usb4java.descriptors; + +import static org.junit.Assert.assertEquals; +import static org.powermock.api.mockito.PowerMockito.when; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import de.ailis.usb4java.jni.USB_Descriptor_Header; +import de.ailis.usb4java.jni.USB_Endpoint_Descriptor; + + +/** + * Tests the LibUsbEndpointDescriptor class. + * + * @author Klaus Reimer (k@ailis.de) + */ + +@RunWith(PowerMockRunner.class) +@PrepareForTest( { USB_Endpoint_Descriptor.class, USB_Descriptor_Header.class }) +public class LibUsbEndpointDescriptorTest +{ + /** The descriptor to test. */ + private static LibUsbEndpointDescriptor descriptor; + + + /** + * Prepares the test subject. + */ + + @BeforeClass + public static void beforeClass() + { + final USB_Endpoint_Descriptor mock = PowerMockito.mock(USB_Endpoint_Descriptor.class); + when(mock.bEndpointAddress()).thenReturn(200); + when(mock.bmAttributes()).thenReturn(201); + when(mock.wMaxPacketSize()).thenReturn(40000); + when(mock.bInterval()).thenReturn(202); + descriptor = new LibUsbEndpointDescriptor(mock); + } + + + /** + * Tests the bEndpointAddress() method. + */ + + @Test + public void testBEndpointAddress() + { + assertEquals((byte) 200, descriptor.bEndpointAddress()); + } + + + /** + * Tests the bmAttributes() method. + */ + + @Test + public void testBmAttributes() + { + assertEquals((byte) 201, descriptor.bmAttributes()); + } + + + /** + * Tests the wMaxPacketSize() method. + */ + + @Test + public void testWMaxPacketSize() + { + assertEquals((short) 40000, descriptor.wMaxPacketSize()); + } + + + /** + * Tests the bInterval() method. + */ + + @Test + public void testBInterval() + { + assertEquals((byte) 202, descriptor.bInterval()); + } +} diff --git a/src/test/java/de/ailis/usb4java/jni/USB_Endpoint_Descriptor_Test.java b/src/test/java/de/ailis/usb4java/jni/USB_Endpoint_Descriptor_Test.java new file mode 100644 index 0000000..2a4a9fd --- /dev/null +++ b/src/test/java/de/ailis/usb4java/jni/USB_Endpoint_Descriptor_Test.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2011 Klaus Reimer + * See LICENSE.txt for licensing information. + */ + +package de.ailis.usb4java.jni; + +import static org.junit.Assert.assertNotNull; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.when; + +import java.nio.ByteBuffer; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + + +/** + * Tests the Services class. + * + * @author Klaus Reimer (k@ailis.de) + */ + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ USB_Endpoint_Descriptor.class }) +public class USB_Endpoint_Descriptor_Test +{ + /** The descriptor to test. */ + private static USB_Endpoint_Descriptor descriptor; + + + /** + * Prepares the test class. + */ + + @BeforeClass + public static void beforeClass() + { + descriptor = new USB_Endpoint_Descriptor(ByteBuffer.allocateDirect(18)); + } + + + /** + * Tests the dump method. + * @throws Exception + */ + + @Test + public void testDump() throws Exception + { + final USB_Endpoint_Descriptor descriptor = mock(USB_Endpoint_Descriptor.class); + when(descriptor.bDescriptorType()).thenReturn(1); + when(descriptor.bEndpointAddress()).thenReturn(2); + when(descriptor.bInterval()).thenReturn(3); + when(descriptor.bLength()).thenReturn(4); + when(descriptor.bmAttributes()).thenReturn(5); + when(descriptor.bRefresh()).thenReturn(6); + when(descriptor.bSynchAddress()).thenReturn(7); + when(descriptor.extra()).thenReturn(ByteBuffer.allocateDirect(4)); + when(descriptor.extralen()).thenReturn(2); + when(descriptor.dump()).thenCallRealMethod(); + final String actual = descriptor.dump(); + assertNotNull(actual); + } +}