From 973678c97253e413abbda1250067c1a0dd05f631 Mon Sep 17 00:00:00 2001 From: Josh Allen Date: Mon, 30 Oct 2017 11:29:10 -0400 Subject: [PATCH 1/3] Exception stack traces always had null for message. You should never override toString() of Exceptions, instead use the message --- ...SetCharacteristicNotificationException.java | 14 ++++++++------ .../exceptions/BleDisconnectedException.java | 12 +++++++----- .../rxandroidble/exceptions/BleException.java | 5 +++-- .../exceptions/BleScanException.java | 18 ++++++++++-------- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationException.java index 642a4a168..cfd119480 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationException.java @@ -63,6 +63,7 @@ public class BleCannotSetCharacteristicNotificationException extends BleExceptio // TODO [DS] 08.07.2017 Remove in 2.0.0 @Deprecated public BleCannotSetCharacteristicNotificationException(BluetoothGattCharacteristic bluetoothGattCharacteristic) { + super(createMessage(bluetoothGattCharacteristic, UNKNOWN, null)); this.bluetoothGattCharacteristic = bluetoothGattCharacteristic; this.reason = UNKNOWN; } @@ -70,9 +71,10 @@ public BleCannotSetCharacteristicNotificationException(BluetoothGattCharacterist @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) public BleCannotSetCharacteristicNotificationException(BluetoothGattCharacteristic bluetoothGattCharacteristic, @Reason int reason, Throwable cause) { - super(cause); + super(createMessage(bluetoothGattCharacteristic, reason, cause)); this.bluetoothGattCharacteristic = bluetoothGattCharacteristic; this.reason = reason; + initCause(cause); } public BluetoothGattCharacteristic getBluetoothGattCharacteristic() { @@ -90,17 +92,17 @@ public int getReason() { return reason; } - @Override - public String toString() { + private static String createMessage(BluetoothGattCharacteristic bluetoothGattCharacteristic, @Reason int reason, + Throwable cause) { return "BleCannotSetCharacteristicNotificationException{" + "bluetoothGattCharacteristic=" + bluetoothGattCharacteristic.getUuid() - + ", reason=" + reasonDescription() + + ", reason=" + reasonDescription(reason) + " (see Javadoc for more comment)" - + toStringCauseIfExists() + + toStringCauseIfExists(cause) + '}'; } - private String reasonDescription() { + private static String reasonDescription(int reason) { switch (reason) { case CANNOT_FIND_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR: diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleDisconnectedException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleDisconnectedException.java index 8cbcbe3da..7b1b88946 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleDisconnectedException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleDisconnectedException.java @@ -1,6 +1,7 @@ package com.polidea.rxandroidble.exceptions; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; /** * Exception emitted when the BLE link has been disconnected either when the connection was already established @@ -17,24 +18,25 @@ public class BleDisconnectedException extends BleException { @Deprecated public BleDisconnectedException() { - super(); + super(createMessage(null, null)); bluetoothDeviceAddress = ""; } public BleDisconnectedException(Throwable throwable, @NonNull String bluetoothDeviceAddress) { - super(throwable); + super(createMessage(throwable, bluetoothDeviceAddress)); this.bluetoothDeviceAddress = bluetoothDeviceAddress; + initCause(throwable); } public BleDisconnectedException(@NonNull String bluetoothDeviceAddress) { + super(createMessage(null, bluetoothDeviceAddress)); this.bluetoothDeviceAddress = bluetoothDeviceAddress; } - @Override - public String toString() { + private static String createMessage(@Nullable Throwable throwable, @Nullable String bluetoothDeviceAddress) { return "BleDisconnectedException{" + "bluetoothDeviceAddress='" + bluetoothDeviceAddress + '\'' - + toStringCauseIfExists() + + toStringCauseIfExists(throwable) + '}'; } } diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleException.java index 9747607f5..4c1682b73 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleException.java @@ -1,5 +1,7 @@ package com.polidea.rxandroidble.exceptions; +import android.support.annotation.Nullable; + public class BleException extends RuntimeException { public BleException() { @@ -14,8 +16,7 @@ public BleException(Throwable throwable) { super(throwable); } - String toStringCauseIfExists() { - Throwable throwableCause = getCause(); + static String toStringCauseIfExists(@Nullable Throwable throwableCause) { return (throwableCause != null ? ", cause=" + throwableCause.toString() : ""); } } diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleScanException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleScanException.java index d5070ac7e..8ac3a5b87 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleScanException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleScanException.java @@ -94,19 +94,22 @@ public class BleScanException extends BleException { private final Date retryDateSuggestion; public BleScanException(@Reason int reason) { + super(createMessage(reason, null, null)); this.reason = reason; this.retryDateSuggestion = null; } public BleScanException(@Reason int reason, @NonNull Date retryDateSuggestion) { + super(createMessage(reason, retryDateSuggestion, null)); this.reason = reason; this.retryDateSuggestion = retryDateSuggestion; } public BleScanException(@Reason int reason, Throwable causeException) { - super(causeException); + super(createMessage(reason, null, causeException)); this.reason = reason; this.retryDateSuggestion = null; + initCause(causeException); } /** @@ -129,16 +132,15 @@ public Date getRetryDateSuggestion() { return retryDateSuggestion; } - @Override - public String toString() { + public static String createMessage(int reason, Date retryDateSuggestion, @Nullable Throwable cause) { return "BleScanException{" - + "reason=" + reasonDescription() - + retryDateSuggestionIfExists() - + toStringCauseIfExists() + + "reason=" + reasonDescription(reason) + + retryDateSuggestionIfExists(retryDateSuggestion) + + toStringCauseIfExists(cause) + '}'; } - private String reasonDescription() { + private static String reasonDescription(int reason) { switch (reason) { case BLUETOOTH_CANNOT_START: return "BLUETOOTH_CANNOT_START"; @@ -169,7 +171,7 @@ private String reasonDescription() { } } - private String retryDateSuggestionIfExists() { + private static String retryDateSuggestionIfExists(Date retryDateSuggestion) { if (retryDateSuggestion == null) { return ""; } else { From 8737b6238086f59ea86d2f1f379865e5459519bf Mon Sep 17 00:00:00 2001 From: Josh Allen Date: Wed, 1 Nov 2017 22:43:59 -0400 Subject: [PATCH 2/3] nicer toStrings --- .../BleAlreadyConnectedException.java | 5 +-- ...etCharacteristicNotificationException.java | 16 +++----- .../BleCharacteristicNotFoundException.java | 5 +-- ...ictingNotificationAlreadySetException.java | 9 +--- .../exceptions/BleDisconnectedException.java | 12 ++---- .../rxandroidble/exceptions/BleException.java | 9 ++-- .../exceptions/BleGattException.java | 17 ++++---- .../exceptions/BleScanException.java | 41 ++++++++----------- .../BleServiceNotFoundException.java | 6 +-- .../rxandroidble/RxBleClientTest.groovy | 1 + .../exceptions/BleScanExceptionTest.groovy | 23 +++++++++++ 11 files changed, 72 insertions(+), 72 deletions(-) create mode 100644 rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleScanExceptionTest.groovy diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleAlreadyConnectedException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleAlreadyConnectedException.java index 0cd8b13eb..c81e10657 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleAlreadyConnectedException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleAlreadyConnectedException.java @@ -5,11 +5,8 @@ public class BleAlreadyConnectedException extends BleException { private final String macAddress; public BleAlreadyConnectedException(String macAddress) { + super("Already connected to device with MAC address " + macAddress); this.macAddress = macAddress; } - @Override - public String toString() { - return "BleAlreadyConnectedException{macAddress=" + macAddress + '}'; - } } diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationException.java index cfd119480..0aa571f59 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationException.java @@ -94,26 +94,22 @@ public int getReason() { private static String createMessage(BluetoothGattCharacteristic bluetoothGattCharacteristic, @Reason int reason, Throwable cause) { - return "BleCannotSetCharacteristicNotificationException{" - + "bluetoothGattCharacteristic=" + bluetoothGattCharacteristic.getUuid() - + ", reason=" + reasonDescription(reason) - + " (see Javadoc for more comment)" - + toStringCauseIfExists(cause) - + '}'; + return reasonDescription(reason) + " (code " + + reason + ") with characteristic UUID " + bluetoothGattCharacteristic.getUuid(); } private static String reasonDescription(int reason) { switch (reason) { case CANNOT_FIND_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR: - return "CANNOT_FIND_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR"; + return "Cannot find client characteristic config descriptor"; case CANNOT_WRITE_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR: - return "CANNOT_WRITE_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR"; + return "Cannot write client characteristic config descriptor"; case CANNOT_SET_LOCAL_NOTIFICATION: - return "CANNOT_SET_LOCAL_NOTIFICATION"; + return "Cannot set local notification"; case UNKNOWN: default: - return "UNKNOWN"; + return "Unknown error"; } } } diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCharacteristicNotFoundException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCharacteristicNotFoundException.java index ab2aad6b4..17dc56798 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCharacteristicNotFoundException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCharacteristicNotFoundException.java @@ -7,6 +7,7 @@ public class BleCharacteristicNotFoundException extends BleException { private final UUID charactersisticUUID; public BleCharacteristicNotFoundException(UUID charactersisticUUID) { + super("Characteristic not found with UUID " + charactersisticUUID); this.charactersisticUUID = charactersisticUUID; } @@ -14,8 +15,4 @@ public UUID getCharactersisticUUID() { return charactersisticUUID; } - @Override - public String toString() { - return "BleCharacteristicNotFoundException{" + "charactersisticUUID=" + charactersisticUUID + '}'; - } } diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleConflictingNotificationAlreadySetException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleConflictingNotificationAlreadySetException.java index 2eff388c1..14cd46f08 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleConflictingNotificationAlreadySetException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleConflictingNotificationAlreadySetException.java @@ -9,6 +9,8 @@ public class BleConflictingNotificationAlreadySetException extends BleException private final boolean alreadySetIsIndication; public BleConflictingNotificationAlreadySetException(UUID characteristicUuid, boolean alreadySetIsIndication) { + super("Characteristic " + characteristicUuid + + " notification already set to " + (alreadySetIsIndication ? "indication" : "notification")); this.characteristicUuid = characteristicUuid; this.alreadySetIsIndication = alreadySetIsIndication; } @@ -25,11 +27,4 @@ public boolean notificationAlreadySet() { return !alreadySetIsIndication; } - @Override - public String toString() { - return "BleCharacteristicNotificationOfOtherTypeAlreadySetException{" - + "characteristicUuid=" + characteristicUuid.toString() - + ", typeAlreadySet=" + (alreadySetIsIndication ? "indication" : "notification") - + '}'; - } } diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleDisconnectedException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleDisconnectedException.java index 7b1b88946..62d6b237b 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleDisconnectedException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleDisconnectedException.java @@ -18,25 +18,21 @@ public class BleDisconnectedException extends BleException { @Deprecated public BleDisconnectedException() { - super(createMessage(null, null)); bluetoothDeviceAddress = ""; } public BleDisconnectedException(Throwable throwable, @NonNull String bluetoothDeviceAddress) { - super(createMessage(throwable, bluetoothDeviceAddress)); + super(createMessage(bluetoothDeviceAddress), throwable); this.bluetoothDeviceAddress = bluetoothDeviceAddress; initCause(throwable); } public BleDisconnectedException(@NonNull String bluetoothDeviceAddress) { - super(createMessage(null, bluetoothDeviceAddress)); + super(createMessage(bluetoothDeviceAddress)); this.bluetoothDeviceAddress = bluetoothDeviceAddress; } - private static String createMessage(@Nullable Throwable throwable, @Nullable String bluetoothDeviceAddress) { - return "BleDisconnectedException{" - + "bluetoothDeviceAddress='" + bluetoothDeviceAddress + '\'' - + toStringCauseIfExists(throwable) - + '}'; + private static String createMessage(@Nullable String bluetoothDeviceAddress) { + return "Disconnected from " + bluetoothDeviceAddress; } } diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleException.java index 4c1682b73..e308d268b 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleException.java @@ -1,7 +1,8 @@ package com.polidea.rxandroidble.exceptions; -import android.support.annotation.Nullable; - +/** + * Base class of exceptions in this project. + */ public class BleException extends RuntimeException { public BleException() { @@ -16,7 +17,7 @@ public BleException(Throwable throwable) { super(throwable); } - static String toStringCauseIfExists(@Nullable Throwable throwableCause) { - return (throwableCause != null ? ", cause=" + throwableCause.toString() : ""); + public BleException(String message, Throwable throwable) { + super(message, throwable); } } diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleGattException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleGattException.java index d5dd5d91d..5f3175116 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleGattException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleGattException.java @@ -41,7 +41,7 @@ public BleGattException(BluetoothGatt gatt, BleGattOperationType bleGattOperatio } public String getMacAddress() { - return gatt != null ? gatt.getDevice().getAddress() : null; + return getMacAddress(gatt); } public BleGattOperationType getBleGattOperationType() { @@ -52,17 +52,20 @@ public int getStatus() { return status; } + private static String getMacAddress(BluetoothGatt gatt) { + return gatt != null ? gatt.getDevice().getAddress() : null; + } + @SuppressLint("DefaultLocale") - @Override - public String toString() { + private static String createMessage(@Nullable BluetoothGatt gatt, int status, BleGattOperationType bleGattOperationType) { if (status == UNKNOWN_STATUS) { - return String.format("%s{macAddress=%s, bleGattOperationType=%s}", - getClass().getSimpleName(), getMacAddress(), bleGattOperationType); + return String.format("GATT exception from MAC address %s, with type %s", + getMacAddress(gatt), bleGattOperationType); } final String link = "https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.1.0_r1/stack/include/gatt_api.h"; - return String.format("%s{macAddress=%s, status=%d (0x%02x -> %s), bleGattOperationType=%s}", - getClass().getSimpleName(), getMacAddress(), status, status, link, bleGattOperationType); + return String.format("GATT exception from MAC address %s, with status %d (0x%02x -> %s), and type %s}", + getMacAddress(gatt), status, status, link, bleGattOperationType); } } diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleScanException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleScanException.java index 8ac3a5b87..288bd618f 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleScanException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleScanException.java @@ -94,22 +94,21 @@ public class BleScanException extends BleException { private final Date retryDateSuggestion; public BleScanException(@Reason int reason) { - super(createMessage(reason, null, null)); + super(createMessage(reason, null)); this.reason = reason; this.retryDateSuggestion = null; } public BleScanException(@Reason int reason, @NonNull Date retryDateSuggestion) { - super(createMessage(reason, retryDateSuggestion, null)); + super(createMessage(reason, retryDateSuggestion)); this.reason = reason; this.retryDateSuggestion = retryDateSuggestion; } public BleScanException(@Reason int reason, Throwable causeException) { - super(createMessage(reason, null, causeException)); + super(createMessage(reason, null), causeException); this.reason = reason; this.retryDateSuggestion = null; - initCause(causeException); } /** @@ -132,42 +131,38 @@ public Date getRetryDateSuggestion() { return retryDateSuggestion; } - public static String createMessage(int reason, Date retryDateSuggestion, @Nullable Throwable cause) { - return "BleScanException{" - + "reason=" + reasonDescription(reason) - + retryDateSuggestionIfExists(retryDateSuggestion) - + toStringCauseIfExists(cause) - + '}'; + private static String createMessage(int reason, Date retryDateSuggestion) { + return reasonDescription(reason) + " (code " + reason + ")" + retryDateSuggestionIfExists(retryDateSuggestion); } private static String reasonDescription(int reason) { switch (reason) { case BLUETOOTH_CANNOT_START: - return "BLUETOOTH_CANNOT_START"; + return "Bluetooth cannot start"; case BLUETOOTH_DISABLED: - return "BLUETOOTH_DISABLED"; + return "Bluetooth disabled"; case BLUETOOTH_NOT_AVAILABLE: - return "BLUETOOTH_NOT_AVAILABLE"; + return "Bluetooth not available"; case LOCATION_PERMISSION_MISSING: - return "LOCATION_PERMISSION_MISSING"; + return "Location Permission missing"; case LOCATION_SERVICES_DISABLED: - return "LOCATION_SERVICES_DISABLED"; + return "Location Services disabled"; case SCAN_FAILED_ALREADY_STARTED: - return "SCAN_FAILED_ALREADY_STARTED"; + return "Scan failed because it has already started"; case SCAN_FAILED_APPLICATION_REGISTRATION_FAILED: - return "SCAN_FAILED_APPLICATION_REGISTRATION_FAILED"; + return "Scan failed because application registration failed"; case SCAN_FAILED_INTERNAL_ERROR: - return "SCAN_FAILED_INTERNAL_ERROR"; + return "Scan failed because of an internal error"; case SCAN_FAILED_FEATURE_UNSUPPORTED: - return "SCAN_FAILED_FEATURE_UNSUPPORTED"; + return "Scan failed because feature unsupported"; case SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES: - return "SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES"; + return "Scan failed because out of hardware resources"; case UNDOCUMENTED_SCAN_THROTTLE: - return "UNDOCUMENTED_SCAN_THROTTLE"; + return "Undocumented scan throttle"; case UNKNOWN_ERROR_CODE: // fallthrough default: - return "UNKNOWN"; + return "Unknown error"; } } @@ -175,7 +170,7 @@ private static String retryDateSuggestionIfExists(Date retryDateSuggestion) { if (retryDateSuggestion == null) { return ""; } else { - return ", retryDateSuggestion=" + retryDateSuggestion; + return ", suggested retry date is " + retryDateSuggestion; } } } diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleServiceNotFoundException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleServiceNotFoundException.java index 9686f64f5..fcde038b8 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleServiceNotFoundException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleServiceNotFoundException.java @@ -7,15 +7,11 @@ public class BleServiceNotFoundException extends BleException { private final UUID serviceUUID; public BleServiceNotFoundException(UUID serviceUUID) { + super("BLE Service not found with uuid " + serviceUUID); this.serviceUUID = serviceUUID; } public UUID getServiceUUID() { return serviceUUID; } - - @Override - public String toString() { - return "BleServiceNotFoundException{serviceUUID=" + serviceUUID + '}'; - } } diff --git a/rxandroidble/src/test/groovy/com/polidea/rxandroidble/RxBleClientTest.groovy b/rxandroidble/src/test/groovy/com/polidea/rxandroidble/RxBleClientTest.groovy index 28d99508d..f188e740d 100644 --- a/rxandroidble/src/test/groovy/com/polidea/rxandroidble/RxBleClientTest.groovy +++ b/rxandroidble/src/test/groovy/com/polidea/rxandroidble/RxBleClientTest.groovy @@ -518,4 +518,5 @@ class RxBleClientTest extends Specification { Thread.sleep(200) // Nasty :< true } + } diff --git a/rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleScanExceptionTest.groovy b/rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleScanExceptionTest.groovy new file mode 100644 index 000000000..b387d5ce6 --- /dev/null +++ b/rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleScanExceptionTest.groovy @@ -0,0 +1,23 @@ +package com.polidea.rxandroidble.exceptions + +import spock.lang.Specification + +import static com.polidea.rxandroidble.exceptions.BleScanException.BLUETOOTH_DISABLED + +/** + * Tests BleScanException + */ +class BleScanExceptionTest extends Specification { + + BleScanException objectUnderTest + + def "toString should include message"() { + + when: + objectUnderTest = new BleScanException(BLUETOOTH_DISABLED) + + then: + assert objectUnderTest.toString() == + "com.polidea.rxandroidble.exceptions.BleScanException: Bluetooth disabled (code 1)" + } +} From 87de34ff0d3ec2b20e29fd30102f2a40e305aa74 Mon Sep 17 00:00:00 2001 From: Josh Allen Date: Wed, 1 Nov 2017 23:15:57 -0400 Subject: [PATCH 3/3] tests --- ...etCharacteristicNotificationException.java | 8 ++---- .../exceptions/BleDisconnectedException.java | 1 - .../exceptions/BleGattException.java | 10 ++++--- ...acteristicNotificationExceptionTest.groovy | 28 +++++++++++++++++++ .../BleDisconnectedExceptionTest.groovy | 21 ++++++++++++++ .../exceptions/BleGattExceptionTest.groovy | 24 ++++++++++++++++ 6 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationExceptionTest.groovy create mode 100644 rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleDisconnectedExceptionTest.groovy create mode 100644 rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleGattExceptionTest.groovy diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationException.java index 0aa571f59..f05f34562 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationException.java @@ -63,7 +63,7 @@ public class BleCannotSetCharacteristicNotificationException extends BleExceptio // TODO [DS] 08.07.2017 Remove in 2.0.0 @Deprecated public BleCannotSetCharacteristicNotificationException(BluetoothGattCharacteristic bluetoothGattCharacteristic) { - super(createMessage(bluetoothGattCharacteristic, UNKNOWN, null)); + super(createMessage(bluetoothGattCharacteristic, UNKNOWN)); this.bluetoothGattCharacteristic = bluetoothGattCharacteristic; this.reason = UNKNOWN; } @@ -71,10 +71,9 @@ public BleCannotSetCharacteristicNotificationException(BluetoothGattCharacterist @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) public BleCannotSetCharacteristicNotificationException(BluetoothGattCharacteristic bluetoothGattCharacteristic, @Reason int reason, Throwable cause) { - super(createMessage(bluetoothGattCharacteristic, reason, cause)); + super(createMessage(bluetoothGattCharacteristic, reason), cause); this.bluetoothGattCharacteristic = bluetoothGattCharacteristic; this.reason = reason; - initCause(cause); } public BluetoothGattCharacteristic getBluetoothGattCharacteristic() { @@ -92,8 +91,7 @@ public int getReason() { return reason; } - private static String createMessage(BluetoothGattCharacteristic bluetoothGattCharacteristic, @Reason int reason, - Throwable cause) { + private static String createMessage(BluetoothGattCharacteristic bluetoothGattCharacteristic, @Reason int reason) { return reasonDescription(reason) + " (code " + reason + ") with characteristic UUID " + bluetoothGattCharacteristic.getUuid(); } diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleDisconnectedException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleDisconnectedException.java index 62d6b237b..abcb75e69 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleDisconnectedException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleDisconnectedException.java @@ -24,7 +24,6 @@ public BleDisconnectedException() { public BleDisconnectedException(Throwable throwable, @NonNull String bluetoothDeviceAddress) { super(createMessage(bluetoothDeviceAddress), throwable); this.bluetoothDeviceAddress = bluetoothDeviceAddress; - initCause(throwable); } public BleDisconnectedException(@NonNull String bluetoothDeviceAddress) { diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleGattException.java b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleGattException.java index 5f3175116..b0eae85fd 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleGattException.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble/exceptions/BleGattException.java @@ -25,12 +25,14 @@ public class BleGattException extends BleException { @Deprecated public BleGattException(int status, BleGattOperationType bleGattOperationType) { + super(createMessage(null, status, bleGattOperationType)); this.gatt = null; this.status = status; this.bleGattOperationType = bleGattOperationType; } public BleGattException(@NonNull BluetoothGatt gatt, int status, BleGattOperationType bleGattOperationType) { + super(createMessage(gatt, status, bleGattOperationType)); this.gatt = gatt; this.status = status; this.bleGattOperationType = bleGattOperationType; @@ -52,8 +54,8 @@ public int getStatus() { return status; } - private static String getMacAddress(BluetoothGatt gatt) { - return gatt != null ? gatt.getDevice().getAddress() : null; + private static String getMacAddress(@Nullable BluetoothGatt gatt) { + return (gatt != null && gatt.getDevice() != null) ? gatt.getDevice().getAddress() : null; } @SuppressLint("DefaultLocale") @@ -65,7 +67,7 @@ private static String createMessage(@Nullable BluetoothGatt gatt, int status, Bl final String link = "https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.1.0_r1/stack/include/gatt_api.h"; - return String.format("GATT exception from MAC address %s, with status %d (0x%02x -> %s), and type %s}", - getMacAddress(gatt), status, status, link, bleGattOperationType); + return String.format("GATT exception from MAC address %s, status %d, type %s. (Look up status 0x%02x here %s)", + getMacAddress(gatt), status, bleGattOperationType, status, link); } } diff --git a/rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationExceptionTest.groovy b/rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationExceptionTest.groovy new file mode 100644 index 000000000..08805a48d --- /dev/null +++ b/rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleCannotSetCharacteristicNotificationExceptionTest.groovy @@ -0,0 +1,28 @@ +package com.polidea.rxandroidble.exceptions + +import android.bluetooth.BluetoothGattCharacteristic +import spock.lang.Specification + +/** + * Tests BleCannotSetCharacteristicNotificationException + */ +class BleCannotSetCharacteristicNotificationExceptionTest extends Specification { + + BleCannotSetCharacteristicNotificationException objectUnderTest + + BluetoothGattCharacteristic mockCharacteristic = Mock BluetoothGattCharacteristic + + def "toString should include message"() { + + given: + mockCharacteristic.uuid >> new UUID(1, 2) + when: + objectUnderTest = new BleCannotSetCharacteristicNotificationException(mockCharacteristic, + BleCannotSetCharacteristicNotificationException.CANNOT_SET_LOCAL_NOTIFICATION, new Exception("because")) + + then: + assert objectUnderTest.toString() == + "com.polidea.rxandroidble.exceptions.BleCannotSetCharacteristicNotificationException: " + + "Cannot set local notification (code 1) with characteristic UUID 00000000-0000-0001-0000-000000000002" + } +} diff --git a/rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleDisconnectedExceptionTest.groovy b/rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleDisconnectedExceptionTest.groovy new file mode 100644 index 000000000..29e1275c5 --- /dev/null +++ b/rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleDisconnectedExceptionTest.groovy @@ -0,0 +1,21 @@ +package com.polidea.rxandroidble.exceptions + +import spock.lang.Specification + +/** + * Created by jallen on 2017-11-01. + */ +class BleDisconnectedExceptionTest extends Specification { + + BleDisconnectedException objectUnderTest + + def "toString should include message"() { + + when: + objectUnderTest = new BleDisconnectedException("myBluetoothAddress") + + then: + assert objectUnderTest.toString() == + "com.polidea.rxandroidble.exceptions.BleDisconnectedException: Disconnected from myBluetoothAddress" + } +} diff --git a/rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleGattExceptionTest.groovy b/rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleGattExceptionTest.groovy new file mode 100644 index 000000000..ab0e62452 --- /dev/null +++ b/rxandroidble/src/test/groovy/com/polidea/rxandroidble/exceptions/BleGattExceptionTest.groovy @@ -0,0 +1,24 @@ +package com.polidea.rxandroidble.exceptions + +import spock.lang.Specification + +/** + * Tests BleGattException. + */ +class BleGattExceptionTest extends Specification { + + BleGattException objectUnderTest + + def "toString should include message"() { + + when: + objectUnderTest = new BleGattException(10, BleGattOperationType.CONNECTION_STATE) + + then: + assert objectUnderTest.toString() == + "com.polidea.rxandroidble.exceptions.BleGattException: GATT exception from MAC address null, status 10, " + + "type BleGattOperation{description='CONNECTION_STATE'}. " + + "(Look up status 0x0a here " + + "https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.1.0_r1/stack/include/gatt_api.h)" + } +}