aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/tests/junit/org/apache/tools/ant/taskdefs/XmlPropertyTest.java
blob: 89069173bfe8378162f4941ee7da396d3bc1b370 (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 *  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.taskdefs;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;

import org.apache.tools.ant.BuildFileRule;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.util.FileUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

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

/**
 */
public class XmlPropertyTest {
    private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();

    @Rule
    public BuildFileRule buildRule = new BuildFileRule();

    @Before
    public void setUp() {
        buildRule.configureProject("src/etc/testcases/taskdefs/xmlproperty.xml");
    }

    @Test
    public void testFile() {
        testProperties("test");
    }

    @Test
    public void testResource() {
        testProperties("testResource");
    }

    private void testProperties(String target) {
        buildRule.executeTarget(target);
        assertEquals("true", buildRule.getProject().getProperty("root-tag(myattr)"));
        assertEquals("Text", buildRule.getProject().getProperty("root-tag.inner-tag"));
        assertEquals("val",
                buildRule.getProject().getProperty("root-tag.inner-tag(someattr)"));
        assertEquals("false", buildRule.getProject().getProperty("root-tag.a2.a3.a4"));
        assertEquals("CDATA failed",
            "<test>", buildRule.getProject().getProperty("root-tag.cdatatag"));
    }

    @Test
    public void testDTD() {
        buildRule.executeTarget("testdtd");
        assertEquals("Text", buildRule.getProject().getProperty("root-tag.inner-tag"));
    }

    @Test
    public void testNone () throws IOException {
        doTest("testNone", false, false, false, false, false);
    }

    @Test
    public void testKeeproot() throws IOException {
        doTest("testKeeproot", true, false, false, false, false);
    }

    @Test
    public void testCollapse () throws IOException {
        doTest("testCollapse", false, true, false, false, false);
    }

    @Test
    public void testSemantic () throws IOException {
        doTest("testSemantic", false, false, true, false, false);
    }

    @Test
    public void testKeeprootCollapse () throws IOException {
        doTest("testKeeprootCollapse", true, true, false, false, false);
    }

    @Test
    public void testKeeprootSemantic () throws IOException {
        doTest("testKeeprootSemantic", true, false, true, false, false);
    }

    @Test
    public void testCollapseSemantic () throws IOException {
        doTest("testCollapseSemantic", false, true, true, false, false);
    }

    @Test
    public void testKeeprootCollapseSemantic () throws IOException {
        doTest("testKeeprootCollapseSemantic", true, true, true, false, false);
    }

    @Test
    public void testInclude () throws IOException {
        doTest("testInclude", false, false, false, true, false);
    }

    @Test
    public void testSemanticInclude () throws IOException {
        doTest("testSemanticInclude", false, false, true, true, false);
    }

    @Test
    public void testSemanticLocal () throws IOException {
        doTest("testSemanticInclude", false, false, true, false, true);
    }

    @Test
    public void testNeedsCatalog() {
        buildRule.executeTarget("testneedscat");
        assertEquals("true", buildRule.getProject().getProperty("skinconfig.foo"));
    }

    /**
     * Actually run a test, finding all input files (and corresponding
     * goldfile)
     */
    private void doTest(String msg, boolean keepRoot, boolean collapse,
                        boolean semantic, boolean include, boolean localRoot) throws IOException {
        Enumeration iter =
            getFiles(new File(System.getProperty("root"), "src/etc/testcases/taskdefs/xmlproperty/inputs"));
        while (iter.hasMoreElements()) {
            File inputFile = (File) iter.nextElement();
            // What's the working directory?  If local, then its the
            // folder of the input file.  Otherwise, its the "current" dir..
            File workingDir;
            if ( localRoot ) {
                workingDir = inputFile.getParentFile();
            } else {
                workingDir = FILE_UTILS.resolveFile(new File("."), ".");
            }


            File propertyFile = getGoldfile(inputFile, keepRoot, collapse,
                                            semantic, include, localRoot);
            if (!propertyFile.exists()) {
//                    System.out.println("Skipping as "
//                                       + propertyFile.getAbsolutePath()
//                                       + ") doesn't exist.");
                continue;
            }

            //                System.out.println(msg + " (" + propertyFile.getName() + ") in (" + workingDir + ")");

            Project p = new Project();

            XmlProperty xmlproperty = new XmlProperty();
            xmlproperty.setProject(p);
            xmlproperty.setFile(inputFile);

            xmlproperty.setKeeproot(keepRoot);
            xmlproperty.setCollapseAttributes(collapse);
            xmlproperty.setSemanticAttributes(semantic);
            xmlproperty.setIncludeSemanticAttribute(include);
            xmlproperty.setRootDirectory(workingDir);

            // Set a property on the project to make sure that loading
            // a property with the same name from an xml file will
            // *not* change it.
            p.setNewProperty("override.property.test", "foo");

            xmlproperty.execute();

            Properties props = new Properties();
            props.load(new FileInputStream(propertyFile));

            //printProperties(p.getProperties());

            ensureProperties(msg, inputFile, workingDir, p, props);
            ensureReferences(msg, inputFile, p.getReferences());

        }
    }

    /**
     * Make sure every property loaded from the goldfile was also
     * read from the XmlProperty.  We could try and test the other way,
     * but some other properties may get set in the XmlProperty due
     * to generic Project/Task configuration.
     */
    private static void ensureProperties (String msg, File inputFile,
                                          File workingDir, Project p,
                                          Properties properties) {
        Hashtable xmlproperties = p.getProperties();
        // Every key identified by the Properties must have been loaded.
        Enumeration propertyKeyEnum = properties.propertyNames();
        while(propertyKeyEnum.hasMoreElements()){
            String currentKey = propertyKeyEnum.nextElement().toString();
            String assertMsg = msg + "-" + inputFile.getName()
                + " Key=" + currentKey;

            String propertyValue = properties.getProperty(currentKey);

            String xmlValue = (String)xmlproperties.get(currentKey);

            if (propertyValue.startsWith("ID.")) {
                // The property is an id's thing -- either a property
                // or a path.  We need to make sure
                // that the object was created with the given id.
                // We don't have an adequate way of testing the actual
                // *value* of the Path object, though...
                String id = currentKey;
                Object obj = p.getReferences().get(id);

                if ( obj == null ) {
                    fail(assertMsg + " Object ID does not exist.");
                }

                // What is the property supposed to be?
                propertyValue =
                    propertyValue.substring(3, propertyValue.length());
                if (propertyValue.equals("path")) {
                    if (!(obj instanceof Path)) {
                        fail(assertMsg + " Path ID is a "
                             + obj.getClass().getName());
                    }
                } else {
                    assertEquals(assertMsg, propertyValue, obj.toString());
                }

            } else {

                if (propertyValue.startsWith("FILE.")) {
                    // The property is the name of a file.  We are testing
                    // a location attribute, so we need to resolve the given
                    // file name in the provided folder.
                    String fileName =
                        propertyValue.substring(5, propertyValue.length());
                    File f = new File(workingDir, fileName);
                    propertyValue = f.getAbsolutePath();
                }

                assertEquals(assertMsg, propertyValue, xmlValue);
            }

        }
    }

    /**
     * Debugging method to print the properties in the given hashtable
     */
    private static void printProperties(Hashtable xmlproperties) {
        Enumeration keyEnum = xmlproperties.keys();
        while (keyEnum.hasMoreElements()) {
            String currentKey = keyEnum.nextElement().toString();
            System.out.println(currentKey + " = "
                               + xmlproperties.get(currentKey));
        }
    }

    /**
     * Ensure all references loaded by the project are valid.
     */
    private static void ensureReferences (String msg, File inputFile,
                                          Hashtable references) {
        Enumeration referenceKeyEnum = references.keys();
        while(referenceKeyEnum.hasMoreElements()){
            String currentKey = referenceKeyEnum.nextElement().toString();
            Object currentValue = references.get(currentKey);

            if (currentValue instanceof Path) {
            } else if (currentValue instanceof String) {
            } else {
                if( ! currentKey.startsWith("ant.") ) {
                    fail(msg + "-" + inputFile.getName() + " Key="
                         + currentKey + " is not a recognized type.");
                }
            }
        }
    }

    /**
     * Munge the name of the input file to find an appropriate goldfile,
     * based on hardwired naming conventions.
     */
    private static File getGoldfile (File input, boolean keepRoot,
                                     boolean collapse, boolean semantic,
                                     boolean include, boolean localRoot) {
        // Substitute .xml with .properties
        String baseName = input.getName().toLowerCase();
        if (baseName.endsWith(".xml")) {
            baseName = baseName.substring(0, baseName.length() - 4)
                + ".properties";
        }

        File dir = input.getParentFile().getParentFile();

        String goldFileFolder = "goldfiles/";

        if (keepRoot) {
            goldFileFolder += "keeproot-";
        } else {
            goldFileFolder += "nokeeproot-";
        }

        if (semantic) {
            goldFileFolder += "semantic-";
            if (include) {
                goldFileFolder += "include-";
            }
        } else {
            if (collapse) {
                goldFileFolder += "collapse-";
            } else {
                goldFileFolder += "nocollapse-";
            }
        }

        return new File(dir, goldFileFolder + baseName);
    }

    /**
     * Retrieve a list of xml files in the specified folder
     * and below.
     */
    private static Enumeration getFiles (final File startingDir) {
        Vector result = new Vector();
        getFiles(startingDir, result);
        return result.elements();
    }

    /**
     * Collect a list of xml files in the specified folder
     * and below.
     */
    private static void getFiles (final File startingDir, Vector collect) {
        FileFilter filter = new FileFilter() {
            public boolean accept (File file) {
                if (file.isDirectory()) {
                    return true;
                } else {
                    return (file.getPath().indexOf("taskdefs") > 0 &&
                            file.getPath().toLowerCase().endsWith(".xml") );
                }
            }
        };

        File[] files = startingDir.listFiles(filter);
        for (int i=0;i<files.length;i++) {
            File f = files[i];
            if (!f.isDirectory()) {
                collect.addElement(f);
            } else {
                getFiles(f, collect);
            }
        }
    }
}