Skip to content

Commit a37ecf1

Browse files
committed
[Minor fixes and UT] Added some more unit test cases and fixed some
minor issues found. Signed-off-by: Tatiana Leon <tatiana.leon@digi.com>
1 parent d9ba7db commit a37ecf1

17 files changed

+2961
-39
lines changed

library/src/main/java/com/digi/xbee/api/XBeeNetwork.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ public RemoteXBeeDevice getDevice(XBee64BitAddress address) {
412412
if (address == null)
413413
throw new NullPointerException("64-bit address cannot be null.");
414414
if (address.equals(XBee64BitAddress.UNKNOWN_ADDRESS))
415-
throw new NullPointerException("64-bit address cannot be unknown.");
415+
throw new IllegalArgumentException("64-bit address cannot be unknown.");
416416

417417
logger.debug("{}Getting device '{}' from network.", localDevice.toString(), address);
418418

@@ -443,7 +443,7 @@ public RemoteXBeeDevice getDevice(XBee16BitAddress address) throws OperationNotS
443443
if (address == null)
444444
throw new NullPointerException("16-bit address cannot be null.");
445445
if (address.equals(XBee16BitAddress.UNKNOWN_ADDRESS))
446-
throw new NullPointerException("16-bit address cannot be unknown.");
446+
throw new IllegalArgumentException("16-bit address cannot be unknown.");
447447

448448
logger.debug("{}Getting device '{}' from network.", localDevice.toString(), address);
449449

@@ -492,7 +492,7 @@ public RemoteXBeeDevice getDevice(XBee16BitAddress address) throws OperationNotS
492492
* @return The remote XBee Device instance in the network, {@code null} if
493493
* the device could not be successfully added.
494494
*
495-
* @throws NullPointerException if {@code RemoteDevice == null}.
495+
* @throws NullPointerException if {@code remoteDevice == null}.
496496
*
497497
* @see #addRemoteDevices(List)
498498
* @see #removeRemoteDevice(RemoteXBeeDevice)
@@ -618,7 +618,11 @@ public List<RemoteXBeeDevice> addRemoteDevices(List<RemoteXBeeDevice> list) {
618618
logger.debug("{}Adding '{}' devices to network.", localDevice.toString(), list.size());
619619

620620
for (int i = 0; i < list.size(); i++) {
621-
RemoteXBeeDevice d = addRemoteDevice(list.get(i));
621+
RemoteXBeeDevice toAdd = list.get(i);
622+
if (toAdd == null)
623+
continue;
624+
625+
RemoteXBeeDevice d = addRemoteDevice(toAdd);
622626
if (d != null)
623627
addedList.add(d);
624628
}
@@ -716,7 +720,7 @@ public void removeRemoteDevice(RemoteXBeeDevice remoteDevice) {
716720
public void clearDeviceList() {
717721
logger.debug("{}Clearing the network.", localDevice.toString());
718722
remotesBy64BitAddr.clear();
719-
remotesBy64BitAddr.clear();
723+
remotesBy16BitAddr.clear();
720724
}
721725

722726
/**

library/src/main/java/com/digi/xbee/api/models/HardwareVersionEnum.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
import java.util.HashMap;
1515

16-
import com.digi.xbee.api.utils.HexUtils;
17-
1816
/**
1917
* Enumerates the different hardware versions of the XBee devices.
2018
*/

library/src/main/java/com/digi/xbee/api/utils/ByteUtils.java

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ public static byte[] readBytes(int numBytes, ByteArrayInputStream inputStream) {
3838
if (inputStream == null)
3939
throw new NullPointerException("Input stream cannot be null.");
4040
if (numBytes < 0)
41-
throw new IllegalArgumentException("Number of bytes to read must be greater than 0.");
41+
throw new IllegalArgumentException("Number of bytes to read must be equal or greater than 0.");
4242

4343
byte[] data = new byte[numBytes];
4444
int len = inputStream.read(data, 0, numBytes);
45+
if (len == - 1)
46+
return new byte[0];
4547
if (len < numBytes) {
4648
byte[] d = new byte[len];
4749
System.arraycopy(data, 0, d, 0, len);
@@ -110,13 +112,16 @@ public static long byteArrayToLong(byte[] byteArray) {
110112
if (byteArray == null)
111113
throw new NullPointerException("Byte array cannot be null.");
112114

115+
if (byteArray.length == 0)
116+
return 0;
117+
113118
byte[] values = byteArray;
114119
if (byteArray.length < 8) {
115120
values = new byte[8];
116-
int diff = 8 - byteArray.length;
121+
int diff = values.length - byteArray.length;
117122
for (int i = 0; i < diff; i++)
118123
values[i] = 0;
119-
for (int i = diff; i < 8; i++)
124+
for (int i = diff; i < values.length; i++)
120125
values[i] = byteArray[i - diff];
121126
}
122127
return ((long)values[0] << 56)
@@ -162,13 +167,16 @@ public static int byteArrayToInt(byte[] byteArray) {
162167
if (byteArray == null)
163168
throw new NullPointerException("Byte array cannot be null.");
164169

170+
if (byteArray.length == 0)
171+
return 0;
172+
165173
byte[] values = byteArray;
166174
if (byteArray.length < 4) {
167175
values = new byte[4];
168-
int diff = 4 - byteArray.length;
176+
int diff = values.length - byteArray.length;
169177
for (int i = 0; i < diff; i++)
170178
values[i] = 0;
171-
for (int i = diff; i < 4; i++)
179+
for (int i = diff; i < values.length; i++)
172180
values[i] = byteArray[i - diff];
173181
}
174182
return ((values[0] & 0xFF) << 24)
@@ -208,8 +216,18 @@ public static short byteArrayToShort(byte[] byteArray) {
208216
if (byteArray == null)
209217
throw new NullPointerException("Byte array cannot be null.");
210218

211-
return (short) (((byteArray[0] << 8) & 0xFF00)
212-
| byteArray[1] & 0x00FF);
219+
if (byteArray.length == 0)
220+
return 0;
221+
222+
byte[] values = byteArray;
223+
if (byteArray.length < 2) {
224+
values = new byte[2];
225+
values[1] = byteArray[0];
226+
values[0] = 0;
227+
}
228+
229+
return (short) (((values[0] << 8) & 0xFF00)
230+
| values[1] & 0x00FF);
213231
}
214232

215233
/**
@@ -267,8 +285,14 @@ public static int byteToInt(byte b) {
267285
*
268286
* @return {@code true} if the given bit position is set to {@code 1}
269287
* in the {@code containerInteger}, {@code false} otherwise.
288+
*
289+
* @throws IllegalArgumentException if {@code bitPosition < 0} or
290+
* if {@code bitPosition > 31}.
270291
*/
271292
public static boolean isBitEnabled(int containerInteger, int bitPosition) {
293+
if (bitPosition < 0 || bitPosition > 31)
294+
throw new IllegalArgumentException("Bit position must be between 0 and 31.");
295+
272296
return (((containerInteger & 0xFFFFFFFF) >> bitPosition) & 0x01) == 0x01;
273297
}
274298

@@ -281,10 +305,22 @@ public static boolean isBitEnabled(int containerInteger, int bitPosition) {
281305
* @param bitLength Size in bits of the integer value to read.
282306
*
283307
* @return The integer read value.
308+
*
309+
* @throws IllegalArgumentException if {@code bitOffset < 0} or
310+
* if {@code bitOffset > 7} or
311+
* if {@code bitLength < 0} or
312+
* if {@code bitLength > 8}.
284313
*/
285314
public static int readIntegerFromByte(byte containerByte, int bitOffset, int bitLength) {
315+
if (bitOffset < 0 || bitOffset > 7)
316+
throw new IllegalArgumentException("Offset must be between 0 and 7.");
317+
if (bitLength < 0 || bitLength > 7)
318+
throw new IllegalArgumentException("Length must be between 0 and 8.");
319+
286320
int readInteger = 0;
287321
for (int i = 0; i < bitLength; i++) {
322+
if (bitOffset + i > 7)
323+
break;
288324
if (isBitEnabled(containerByte, bitOffset + i))
289325
readInteger = readInteger | (int)Math.pow(2, i);
290326
}
@@ -298,8 +334,14 @@ public static int readIntegerFromByte(byte containerByte, int bitOffset, int bit
298334
* @param bitOffset Offset inside the byte to read the boolean value.
299335
*
300336
* @return The read boolean value.
337+
*
338+
* @throws IllegalArgumentException if {@code bitOffset < 0} or
339+
* if {@code bitOffset > 31}.
301340
*/
302341
public static boolean readBooleanFromByte(byte containerByte, int bitOffset) {
342+
if (bitOffset < 0 || bitOffset > 31)
343+
throw new IllegalArgumentException("Bit offset must be between 0 and 7.");
344+
303345
return isBitEnabled(containerByte, bitOffset);
304346
}
305347

@@ -339,11 +381,17 @@ public static byte[] readUntilCR(ByteArrayInputStream inputStream) {
339381
* @return Final byte array of the given size containing the given data and
340382
* replacing with zeros the remaining space.
341383
*
384+
* @throws IllegalArgumentException if {@code finalSize < 0}.
342385
* @throws NullPointerException if {@code data == null}.
343386
*/
344387
public static byte[] newByteArray(byte[] data, int finalSize) {
345388
if (data == null)
346389
throw new NullPointerException("Data cannot be null.");
390+
if (finalSize < 0)
391+
throw new IllegalArgumentException("Final size must be equal or greater than 0.");
392+
393+
if (finalSize == 0)
394+
return new byte[0];
347395

348396
byte[] filledArray = new byte[finalSize];
349397
int diff = finalSize - data.length;
@@ -362,8 +410,16 @@ public static byte[] newByteArray(byte[] data, int finalSize) {
362410
* @param source Byte array to swap.
363411
*
364412
* @return The swapped byte array.
413+
*
414+
* @throws NullPointerException if {@code source == null}.
365415
*/
366416
public static byte[] swapByteArray(byte[] source) {
417+
if (source == null)
418+
throw new NullPointerException("Source cannot be null.");
419+
420+
if (source.length == 0)
421+
return new byte[0];
422+
367423
byte[] swapped = new byte[source.length];
368424
for (int i = 0; i < source.length; i++)
369425
swapped[source.length - i - 1] = source[i];

library/src/main/java/com/digi/xbee/api/utils/HexUtils.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public class HexUtils {
3434
*/
3535
public static String byteArrayToHexString(byte[] value) {
3636
if (value == null )
37-
throw new NullPointerException("Value to convert cannot be null");
37+
throw new NullPointerException("Value to convert cannot be null.");
38+
3839
final StringBuilder hex = new StringBuilder(2 * value.length );
3940
for (final byte b : value) {
4041
hex.append(HEXES.charAt((b & 0xF0) >> 4))
@@ -118,18 +119,15 @@ public static boolean containsLetters(String parameter) {
118119
* @param minBytes The minimum number of bytes to be represented.
119120
*
120121
* @return The integer value as hexadecimal string.
122+
*
123+
* @throws IllegalArgumentException if {@code minBytes <= 0}.
121124
*/
122125
public static String integerToHexString(int value, int minBytes) {
123-
byte[] intAsByteArray = ByteUtils.intToByteArray(value);
124-
String intAsHexString = "";
125-
boolean numberFound = false;
126-
for (int i = 0; i < intAsByteArray.length; i++) {
127-
if (intAsByteArray[i] == 0x00 && !numberFound && intAsByteArray.length - i > minBytes)
128-
continue;
129-
intAsHexString += HexUtils.byteArrayToHexString(new byte[] {(byte)(intAsByteArray[i] & 0xFF)});
130-
numberFound = true;
131-
}
132-
return intAsHexString;
126+
if (minBytes <= 0)
127+
throw new IllegalArgumentException("Minimum number of bytes must be greater than 0.");
128+
129+
String f = String.format("%%0%dX", minBytes*2);
130+
return String.format(f, value);
133131
}
134132

135133
/**
@@ -148,12 +146,18 @@ public static String prettyHexString(String hexString) {
148146
if (hexString == null)
149147
throw new NullPointerException("Hexadecimal string cannot be null.");
150148

149+
String copy = hexString.toUpperCase();
150+
for (final char c : copy.toCharArray()) {
151+
if (!HEXES.contains(""+c))
152+
throw new IllegalArgumentException("Given string cannot contain non-hexadecimal characters.");
153+
}
154+
151155
String prettyHexString = "";
152-
if (hexString.length() % 2 != 0)
153-
hexString = "0" + hexString;
154-
int iterations = hexString.length() / 2;
156+
if (copy.length() % 2 != 0)
157+
copy = "0" + copy;
158+
int iterations = copy.length() / 2;
155159
for (int i = 0; i < iterations; i++)
156-
prettyHexString += hexString.substring(2 * i, 2 * i + 2) + " ";
160+
prettyHexString += copy.substring(2 * i, 2 * i + 2) + " ";
157161
return prettyHexString.trim();
158162
}
159163

0 commit comments

Comments
 (0)