This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git
The following commit(s) were added to refs/heads/master by this push:
new 3ed42d7 Return the class type of the unrecognised seed.
3ed42d7 is described below
commit 3ed42d76d5da8787741f4ce57e321cae2a111d4a
Author: aherbert <[email protected]>
AuthorDate: Tue Mar 15 16:06:10 2022 +0000
Return the class type of the unrecognised seed.
This avoids creating a message containing the string representation of
an Object. This may be a very large String, for example a BitSet.
fix
---
.../rng/simple/internal/NativeSeedType.java | 14 ++++++--
.../rng/simple/internal/NativeSeedTypeTest.java | 41 +++++++++++++++++++---
2 files changed, 48 insertions(+), 7 deletions(-)
diff --git
a/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/NativeSeedType.java
b/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/NativeSeedType.java
index 0fe9da5..9bbc126 100644
---
a/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/NativeSeedType.java
+++
b/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/NativeSeedType.java
@@ -254,7 +254,7 @@ public enum NativeSeedType {
return convert((byte[]) seed, size);
}
- throw new UnsupportedOperationException(UNRECOGNISED_SEED + seed);
+ throw new UnsupportedOperationException(unrecognisedSeedMessage(seed));
}
/**
@@ -322,6 +322,16 @@ public enum NativeSeedType {
return (byte[]) seed;
}
- throw new UnsupportedOperationException(UNRECOGNISED_SEED + seed);
+ throw new UnsupportedOperationException(unrecognisedSeedMessage(seed));
+ }
+
+ /**
+ * Create an unrecognised seed message. This will add the class type of
the seed.
+ *
+ * @param seed the seed
+ * @return the message
+ */
+ private static String unrecognisedSeedMessage(Object seed) {
+ return UNRECOGNISED_SEED + ((seed == null) ? "null" :
seed.getClass().getName());
}
}
diff --git
a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NativeSeedTypeTest.java
b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NativeSeedTypeTest.java
index 54ded36..b5280c9 100644
---
a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NativeSeedTypeTest.java
+++
b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NativeSeedTypeTest.java
@@ -16,20 +16,51 @@
*/
package org.apache.commons.rng.simple.internal;
+import java.math.BigDecimal;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
/**
* Tests for the {@link NativeSeedType} factory seed conversions.
+ *
+ * <p>Note: All supported types are tested in the {@link
NativeSeedTypeParametricTest}
*/
class NativeSeedTypeTest {
/**
- * Test the conversion throws for an unsupported type. All supported types
are
- * tested in the {@link NativeSeedTypeParametricTest}.
+ * Test the conversion throws for an unsupported seed type.
+ * The error message should contain the type, not the string
representation of the seed.
*/
- @Test
- void testConvertSeedToBytesUsingNullThrows() {
- Assertions.assertThrows(UnsupportedOperationException.class, () ->
NativeSeedType.convertSeedToBytes(null));
+ @ParameterizedTest
+ @MethodSource
+ void testConvertSeedToBytesUsingUnsupportedSeedThrows(Object seed) {
+ final UnsupportedOperationException ex = Assertions.assertThrows(
+ UnsupportedOperationException.class, () ->
NativeSeedType.convertSeedToBytes(seed));
+ if (seed == null) {
+ Assertions.assertTrue(ex.getMessage().contains("null"));
+ } else {
+
Assertions.assertTrue(ex.getMessage().contains(seed.getClass().getName()));
+ }
+ }
+
+ /**
+ * Return an array of unsupported seed objects.
+ *
+ * @return the seeds
+ */
+ static Object[] testConvertSeedToBytesUsingUnsupportedSeedThrows() {
+ return new Object[] {
+ null,
+ BigDecimal.ONE,
+ "The quick brown fox jumped over the lazy dog",
+ new Object() {
+ @Override
+ public String toString() {
+ throw new IllegalStateException("error message should not
call toString()");
+ }
+ }
+ };
}
/**