Skip to content

Commit

Permalink
Added exception handler for bad Locale input
Browse files Browse the repository at this point in the history
More tests for exceptions to ensure all exceptions from Converter are IllegalArgument
  • Loading branch information
jdereg committed Mar 21, 2024
1 parent ecff2ef commit 1eab09e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 23 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ java-util
Rare, hard-to-find utilities that are thoroughly tested (> 98% code coverage via JUnit tests).
Available on [Maven Central](https://central.sonatype.com/search?q=java-util&namespace=com.cedarsoftware).
This library has <b>no dependencies</b> on other libraries for runtime.
The`.jar`file is `232K.`
The`.jar`file is `250K.`
Works with`JDK 1.8`through`JDK 21`.
The classes in the`.jar`file are version 52 (`JDK 1.8`).

---
To include in your project:
##### GradleF
```
implementation 'com.cedarsoftware:java-util:2.4.6'
implementation 'com.cedarsoftware:java-util:2.4.7'
```

##### Maven
```
<dependency>
<groupId>com.cedarsoftware</groupId>
<artifactId>java-util</artifactId>
<version>2.4.6</version>
<version>2.4.7</version>
</dependency>
```
---
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.cedarsoftware</groupId>
<artifactId>java-util</artifactId>
<packaging>jar</packaging>
<version>2.4.6</version>
<version>2.4.7</version>
<description>Java Utilities</description>
<url>https://github.com/jdereg/java-util</url>

Expand Down
24 changes: 20 additions & 4 deletions src/main/java/com/cedarsoftware/util/convert/MapConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,31 @@ static Locale toLocale(Object from, Converter converter) {
String variant = converter.convert(map.get(VARIANT), String.class);

Locale.Builder builder = new Locale.Builder();
builder.setLanguage(language);
try {
builder.setLanguage(language);
} catch (Exception e) {
throw new IllegalArgumentException("Locale language '" + language + "' invalid.", e);
}
if (StringUtilities.hasContent(country)) {
builder.setRegion(country);
try {
builder.setRegion(country);
} catch (Exception e) {
throw new IllegalArgumentException("Locale region '" + country + "' invalid.", e);
}
}
if (StringUtilities.hasContent(script)) {
builder.setScript(script);
try {
builder.setScript(script);
} catch (Exception e) {
throw new IllegalArgumentException("Locale script '" + script + "' invalid.", e);
}
}
if (StringUtilities.hasContent(variant)) {
builder.setVariant(variant);
try {
builder.setVariant(variant);
} catch (Exception e) {
throw new IllegalArgumentException("Locale variant '" + variant + "' invalid.", e);
}
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ private static void loadLocaleTests() {
{new Locale.Builder().setLanguage("en").setRegion("US").build(), new Locale.Builder().setLanguage("en").setRegion("US").build()},
});
TEST_DB.put(pair(Map.class, Locale.class), new Object[][]{
{mapOf(LANGUAGE, "joker 75", COUNTRY, "US", SCRIPT, "Latn", VARIANT, "POSIX"), new IllegalArgumentException("joker")},
{mapOf(LANGUAGE, "en", COUNTRY, "Amerika", SCRIPT, "Latn", VARIANT, "POSIX"), new IllegalArgumentException("Amerika")},
{mapOf(LANGUAGE, "en", COUNTRY, "US", SCRIPT, "Jello", VARIANT, "POSIX"), new IllegalArgumentException("Jello")},
{mapOf(LANGUAGE, "en", COUNTRY, "US", SCRIPT, "Latn", VARIANT, "Monkey @!#!# "), new IllegalArgumentException("Monkey")},
{mapOf(LANGUAGE, "en", COUNTRY, "US", SCRIPT, "Latn", VARIANT, "POSIX"), new Locale.Builder().setLanguage("en").setRegion("US").setScript("Latn").setVariant("POSIX").build(), true},
{mapOf(LANGUAGE, "en", COUNTRY, "US", SCRIPT, "Latn"), new Locale.Builder().setLanguage("en").setRegion("US").setScript("Latn").build(), true},
{mapOf(LANGUAGE, "en", COUNTRY, "US"), new Locale.Builder().setLanguage("en").setRegion("US").build(), true},
Expand All @@ -410,6 +414,10 @@ private static void loadClassTests() {
TEST_DB.put(pair(Class.class, Class.class), new Object[][]{
{int.class, int.class}
});
TEST_DB.put(pair(String.class, Class.class), new Object[][]{
{"java.util.Date", Date.class, true},
{"NoWayJose", new IllegalArgumentException("not found")},
});
}

/**
Expand All @@ -422,10 +430,6 @@ private static void loadMapTests() {
TEST_DB.put(pair(Map.class, Map.class), new Object[][]{
{ new HashMap<>(), new IllegalArgumentException("Unsupported conversion") }
});
TEST_DB.put(pair(Boolean.class, Map.class), new Object[][]{
{true, mapOf(VALUE, true)},
{false, mapOf(VALUE, false)}
});
TEST_DB.put(pair(Byte.class, Map.class), new Object[][]{
{(byte)1, mapOf(VALUE, (byte)1)},
{(byte)2, mapOf(VALUE, (byte)2)}
Expand Down Expand Up @@ -764,14 +768,6 @@ private static void loadStringTests() {
{BigInteger.ZERO, "0"},
{new BigInteger("1"), "1"},
});
TEST_DB.put(pair(BigDecimal.class, String.class), new Object[][]{
{new BigDecimal("-1"), "-1", true},
{new BigDecimal("-1.0"), "-1", true},
{BigDecimal.ZERO, "0", true},
{new BigDecimal("0.0"), "0", true},
{new BigDecimal("1.0"), "1", true},
{new BigDecimal("3.141519265358979323846264338"), "3.141519265358979323846264338", true},
});
TEST_DB.put(pair(byte[].class, String.class), new Object[][]{
{new byte[]{(byte) 0xf0, (byte) 0x9f, (byte) 0x8d, (byte) 0xba}, "\uD83C\uDF7A", true}, // beer mug, byte[] treated as UTF-8.
{new byte[]{(byte) 65, (byte) 66, (byte) 67, (byte) 68}, "ABCD", true}
Expand All @@ -785,9 +781,6 @@ private static void loadStringTests() {
TEST_DB.put(pair(ByteBuffer.class, String.class), new Object[][]{
{ByteBuffer.wrap(new byte[]{(byte) 0x30, (byte) 0x31, (byte) 0x32, (byte) 0x33}), "0123", true}
});
TEST_DB.put(pair(Class.class, String.class), new Object[][]{
{Date.class, "java.util.Date", true}
});
TEST_DB.put(pair(Date.class, String.class), new Object[][]{
{new Date(-1), "1970-01-01T08:59:59.999+09:00", true}, // Tokyo (set in options - defaults to system when not set explicitly)
{new Date(0), "1970-01-01T09:00:00.000+09:00", true},
Expand Down Expand Up @@ -2007,6 +2000,15 @@ private static void loadBigDecimalTests() {
{Instant.parse("1970-01-02T00:00:00Z"), new BigDecimal("86400"), true},
{Instant.parse("1970-01-02T00:00:00.000000001Z"), new BigDecimal("86400.000000001"), true},
});
TEST_DB.put(pair(String.class, BigDecimal.class), new Object[][]{
{"-1", new BigDecimal("-1"), true},
{"-1", new BigDecimal("-1.0"), true},
{"0", BigDecimal.ZERO, true},
{"0", new BigDecimal("0.0"), true},
{"1", new BigDecimal("1.0"), true},
{"3.141519265358979323846264338", new BigDecimal("3.141519265358979323846264338"), true},
{"1.gf.781", new IllegalArgumentException("not parseable")},
});
TEST_DB.put(pair(Map.class, BigDecimal.class), new Object[][]{
{mapOf("_v", "0"), BigDecimal.ZERO},
{mapOf("_v", BigDecimal.valueOf(0)), BigDecimal.ZERO, true},
Expand Down Expand Up @@ -2384,6 +2386,8 @@ private static void loadBooleanTests() {
{mapOf("_v", "0"), false},
{mapOf("_v", "1"), true},
{mapOf("_v", mapOf("_v", 5.0)), true},
{mapOf(VALUE, true), true, true},
{mapOf(VALUE, false), false, true},
});
TEST_DB.put(pair(String.class, Boolean.class), new Object[][]{
{"0", false},
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/cedarsoftware/util/convert/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -4271,6 +4272,32 @@ void testKnownUnsupportedConversions() {
.hasMessageContaining("Unsupported conversion");
}

@Test
void testForExceptionsThatAreNotIllegalArgument() {
Map<Class<?>, Set<Class<?>>> map = com.cedarsoftware.util.Converter.allSupportedConversions();

for (Map.Entry<Class<?>, Set<Class<?>>> entry : map.entrySet()) {
Class<?> sourceClass = entry.getKey();
try {
converter.convert("junky", sourceClass);
} catch (IllegalArgumentException ok) {
} catch (Throwable e) {
fail("Conversion throwing an exception that is not an IllegalArgumentException");
}

Set<Class<?>> targetClasses = entry.getValue();
for (Class<?> targetClass : targetClasses) {
try {
converter.convert("junky", targetClass);
} catch (IllegalArgumentException ok) {
} catch (Throwable e) {
fail("Conversion throwing an exception that is not an IllegalArgumentException");
}
}
}

}

private ConverterOptions createCharsetOptions(final Charset charset) {
return new ConverterOptions() {
@Override
Expand Down

0 comments on commit 1eab09e

Please sign in to comment.