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.

AjdeCoreTestCase.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /********************************************************************
  2. * Copyright (c) 2007 Contributors. All rights reserved.
  3. * This program and the accompanying materials are made available
  4. * under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution and is available at
  6. * http://eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: IBM Corporation - initial API and implementation
  9. * Helen Hawkins - initial version
  10. *******************************************************************/
  11. package org.aspectj.ajde.core;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import junit.framework.AssertionFailedError;
  17. import junit.framework.TestCase;
  18. import org.aspectj.ajde.core.TestMessageHandler.TestMessage;
  19. import org.aspectj.tools.ajc.Ajc;
  20. /**
  21. * Testcase class to be used by all ajde.core tests. Provides helper methods to set up the environment in a sandbox as well as to
  22. * drive a build.
  23. */
  24. public class AjdeCoreTestCase extends TestCase {
  25. public final static String testdataSrcDir = "../ajde.core/testdata";
  26. protected static File sandboxDir;
  27. private String projectDir;
  28. private AjCompiler compiler;
  29. protected void setUp() throws Exception {
  30. super.setUp();
  31. // Create a sandbox in which to work
  32. sandboxDir = Ajc.createEmptySandbox();
  33. // AMC - added this next line as a temporary workaround for
  34. // listener leakage in AsmManager induced by the Ajde test suite.
  35. // AsmManager.getDefault().removeAllListeners();
  36. }
  37. protected void tearDown() throws Exception {
  38. super.tearDown();
  39. compiler.clearLastState();
  40. compiler = null;
  41. }
  42. /**
  43. * Fill in the working directory with the project files and creates a compiler instance for this project
  44. */
  45. public void initialiseProject(String projectName) throws IOException {
  46. File projectSrc = new File(testdataSrcDir + File.separatorChar + projectName);
  47. File destination = new File(getWorkingDir(), projectName);
  48. if (!destination.exists()) {
  49. destination.mkdir();
  50. }
  51. copy(projectSrc, destination);
  52. projectDir = destination.getCanonicalPath();// getAbsolutePath();
  53. compiler = new AjCompiler(projectDir, new TestCompilerConfiguration(projectDir), new TestBuildProgressMonitor(),
  54. new TestMessageHandler());
  55. }
  56. /**
  57. * @return the working directory
  58. */
  59. protected File getWorkingDir() {
  60. return sandboxDir;
  61. }
  62. /**
  63. * @return the absolute path of the project directory for example c:\temp\ajcSandbox\ajcTest15200.tmp\myProject
  64. */
  65. protected String getAbsoluteProjectDir() {
  66. return projectDir;
  67. }
  68. /**
  69. * Copy the contents of some directory to another location - the copy is recursive.
  70. */
  71. private void copy(File from, File to) {
  72. String contents[] = from.list();
  73. if (contents == null)
  74. return;
  75. for (int i = 0; i < contents.length; i++) {
  76. String string = contents[i];
  77. File f = new File(from, string);
  78. File t = new File(to, string);
  79. if (f.isDirectory()) {
  80. t.mkdir();
  81. copy(f, t);
  82. } else if (f.isFile()) {
  83. try {
  84. org.aspectj.util.FileUtil.copyFile(f, t);
  85. } catch (IOException e) {
  86. throw new AssertionFailedError("Unable to copy " + f + " to " + t);
  87. }
  88. }
  89. }
  90. }
  91. protected File openFile(String path) {
  92. return new File(projectDir + File.separatorChar + path);
  93. }
  94. public void doBuild() {
  95. doBuild(true);
  96. }
  97. public void doBuild(boolean buildFresh) {
  98. if (buildFresh) {
  99. compiler.buildFresh();
  100. } else {
  101. compiler.build();
  102. }
  103. }
  104. public AjCompiler getCompiler() {
  105. return compiler;
  106. }
  107. public boolean checkFor(String what) {
  108. List<TestMessage> ll = ((TestMessageHandler) compiler.getMessageHandler()).getMessages();
  109. for (TestMessage element: ll) {
  110. if (element.toString().indexOf(what) != -1)
  111. return true;
  112. }
  113. return false;
  114. }
  115. public void dumpTaskData() {
  116. List<TestMessage> ll = ((TestMessageHandler) compiler.getMessageHandler()).getMessages();
  117. for (TestMessage element: ll) {
  118. System.out.println("RecordedMessage>" + element);
  119. }
  120. }
  121. public List<String> getSourceFileList(String[] files) {
  122. List<String> sourceFiles = new ArrayList<String>();
  123. for (int i = 0; i < files.length; i++) {
  124. sourceFiles.add(getAbsoluteProjectDir() + File.separator + files[i]);
  125. }
  126. return sourceFiles;
  127. }
  128. }