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 6.9KB

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