From 449c7c7a67cc89c3516012b5c45da4ed44395172 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 19 Jul 2026 15:40:58 +0000 Subject: [PATCH] test: add 16 unit tests for ConfigManager edge cases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tests cover: - get(): missing key → null, present key → value - get(key, default): missing/present key with default - getInt(): missing key, valid int, negative int, non-numeric, decimal, empty string — all return default or parsed value - getBoolean(): missing key, "true"/"TRUE" → true, "false" → false, "yes"/"1" → false (Boolean.parseBoolean only accepts "true") Uses reflection to inject test values into the singleton's Properties field; @AfterEach removes test keys to avoid pollution. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../com/taskflow/util/ConfigManagerTest.java | 153 ++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 src/test/java/com/taskflow/util/ConfigManagerTest.java diff --git a/src/test/java/com/taskflow/util/ConfigManagerTest.java b/src/test/java/com/taskflow/util/ConfigManagerTest.java new file mode 100644 index 0000000..c06c40d --- /dev/null +++ b/src/test/java/com/taskflow/util/ConfigManagerTest.java @@ -0,0 +1,153 @@ +package com.taskflow.util; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Field; +import java.util.Properties; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Unit tests for ConfigManager edge-case behaviour. + * + * ConfigManager is a non-resettable singleton whose internal {@link Properties} + * object is only accessible via reflection. Tests manipulate that object + * directly, cleaning up in @AfterEach to avoid cross-test pollution. + */ +class ConfigManagerTest { + + private static final String TEST_KEY_INT = "test.configmanager.int"; + private static final String TEST_KEY_BOOL = "test.configmanager.bool"; + private static final String TEST_KEY_STR = "test.configmanager.str"; + + // ----------------------------------------------------------------------- + // Helpers + // ----------------------------------------------------------------------- + + private Properties getConfigProps() throws Exception { + ConfigManager cm = ConfigManager.getInstance(); + Field f = ConfigManager.class.getDeclaredField("config"); + f.setAccessible(true); + return (Properties) f.get(cm); + } + + private void setProperty(String key, String value) throws Exception { + getConfigProps().setProperty(key, value); + } + + @AfterEach + void cleanup() throws Exception { + Properties props = getConfigProps(); + props.remove(TEST_KEY_INT); + props.remove(TEST_KEY_BOOL); + props.remove(TEST_KEY_STR); + } + + // ----------------------------------------------------------------------- + // get(String key) and get(String key, String defaultValue) + // ----------------------------------------------------------------------- + + @Test + void get_missingKey_returnsNull() { + assertNull(ConfigManager.getInstance().get(TEST_KEY_STR)); + } + + @Test + void get_presentKey_returnsValue() throws Exception { + setProperty(TEST_KEY_STR, "hello"); + assertEquals("hello", ConfigManager.getInstance().get(TEST_KEY_STR)); + } + + @Test + void get_missingKeyWithDefault_returnsDefault() { + assertEquals("fallback", ConfigManager.getInstance().get(TEST_KEY_STR, "fallback")); + } + + @Test + void get_presentKeyWithDefault_returnsValue() throws Exception { + setProperty(TEST_KEY_STR, "actual"); + assertEquals("actual", ConfigManager.getInstance().get(TEST_KEY_STR, "fallback")); + } + + // ----------------------------------------------------------------------- + // getInt + // ----------------------------------------------------------------------- + + @Test + void getInt_missingKey_returnsDefault() { + assertEquals(42, ConfigManager.getInstance().getInt(TEST_KEY_INT, 42)); + } + + @Test + void getInt_validIntValue_returnsParsedInt() throws Exception { + setProperty(TEST_KEY_INT, "7"); + assertEquals(7, ConfigManager.getInstance().getInt(TEST_KEY_INT, 0)); + } + + @Test + void getInt_negativeIntValue_returnsParsedInt() throws Exception { + setProperty(TEST_KEY_INT, "-5"); + assertEquals(-5, ConfigManager.getInstance().getInt(TEST_KEY_INT, 0)); + } + + @Test + void getInt_nonNumericValue_returnsDefault() throws Exception { + setProperty(TEST_KEY_INT, "not-a-number"); + assertEquals(99, ConfigManager.getInstance().getInt(TEST_KEY_INT, 99)); + } + + @Test + void getInt_decimalValue_returnsDefault() throws Exception { + // Integer.parseInt("3.14") throws NumberFormatException → default + setProperty(TEST_KEY_INT, "3.14"); + assertEquals(0, ConfigManager.getInstance().getInt(TEST_KEY_INT, 0)); + } + + @Test + void getInt_emptyString_returnsDefault() throws Exception { + setProperty(TEST_KEY_INT, ""); + assertEquals(5, ConfigManager.getInstance().getInt(TEST_KEY_INT, 5)); + } + + // ----------------------------------------------------------------------- + // getBoolean + // ----------------------------------------------------------------------- + + @Test + void getBoolean_missingKey_returnsDefault() { + assertTrue(ConfigManager.getInstance().getBoolean(TEST_KEY_BOOL, true)); + assertFalse(ConfigManager.getInstance().getBoolean(TEST_KEY_BOOL, false)); + } + + @Test + void getBoolean_trueString_returnsTrue() throws Exception { + setProperty(TEST_KEY_BOOL, "true"); + assertTrue(ConfigManager.getInstance().getBoolean(TEST_KEY_BOOL, false)); + } + + @Test + void getBoolean_trueStringCaseInsensitive_returnsTrue() throws Exception { + setProperty(TEST_KEY_BOOL, "TRUE"); + assertTrue(ConfigManager.getInstance().getBoolean(TEST_KEY_BOOL, false)); + } + + @Test + void getBoolean_falseString_returnsFalse() throws Exception { + setProperty(TEST_KEY_BOOL, "false"); + assertFalse(ConfigManager.getInstance().getBoolean(TEST_KEY_BOOL, true)); + } + + /** Boolean.parseBoolean treats anything other than "true" (case-insensitive) as false. */ + @Test + void getBoolean_yesString_returnsFalse() throws Exception { + setProperty(TEST_KEY_BOOL, "yes"); + assertFalse(ConfigManager.getInstance().getBoolean(TEST_KEY_BOOL, true)); + } + + @Test + void getBoolean_numericOne_returnsFalse() throws Exception { + setProperty(TEST_KEY_BOOL, "1"); + assertFalse(ConfigManager.getInstance().getBoolean(TEST_KEY_BOOL, true)); + } +}