|
| 1 | +package com.baeldung.java.properties; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 5 | + |
| 6 | +import java.io.FileInputStream; |
| 7 | +import java.io.FileOutputStream; |
| 8 | +import java.io.FileWriter; |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.Properties; |
| 11 | + |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +public class PropertiesUnitTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + public void givenPropertyValue_whenPropertiesFileLoaded_thenCorrect() throws IOException { |
| 18 | + |
| 19 | + String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); |
| 20 | + String appConfigPath = rootPath + "app.properties"; |
| 21 | + String catalogConfigPath = rootPath + "catalog"; |
| 22 | + |
| 23 | + Properties appProps = new Properties(); |
| 24 | + appProps.load(new FileInputStream(appConfigPath)); |
| 25 | + |
| 26 | + Properties catalogProps = new Properties(); |
| 27 | + catalogProps.load(new FileInputStream(catalogConfigPath)); |
| 28 | + |
| 29 | + String appVersion = appProps.getProperty("version"); |
| 30 | + assertEquals("1.0", appVersion); |
| 31 | + |
| 32 | + assertEquals("files", catalogProps.getProperty("c1")); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void givenPropertyValue_whenXMLPropertiesFileLoaded_thenCorrect() throws IOException { |
| 37 | + |
| 38 | + String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); |
| 39 | + String iconConfigPath = rootPath + "icons.xml"; |
| 40 | + Properties iconProps = new Properties(); |
| 41 | + iconProps.loadFromXML(new FileInputStream(iconConfigPath)); |
| 42 | + |
| 43 | + assertEquals("icon1.jpg", iconProps.getProperty("fileIcon")); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void givenAbsentProperty_whenPropertiesFileLoaded_thenReturnsDefault() throws IOException { |
| 48 | + |
| 49 | + String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); |
| 50 | + String appConfigPath = rootPath + "app.properties"; |
| 51 | + Properties appProps = new Properties(); |
| 52 | + appProps.load(new FileInputStream(appConfigPath)); |
| 53 | + |
| 54 | + String appVersion = appProps.getProperty("version"); |
| 55 | + String appName = appProps.getProperty("name", "defaultName"); |
| 56 | + String appGroup = appProps.getProperty("group", "baeldung"); |
| 57 | + String appDownloadAddr = appProps.getProperty("downloadAddr"); |
| 58 | + |
| 59 | + assertEquals("1.0", appVersion); |
| 60 | + assertEquals("TestApp", appName); |
| 61 | + assertEquals("baeldung", appGroup); |
| 62 | + assertNull(appDownloadAddr); |
| 63 | + } |
| 64 | + |
| 65 | + @Test(expected = Exception.class) |
| 66 | + public void givenImproperObjectCasting_whenPropertiesFileLoaded_thenThrowsException() throws IOException { |
| 67 | + |
| 68 | + String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); |
| 69 | + String appConfigPath = rootPath + "app.properties"; |
| 70 | + Properties appProps = new Properties(); |
| 71 | + appProps.load(new FileInputStream(appConfigPath)); |
| 72 | + |
| 73 | + float appVerFloat = (float) appProps.get("version"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void givenPropertyValue_whenPropertiesSet_thenCorrect() throws IOException { |
| 78 | + |
| 79 | + String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); |
| 80 | + String appConfigPath = rootPath + "app.properties"; |
| 81 | + Properties appProps = new Properties(); |
| 82 | + appProps.load(new FileInputStream(appConfigPath)); |
| 83 | + |
| 84 | + appProps.setProperty("name", "NewAppName"); |
| 85 | + appProps.setProperty("downloadAddr", "www.baeldung.com/downloads"); |
| 86 | + |
| 87 | + String newAppName = appProps.getProperty("name"); |
| 88 | + assertEquals("NewAppName", newAppName); |
| 89 | + |
| 90 | + String newAppDownloadAddr = appProps.getProperty("downloadAddr"); |
| 91 | + assertEquals("www.baeldung.com/downloads", newAppDownloadAddr); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void givenPropertyValueNull_whenPropertiesRemoved_thenCorrect() throws IOException { |
| 96 | + |
| 97 | + String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); |
| 98 | + String appConfigPath = rootPath + "app.properties"; |
| 99 | + Properties appProps = new Properties(); |
| 100 | + appProps.load(new FileInputStream(appConfigPath)); |
| 101 | + |
| 102 | + String versionBeforeRemoval = appProps.getProperty("version"); |
| 103 | + assertEquals("1.0", versionBeforeRemoval); |
| 104 | + |
| 105 | + appProps.remove("version"); |
| 106 | + String versionAfterRemoval = appProps.getProperty("version"); |
| 107 | + assertNull(versionAfterRemoval); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void whenPropertiesStoredInFile_thenCorrect() throws IOException { |
| 112 | + |
| 113 | + String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); |
| 114 | + String appConfigPath = rootPath + "app.properties"; |
| 115 | + Properties appProps = new Properties(); |
| 116 | + appProps.load(new FileInputStream(appConfigPath)); |
| 117 | + |
| 118 | + String newAppConfigPropertiesFile = rootPath + "newApp.properties"; |
| 119 | + appProps.store(new FileWriter(newAppConfigPropertiesFile), "store to properties file"); |
| 120 | + |
| 121 | + String newAppConfigXmlFile = rootPath + "newApp.xml"; |
| 122 | + appProps.storeToXML(new FileOutputStream(newAppConfigXmlFile), "store to xml file"); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void givenPropertyValueAbsent_LoadsValuesFromDefaultProperties() throws IOException { |
| 127 | + |
| 128 | + String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); |
| 129 | + |
| 130 | + String defaultConfigPath = rootPath + "default.properties"; |
| 131 | + Properties defaultProps = new Properties(); |
| 132 | + defaultProps.load(new FileInputStream(defaultConfigPath)); |
| 133 | + |
| 134 | + String appConfigPath = rootPath + "app.properties"; |
| 135 | + Properties appProps = new Properties(defaultProps); |
| 136 | + appProps.load(new FileInputStream(appConfigPath)); |
| 137 | + |
| 138 | + String appName = appProps.getProperty("name"); |
| 139 | + String appVersion = appProps.getProperty("version"); |
| 140 | + String defaultSite = appProps.getProperty("site"); |
| 141 | + |
| 142 | + assertEquals("1.0", appVersion); |
| 143 | + assertEquals("TestApp", appName); |
| 144 | + assertEquals("www.google.com", defaultSite); |
| 145 | + } |
| 146 | +} |
0 commit comments