Permalink
Browse files

Move error checking into a JawboneException class

  • Loading branch information...
1 parent 47b949c commit eb48165a0fc0ddb70a664a39841645fbfcd16265 @samstokes samstokes committed May 24, 2012
@@ -22,22 +22,4 @@
public native int wbxml_conv_xml2wbxml_run(Pointer conv, byte[] xml, long xmlLength, PointerByReference wbxmlPtr, LongByReference wbxmlLength);
public native void wbxml_conv_xml2wbxml_destroy(Pointer conv);
-
-
- public native String wbxml_errors_string(int ret);
-
-
- public void check(int ret) {
- if (ret != 0) {
- throw new JawboneException(ret);
- }
- }
-
-
- @SuppressWarnings("serial")
- class JawboneException extends RuntimeException {
- JawboneException(int ret) {
- super(wbxml_errors_string(ret));
- }
- }
}
@@ -0,0 +1,41 @@
+package com.rapportive.jawbone;
+
+import com.sun.jna.Native;
+
+@SuppressWarnings("serial")
+public class JawboneException extends RuntimeException {
+ static {
+ Native.register("wbxml2");
+ }
+
+ private static final String DEFAULT_PREFIX = "libwbxml2 error";
+
+ private final String message;
+
+ public JawboneException(int ret) {
+ this(DEFAULT_PREFIX, ret);
+ }
+
+ public JawboneException(String prefix, int ret) {
+ this.message = prefix + ": " + wbxml_errors_string(ret);
+ }
+
+ @Override
+ public String getMessage() {
+ return message;
+ }
+
+
+ private native String wbxml_errors_string(int ret);
+
+
+ public static void check(int ret) {
+ check(DEFAULT_PREFIX, ret);
+ }
+
+ public static void check(String prefix, int ret) {
+ if (ret != 0) {
+ throw new JawboneException(prefix, ret);
+ }
+ }
+}
@@ -1,5 +1,7 @@
package com.rapportive.jawbone;
+import static com.rapportive.jawbone.JawboneException.check;
+
import com.sun.jna.Native;
import com.sun.jna.Pointer;
@@ -54,20 +56,6 @@ public void finalize() {
}
- private void check(String function, int ret) {
- if (ret != 0) {
- throw new WbxmlToXmlException(function + ": " + wbxml_errors_string(ret));
- }
- }
-
- @SuppressWarnings("serial")
- private class WbxmlToXmlException extends RuntimeException {
- public WbxmlToXmlException(String message) {
- super(message);
- }
- }
-
-
private native int wbxml_conv_wbxml2xml_create(PointerByReference convPtr);
private native void wbxml_conv_wbxml2xml_set_gen_type(Pointer conv, int genType);
@@ -79,6 +67,4 @@ public WbxmlToXmlException(String message) {
private native int wbxml_conv_wbxml2xml_run(Pointer conv, byte[] wbxml, long wbxmlLength, PointerByReference xmlPtr, LongByReference xmlLength);
private native void wbxml_conv_wbxml2xml_destroy(Pointer conv);
-
- private native String wbxml_errors_string(int ret);
}
@@ -6,6 +6,8 @@
import java.io.InputStream;
import java.io.IOException;
+import static com.rapportive.jawbone.JawboneException.check;
+
import static org.junit.Assert.*;
import org.junit.BeforeClass;
@@ -63,7 +65,7 @@ public void testXmlToWbxml() throws Exception {
LongByReference wbxmlLength = new LongByReference();
ret = binding.wbxml_conv_xml2wbxml_run(conv, xml, xml.length, wbxmlPtr, wbxmlLength);
- assertSuccess("wbxml_conv_xml2wbxml_run", ret);
+ check("wbxml_conv_xml2wbxml_run", ret);
assertTrue("returned no WBXML", wbxmlLength.getValue() > 0);
byte[] wbxmlBytes = wbxmlPtr.getValue().getByteArray(0L, (int) wbxmlLength.getValue());
@@ -78,12 +80,6 @@ public void testXmlToWbxml() throws Exception {
}
- private void assertSuccess(String function, int ret) {
- if (ret != 0) {
- fail(function + ": " + binding.wbxml_errors_string(ret));
- }
- }
-
private static String testResource(String path) {
String testResourcesDir = System.getProperty("test.resources");
if (testResourcesDir == null) {

0 comments on commit eb48165

Please sign in to comment.