123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /********************************************************************
- * Copyright (c) 2007 Contributors. All rights reserved.
- * This program and the accompanying materials are made available
- * under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution and is available at
- * http://eclipse.org/legal/epl-v10.html
- *
- * Contributors: IBM Corporation - initial API and implementation
- * Helen Hawkins - initial version
- *******************************************************************/
- package org.aspectj.ajde.core;
-
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
-
- import junit.framework.AssertionFailedError;
- import junit.framework.TestCase;
-
- import org.aspectj.tools.ajc.Ajc;
-
- /**
- * 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
- * drive a build.
- */
- public class AjdeCoreTestCase extends TestCase {
-
- public final static String testdataSrcDir = "../ajde.core/testdata";
- protected static File sandboxDir;
-
- private String projectDir;
- private AjCompiler compiler;
-
- protected void setUp() throws Exception {
- super.setUp();
- // Create a sandbox in which to work
- sandboxDir = Ajc.createEmptySandbox();
- // AMC - added this next line as a temporary workaround for
- // listener leakage in AsmManager induced by the Ajde test suite.
- // AsmManager.getDefault().removeAllListeners();
- }
-
- protected void tearDown() throws Exception {
- super.tearDown();
- compiler.clearLastState();
- compiler = null;
- }
-
- /**
- * Fill in the working directory with the project files and creates a compiler instance for this project
- */
- public void initialiseProject(String projectName) throws IOException {
- File projectSrc = new File(testdataSrcDir + File.separatorChar + projectName);
- File destination = new File(getWorkingDir(), projectName);
- if (!destination.exists()) {
- destination.mkdir();
- }
- copy(projectSrc, destination);
- projectDir = destination.getCanonicalPath();// getAbsolutePath();
-
- compiler = new AjCompiler(projectDir, new TestCompilerConfiguration(projectDir), new TestBuildProgressMonitor(),
- new TestMessageHandler());
- }
-
- /**
- * @return the working directory
- */
- protected File getWorkingDir() {
- return sandboxDir;
- }
-
- /**
- * @return the absolute path of the project directory for example c:\temp\ajcSandbox\ajcTest15200.tmp\myProject
- */
- protected String getAbsoluteProjectDir() {
- return projectDir;
- }
-
- /**
- * Copy the contents of some directory to another location - the copy is recursive.
- */
- private void copy(File from, File to) {
- String contents[] = from.list();
- if (contents == null)
- return;
- for (int i = 0; i < contents.length; i++) {
- String string = contents[i];
- File f = new File(from, string);
- File t = new File(to, string);
-
- if (f.isDirectory()) {
- t.mkdir();
- copy(f, t);
- } else if (f.isFile()) {
- try {
- org.aspectj.util.FileUtil.copyFile(f, t);
- } catch (IOException e) {
- throw new AssertionFailedError("Unable to copy " + f + " to " + t);
- }
- }
- }
- }
-
- protected File openFile(String path) {
- return new File(projectDir + File.separatorChar + path);
- }
-
- public void doBuild() {
- doBuild(true);
- }
-
- public void doBuild(boolean buildFresh) {
- if (buildFresh) {
- compiler.buildFresh();
- } else {
- compiler.build();
- }
- }
-
- public AjCompiler getCompiler() {
- return compiler;
- }
-
- public boolean checkFor(String what) {
- List ll = ((TestMessageHandler) compiler.getMessageHandler()).getMessages();
- for (Iterator iter = ll.iterator(); iter.hasNext();) {
- Object element = iter.next();
- if (element.toString().indexOf(what) != -1)
- return true;
- }
- return false;
- }
-
- public void dumpTaskData() {
- List ll = ((TestMessageHandler) compiler.getMessageHandler()).getMessages();
- for (Iterator iter = ll.iterator(); iter.hasNext();) {
- Object element = iter.next();
- System.out.println("RecordedMessage>" + element);
- }
- }
-
- public List<String> getSourceFileList(String[] files) {
- List<String> sourceFiles = new ArrayList<String>();
- for (int i = 0; i < files.length; i++) {
- sourceFiles.add(getAbsoluteProjectDir() + File.separator + files[i]);
- }
- return sourceFiles;
- }
-
- }
|