8 changed files with 200 additions and 40 deletions
@ -0,0 +1,87 @@ |
|||||||
|
package org.leolo.nrdatad; |
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogBuilder; |
||||||
|
import org.apache.logging.log4j.LogManager; |
||||||
|
import org.apache.logging.log4j.Logger; |
||||||
|
import org.junit.*; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.io.FileNotFoundException; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.PrintWriter; |
||||||
|
import java.util.Random; |
||||||
|
import static org.junit.Assert.*; |
||||||
|
|
||||||
|
public class ConfigurationTest { |
||||||
|
|
||||||
|
public static final char[] randomChars = "1234567890qwertyuiopasdfghjklzxcvbnm".toCharArray(); |
||||||
|
private static Logger log = LogManager.getLogger(); |
||||||
|
|
||||||
|
private static String fileName; |
||||||
|
|
||||||
|
@Before public void setUpClass(){ |
||||||
|
int round = 0; |
||||||
|
while(true) { |
||||||
|
fileName = "test-"+getRandomFileName()+".tmp"; |
||||||
|
log.always().log("File name is {}", fileName); |
||||||
|
if(!new File(fileName).exists()){ |
||||||
|
break; |
||||||
|
} |
||||||
|
if(++round>48){ |
||||||
|
assertTrue("Cannot get a file name", false); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static String getRandomFileName(){ |
||||||
|
StringBuilder sb = new StringBuilder(); |
||||||
|
Random r = new Random(); |
||||||
|
for(int i=0;i<16;i++){ |
||||||
|
sb.append(randomChars[r.nextInt(randomChars.length)]); |
||||||
|
} |
||||||
|
return sb.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test public void basicLoad() throws Exception{ |
||||||
|
assertEquals("Dirty configs", 0, ConfigurationManager.getInstance().size()); |
||||||
|
//Step 1: Create a simple one
|
||||||
|
try(PrintWriter out = new PrintWriter(new File(fileName))){ |
||||||
|
out.println("key1=value1"); |
||||||
|
out.println("key2=value2"); |
||||||
|
out.println("key3=value3"); |
||||||
|
out.println("key4=value4"); |
||||||
|
out.println("#key5=value5"); |
||||||
|
}catch(IOException e){ |
||||||
|
log.always().withThrowable(e).log("Unable to test"); |
||||||
|
assert false; |
||||||
|
} |
||||||
|
ConfigurationManager.getInstance().loadConfiguration(fileName); |
||||||
|
assertEquals("Config key count mismatch", 4, ConfigurationManager.getInstance().size()); |
||||||
|
assertEquals("Config key count mismatch", "value1", ConfigurationManager.getInstance().getProperty("key1")); |
||||||
|
assertEquals("Config key count mismatch", "value3", ConfigurationManager.getInstance().getProperty("key3")); |
||||||
|
assertEquals("Config key count mismatch", "value4", ConfigurationManager.getInstance().getProperty("key4")); |
||||||
|
assertEquals("Config key count mismatch", "value2", ConfigurationManager.getInstance().getProperty("key2")); |
||||||
|
|
||||||
|
assertEquals("Config key count mismatch", null, ConfigurationManager.getInstance().getProperty("key5")); |
||||||
|
} |
||||||
|
|
||||||
|
@Test public void notExistFile(){ |
||||||
|
assertEquals("Dirty configs", 0, ConfigurationManager.getInstance().size()); |
||||||
|
try { |
||||||
|
ConfigurationManager.getInstance().loadConfiguration(fileName); |
||||||
|
}catch(IOException e){ |
||||||
|
return; |
||||||
|
} |
||||||
|
assertFalse("Exception not thrown", true); |
||||||
|
} |
||||||
|
|
||||||
|
@After public void cleanUpEach(){ |
||||||
|
if(new File(fileName).delete()) |
||||||
|
log.always().log("Removed file {}", fileName); |
||||||
|
ConfigurationManager.getInstance().clear(); |
||||||
|
} |
||||||
|
|
||||||
|
@AfterClass public static void cleanup(){ |
||||||
|
new File(fileName).deleteOnExit(); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue