1 37 package net.sourceforge.cruisecontrol.util; 38 39 import junit.framework.TestCase; 40 41 import java.io.BufferedWriter ; 42 import java.io.File ; 43 import java.io.FileWriter ; 44 import java.io.IOException ; 45 import java.util.Properties ; 46 47 import net.sourceforge.cruisecontrol.CruiseControlException; 48 49 public class UtilTest extends TestCase { 50 51 private File propsFile; 52 private Properties testProps; 53 54 public void setUp() throws Exception { 55 56 propsFile = File.createTempFile("testload", "properties"); 58 propsFile.deleteOnExit(); 59 StringBuffer props = new StringBuffer (); 60 props.append("#Test properties file\n"); 61 props.append("property1=value1\n"); 62 props.append("property2 = value2\n"); 63 props.append("property3=value3\n"); 64 BufferedWriter writer = new BufferedWriter (new FileWriter (propsFile)); 65 writer.write(props.toString()); 66 writer.close(); 67 68 testProps = new Properties (); 70 testProps.setProperty("stored1", "value1"); 71 testProps.setProperty("stored2", "value2"); 72 } 73 74 public void testLoadPropertiesFromFileMustExist() { 75 File file = new File ("NoSuchFile"); 76 try { 77 Util.loadPropertiesFromFile(file); 78 fail("A non-existant properties file should cause an exception!"); 79 } catch (Exception e) { 80 } 81 } 82 83 public void testLoadPropertiesFromFile() throws CruiseControlException, 84 IOException { 85 Properties properties = Util.loadPropertiesFromFile(propsFile); 86 assertEquals(3, properties.size()); 87 assertEquals("value1", properties.getProperty("property1")); 88 assertEquals("value2", properties.getProperty("property2")); 89 assertEquals("value3", properties.getProperty("property3")); 90 } 91 92 public void testStorePropertiesToFile() throws CruiseControlException, 93 IOException { 94 File file = File.createTempFile("teststore", "properties"); 95 file.deleteOnExit(); 96 Util.storePropertiesToFile(testProps, "Sample Header", file); 97 Properties properties = Util.loadPropertiesFromFile(file); 98 assertEquals(2, properties.size()); 99 assertEquals("value1", properties.getProperty("stored1")); 100 assertEquals("value2", properties.getProperty("stored2")); 101 } 102 } 103 | Popular Tags |