Skip to content
Permalink
Browse files
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
  • Loading branch information
aherbert committed Mar 15, 2022
1 parent f970ee4 commit 3ed42d76d5da8787741f4ce57e321cae2a111d4a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
@@ -254,7 +254,7 @@ public Object convertSeed(Object seed,
return convert((byte[]) seed, size);
}

throw new UnsupportedOperationException(UNRECOGNISED_SEED + seed);
throw new UnsupportedOperationException(unrecognisedSeedMessage(seed));
}

/**
@@ -322,6 +322,16 @@ public static byte[] convertSeedToBytes(Object seed) {
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());
}
}
@@ -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()");
}
}
};
}

/**

0 comments on commit 3ed42d7

Please sign in to comment.