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.

TestUtils.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*******************************************************************************
  2. * Copyright (c) 2004 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Common Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/cpl-v10.html
  7. *
  8. * Contributors:
  9. * Andy Clement - initial implementation
  10. *******************************************************************************/
  11. package org.aspectj.systemtest.ajc150;
  12. import java.io.File;
  13. import java.util.ArrayList;
  14. import java.util.Collection;
  15. import java.util.Iterator;
  16. import java.util.List;
  17. import org.aspectj.bridge.IMessage;
  18. import org.aspectj.tools.ajc.AjcTestCase;
  19. import org.aspectj.tools.ajc.CompilationResult;
  20. public abstract class TestUtils extends AjcTestCase {
  21. private static final boolean verbose = false;
  22. protected File baseDir;
  23. protected CompilationResult binaryWeave(String inpath,String insource,int expErrors,int expWarnings) {
  24. return binaryWeave(inpath,insource,expErrors,expWarnings,false);
  25. }
  26. protected CompilationResult binaryWeave(String inpath, String insource,int expErrors,int expWarnings,boolean xlinterror) {
  27. String[] args = null;
  28. if (xlinterror) {
  29. args = new String[] {"-inpath",inpath,insource,"-showWeaveInfo","-proceedOnError","-Xlint:warning"};
  30. } else {
  31. args = new String[] {"-inpath",inpath,insource,"-showWeaveInfo","-proceedOnError"};
  32. }
  33. CompilationResult result = ajc(baseDir,args);
  34. if (verbose || result.hasErrorMessages()) System.out.println(result);
  35. assertTrue("Expected "+expErrors+" errors but got "+result.getErrorMessages().size()+":\n"+
  36. formatCollection(result.getErrorMessages()),result.getErrorMessages().size()==expErrors);
  37. assertTrue("Expected "+expWarnings+" warnings but got "+result.getWarningMessages().size()+":\n"+
  38. formatCollection(result.getWarningMessages()),result.getWarningMessages().size()==expWarnings);
  39. return result;
  40. }
  41. private String formatCollection(Collection s) {
  42. StringBuffer sb = new StringBuffer();
  43. for (Iterator iter = s.iterator(); iter.hasNext();) {
  44. Object element = (Object) iter.next();
  45. sb.append(element).append("\n");
  46. }
  47. return sb.toString();
  48. }
  49. private List getWeavingMessages(List msgs) {
  50. List result = new ArrayList();
  51. for (Iterator iter = msgs.iterator(); iter.hasNext();) {
  52. IMessage element = (IMessage) iter.next();
  53. if (element.getKind()==IMessage.WEAVEINFO) {
  54. result.add(element.toString());
  55. }
  56. }
  57. return result;
  58. }
  59. protected void verifyWeavingMessagesOutput(CompilationResult cR,String[] expected) {
  60. List weavingmessages = getWeavingMessages(cR.getInfoMessages());
  61. dump(weavingmessages);
  62. for (int i = 0; i < expected.length; i++) {
  63. boolean found = weavingmessages.contains(expected[i]);
  64. if (found) {
  65. weavingmessages.remove(expected[i]);
  66. } else {
  67. System.err.println(dump(getWeavingMessages(cR.getInfoMessages())));
  68. fail("Expected message not found.\nExpected:\n"+expected[i]+"\nObtained:\n"+dump(getWeavingMessages(cR.getInfoMessages())));
  69. }
  70. }
  71. if (weavingmessages.size()!=0) {
  72. fail("Unexpected messages obtained from program:\n"+dump(weavingmessages));
  73. }
  74. }
  75. private String dump(List l) {
  76. StringBuffer sb = new StringBuffer();
  77. int i =0;
  78. sb.append("--- Weaving Messages ---\n");
  79. for (Iterator iter = l.iterator(); iter.hasNext();) {
  80. sb.append(i+") "+iter.next()+"\n");
  81. }
  82. sb.append("------------------------\n");
  83. return sb.toString();
  84. }
  85. }