aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java
blob: 89cd3daaa0f13a7c1eff5215906a90765f368598 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.tools.ant.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Properties;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class LayoutPreservingPropertiesTest {

    /**
     * Tests that a properties file read by the
     * LayoutPreservingPropertiesFile and then saves the properties in
     * it.
     */
    @Test
    public void testPreserve() throws Exception {
        File simple = new File(System.getProperty("root"),
                               "src/etc/testcases/util/simple.properties");
        FileInputStream fis = new FileInputStream(simple);
        LayoutPreservingProperties lpf = new LayoutPreservingProperties();
        lpf.load(fis);

        File tmp = File.createTempFile("tmp", "props");
        tmp.deleteOnExit();
        lpf.saveAs(tmp);

        // now compare original and tmp for property equivalence
        Properties originalProps = new Properties();
        originalProps.load(new FileInputStream(simple));

        Properties tmpProps = new Properties();
        tmpProps.load(new FileInputStream(tmp));

        assertEquals("properties corrupted", originalProps, tmpProps);

        // and now make sure that the comments made it into the new file
        String s = readFile(tmp);
        assertTrue("missing comment", s.indexOf("# a comment") > -1);
        assertTrue("missing comment", s.indexOf("! more comment") > -1);
    }

    /**
     * Tests that names and value are properly escaped when being
     * written out.
     */
    @Test
    public void testEscaping() throws Exception {
        LayoutPreservingProperties lpf = new LayoutPreservingProperties();

        lpf.setProperty(" prop one ", "  leading and trailing spaces ");
        lpf.setProperty("prop\ttwo", "contains\ttab");
        lpf.setProperty("prop\nthree", "contains\nnewline");
        lpf.setProperty("prop\rfour", "contains\rcarraige return");
        lpf.setProperty("prop\ffive", "contains\fform feed");
        lpf.setProperty("prop\\six", "contains\\backslash");
        lpf.setProperty("prop:seven", "contains:colon");
        lpf.setProperty("prop=eight", "contains=equals");
        lpf.setProperty("prop#nine", "contains#hash");
        lpf.setProperty("prop!ten", "contains!exclamation");

        File tmp = File.createTempFile("tmp", "props");
        tmp.deleteOnExit();
        lpf.saveAs(tmp);

        // and check that the resulting file looks okay
        String s = readFile(tmp);

        assertTrue(s.indexOf("\\ prop\\ one\\ =\\ \\ leading and trailing"
                             + " spaces ") > -1);
        assertTrue(s.indexOf("prop\\ttwo=contains\\ttab") > -1);
        assertTrue(s.indexOf("prop\\nthree=contains\\nnewline") > -1);
        assertTrue(s.indexOf("prop\\rfour=contains\\rcarraige return") > -1);
        assertTrue(s.indexOf("prop\\\\six=contains\\\\backslash") > -1);
        assertTrue(s.indexOf("prop\\:seven=contains\\:colon") > -1);
        assertTrue(s.indexOf("prop\\=eight=contains\\=equals") > -1);
        assertTrue(s.indexOf("prop\\#nine=contains\\#hash") > -1);
        assertTrue(s.indexOf("prop\\!ten=contains\\!exclamation") > -1);
    }

    /**
     * Tests that properties are correctly indexed, so that when we set
     * an existing property, it updates the logical line, and it doesn't
     * append a new one.
     */
    @Test
    public void testOverwrite() throws Exception {
        File unusual = new File(System.getProperty("root"),
                                "src/etc/testcases/util/unusual.properties");
        FileInputStream fis = new FileInputStream(unusual);
        LayoutPreservingProperties lpf = new LayoutPreservingProperties();
        lpf.load(fis);

        lpf.setProperty(" prop one ", "new one");
        lpf.setProperty("prop\ttwo", "new two");
        lpf.setProperty("prop\nthree", "new three");

        File tmp = File.createTempFile("tmp", "props");
        tmp.deleteOnExit();
        lpf.saveAs(tmp);

        // and check that the resulting file looks okay
        String s = readFile(tmp);

        assertTrue(s.indexOf("\\ prop\\ one\\ =\\ \\ leading and"
                             + " trailing spaces ") == -1);
        assertTrue(s.indexOf("\\ prop\\ one\\ =new one") > -1);
        assertTrue(s.indexOf("prop\\ttwo=contains\\ttab") == -1);
        assertTrue(s.indexOf("prop\\ttwo=new two") > -1);
        assertTrue(s.indexOf("prop\\nthree=contains\\nnewline") == -1);
        assertTrue(s.indexOf("prop\\nthree=new three") > -1);
    }

    @Test
    public void testStoreWithHeader() throws Exception {
        File simple = new File(System.getProperty("root"),
                               "src/etc/testcases/util/simple.properties");
        FileInputStream fis = new FileInputStream(simple);
        LayoutPreservingProperties lpf = new LayoutPreservingProperties();
        lpf.load(fis);

        File tmp = File.createTempFile("tmp", "props");
        tmp.deleteOnExit();
        FileOutputStream fos = new FileOutputStream(tmp);
        lpf.store(fos, "file-header");
        fos.close();

        // and check that the resulting file looks okay
        String s = readFile(tmp);

        assertTrue("should have had header ", s.startsWith("#file-header"));
    }

    @Test
    public void testClear() throws Exception {
        File simple = new File(System.getProperty("root"),
                               "src/etc/testcases/util/simple.properties");
        FileInputStream fis = new FileInputStream(simple);
        LayoutPreservingProperties lpf = new LayoutPreservingProperties();
        lpf.load(fis);

        lpf.clear();

        File tmp = File.createTempFile("tmp", "props");
        tmp.deleteOnExit();
        lpf.saveAs(tmp);

        // and check that the resulting file looks okay
        String s = readFile(tmp);

        assertTrue("should have had no properties ",
                   s.indexOf("prop.alpha") == -1);
        assertTrue("should have had no properties ",
                   s.indexOf("prop.beta") == -1);
        assertTrue("should have had no properties ",
                   s.indexOf("prop.gamma") == -1);

        assertTrue("should have had no comments",
                   s.indexOf("# a comment") == -1);
        assertTrue("should have had no comments",
                   s.indexOf("! more comment") == -1);
        assertTrue("should have had no comments",
                   s.indexOf("# now a line wrapping one") == -1);
    }

    @Test
    public void testRemove() throws Exception {
        File simple = new File(System.getProperty("root"),
                               "src/etc/testcases/util/simple.properties");
        FileInputStream fis = new FileInputStream(simple);
        LayoutPreservingProperties lpf = new LayoutPreservingProperties();
        lpf.load(fis);

        lpf.remove("prop.beta");

        File tmp = File.createTempFile("tmp", "props");
        tmp.deleteOnExit();
        lpf.saveAs(tmp);

        // and check that the resulting file looks okay
        String s = readFile(tmp);

        assertTrue("should not have had prop.beta",
                   s.indexOf("prop.beta") == -1);
        assertTrue("should have had prop.beta's comment",
                   s.indexOf("! more comment") > -1);
    }

    @Test
    public void testRemoveWithComment() throws Exception {
        File simple = new File(System.getProperty("root"),
                               "src/etc/testcases/util/simple.properties");
        FileInputStream fis = new FileInputStream(simple);
        LayoutPreservingProperties lpf = new LayoutPreservingProperties();
        lpf.load(fis);

        lpf.setRemoveComments(true);

        lpf.remove("prop.beta");

        File tmp = File.createTempFile("tmp", "props");
        tmp.deleteOnExit();
        lpf.saveAs(tmp);

        // and check that the resulting file looks okay
        String s = readFile(tmp);

        assertTrue("should not have had prop.beta",
                   s.indexOf("prop.beta") == -1);
        assertTrue("should not have had prop.beta's comment",
                   s.indexOf("! more comment") == -1);
    }

    @Test
    public void testClone() throws Exception {
        File simple = new File(System.getProperty("root"),
                               "src/etc/testcases/util/simple.properties");
        FileInputStream fis = new FileInputStream(simple);
        LayoutPreservingProperties lpf1 = new LayoutPreservingProperties();
        lpf1.load(fis);

        LayoutPreservingProperties lpf2 =
            (LayoutPreservingProperties) lpf1.clone();

        lpf2.setProperty("prop.new", "a new property");
        lpf2.setProperty("prop.beta", "a new value for beta");

        assertEquals("size of original is wrong", 3, lpf1.size());
        assertEquals("size of clone is wrong", 4, lpf2.size());

        File tmp1 = File.createTempFile("tmp", "props");
        tmp1.deleteOnExit();
        lpf1.saveAs(tmp1);
        String s1 = readFile(tmp1);

        File tmp2 = File.createTempFile("tmp", "props");
        tmp2.deleteOnExit();
        lpf2.saveAs(tmp2);
        String s2 = readFile(tmp2);

        // check original is untouched
        assertTrue("should have had 'simple'", s1.indexOf("simple") > -1);
        assertTrue("should not have had prop.new", s1.indexOf("prop.new") == -1);

        // check clone has the changes
        assertTrue("should have had 'a new value for beta'",
                   s2.indexOf("a new value for beta") > -1);
        assertTrue("should have had prop.new", s2.indexOf("prop.new") > -1);
    }

    @Test
    public void testPreserveEscapeName() throws Exception {
        LayoutPreservingProperties lpf = new LayoutPreservingProperties();
        File unusual = new File(System.getProperty("root"),
                                "src/etc/testcases/util/unusual.properties");
        FileInputStream fis = new FileInputStream(unusual);
        lpf.load(fis);

        lpf.setProperty("prop:seven", "new value for seven");
        lpf.setProperty("prop=eight", "new value for eight");
        lpf.setProperty("prop eleven", "new value for eleven");

        lpf.setProperty("alpha", "new value for alpha");
        lpf.setProperty("beta", "new value for beta");

        File tmp = File.createTempFile("tmp", "props");
        tmp.deleteOnExit();
        lpf.saveAs(tmp);

        // and check that the resulting file looks okay
        String s = readFile(tmp);

        assertTrue(s.indexOf("prop\\:seven=new value for seven") > -1);
        assertTrue(s.indexOf("prop\\=eight=new value for eight") > -1);
        assertTrue(s.indexOf("prop\\ eleven=new value for eleven") > -1);
        assertTrue(s.indexOf("alpha=new value for alpha") > -1);
        assertTrue(s.indexOf("beta=new value for beta") > -1);

        assertTrue(s.indexOf("prop\\:seven=contains\\:colon") == -1);
        assertTrue(s.indexOf("prop\\=eight=contains\\=equals") == -1);
        assertTrue(s.indexOf("alpha:set with a colon") == -1);
        assertTrue(s.indexOf("beta set with a space") == -1);
    }

    private static String readFile(File f) throws IOException {
        FileInputStream fis = new FileInputStream(f);
        InputStreamReader isr = new InputStreamReader(fis);
        String s = FileUtils.readFully(isr);
        isr.close();
        fis.close();
        return s;
    }
}