KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > UtilTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.util;
38
39 import junit.framework.TestCase;
40
41 import java.io.BufferedWriter JavaDoc;
42 import java.io.File JavaDoc;
43 import java.io.FileWriter JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.util.Properties JavaDoc;
46
47 import net.sourceforge.cruisecontrol.CruiseControlException;
48
49 public class UtilTest extends TestCase {
50
51     private File JavaDoc propsFile;
52     private Properties JavaDoc testProps;
53
54     public void setUp() throws Exception JavaDoc {
55
56         //Create a properties file to test properties loading
57
propsFile = File.createTempFile("testload", "properties");
58         propsFile.deleteOnExit();
59         StringBuffer JavaDoc props = new StringBuffer JavaDoc();
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 JavaDoc writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(propsFile));
65         writer.write(props.toString());
66         writer.close();
67
68         //Create a Properties object to test storing properties
69
testProps = new Properties JavaDoc();
70         testProps.setProperty("stored1", "value1");
71         testProps.setProperty("stored2", "value2");
72     }
73
74     public void testLoadPropertiesFromFileMustExist() {
75         File JavaDoc file = new File JavaDoc("NoSuchFile");
76         try {
77             Util.loadPropertiesFromFile(file);
78             fail("A non-existant properties file should cause an exception!");
79         } catch (Exception JavaDoc e) {
80         }
81     }
82
83     public void testLoadPropertiesFromFile() throws CruiseControlException,
84             IOException JavaDoc {
85         Properties JavaDoc 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 JavaDoc {
94         File JavaDoc file = File.createTempFile("teststore", "properties");
95         file.deleteOnExit();
96         Util.storePropertiesToFile(testProps, "Sample Header", file);
97         Properties JavaDoc 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