001: package org.eclipse.jdt.apt.pluggable.tests;
002:
003: import java.io.File;
004: import java.io.InputStream;
005: import java.net.URL;
006:
007: import org.eclipse.core.resources.IFile;
008: import org.eclipse.core.resources.IProject;
009: import org.eclipse.core.runtime.IPath;
010: import org.eclipse.jdt.core.IJavaProject;
011: import org.eclipse.jdt.core.tests.builder.BuilderTests;
012: import org.eclipse.jdt.core.tests.util.Util;
013:
014: public class TestBase extends BuilderTests {
015:
016: protected static final String JAVA_16_COMPLIANCE = "1.6";
017:
018: protected String _projectName;
019: protected static int _projectSerial = 0; // used to create unique project names, to avoid resource deletion problems
020:
021: public TestBase(String name) {
022: super (name);
023: }
024:
025: /**
026: * Extract lib/annotations.jar from the test bundle and add it to the specified project
027: */
028: private static void addAnnotationJar(IJavaProject jproj)
029: throws Exception {
030: final String resName = "lib/annotations.jar"; // name in bundle
031: final String libName = resName; // name in destination project
032: InputStream is = null;
033: URL resURL = Apt6TestsPlugin.thePlugin().getBundle().getEntry(
034: resName);
035: is = resURL.openStream();
036: IPath projPath = jproj.getPath();
037: IProject proj = jproj.getProject();
038: IFile libFile = proj.getFile(libName);
039: env.addFolder(projPath, "lib");
040: if (libFile.exists()) {
041: libFile.setContents(is, true, false, null);
042: } else {
043: libFile.create(is, true, null);
044: }
045: env.addLibrary(projPath, libFile.getFullPath(), null, null);
046: }
047:
048: /**
049: * Create a java project with java libraries and test annotations on classpath
050: * (compiler level is 1.6). Use "src" as source folder and "bin" as output folder. APT
051: * is not enabled.
052: *
053: * @param projectName
054: * @return a java project that has been added to the current workspace.
055: * @throws Exception
056: */
057: protected static IJavaProject createJavaProject(
058: final String projectName) throws Exception {
059: IPath projectPath = env.addProject(projectName,
060: JAVA_16_COMPLIANCE);
061: env.addExternalJars(projectPath, Util.getJavaClassLibs());
062: // remove old package fragment root so that names don't collide
063: env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
064: env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
065: env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
066: final IJavaProject javaProj = env.getJavaProject(projectPath);
067: addAnnotationJar(javaProj);
068: return javaProj;
069: }
070:
071: /**
072: * Verify that an expected file exists within a project.
073: * @param fileName the filename relative to the project root.
074: */
075: protected void expectingFile(IProject proj, String fileName)
076: throws Exception {
077: IPath path = proj.getLocation().append(fileName);
078: File file = new File(path.toOSString());
079: assertTrue("Expected file " + fileName
080: + " was missing from project", file != null
081: && file.exists());
082: }
083:
084: /**
085: * Verify that an expected file exists within a project.
086: * @param fileName the filename relative to the project root.
087: */
088: protected void expectingNoFile(IProject proj, String fileName)
089: throws Exception {
090: IPath path = proj.getLocation().append(fileName);
091: File file = new File(path.toOSString());
092: boolean exists = file.exists();
093: // work around a timing bug in some versions of JRE 1.6 on Linux:
094: // Before assuming the test has failed, wait half a second and try again.
095: // This delay is not encountered when the test is passing normally.
096: if (exists) {
097: Thread.sleep(500);
098: exists = file.exists();
099: }
100: assertTrue("File " + fileName + " was expected to not exist",
101: file == null || !exists);
102: }
103:
104: @Override
105: protected void setUp() throws Exception {
106: super .setUp();
107: env.setAutoBuilding(false);
108: _projectName = String.format("testproj%04d", ++_projectSerial);
109: }
110:
111: }
|