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

ParseTestCase.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Xerox/PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.testing.harness.bridge;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.Iterator;
  17. import java.util.List;
  18. import javax.xml.parsers.DocumentBuilder;
  19. import javax.xml.parsers.DocumentBuilderFactory;
  20. import javax.xml.parsers.ParserConfigurationException;
  21. import org.aspectj.bridge.IMessage;
  22. import org.aspectj.bridge.IMessageHolder;
  23. import org.aspectj.bridge.ISourceLocation;
  24. import org.aspectj.bridge.Message;
  25. import org.aspectj.bridge.MessageHandler;
  26. import org.aspectj.bridge.MessageUtil;
  27. import org.aspectj.bridge.SourceLocation;
  28. import org.aspectj.testing.run.IRunIterator;
  29. import org.aspectj.testing.run.IRunListener;
  30. import org.aspectj.testing.run.RunStatus;
  31. import org.aspectj.testing.run.Runner;
  32. import org.w3c.dom.Document;
  33. import org.w3c.dom.Node;
  34. import org.w3c.dom.NodeList;
  35. import org.xml.sax.SAXException;
  36. import junit.framework.TestCase;
  37. public class ParseTestCase extends TestCase {
  38. public ParseTestCase(String name) {
  39. super(name);
  40. }
  41. public void testNothingBecauseOthersSkipped() {}
  42. public void skiptestParse() throws Exception { // XXX failing b/c of iteration
  43. Runner runner = new Runner();
  44. IMessageHolder handler = new MessageHandler();
  45. RunStatus status;
  46. Validator validator = new Validator(handler);
  47. final File suiteFile = new File("../testing/testdata/suite.xml");
  48. List tests = parseSuite(suiteFile);
  49. Sandbox sandbox = new Sandbox(new File("testdata"), validator);
  50. IRunListener listenerNULL = null;
  51. ISourceLocation sl = new SourceLocation(suiteFile, 0, 0,0);
  52. for (Iterator iter = tests.iterator(); iter.hasNext();) {
  53. status = new RunStatus(handler, runner);
  54. AjcTest.Spec test = (AjcTest.Spec) iter.next();
  55. test.setSourceLocation(sl);
  56. IRunIterator child = test.makeRunIterator(sandbox, validator);
  57. //test.setup(new String[0], validator); // XXX
  58. //IRun child = runner.wrap(test, null);
  59. // huh? runIterator not generating child status?
  60. //RunStatus childStatus = runner.makeChildStatus();
  61. runner.runIterator(child, status, listenerNULL);
  62. MessageUtil.print(System.err, status);
  63. }
  64. }
  65. private List parseSuite(File file) throws ParserConfigurationException, IOException, SAXException{
  66. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  67. factory.setValidating(true);
  68. factory.setIgnoringElementContentWhitespace(true);
  69. factory.setIgnoringComments(true);
  70. DocumentBuilder builder = factory.newDocumentBuilder();
  71. System.out.println(file.getAbsoluteFile());
  72. Document doc = builder.parse(file);
  73. dump(doc.getDocumentElement(), 0);
  74. List ret = new ArrayList();
  75. Node suiteNode = doc.getDocumentElement();
  76. NodeList children = suiteNode.getChildNodes();
  77. for (int i=0; i < children.getLength(); i++) {
  78. ret.add(parseTest(children.item(i)));
  79. }
  80. return ret;
  81. }
  82. private AjcTest.Spec parseTest(Node node) {
  83. String title = getAttributeString(node, "title");
  84. String pr = getAttributeString(node, "pr");
  85. String dir = getAttributeString(node, "dir");
  86. ISourceLocation sourceLocation =
  87. new SourceLocation(new File("Missing"), 0, 0, 0);
  88. AjcTest.Spec test = new AjcTest.Spec();
  89. test.setDescription(title);
  90. test.setTestDirOffset(dir);
  91. test.setBugId(Integer.valueOf(pr).intValue());
  92. test.setSourceLocation(sourceLocation);
  93. //AjcTest test = new AjcTest(title, dir, pr, sourceLocation);
  94. System.out.println(test);
  95. // List ret = new ArrayList();
  96. NodeList children = node.getChildNodes();
  97. for (int i=0; i < children.getLength(); i++) {
  98. test.addChild(parseIRun(test, children.item(i), dir));
  99. // test.addRunSpec(parseIRun(test, children.item(i), dir));
  100. }
  101. return test;
  102. }
  103. private IRunSpec parseIRun(AjcTest.Spec test, Node node, String baseDir) {
  104. String kind = node.getNodeName();
  105. if (kind.equals("compile")) {
  106. List args = parseChildrenStrings(node, "arg");
  107. /*List files = */parseChildrenStrings(node, "file");
  108. List expectedMessages = parseChildrenMessages(node);
  109. CompilerRun.Spec spec = new CompilerRun.Spec();
  110. spec.addOptions((String[]) args.toArray(new String[0]));
  111. spec.addPaths((String[]) args.toArray(new String[0]));
  112. spec.addMessages(expectedMessages);
  113. spec.testSrcDirOffset = null; // baseDir;
  114. return spec;
  115. } else if (kind.equals("run")) {
  116. JavaRun.Spec spec = new JavaRun.Spec();
  117. spec.className = getAttributeString(node, "class");
  118. spec.addOptions(new String[0]); //??? could add support here
  119. /*JavaRun run = */new JavaRun(spec);
  120. return spec;
  121. }
  122. return null;
  123. }
  124. private List parseChildrenMessages(Node node) {
  125. List ret = new ArrayList();
  126. NodeList children = node.getChildNodes();
  127. for (int i=0; i < children.getLength(); i++) {
  128. Node child = children.item(i);
  129. if (child.getNodeName().equals("message")) {
  130. ret.add(parseMessage(child));
  131. }
  132. }
  133. return ret;
  134. }
  135. private IMessage parseMessage(Node child) {
  136. IMessage.Kind kind;
  137. String sKind = getAttributeString(child, "kind");
  138. if (sKind.equals("error")) { kind = IMessage.ERROR; }
  139. else if (sKind.equals("warning")) { kind = IMessage.WARNING; }
  140. else {
  141. throw new RuntimeException("unknown kind: " + sKind);
  142. }
  143. String filename = getAttributeString(child, "file");
  144. File file;
  145. if (filename != null) {
  146. file = new File(filename);
  147. } else {
  148. file = new File("XXX"); //XXX
  149. }
  150. int line = Integer.valueOf(getAttributeString(child, "line")).intValue();
  151. ISourceLocation sourceLocation = new SourceLocation(file, line, line, 0);
  152. return new Message("", kind, null, sourceLocation);
  153. }
  154. private List parseChildrenStrings(Node node, String kind) {
  155. List ret = new ArrayList();
  156. NodeList children = node.getChildNodes();
  157. for (int i=0; i < children.getLength(); i++) {
  158. Node child = children.item(i);
  159. if (child.getNodeName().equals(kind)) {
  160. Node first = child.getFirstChild();
  161. if (null != first) {
  162. ret.add(first.getNodeValue());// XXX
  163. }
  164. }
  165. }
  166. return ret;
  167. }
  168. private String getAttributeString(Node node, String name) {
  169. Node attrNode = node.getAttributes().getNamedItem(name);
  170. if (attrNode == null) return null;
  171. return attrNode.getNodeValue();
  172. }
  173. private void dump(Node node, int indent) {
  174. for (int i=0; i < indent; i++) System.out.print(" ");
  175. System.out.println(node);
  176. NodeList children = node.getChildNodes();
  177. for (int i=0; i < children.getLength(); i++) {
  178. dump(children.item(i), indent+1);
  179. }
  180. }
  181. }