1 | /*
|
---|
2 | * Copyright 2002-2004 The Apache Software Foundation
|
---|
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 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 | import java.io.File;
|
---|
21 | import java.io.FileFilter;
|
---|
22 | import java.io.FileInputStream;
|
---|
23 | import java.io.IOException;
|
---|
24 | import java.util.Enumeration;
|
---|
25 | import java.util.Hashtable;
|
---|
26 | import java.util.Properties;
|
---|
27 | import java.util.Vector;
|
---|
28 |
|
---|
29 | import org.apache.tools.ant.BuildFileTest;
|
---|
30 | import org.apache.tools.ant.Project;
|
---|
31 | import org.apache.tools.ant.types.Path;
|
---|
32 | import org.apache.tools.ant.util.FileUtils;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | */
|
---|
36 | public class XmlPropertyTest extends BuildFileTest {
|
---|
37 | private static FileUtils fileUtils = FileUtils.newFileUtils();
|
---|
38 |
|
---|
39 | public XmlPropertyTest(String name) {
|
---|
40 | super(name);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void setUp() {
|
---|
44 | configureProject("src/etc/testcases/taskdefs/xmlproperty.xml");
|
---|
45 | }
|
---|
46 |
|
---|
47 | public void testProperties() {
|
---|
48 | executeTarget("test");
|
---|
49 | assertEquals("true", getProject().getProperty("root-tag(myattr)"));
|
---|
50 | assertEquals("Text", getProject().getProperty("root-tag.inner-tag"));
|
---|
51 | assertEquals("val",
|
---|
52 | getProject().getProperty("root-tag.inner-tag(someattr)"));
|
---|
53 | assertEquals("false", getProject().getProperty("root-tag.a2.a3.a4"));
|
---|
54 | assertEquals("CDATA failed",
|
---|
55 | "<test>", getProject().getProperty("root-tag.cdatatag"));
|
---|
56 | }
|
---|
57 |
|
---|
58 | public void testDTD() {
|
---|
59 | executeTarget("testdtd");
|
---|
60 | assertEquals("Text", getProject().getProperty("root-tag.inner-tag"));
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void testNone () {
|
---|
64 | doTest("testNone", false, false, false, false, false);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public void testKeeproot() {
|
---|
68 | doTest("testKeeproot", true, false, false, false, false);
|
---|
69 | }
|
---|
70 |
|
---|
71 | public void testCollapse () {
|
---|
72 | doTest("testCollapse", false, true, false, false, false);
|
---|
73 | }
|
---|
74 |
|
---|
75 | public void testSemantic () {
|
---|
76 | doTest("testSemantic", false, false, true, false, false);
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void testKeeprootCollapse () {
|
---|
80 | doTest("testKeeprootCollapse", true, true, false, false, false);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void testKeeprootSemantic () {
|
---|
84 | doTest("testKeeprootSemantic", true, false, true, false, false);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void testCollapseSemantic () {
|
---|
88 | doTest("testCollapseSemantic", false, true, true, false, false);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void testKeeprootCollapseSemantic () {
|
---|
92 | doTest("testKeeprootCollapseSemantic", true, true, true, false, false);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void testInclude () {
|
---|
96 | doTest("testInclude", false, false, false, true, false);
|
---|
97 | }
|
---|
98 |
|
---|
99 | public void testSemanticInclude () {
|
---|
100 | doTest("testSemanticInclude", false, false, true, true, false);
|
---|
101 | }
|
---|
102 |
|
---|
103 | public void testSemanticLocal () {
|
---|
104 | doTest("testSemanticInclude", false, false, true, false, true);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public void testNeedsCatalog() {
|
---|
108 | executeTarget("testneedscat");
|
---|
109 | assertEquals("true", getProject().getProperty("skinconfig.foo"));
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Actually run a test, finding all input files (and corresponding
|
---|
114 | * goldfile)
|
---|
115 | */
|
---|
116 | private void doTest(String msg, boolean keepRoot, boolean collapse,
|
---|
117 | boolean semantic, boolean include, boolean localRoot) {
|
---|
118 | Enumeration iter =
|
---|
119 | getFiles(new File("src/etc/testcases/taskdefs/xmlproperty/inputs"));
|
---|
120 | while (iter.hasMoreElements()) {
|
---|
121 | File inputFile = (File) iter.nextElement();
|
---|
122 | // What's the working directory? If local, then its the
|
---|
123 | // folder of the input file. Otherwise, its the "current" dir..
|
---|
124 | File workingDir;
|
---|
125 | if ( localRoot ) {
|
---|
126 | workingDir = fileUtils.getParentFile(inputFile);
|
---|
127 | } else {
|
---|
128 | workingDir = fileUtils.resolveFile(new File("."), ".");
|
---|
129 | }
|
---|
130 |
|
---|
131 | try {
|
---|
132 |
|
---|
133 | File propertyFile = getGoldfile(inputFile, keepRoot, collapse,
|
---|
134 | semantic, include, localRoot);
|
---|
135 | if (!propertyFile.exists()) {
|
---|
136 | // System.out.println("Skipping as "
|
---|
137 | // + propertyFile.getAbsolutePath()
|
---|
138 | // + ") doesn't exist.");
|
---|
139 | continue;
|
---|
140 | }
|
---|
141 |
|
---|
142 | // System.out.println(msg + " (" + propertyFile.getName() + ") in (" + workingDir + ")");
|
---|
143 |
|
---|
144 | Project project = new Project();
|
---|
145 |
|
---|
146 | XmlProperty xmlproperty = new XmlProperty();
|
---|
147 | xmlproperty.setProject(project);
|
---|
148 | xmlproperty.setFile(inputFile);
|
---|
149 |
|
---|
150 | xmlproperty.setKeeproot(keepRoot);
|
---|
151 | xmlproperty.setCollapseAttributes(collapse);
|
---|
152 | xmlproperty.setSemanticAttributes(semantic);
|
---|
153 | xmlproperty.setIncludeSemanticAttribute(include);
|
---|
154 | xmlproperty.setRootDirectory(workingDir);
|
---|
155 |
|
---|
156 | // Set a property on the project to make sure that loading
|
---|
157 | // a property with the same name from an xml file will
|
---|
158 | // *not* change it.
|
---|
159 | project.setNewProperty("override.property.test", "foo");
|
---|
160 |
|
---|
161 | xmlproperty.execute();
|
---|
162 |
|
---|
163 | Properties props = new Properties();
|
---|
164 | props.load(new FileInputStream(propertyFile));
|
---|
165 |
|
---|
166 | //printProperties(project.getProperties());
|
---|
167 |
|
---|
168 | ensureProperties(msg, inputFile, workingDir, project, props);
|
---|
169 | ensureReferences(msg, inputFile, project.getReferences());
|
---|
170 |
|
---|
171 | } catch (IOException ex) {
|
---|
172 | fail(ex.toString());
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Make sure every property loaded from the goldfile was also
|
---|
179 | * read from the XmlProperty. We could try and test the other way,
|
---|
180 | * but some other properties may get set in the XmlProperty due
|
---|
181 | * to generic Project/Task configuration.
|
---|
182 | */
|
---|
183 | private static void ensureProperties (String msg, File inputFile,
|
---|
184 | File workingDir, Project project,
|
---|
185 | Properties properties) {
|
---|
186 | Hashtable xmlproperties = project.getProperties();
|
---|
187 | // Every key identified by the Properties must have been loaded.
|
---|
188 | Enumeration propertyKeyEnum = properties.propertyNames();
|
---|
189 | while(propertyKeyEnum.hasMoreElements()){
|
---|
190 | String currentKey = propertyKeyEnum.nextElement().toString();
|
---|
191 | String assertMsg = msg + "-" + inputFile.getName()
|
---|
192 | + " Key=" + currentKey;
|
---|
193 |
|
---|
194 | String propertyValue = properties.getProperty(currentKey);
|
---|
195 |
|
---|
196 | String xmlValue = (String)xmlproperties.get(currentKey);
|
---|
197 |
|
---|
198 | if ( propertyValue.indexOf("ID.") == 0 ) {
|
---|
199 | // The property is an id's thing -- either a property
|
---|
200 | // or a path. We need to make sure
|
---|
201 | // that the object was created with the given id.
|
---|
202 | // We don't have an adequate way of testing the actual
|
---|
203 | // *value* of the Path object, though...
|
---|
204 | String id = currentKey;
|
---|
205 | Object obj = project.getReferences().get(id);
|
---|
206 |
|
---|
207 | if ( obj == null ) {
|
---|
208 | fail(assertMsg + " Object ID does not exist.");
|
---|
209 | }
|
---|
210 |
|
---|
211 | // What is the property supposed to be?
|
---|
212 | propertyValue =
|
---|
213 | propertyValue.substring(3, propertyValue.length());
|
---|
214 | if (propertyValue.equals("path")) {
|
---|
215 | if (!(obj instanceof Path)) {
|
---|
216 | fail(assertMsg + " Path ID is a "
|
---|
217 | + obj.getClass().getName());
|
---|
218 | }
|
---|
219 | } else {
|
---|
220 | assertEquals(assertMsg, propertyValue, obj.toString());
|
---|
221 | }
|
---|
222 |
|
---|
223 | } else {
|
---|
224 |
|
---|
225 | if (propertyValue.indexOf("FILE.") == 0) {
|
---|
226 | // The property is the name of a file. We are testing
|
---|
227 | // a location attribute, so we need to resolve the given
|
---|
228 | // file name in the provided folder.
|
---|
229 | String fileName =
|
---|
230 | propertyValue.substring(5, propertyValue.length());
|
---|
231 | File f = new File(workingDir, fileName);
|
---|
232 | propertyValue = f.getAbsolutePath();
|
---|
233 | }
|
---|
234 |
|
---|
235 | assertEquals(assertMsg, propertyValue, xmlValue);
|
---|
236 | }
|
---|
237 |
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Debugging method to print the properties in the given hashtable
|
---|
243 | */
|
---|
244 | private static void printProperties(Hashtable xmlproperties) {
|
---|
245 | Enumeration keyEnum = xmlproperties.keys();
|
---|
246 | while (keyEnum.hasMoreElements()) {
|
---|
247 | String currentKey = keyEnum.nextElement().toString();
|
---|
248 | System.out.println(currentKey + " = "
|
---|
249 | + xmlproperties.get(currentKey));
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * Ensure all references loaded by the project are valid.
|
---|
255 | */
|
---|
256 | private static void ensureReferences (String msg, File inputFile,
|
---|
257 | Hashtable references) {
|
---|
258 | Enumeration referenceKeyEnum = references.keys();
|
---|
259 | while(referenceKeyEnum.hasMoreElements()){
|
---|
260 | String currentKey = referenceKeyEnum.nextElement().toString();
|
---|
261 | Object currentValue = references.get(currentKey);
|
---|
262 |
|
---|
263 | if (currentValue instanceof Path) {
|
---|
264 | } else if (currentValue instanceof String) {
|
---|
265 | } else {
|
---|
266 | if( ! currentKey.startsWith("ant.") ) {
|
---|
267 | fail(msg + "-" + inputFile.getName() + " Key="
|
---|
268 | + currentKey + " is not a recognized type.");
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Munge the name of the input file to find an appropriate goldfile,
|
---|
276 | * based on hardwired naming conventions.
|
---|
277 | */
|
---|
278 | private static File getGoldfile (File input, boolean keepRoot,
|
---|
279 | boolean collapse, boolean semantic,
|
---|
280 | boolean include, boolean localRoot) {
|
---|
281 | // Substitute .xml with .properties
|
---|
282 | String baseName = input.getName().toLowerCase();
|
---|
283 | if (baseName.endsWith(".xml")) {
|
---|
284 | baseName = baseName.substring(0, baseName.length() - 4)
|
---|
285 | + ".properties";
|
---|
286 | }
|
---|
287 |
|
---|
288 | File dir = fileUtils.getParentFile(fileUtils.getParentFile(input));
|
---|
289 |
|
---|
290 | String goldFileFolder = "goldfiles/";
|
---|
291 |
|
---|
292 | if (keepRoot) {
|
---|
293 | goldFileFolder += "keeproot-";
|
---|
294 | } else {
|
---|
295 | goldFileFolder += "nokeeproot-";
|
---|
296 | }
|
---|
297 |
|
---|
298 | if (semantic) {
|
---|
299 | goldFileFolder += "semantic-";
|
---|
300 | if (include) {
|
---|
301 | goldFileFolder += "include-";
|
---|
302 | }
|
---|
303 | } else {
|
---|
304 | if (collapse) {
|
---|
305 | goldFileFolder += "collapse-";
|
---|
306 | } else {
|
---|
307 | goldFileFolder += "nocollapse-";
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | return new File(dir, goldFileFolder + baseName);
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Retrieve a list of xml files in the specified folder
|
---|
316 | * and below.
|
---|
317 | */
|
---|
318 | private static Enumeration getFiles (final File startingDir) {
|
---|
319 | Vector result = new Vector();
|
---|
320 | getFiles(startingDir, result);
|
---|
321 | return result.elements();
|
---|
322 | }
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Collect a list of xml files in the specified folder
|
---|
326 | * and below.
|
---|
327 | */
|
---|
328 | private static void getFiles (final File startingDir, Vector collect) {
|
---|
329 | FileFilter filter = new FileFilter() {
|
---|
330 | public boolean accept (File file) {
|
---|
331 | if (file.isDirectory()) {
|
---|
332 | return true;
|
---|
333 | } else {
|
---|
334 | return (file.getPath().indexOf("taskdefs") > 0 &&
|
---|
335 | file.getPath().toLowerCase().endsWith(".xml") );
|
---|
336 | }
|
---|
337 | }
|
---|
338 | };
|
---|
339 |
|
---|
340 | File[] files = startingDir.listFiles(filter);
|
---|
341 | for (int i=0;i<files.length;i++) {
|
---|
342 | File f = files[i];
|
---|
343 | if (!f.isDirectory()) {
|
---|
344 | collect.addElement(f);
|
---|
345 | } else {
|
---|
346 | getFiles(f, collect);
|
---|
347 | }
|
---|
348 | }
|
---|
349 | }
|
---|
350 | }
|
---|