1 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 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 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 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 continue; 140 } 141 142 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 project.setNewProperty("override.property.test", "foo"); 160 161 xmlproperty.execute(); 162 163 Properties props = new Properties (); 164 props.load(new FileInputStream (propertyFile)); 165 166 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 183 private static void ensureProperties (String msg, File inputFile, 184 File workingDir, Project project, 185 Properties properties) { 186 Hashtable xmlproperties = project.getProperties(); 187 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 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 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 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 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 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 278 private static File getGoldfile (File input, boolean keepRoot, 279 boolean collapse, boolean semantic, 280 boolean include, boolean localRoot) { 281 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 318 private static Enumeration getFiles (final File startingDir) { 319 Vector result = new Vector (); 320 getFiles(startingDir, result); 321 return result.elements(); 322 } 323 324 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 } 351 | Popular Tags |