Add unit test for Version class and catch uninitialized usage of it.

This commit is contained in:
Klaus Reimer 2013-07-13 21:45:59 +02:00
parent 40460f34ec
commit 5efc4f8573
2 changed files with 99 additions and 4 deletions

View File

@ -23,7 +23,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(Version, major)
JNIEnv *env, jobject this
)
{
return unwrapVersion(env, this)->major;
const struct libusb_version *version = unwrapVersion(env, this);
if (!version) return 0;
return version->major;
}
/**
@ -34,7 +36,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(Version, minor)
JNIEnv *env, jobject this
)
{
return unwrapVersion(env, this)->minor;
const struct libusb_version *version = unwrapVersion(env, this);
if (!version) return 0;
return version->minor;
}
/**
@ -45,7 +49,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(Version, micro)
JNIEnv *env, jobject this
)
{
return unwrapVersion(env, this)->micro;
const struct libusb_version *version = unwrapVersion(env, this);
if (!version) return 0;
return version->micro;
}
/**
@ -56,7 +62,9 @@ JNIEXPORT jstring JNICALL METHOD_NAME(Version, rc)
JNIEnv *env, jobject this
)
{
return (*env)->NewStringUTF(env, unwrapVersion(env, this)->rc);
const struct libusb_version *version = unwrapVersion(env, this);
if (!version) return NULL;
return (*env)->NewStringUTF(env, version->rc);
}

View File

@ -0,0 +1,87 @@
/*
* Copyright (C) 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*/
package de.ailis.usb4java.libusb;
import static de.ailis.usb4java.test.UsbAssume.assumeUsbTestsEnabled;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Tests the {@link Version} class.
*
* @author Klaus Reimer (k@ailis.de)
*/
public class VersionTest
{
/** The test subject. */
private Version version;
/**
* Setup test.
*/
@Before
public void setUp()
{
assumeUsbTestsEnabled();
LibUsb.init(null);
this.version = new Version();
}
/**
* Tear down test.
*/
@After
public void tearDown()
{
LibUsb.exit(null);
}
/**
* Tests uninitialized access to
* {@link Version#major()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedMajor()
{
assumeUsbTestsEnabled();
this.version.major();
}
/**
* Tests uninitialized access to
* {@link Version#minor()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedMinor()
{
assumeUsbTestsEnabled();
this.version.minor();
}
/**
* Tests uninitialized access to
* {@link Version#micro()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedMicro()
{
assumeUsbTestsEnabled();
this.version.micro();
}
/**
* Tests uninitialized access to
* {@link Version#rc()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedRc()
{
assumeUsbTestsEnabled();
this.version.rc();
}
}