You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

XmlPropertyTest.java 13 kB

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