|
| 1 | +package com.baeldung.commons.configuration; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.util.List; |
| 10 | +import java.util.NoSuchElementException; |
| 11 | + |
| 12 | +import org.apache.commons.configuration2.AbstractConfiguration; |
| 13 | +import org.apache.commons.configuration2.Configuration; |
| 14 | +import org.apache.commons.configuration2.PropertiesConfiguration; |
| 15 | +import org.apache.commons.configuration2.XMLConfiguration; |
| 16 | +import org.apache.commons.configuration2.builder.fluent.Configurations; |
| 17 | +import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler; |
| 18 | +import org.apache.commons.configuration2.ex.ConfigurationException; |
| 19 | +import org.apache.commons.configuration2.ex.ConversionException; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +class ConfigurationClassUnitTest { |
| 23 | + |
| 24 | + @Test |
| 25 | + void givenPropertiesFile_whenReadingWithConfigurationClass_thenIsLoaded() throws ConfigurationException { |
| 26 | + Configurations configs = new Configurations(); |
| 27 | + Configuration config = configs.properties(new File("src/test/resources/configuration/file.properties")); |
| 28 | + String dbHost = config.getString("db.host"); |
| 29 | + int dbPort = config.getInt("db.port"); |
| 30 | + String dbUser = config.getString("db.user"); |
| 31 | + String dbPassword = config.getString("undefinedKey", "defaultValue"); |
| 32 | + assertEquals("baeldung.com", dbHost); |
| 33 | + assertEquals(9999, dbPort); |
| 34 | + assertEquals("admin", dbUser); |
| 35 | + assertEquals("defaultValue", dbPassword); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void givenXMLFile_whenReadingWithConfigurationClass_thenIsLoaded() throws ConfigurationException { |
| 40 | + Configurations configs = new Configurations(); |
| 41 | + XMLConfiguration config = configs.xml(new File("src/test/resources/configuration/hierarchical.xml")); |
| 42 | + String appender = config.getString("appender[@name]"); |
| 43 | + List<String> encoderPatterns = config.getList(String.class, "appender.encoder.pattern"); |
| 44 | + String pattern1 = config.getString("appender.encoder.pattern(0)"); |
| 45 | + assertEquals("STDOUT", appender); |
| 46 | + assertEquals(2, encoderPatterns.size()); |
| 47 | + assertEquals("Pattern1", pattern1); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void givenPropertiesFile_whenCopyingConfiguration_thenIsSuccessful() throws ConfigurationException { |
| 52 | + Configurations configs = new Configurations(); |
| 53 | + Configuration baseConfig = configs.properties(new File("src/test/resources/configuration/file.properties")); |
| 54 | + Configuration subConfig = new PropertiesConfiguration(); |
| 55 | + subConfig.addProperty("db.host", "baeldung"); |
| 56 | + subConfig.addProperty("db.driver", "dummyDriver"); |
| 57 | + ((AbstractConfiguration) subConfig).copy(baseConfig); |
| 58 | + String dbHost = subConfig.getString("db.host"); |
| 59 | + String dbDriver = subConfig.getString("db.driver"); |
| 60 | + int dbPort = subConfig.getInt("db.port"); |
| 61 | + String dbUser = subConfig.getString("db.user"); |
| 62 | + assertEquals("baeldung.com", dbHost); |
| 63 | + assertEquals(9999, dbPort); |
| 64 | + assertEquals("admin", dbUser); |
| 65 | + assertEquals("dummyDriver", dbDriver); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void givenPropertiesFile_whenAppendingConfiguration_thenIsSuccessful() throws ConfigurationException { |
| 70 | + Configurations configs = new Configurations(); |
| 71 | + Configuration baseConfig = configs.properties(new File("src/test/resources/configuration/file.properties")); |
| 72 | + Configuration subConfig = new PropertiesConfiguration(); |
| 73 | + subConfig.addProperty("db.host", "baeldung"); |
| 74 | + subConfig.addProperty("db.driver", "dummyDriver"); |
| 75 | + ((AbstractConfiguration) subConfig).append(baseConfig); |
| 76 | + String dbHost = subConfig.getString("db.host"); |
| 77 | + String dbDriver = subConfig.getString("db.driver"); |
| 78 | + int dbPort = subConfig.getInt("db.port"); |
| 79 | + String dbUser = subConfig.getString("db.user"); |
| 80 | + assertEquals("baeldung", dbHost); |
| 81 | + assertEquals(9999, dbPort); |
| 82 | + assertEquals("admin", dbUser); |
| 83 | + assertEquals("dummyDriver", dbDriver); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void givenXMLFile_whenCloningConfiguration_thenIsSuccessful() throws ConfigurationException { |
| 88 | + Configurations configs = new Configurations(); |
| 89 | + XMLConfiguration baseConfig = configs.xml(new File("src/test/resources/configuration/hierarchical.xml")); |
| 90 | + XMLConfiguration subConfig = new XMLConfiguration(); |
| 91 | + //subConfig = (XMLConfiguration) baseConfig.clone(); |
| 92 | + subConfig = new XMLConfiguration(baseConfig); |
| 93 | + String appender = subConfig.getString("appender[@name]"); |
| 94 | + List<String> encoderPatterns = subConfig.getList(String.class, "appender.encoder.pattern"); |
| 95 | + String pattern1 = subConfig.getString("appender.encoder.pattern(0)"); |
| 96 | + assertEquals("STDOUT", appender); |
| 97 | + assertEquals(2, encoderPatterns.size()); |
| 98 | + assertEquals("Pattern1", pattern1); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void givenEncodedProperty_whenCustomDecoderImplemented_thenIsSuccessful() throws ConfigurationException { |
| 103 | + Configurations configs = new Configurations(); |
| 104 | + Configuration config = configs.properties(new File("src/test/resources/configuration/file.properties")); |
| 105 | + ((AbstractConfiguration) config).setConfigurationDecoder(new CustomDecoder()); |
| 106 | + assertEquals("mySecretString", config.getEncodedString("db.password")); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void whenDataTypeConversionAttempted_thenIsSuccessful() { |
| 111 | + Configuration config = new PropertiesConfiguration(); |
| 112 | + config.addProperty("stringProperty", "This is a string"); |
| 113 | + config.addProperty("numericProperty", "9999"); |
| 114 | + config.addProperty("booleanProperty", "true"); |
| 115 | + assertEquals("This is a string", config.getString("stringProperty")); |
| 116 | + assertEquals(9999, config.getInt("numericProperty")); |
| 117 | + assertTrue(config.getBoolean("booleanProperty")); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void whenDataTypeConversionAttempted_thenThrowsException() { |
| 122 | + Configuration config = new PropertiesConfiguration(); |
| 123 | + config.addProperty("numericProperty", "9999a"); |
| 124 | + assertThrows(ConversionException.class, () -> config.getInt("numericProperty")); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + void whenInterpolationIsAttempted_thenIsSuccessful() throws ConfigurationException { |
| 129 | + System.setProperty("user.name", "Baeldung"); |
| 130 | + Configurations configs = new Configurations(); |
| 131 | + Configuration config = configs.properties(new File("src/test/resources/configuration/file.properties")); |
| 132 | + String dbUrl = config.getString("db.url"); |
| 133 | + String userName = config.getString("db.username"); |
| 134 | + String externalService = config.getString("db.external-service"); |
| 135 | + assertEquals("baeldung.com:9999", dbUrl); |
| 136 | + assertEquals("Baeldung", userName); |
| 137 | + assertEquals("https://www.baeldung.com", externalService); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void whenDelimiterIsSpecified_thenMultiValuePropertyIsLoaded() { |
| 142 | + PropertiesConfiguration propertiesConfig = new PropertiesConfiguration(); |
| 143 | + propertiesConfig.setListDelimiterHandler(new DefaultListDelimiterHandler(';')); |
| 144 | + propertiesConfig.addProperty("delimitedProperty", "admin;read-only;read-write"); |
| 145 | + propertiesConfig.addProperty("arrayProperty", "value1;value2"); |
| 146 | + List<Object> delimitedProperties = propertiesConfig.getList("delimitedProperty"); |
| 147 | + String[] arrayProperties = propertiesConfig.getStringArray("arrayProperty"); |
| 148 | + assertEquals(3, delimitedProperties.size()); |
| 149 | + assertEquals("admin", delimitedProperties.get(0)); |
| 150 | + assertEquals(2, arrayProperties.length); |
| 151 | + assertEquals("value1", propertiesConfig.getString("arrayProperty")); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + void whenPropertiesAreMissing_thenIsHandled() { |
| 156 | + PropertiesConfiguration propertiesConfig = new PropertiesConfiguration(); |
| 157 | + String objectProperty = propertiesConfig.getString("anyProperty"); |
| 158 | + int primitiveProperty = propertiesConfig.getInt("anyProperty", 1); |
| 159 | + assertNull(objectProperty); |
| 160 | + assertEquals(1, primitiveProperty); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + void whenPropertiesAreMissing_thenExceptionIsThrown() { |
| 165 | + PropertiesConfiguration propertiesConfig = new PropertiesConfiguration(); |
| 166 | + assertThrows(NoSuchElementException.class, () -> propertiesConfig.getInt("anyProperty")); |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments