diff options
author | Walter Harley | 2008-01-05 01:19:26 +0000 |
---|---|---|
committer | Walter Harley | 2008-01-05 01:19:26 +0000 |
commit | a626dbe34a0108abdf11028aa7b5540f10df6847 (patch) | |
tree | 20a0e3e0c09a5a725b83e1b7262b720d745e2ef7 | |
parent | d1124c61c8f3ff39bbe128bdbccc90a8a92cba7f (diff) | |
download | eclipse.jdt.core-a626dbe34a0108abdf11028aa7b5540f10df6847.tar.gz eclipse.jdt.core-a626dbe34a0108abdf11028aa7b5540f10df6847.tar.xz eclipse.jdt.core-a626dbe34a0108abdf11028aa7b5540f10df6847.zip |
Test case for bug 213539 (support Filer.getResource)
9 files changed, 303 insertions, 2 deletions
diff --git a/org.eclipse.jdt.apt.pluggable.tests/META-INF/services/javax.annotation.processing.Processor b/org.eclipse.jdt.apt.pluggable.tests/META-INF/services/javax.annotation.processing.Processor index 03e72ecde9..d0380e084c 100644 --- a/org.eclipse.jdt.apt.pluggable.tests/META-INF/services/javax.annotation.processing.Processor +++ b/org.eclipse.jdt.apt.pluggable.tests/META-INF/services/javax.annotation.processing.Processor @@ -1,2 +1,3 @@ org.eclipse.jdt.apt.pluggable.tests.processors.genclass6.GenClass6Proc org.eclipse.jdt.apt.pluggable.tests.processors.message6.Message6Proc +org.eclipse.jdt.apt.pluggable.tests.processors.filertester.FilerTesterProc diff --git a/org.eclipse.jdt.apt.pluggable.tests/lib/annotations.jar b/org.eclipse.jdt.apt.pluggable.tests/lib/annotations.jar Binary files differindex 47108afacf..c0d88bde37 100644 --- a/org.eclipse.jdt.apt.pluggable.tests/lib/annotations.jar +++ b/org.eclipse.jdt.apt.pluggable.tests/lib/annotations.jar diff --git a/org.eclipse.jdt.apt.pluggable.tests/plugin.xml b/org.eclipse.jdt.apt.pluggable.tests/plugin.xml index bb01e4a379..6416730842 100644 --- a/org.eclipse.jdt.apt.pluggable.tests/plugin.xml +++ b/org.eclipse.jdt.apt.pluggable.tests/plugin.xml @@ -11,6 +11,9 @@ <java6processor class="org.eclipse.jdt.apt.pluggable.tests.processors.message6.Message6Proc"> </java6processor> + <java6processor + class="org.eclipse.jdt.apt.pluggable.tests.processors.filertester.FilerTesterProc"> + </java6processor> </java6processors> </extension> </plugin> diff --git a/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/FilerTests.java b/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/FilerTests.java index 93a438d0dc..01ef102421 100644 --- a/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/FilerTests.java +++ b/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/FilerTests.java @@ -139,5 +139,39 @@ public class FilerTests extends TestBase expectingNoFile(proj, ".apt_generated/gen6/Generated02.java"); } + + // Temporarily disabled, functionality not yet implemented + public void _testGetResource01() throws Throwable { + ProcessorTestStatus.reset(); + IJavaProject jproj = createJavaProject(_projectName); + IProject proj = jproj.getProject(); + IPath projPath = proj.getFullPath(); + + env.addClass(projPath.append("src"), "p", "Trigger", + "package p;\n" + + "import org.eclipse.jdt.apt.pluggable.tests.annotations.FilerTestTrigger;\n" + + "@FilerTestTrigger(test = \"testGetResource01\", arg0 = \"src/p\", arg1 = \"Trigger.java\")" + + "public class Trigger {\n" + + "}" + ); + + // Make sure that there are no Java 5 processors on the factory path - see comment below. + FactoryPath fp = (FactoryPath) AptConfig.getFactoryPath(jproj); + for (Map.Entry<FactoryContainer, FactoryPath.Attributes> entry : fp.getAllContainers().entrySet()) { + if (entry.getKey().getType() == FactoryType.PLUGIN) { + String id = entry.getKey().getId(); + if (!Apt6TestsPlugin.PLUGIN_ID.equals(id)) { + fp.disablePlugin(id); + } + } + } + AptConfig.setFactoryPath(jproj, fp); + AptConfig.setEnabled(jproj, true); + fullBuild(); + expectingNoProblems(); + assertTrue("Processor did not run", ProcessorTestStatus.processorRan()); + assertEquals("Processor reported errors", ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors()); + } + } diff --git a/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/ProcessorTestStatus.java b/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/ProcessorTestStatus.java new file mode 100644 index 0000000000..a317b05207 --- /dev/null +++ b/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/ProcessorTestStatus.java @@ -0,0 +1,108 @@ +/******************************************************************************* + * Copyright (c) 2008 BEA Systems, Inc. + * 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://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * jgarms@bea.com - original implementation in org.eclipse.jdt.apt.tests + * wharley@bea.com - copied to org.eclipse.jdt.apt.pluggable.tests + * + *******************************************************************************/ + +package org.eclipse.jdt.apt.pluggable.tests; + +/** + * Utility class to hold results of processor-based tests. + * All methods are static. + */ +public final class ProcessorTestStatus { + + /** + * Marker string to indicate that no errors were encountered. + */ + public static final String NO_ERRORS = "NO ERRORS"; + + /** + * Marker string to indicate processor never ran. + */ + public static final String NOT_RUN = "NOT RUN"; + + /** Error status. Will be == NO_ERRORS if no errors were encountered **/ + private static String s_errorStatus = NOT_RUN; + + /** + * Was a processor run at all? + */ + private static boolean s_processorRan = false; + + /** An expected condition failed. Record the error **/ + public static void failWithoutException(final String error) { + s_errorStatus = error; + } + + /** Returns true if any errors were encountered **/ + public static boolean hasErrors() { + return s_errorStatus != NO_ERRORS; + } + + /** Get the error string. Will be NO_ERRORS if none were encountered **/ + public static String getErrors() { + return s_errorStatus; + } + + /** Reset the status. Needs to be called before each set of tests that could fail **/ + public static void reset() { + s_errorStatus = NOT_RUN; + s_processorRan = false; + } + + /** Did a processor call the setProcessorRan() method since the last reset()? */ + public static boolean processorRan() { + return s_processorRan; + } + + /** A processor can call this to indicate that it has run (with or without errors) */ + public static void setProcessorRan() { + s_processorRan = true; + if (NOT_RUN.equals(s_errorStatus)) + s_errorStatus = NO_ERRORS; + } + + // Private c-tor to prevent construction + private ProcessorTestStatus() {} + + public static void assertEquals(String reason, Object expected, Object actual) { + if (expected == actual) + return; + if (expected != null && expected.equals(actual)) + return; + ProcessorTestStatus.fail("Expected " + expected + ", but saw " + actual + ". Reason: " + reason); + } + + public static void assertEquals(String reason, String expected, String actual) { + if (expected == actual) + return; + if (expected != null && expected.equals(actual)) + return; + ProcessorTestStatus.fail("Expected " + expected + ", but saw " + actual + ". Reason: " + reason); + } + + public static void assertEquals(String reason, int expected, int actual) { + if (expected == actual) + return; + ProcessorTestStatus.fail("Expected " + expected + ", but saw " + actual + ". Reason: " + reason); + } + + public static void assertTrue(String reason, boolean expected) { + if (!expected) + ProcessorTestStatus.fail(reason); + } + + public static void fail(final String reason) { + failWithoutException(reason); + throw new IllegalStateException("Failed during test: " + reason); + } + +} diff --git a/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/annotations/FilerTestTrigger.java b/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/annotations/FilerTestTrigger.java new file mode 100644 index 0000000000..9f596dcb83 --- /dev/null +++ b/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/annotations/FilerTestTrigger.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright (c) 2007 BEA Systems, Inc. + * 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://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wharley@bea.com - initial API and implementation + * + *******************************************************************************/ + +package org.eclipse.jdt.apt.pluggable.tests.annotations; + +/** + * + * @since 3.4 + */ +public @interface FilerTestTrigger { + /** Name of test method to run */ + String test(); + + /** Arbitrary argument */ + String arg0() default ""; + + /** Arbitrary argument */ + String arg1() default ""; +} diff --git a/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/processors/filertester/FilerTesterProc.java b/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/processors/filertester/FilerTesterProc.java new file mode 100644 index 0000000000..b8ae7f5b78 --- /dev/null +++ b/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/processors/filertester/FilerTesterProc.java @@ -0,0 +1,123 @@ +/******************************************************************************* + * Copyright (c) 2007 BEA Systems, Inc. + * 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://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wharley@bea.com - initial API and implementation + * + *******************************************************************************/ + +package org.eclipse.jdt.apt.pluggable.tests.processors.filertester; + +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Set; + +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.Filer; +import javax.annotation.processing.ProcessingEnvironment; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +import javax.annotation.processing.SupportedOptions; +import javax.annotation.processing.SupportedSourceVersion; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.Element; +import javax.lang.model.element.TypeElement; +import javax.tools.FileObject; +import javax.tools.StandardLocation; + +import org.eclipse.jdt.apt.pluggable.tests.ProcessorTestStatus; +import org.eclipse.jdt.apt.pluggable.tests.annotations.FilerTestTrigger; + +/** + * Testing annotation processors through JUnit in the IDE is complex, because each test requires + * something different of the processor and all processors must coexist in the plugin registry, and + * because the processor has very limited communication with the rest of the IDE. So, we make one + * processor run many tests. The JUnit tests specify which test to run by passing its name in to the + * FilerTest annotation. Test failures are reported via the Messager interface. + * + * @since 3.4 + */ +@SupportedAnnotationTypes( { "org.eclipse.jdt.apt.pluggable.tests.annotations.FilerTestTrigger" }) +@SupportedSourceVersion(SourceVersion.RELEASE_6) +@SupportedOptions( {}) +public class FilerTesterProc extends AbstractProcessor { + + private ProcessingEnvironment _processingEnv; + private Filer _filer; + + /* + * (non-Javadoc) + * + * @see javax.annotation.processing.AbstractProcessor#init(javax.annotation.processing.ProcessingEnvironment) + */ + @Override + public synchronized void init(ProcessingEnvironment processingEnv) { + super.init(processingEnv); + _processingEnv = processingEnv; + _filer = _processingEnv.getFiler(); + } + + /* + * (non-Javadoc) + * + * @see javax.annotation.processing.AbstractProcessor#process(java.util.Set, + * javax.annotation.processing.RoundEnvironment) + */ + @Override + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { + ProcessorTestStatus.setProcessorRan(); + if (!roundEnv.processingOver() && !annotations.isEmpty()) { + round(annotations, roundEnv); + } + return true; + } + + /** + * Perform a round of processing: for a given annotation instance, determine what test method it + * specifies, and invoke that method, passing in the annotated element. + */ + private void round(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { + TypeElement filerTesterAnno = annotations.iterator().next(); + Set<? extends Element> annotatedEls = roundEnv.getElementsAnnotatedWith(filerTesterAnno); + for (Element annotatedEl : annotatedEls) { + FilerTestTrigger filerTesterMirror = annotatedEl.getAnnotation(FilerTestTrigger.class); + String testMethodName = filerTesterMirror.test(); + String arg0 = filerTesterMirror.arg0(); + String arg1 = filerTesterMirror.arg1(); + if (null != testMethodName && testMethodName.length() > 0) { + try { + Method testMethod = FilerTesterProc.class.getMethod(testMethodName, + Element.class, String.class, String.class); + testMethod.invoke(this, annotatedEl, arg0, arg1); + } catch (Exception e) { + Throwable t; + t = (e instanceof InvocationTargetException) ? t = e.getCause() : e; + t.printStackTrace(); + // IllegalStateException probably means test method called ProcessorTestStatus.fail() + String msg = (t instanceof IllegalStateException) ? + t.getMessage() : + t.getClass().getSimpleName() + " invoking test method " + + testMethodName + " - see console for details"; + ProcessorTestStatus.fail(msg); + } + } + } + } + + /** + * Attempt to get an existing resource from the SOURCE_PATH. + */ + public void testGetResource01(Element e, String arg0, String arg1) throws Exception { + FileObject resource = _filer.getResource(StandardLocation.SOURCE_PATH, arg0, arg1); + InputStream stream = resource.openInputStream(); + if (stream.available() <= 0) { + ProcessorTestStatus.fail("stream contained no data"); + } + } + +} diff --git a/org.eclipse.jdt.apt.tests/src/org/eclipse/jdt/apt/tests/annotations/ProcessorTestStatus.java b/org.eclipse.jdt.apt.tests/src/org/eclipse/jdt/apt/tests/annotations/ProcessorTestStatus.java index 66b6ac6c5f..142e42b509 100644 --- a/org.eclipse.jdt.apt.tests/src/org/eclipse/jdt/apt/tests/annotations/ProcessorTestStatus.java +++ b/org.eclipse.jdt.apt.tests/src/org/eclipse/jdt/apt/tests/annotations/ProcessorTestStatus.java @@ -64,7 +64,8 @@ public final class ProcessorTestStatus { /** A processor can call this to indicate that it has run (with or without errors) */ public static void setProcessorRan() { s_processorRan = true; - s_errorStatus = NO_ERRORS; + if (NOT_RUN.equals(s_errorStatus)) + s_errorStatus = NO_ERRORS; } // Private c-tor to prevent construction diff --git a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/dispatch/BaseMessagerImpl.java b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/dispatch/BaseMessagerImpl.java index 990a294b7d..55eb0b258e 100644 --- a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/dispatch/BaseMessagerImpl.java +++ b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/dispatch/BaseMessagerImpl.java @@ -111,7 +111,10 @@ public class BaseMessagerImpl { case TYPE_PARAMETER : } } - StringBuilder builder = new StringBuilder(msg); + StringBuilder builder = new StringBuilder(); + if (msg != null) { + builder.append(msg); + } int lineNumber = 0; int columnNumber = 1; char[] fileName = null; |