blob: 69f844be19dabc081b2b8216e7699efc00240fd5 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2007, 2015 BEA Systems, Inc. and others
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* wharley@bea.com - initial API and implementation
* IBM Corporation - fix for 342936
*******************************************************************************/
package org.eclipse.jdt.compiler.apt.tests;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.ToolProvider;
import junit.framework.TestCase;
/**
* Tests for the implementation of javax.annotation.processing.Messager
* @since 3.3
*/
public class MessagerTests extends TestCase {
public final class DiagnosticReport<S> implements DiagnosticListener<S> {
public StringBuffer buffer;
private List<Diagnostic<? extends S>> diagnostics = new ArrayList<>();
DiagnosticReport() {
this.buffer = new StringBuffer();
}
@Override
public void report(Diagnostic<? extends S> diagnostic) {
diagnostics.add(diagnostic);
buffer.append(diagnostic.getMessage(Locale.getDefault()));
buffer.append("\n");
}
public List<Diagnostic<? extends S>> get(Diagnostic.Kind first, Diagnostic.Kind... rest) {
Set<Diagnostic.Kind> wanted = EnumSet.of(first, rest);
return diagnostics.stream().filter(d -> wanted.contains(d.getKind())).collect(Collectors.toList());
}
@Override
public String toString() {
return this.buffer.toString();
}
}
// See corresponding usages in the MessagerProc class
private static final String MESSAGERPROCNAME = "org.eclipse.jdt.compiler.apt.tests.processors.messager.MessagerProc";
@Override
protected void setUp() throws Exception {
super.setUp();
BatchTestUtils.init();
}
/**
* Validate the testMessager test against the javac compiler.
* @throws IOException
*/
public void testMessagerWithSystemCompiler() throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
System.out.println("No system java compiler available");
return;
}
DiagnosticReport<JavaFileObject> diagnosticListener = new DiagnosticReport<JavaFileObject>();
internalTestMessager(compiler, diagnosticListener);
// surprisingly enough javac 1.7 only reports 3 errors
// javac 1.6 reports 4 errors as expected
assertTrue("Wrong number of reported errors", diagnosticListener.get(Diagnostic.Kind.ERROR).size() >= 3);
}
/**
* Attempt to report errors on various elements, using the Eclipse compiler.
* @throws IOException
*/
public void testMessagerWithEclipseCompiler() throws IOException {
JavaCompiler compiler = BatchTestUtils.getEclipseCompiler();
DiagnosticReport<JavaFileObject> diagnosticListener = new DiagnosticReport<JavaFileObject>();
internalTestMessager(compiler, diagnosticListener);
List<Diagnostic<? extends JavaFileObject>> infos = diagnosticListener.get(Diagnostic.Kind.NOTE);
assertEquals("Wrong number of reported infos", 2, infos.size());
List<Diagnostic<? extends JavaFileObject>> warnings = diagnosticListener.get(Diagnostic.Kind.WARNING, Diagnostic.Kind.MANDATORY_WARNING);
assertEquals("Wrong number of reported warnings", 2, warnings.size());
List<Diagnostic<? extends JavaFileObject>> errors = diagnosticListener.get(Diagnostic.Kind.ERROR);
assertEquals("Wrong number of reported errors", 5, errors.size());
Diagnostic<? extends JavaFileObject> diag = errors.get(2);
JavaFileObject fileObject = diag.getSource();
CharSequence content = fileObject.getCharContent(true);
String marker = content.subSequence((int) diag.getStartPosition(), (int) diag.getEndPosition() + 1).toString();
assertEquals("Wrong error postion", "@Nested", marker);
}
/**
* Attempt to report errors on various elements.
* @throws IOException
*/
private void internalTestMessager(JavaCompiler compiler, DiagnosticListener<? super JavaFileObject> diagnosticListener) throws IOException {
System.clearProperty(MESSAGERPROCNAME);
File targetFolder = TestUtils.concatPath(BatchTestUtils.getSrcFolderName(), "targets", "errors");
BatchTestUtils.copyResources("targets/errors", targetFolder);
// Turn on the MessagerProc - without this, it will just return without doing anything
List<String> options = new ArrayList<String>();
options.add("-A" + MESSAGERPROCNAME);
options.add("-nowarn");
// Invoke processing by compiling the targets.errors resources
boolean success = BatchTestUtils.compileTreeWithErrors(compiler, options, targetFolder, diagnosticListener);
assertTrue("Compilation should have failed due to expected errors, but it didn't", !success);
// If it succeeded, the processor will have set this property to "succeeded";
// if not, it will set it to an error value.
String property = System.getProperty(MESSAGERPROCNAME);
assertNotNull("No property", property);
assertEquals("succeeded", property);
}
/* (non-Javadoc)
* @see junit.framework.TestCase#tearDown()
*/
@Override
protected void tearDown() throws Exception {
System.clearProperty(MESSAGERPROCNAME);
super.tearDown();
}
}