KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > PropertiesPersisterTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.util;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.StringReader JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import junit.framework.TestCase;
27
28 /**
29  * @author Juergen Hoeller
30  * @since 11.01.2005
31  */

32 public class PropertiesPersisterTests extends TestCase {
33
34     public void testPropertiesPersister() throws IOException JavaDoc {
35         String JavaDoc propString = "code1=message1\ncode2:message2";
36         Properties JavaDoc props = loadProperties(propString, false);
37         String JavaDoc propCopy = storeProperties(props, null, false);
38         loadProperties(propCopy, false);
39     }
40
41     public void testPropertiesPersisterWithWhitespace() throws IOException JavaDoc {
42         String JavaDoc propString = " code1\t= \tmessage1\n code2 \t :\t mess\\\n \t age2";
43         Properties JavaDoc props = loadProperties(propString, false);
44         String JavaDoc propCopy = storeProperties(props, null, false);
45         loadProperties(propCopy, false);
46     }
47
48     public void testPropertiesPersisterWithHeader() throws IOException JavaDoc {
49         String JavaDoc propString = "code1=message1\ncode2:message2";
50         Properties JavaDoc props = loadProperties(propString, false);
51         String JavaDoc propCopy = storeProperties(props, "myHeader", false);
52         loadProperties(propCopy, false);
53     }
54
55     public void testPropertiesPersisterWithEmptyValue() throws IOException JavaDoc {
56         String JavaDoc propString = "code1=message1\ncode2:message2\ncode3=";
57         Properties JavaDoc props = loadProperties(propString, false);
58         String JavaDoc propCopy = storeProperties(props, null, false);
59         loadProperties(propCopy, false);
60     }
61
62     public void testPropertiesPersisterWithReader() throws IOException JavaDoc {
63         String JavaDoc propString = "code1=message1\ncode2:message2";
64         Properties JavaDoc props = loadProperties(propString, true);
65         String JavaDoc propCopy = storeProperties(props, null, true);
66         loadProperties(propCopy, false);
67     }
68
69     public void testPropertiesPersisterWithReaderAndWhitespace() throws IOException JavaDoc {
70         String JavaDoc propString = " code1\t= \tmessage1\n code2 \t :\t mess\\\n \t age2";
71         Properties JavaDoc props = loadProperties(propString, true);
72         String JavaDoc propCopy = storeProperties(props, null, true);
73         loadProperties(propCopy, false);
74     }
75
76     public void testPropertiesPersisterWithReaderAndHeader() throws IOException JavaDoc {
77         String JavaDoc propString = "code1\t=\tmessage1\n code2 \t : \t message2";
78         Properties JavaDoc props = loadProperties(propString, true);
79         String JavaDoc propCopy = storeProperties(props, "myHeader", true);
80         loadProperties(propCopy, false);
81     }
82
83     public void testPropertiesPersisterWithReaderAndEmptyValue() throws IOException JavaDoc {
84         String JavaDoc propString = "code1=message1\ncode2:message2\ncode3=";
85         Properties JavaDoc props = loadProperties(propString, true);
86         String JavaDoc propCopy = storeProperties(props, null, true);
87         loadProperties(propCopy, false);
88     }
89
90     private Properties JavaDoc loadProperties(String JavaDoc propString, boolean useReader) throws IOException JavaDoc {
91         DefaultPropertiesPersister persister = new DefaultPropertiesPersister();
92         Properties JavaDoc props = new Properties JavaDoc();
93         if (useReader) {
94             persister.load(props, new StringReader JavaDoc(propString));
95         }
96         else {
97             persister.load(props, new ByteArrayInputStream JavaDoc(propString.getBytes()));
98         }
99         assertEquals("message1", props.getProperty("code1"));
100         assertEquals("message2", props.getProperty("code2"));
101         return props;
102     }
103
104     private String JavaDoc storeProperties(Properties JavaDoc props, String JavaDoc header, boolean useWriter) throws IOException JavaDoc {
105         DefaultPropertiesPersister persister = new DefaultPropertiesPersister();
106         String JavaDoc propCopy = null;
107         if (useWriter) {
108             StringWriter JavaDoc propWriter = new StringWriter JavaDoc();
109             persister.store(props, propWriter, header);
110             propCopy = propWriter.toString();
111         }
112         else {
113             ByteArrayOutputStream JavaDoc propOut = new ByteArrayOutputStream JavaDoc();
114             persister.store(props, propOut, header);
115             propCopy = new String JavaDoc(propOut.toByteArray());
116         }
117         if (header != null) {
118             assertTrue(propCopy.indexOf(header) != -1);
119         }
120         assertTrue(propCopy.indexOf("\ncode1=message1") != -1);
121         assertTrue(propCopy.indexOf("\ncode2=message2") != -1);
122         return propCopy;
123     }
124
125 }
126
Popular Tags