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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. protected 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. return binaryWeave(inpath,insource,expErrors,expWarnings,xlinterror,(String[])null);
  28. }
  29. protected CompilationResult binaryWeave(String inpath, String insource,int expErrors,int expWarnings,String extraOption) {
  30. return binaryWeave(inpath,insource,expErrors,expWarnings,false,extraOption);
  31. }
  32. protected CompilationResult binaryWeave(String inpath, String insource,int expErrors,int expWarnings,boolean xlinterror,String extraOption) {
  33. return binaryWeave(inpath, insource, expErrors, expWarnings,xlinterror,new String[] {extraOption});
  34. }
  35. protected CompilationResult binaryWeave(String inpath, String insource,int expErrors,int expWarnings,boolean xlinterror,String[] extraOptions) {
  36. String[] args = null;
  37. if (xlinterror) {
  38. if (extraOptions!=null && extraOptions.length > 0) {
  39. String[] firstargs = new String[] {"-inpath",inpath,insource,"-showWeaveInfo","-proceedOnError","-Xlint:warning"};
  40. args = new String[firstargs.length + extraOptions.length];
  41. System.arraycopy(firstargs,0,args,0,firstargs.length);
  42. System.arraycopy(extraOptions,0,args,firstargs.length,extraOptions.length);
  43. }
  44. else
  45. args = new String[] {"-inpath",inpath,insource,"-showWeaveInfo","-proceedOnError","-Xlint:warning"};
  46. } else {
  47. if (extraOptions!=null && extraOptions.length>0) {
  48. String[] firstargs = new String[] {"-inpath",inpath,insource,"-showWeaveInfo","-proceedOnError"};
  49. args = new String[firstargs.length + extraOptions.length];
  50. System.arraycopy(firstargs,0,args,0,firstargs.length);
  51. System.arraycopy(extraOptions,0,args,firstargs.length,extraOptions.length);
  52. }
  53. else
  54. args = new String[] {"-inpath",inpath,insource,"-showWeaveInfo","-proceedOnError"};
  55. }
  56. CompilationResult result = ajc(baseDir,args);
  57. if (verbose || result.hasErrorMessages()) System.out.println(result);
  58. assertTrue("Expected "+expErrors+" errors but got "+result.getErrorMessages().size()+":\n"+
  59. formatCollection(result.getErrorMessages()),result.getErrorMessages().size()==expErrors);
  60. assertTrue("Expected "+expWarnings+" warnings but got "+result.getWarningMessages().size()+":\n"+
  61. formatCollection(result.getWarningMessages()),result.getWarningMessages().size()==expWarnings);
  62. return result;
  63. }
  64. private String formatCollection(Collection s) {
  65. StringBuffer sb = new StringBuffer();
  66. for (Iterator iter = s.iterator(); iter.hasNext();) {
  67. Object element = (Object) iter.next();
  68. sb.append(element).append("\n");
  69. }
  70. return sb.toString();
  71. }
  72. protected List getWeavingMessages(List msgs) {
  73. List result = new ArrayList();
  74. for (Iterator iter = msgs.iterator(); iter.hasNext();) {
  75. IMessage element = (IMessage) iter.next();
  76. if (element.getKind()==IMessage.WEAVEINFO) {
  77. result.add(element.toString());
  78. }
  79. }
  80. return result;
  81. }
  82. protected void verifyWeavingMessagesOutput(CompilationResult cR,String[] expected) {
  83. List weavingmessages = getWeavingMessages(cR.getInfoMessages());
  84. dump(weavingmessages);
  85. for (int i = 0; i < expected.length; i++) {
  86. boolean found = weavingmessages.contains(expected[i]);
  87. if (found) {
  88. weavingmessages.remove(expected[i]);
  89. } else {
  90. System.err.println(dump(getWeavingMessages(cR.getInfoMessages())));
  91. fail("Expected message not found.\nExpected:\n"+expected[i]+"\nObtained:\n"+dump(getWeavingMessages(cR.getInfoMessages())));
  92. }
  93. }
  94. if (weavingmessages.size()!=0) {
  95. fail("Unexpected messages obtained from program:\n"+dump(weavingmessages));
  96. }
  97. }
  98. private String dump(List l) {
  99. StringBuffer sb = new StringBuffer();
  100. int i =0;
  101. sb.append("--- Weaving Messages ---\n");
  102. for (Iterator iter = l.iterator(); iter.hasNext();) {
  103. sb.append(i+") "+iter.next()+"\n");
  104. }
  105. sb.append("------------------------\n");
  106. return sb.toString();
  107. }
  108. }