Skip to content

Commit

Permalink
ctors of TinyUUID throws now an IllegalArgumentException for wrong ar…
Browse files Browse the repository at this point in the history
…guments
  • Loading branch information
oboehm committed Sep 8, 2018
1 parent 11e876a commit 8184c36
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
Expand Up @@ -20,6 +20,7 @@
import org.apache.commons.lang3.Range;

import java.io.Serializable;
import java.util.Arrays;

/**
* Die InvalidValueException ist eine Exception fuer ungueltige Werte.
Expand Down Expand Up @@ -74,6 +75,18 @@ public LocalizedIllegalArgumentException(Serializable value, String context, Thr
this("illegal value for " + context.replace('_', ' ') + ": \"" + value + '"', cause);
}

/**
* Dieser Constructor kann bei Arrays mit falscher Groesse eingesetzt
* werden.
*
* @param array fehlerhaftes Array
* @param expected erwartete Array-Groesse
*/
public LocalizedIllegalArgumentException(byte[] array, int expected) {
this("array=" + Arrays.toString(array) + " has not length " + expected + " (but " + array.length + ")",
new InvalidLengthException(array, expected));
}

/**
* Erzeugt eine neue Exception fuer einen fehlerhaften Wert.
*
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/de/jfachwert/util/TinyUUID.java
Expand Up @@ -18,8 +18,8 @@
package de.jfachwert.util;

import de.jfachwert.AbstractFachwert;
import de.jfachwert.pruefung.exception.InvalidLengthException;
import de.jfachwert.pruefung.exception.InvalidValueException;
import de.jfachwert.pruefung.exception.LocalizedIllegalArgumentException;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -93,12 +93,12 @@ public TinyUUID(byte[] bytes) {
}

private static UUID toUUID(byte[] bytes) {
return UUID.fromString(toString(validate(bytes)));
return UUID.fromString(toString(verify(bytes)));
}

private static byte[] validate(byte[] bytes) {
private static byte[] verify(byte[] bytes) {
if (bytes.length != 16) {
throw new InvalidLengthException(bytes, 16);
throw new LocalizedIllegalArgumentException(bytes, 16);
}
return bytes;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/de/jfachwert/util/TinyUUIDTest.java
Expand Up @@ -71,7 +71,7 @@ public void toNumberTen() {
* 16 Bytes uebergeben werden, sollte eine {@link ValidationException}
* kommen.
*/
@Test(expected = ValidationException.class)
@Test(expected = IllegalArgumentException.class)
public void testTinyUUIDInvalidBytes() {
new TinyUUID(new byte[1]);
}
Expand Down

0 comments on commit 8184c36

Please sign in to comment.