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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.apache.bcel.classfile.JavaClass;
  18. import org.aspectj.apache.bcel.util.ClassPath;
  19. import org.aspectj.apache.bcel.util.SyntheticRepository;
  20. import org.aspectj.bridge.IMessage;
  21. import org.aspectj.tools.ajc.AjcTestCase;
  22. import org.aspectj.tools.ajc.CompilationResult;
  23. public abstract class TestUtils extends AjcTestCase {
  24. protected static final boolean verbose = false;
  25. protected File baseDir;
  26. protected CompilationResult binaryWeave(String inpath,String insource,int expErrors,int expWarnings) {
  27. return binaryWeave(inpath,insource,expErrors,expWarnings,false);
  28. }
  29. protected CompilationResult binaryWeave(String inpath, String insource,int expErrors,int expWarnings,boolean xlinterror) {
  30. return binaryWeave(inpath,insource,expErrors,expWarnings,xlinterror,(String[])null);
  31. }
  32. protected CompilationResult binaryWeave(String inpath, String insource,int expErrors,int expWarnings,String extraOption) {
  33. return binaryWeave(inpath,insource,expErrors,expWarnings,false,extraOption);
  34. }
  35. protected CompilationResult binaryWeave(String inpath, String insource,int expErrors,int expWarnings,boolean xlinterror,String extraOption) {
  36. return binaryWeave(inpath, insource, expErrors, expWarnings,xlinterror,new String[] {extraOption});
  37. }
  38. protected CompilationResult binaryWeave(String inpath, String insource,int expErrors,int expWarnings,boolean xlinterror,String[] extraOptions) {
  39. String[] args = null;
  40. if (xlinterror) {
  41. if (extraOptions!=null && extraOptions.length > 0) {
  42. String[] firstargs = new String[] {"-inpath",inpath,insource,"-showWeaveInfo","-proceedOnError","-Xlint:warning"};
  43. args = new String[firstargs.length + extraOptions.length];
  44. System.arraycopy(firstargs,0,args,0,firstargs.length);
  45. System.arraycopy(extraOptions,0,args,firstargs.length,extraOptions.length);
  46. }
  47. else
  48. args = new String[] {"-inpath",inpath,insource,"-showWeaveInfo","-proceedOnError","-Xlint:warning"};
  49. } else {
  50. if (extraOptions!=null && extraOptions.length>0) {
  51. String[] firstargs = new String[] {"-inpath",inpath,insource,"-showWeaveInfo","-proceedOnError"};
  52. args = new String[firstargs.length + extraOptions.length];
  53. System.arraycopy(firstargs,0,args,0,firstargs.length);
  54. System.arraycopy(extraOptions,0,args,firstargs.length,extraOptions.length);
  55. }
  56. else
  57. args = new String[] {"-inpath",inpath,insource,"-showWeaveInfo","-proceedOnError"};
  58. }
  59. CompilationResult result = ajc(baseDir,args);
  60. if (verbose || result.hasErrorMessages()) System.out.println(result);
  61. assertTrue("Expected "+expErrors+" errors but got "+result.getErrorMessages().size()+":\n"+
  62. formatCollection(result.getErrorMessages()),result.getErrorMessages().size()==expErrors);
  63. assertTrue("Expected "+expWarnings+" warnings but got "+result.getWarningMessages().size()+":\n"+
  64. formatCollection(result.getWarningMessages()),result.getWarningMessages().size()==expWarnings);
  65. return result;
  66. }
  67. private String formatCollection(Collection s) {
  68. StringBuffer sb = new StringBuffer();
  69. for (Iterator iter = s.iterator(); iter.hasNext();) {
  70. Object element = (Object) iter.next();
  71. sb.append(element).append("\n");
  72. }
  73. return sb.toString();
  74. }
  75. protected List getWeavingMessages(List msgs) {
  76. List result = new ArrayList();
  77. for (Iterator iter = msgs.iterator(); iter.hasNext();) {
  78. IMessage element = (IMessage) iter.next();
  79. if (element.getKind()==IMessage.WEAVEINFO) {
  80. result.add(element.toString());
  81. }
  82. }
  83. return result;
  84. }
  85. protected void verifyWeavingMessagesOutput(CompilationResult cR,String[] expected) {
  86. List weavingmessages = getWeavingMessages(cR.getInfoMessages());
  87. dump(weavingmessages);
  88. for (int i = 0; i < expected.length; i++) {
  89. boolean found = weavingmessages.contains(expected[i]);
  90. if (found) {
  91. weavingmessages.remove(expected[i]);
  92. } else {
  93. System.err.println(dump(getWeavingMessages(cR.getInfoMessages())));
  94. fail("Expected message not found.\nExpected:\n"+expected[i]+"\nObtained:\n"+dump(getWeavingMessages(cR.getInfoMessages())));
  95. }
  96. }
  97. if (weavingmessages.size()!=0) {
  98. fail("Unexpected messages obtained from program:\n"+dump(weavingmessages));
  99. }
  100. }
  101. private String dump(List l) {
  102. StringBuffer sb = new StringBuffer();
  103. int i =0;
  104. sb.append("--- Weaving Messages ---\n");
  105. for (Iterator iter = l.iterator(); iter.hasNext();) {
  106. sb.append(i+") "+iter.next()+"\n");
  107. }
  108. sb.append("------------------------\n");
  109. return sb.toString();
  110. }
  111. public SyntheticRepository createRepos(File cpentry) {
  112. ClassPath cp = new ClassPath(cpentry+File.pathSeparator+System.getProperty("java.class.path"));
  113. return SyntheticRepository.getInstance(cp);
  114. }
  115. protected JavaClass getClassFrom(File where,String clazzname) throws ClassNotFoundException {
  116. SyntheticRepository repos = createRepos(where);
  117. return repos.loadClass(clazzname);
  118. }
  119. }