From 5d453e9b7425b96105d5a7d92a503f4de759c639 Mon Sep 17 00:00:00 2001 From: Ben Manes Date: Sun, 31 Mar 2024 09:48:48 -0700 Subject: [PATCH] improved spec parsing error handing --- .../github/benmanes/caffeine/cache/CaffeineSpec.java | 11 ++++++----- .../benmanes/caffeine/cache/CaffeineSpecTest.java | 10 ++++++++++ gradle/libs.versions.toml | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CaffeineSpec.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CaffeineSpec.java index 08edd68e7f..73ce1d61b5 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CaffeineSpec.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CaffeineSpec.java @@ -155,18 +155,19 @@ void parseOption(String option) { } @SuppressWarnings("StringSplitter") - String[] keyAndValue = option.split(SPLIT_KEY_VALUE); - requireArgument((keyAndValue.length >= 1) && (keyAndValue.length <= 2), + String[] keyAndValue = option.split(SPLIT_KEY_VALUE, 3); + requireArgument(keyAndValue.length >= 1, "blank key-value pair"); + requireArgument(keyAndValue.length <= 2, "key-value pair %s with more than one equals sign", option); String key = keyAndValue[0].trim(); String value = (keyAndValue.length == 1) ? null : keyAndValue[1].trim(); - configure(key, value); + configure(option, key, value); } /** Configures the setting. */ - void configure(String key, @Nullable String value) { + void configure(String option, String key, @Nullable String value) { switch (key) { case "initialCapacity": initialCapacity(key, value); @@ -199,7 +200,7 @@ void configure(String key, @Nullable String value) { recordStats(value); return; default: - throw new IllegalArgumentException("Unknown key " + key); + throw new IllegalArgumentException("Invalid option " + option); } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/CaffeineSpecTest.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/CaffeineSpecTest.java index 445d3f4405..97b1308a76 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/CaffeineSpecTest.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/CaffeineSpecTest.java @@ -69,6 +69,15 @@ public void parseTimeUnit_exception() { assertThrows(IllegalArgumentException.class, () -> CaffeineSpec.parseTimeUnit("key", "value")); } + @Test + public void parse_exception() { + assertThrows(IllegalArgumentException.class, () -> CaffeineSpec.parse("=")); + assertThrows(IllegalArgumentException.class, () -> CaffeineSpec.parse("==")); + assertThrows(IllegalArgumentException.class, () -> CaffeineSpec.parse("key=")); + assertThrows(IllegalArgumentException.class, () -> CaffeineSpec.parse("=value")); + assertThrows(IllegalArgumentException.class, () -> CaffeineSpec.parse("key=value=")); + } + @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY, initialCapacity = {InitialCapacity.DEFAULT, InitialCapacity.FULL}, @@ -122,6 +131,7 @@ private void runTest(CacheContext context, Epoch epoch, Function