001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 BEA Systems, Inc.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * tyeung@bea.com - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.apt.tests;
011:
012: import java.io.File;
013:
014: import junit.framework.Test;
015: import junit.framework.TestSuite;
016:
017: import org.eclipse.core.resources.IFolder;
018: import org.eclipse.core.resources.IProject;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.Path;
021: import org.eclipse.jdt.apt.tests.annotations.ProcessorTestStatus;
022: import org.eclipse.jdt.apt.tests.annotations.readannotation.CodeExample;
023: import org.eclipse.jdt.apt.tests.plugin.AptTestsPlugin;
024: import org.eclipse.jdt.core.IJavaProject;
025: import org.eclipse.jdt.core.tests.builder.BuilderTests;
026: import org.eclipse.jdt.core.tests.util.Util;
027:
028: public class ReadAnnotationTests extends BuilderTests {
029: private int counter = 0;
030: private String projectName = null;
031:
032: public ReadAnnotationTests(final String name) {
033: super (name);
034: }
035:
036: public static Test suite() {
037: return new TestSuite(ReadAnnotationTests.class);
038: }
039:
040: public String getProjectName() {
041: return projectName;
042: }
043:
044: public String getUniqueProjectName() {
045: projectName = ReadAnnotationTests.class.getName()
046: + "Project" + counter; //$NON-NLS-1$
047: counter++;
048: return projectName;
049: }
050:
051: public IPath getSourcePath() {
052: IProject project = env.getProject(getProjectName());
053: IFolder srcFolder = project.getFolder("src"); //$NON-NLS-1$
054: IPath srcRoot = srcFolder.getFullPath();
055: return srcRoot;
056: }
057:
058: public IPath getBinaryPath() {
059: IProject project = env.getProject(getProjectName());
060: IFolder srcFolder = project.getFolder("binary"); //$NON-NLS-1$
061: IPath lib = srcFolder.getFullPath();
062: return lib;
063: }
064:
065: public IPath getOutputPath() {
066: IProject project = env.getProject(getProjectName());
067: IFolder binFolder = project.getFolder("bin"); //$NON-NLS-1$
068: IPath bin = binFolder.getFullPath();
069: return bin;
070: }
071:
072: private void addAllSources() {
073: addQuestionSources();
074: addTriggerSource();
075: }
076:
077: private void addQuestionSources() {
078: IPath srcRoot = getSourcePath();
079: // SimpleAnnotation.java
080: env.addClass(srcRoot, CodeExample.PACKAGE_QUESTION,
081: CodeExample.SIMPLE_ANNOTATION_CLASS,
082: CodeExample.SIMPLE_ANNOTATION_CODE);
083:
084: // RTVisibleAnnotation.java
085: env.addClass(srcRoot, CodeExample.PACKAGE_QUESTION,
086: CodeExample.RTVISIBLE_CLASS,
087: CodeExample.RTVISIBLE_ANNOTATION_CODE);
088:
089: // RTInvisibleAnnotation.java
090: env.addClass(srcRoot, CodeExample.PACKAGE_QUESTION,
091: CodeExample.RTINVISIBLE_CLASS,
092: CodeExample.RTINVISIBLE_ANNOTATION_CODE);
093:
094: // package-info.java
095: env.addClass(srcRoot, CodeExample.PACKAGE_QUESTION,
096: CodeExample.PACKAGE_INFO_CLASS,
097: CodeExample.PACKAGE_INFO_CODE);
098:
099: // Color.java
100: env.addClass(srcRoot, CodeExample.PACKAGE_QUESTION,
101: CodeExample.COLOR_CLASS, CodeExample.COLOR_CODE);
102:
103: // AnnotationTest.java
104: env.addClass(srcRoot, CodeExample.PACKAGE_QUESTION,
105: CodeExample.ANNOTATION_TEST_CLASS,
106: CodeExample.ANNOTATION_TEST_CODE);
107: }
108:
109: private void addTriggerSource() {
110: IPath srcRoot = getSourcePath();
111: // MyMarkerAnnotation.java
112: env.addClass(srcRoot, CodeExample.PACKAGE_TRIGGER,
113: CodeExample.MYMARKERANNOTATION_CLASS,
114: CodeExample.MYMARKERANNOTATION_CODE);
115:
116: // Trigger.java
117: env.addClass(srcRoot, CodeExample.PACKAGE_TRIGGER,
118: CodeExample.TRIGGER_CLASS, CodeExample.TRIGGER_CODE);
119: }
120:
121: private IProject setupTest() throws Exception {
122: ProcessorTestStatus.reset();
123: // project will be deleted by super-class's tearDown() method
124: IPath projectPath = env.addProject(getUniqueProjectName(),
125: "1.5"); //$NON-NLS-1$
126: env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$
127: env.addExternalJars(projectPath, Util.getJavaClassLibs());
128:
129: // This should not be necessary, but see https://bugs.eclipse.org/bugs/show_bug.cgi?id=99638
130: IJavaProject jproj = env.getJavaProject(projectPath);
131: jproj.setOption(
132: "org.eclipse.jdt.core.compiler.problem.deprecation",
133: "ignore");
134:
135: fullBuild(projectPath);
136:
137: // remove old package fragment root so that names don't collide
138: env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
139:
140: env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
141: return env.getProject(getProjectName());
142: }
143:
144: /**
145: * Set up all the source files for testing.
146: * Runs the AnnotationReaderProcessor, which contains
147: * the actual testing.
148: */
149:
150: public void test0() throws Exception {
151: // reset the error reset the error;
152: IProject project = setupTest();
153: addAllSources();
154: fullBuild(project.getFullPath());
155: expectingNoProblems();
156:
157: assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus
158: .getErrors());
159: }
160:
161: /**
162: * Set up the jar file for testing.
163: * Runs the AnnotationReaderProcessor, which contains
164: * the actual testing.
165: */
166: public void test1() throws Exception {
167: IProject project = setupTest();
168: final File jar = TestUtil.getFileInPlugin(AptTestsPlugin
169: .getDefault(), new Path("/resources/question.jar")); //$NON-NLS-1$
170: final String path = jar.getAbsolutePath();
171: env.addExternalJar(project.getFullPath(), path);
172:
173: addTriggerSource();
174:
175: fullBuild(project.getFullPath());
176: expectingNoProblems();
177:
178: assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus
179: .getErrors());
180: }
181: }
|