Skip to content

Commit

Permalink
Parametrise some error message parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kriegaex committed Dec 18, 2023
1 parent 4377aa9 commit 32ba367
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 13 deletions.
5 changes: 5 additions & 0 deletions plexus-compilers/plexus-compiler-javac/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import org.codehaus.plexus.compiler.CompilerMessage;
import org.codehaus.plexus.util.Os;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
Expand Down Expand Up @@ -775,19 +779,73 @@ public void testJava7Error() throws Exception {
assertThat(message2.getEndLine(), is(3));
}

@Test
public void testBugParade() throws Exception {
String out = "An exception has occurred in the compiler (1.7.0_80). "
+ "Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates. "
+ "Include your program and the following diagnostic in your report. Thank you." + EOL
+ "com.sun.tools.javac.code.Symbol$CompletionFailure: class file for java.util.Optional not found";
@ParameterizedTest(name = "{0}")
@MethodSource("testBugParade_args")
public void testBugParade(String jdkAndLocale, String stackTraceHeader) throws Exception {
String out = stackTraceHeader + "\tat com.sun.tools.javac.comp.MemberEnter.baseEnv(MemberEnter.java:1388)\n"
+ "\tat com.sun.tools.javac.comp.MemberEnter.complete(MemberEnter.java:1046)\n"
+ "\tat com.sun.tools.javac.code.Symbol.complete(Symbol.java:574)\n"
+ "\tat com.sun.tools.javac.code.Symbol$ClassSymbol.complete(Symbol.java:1037)\n"
+ "\tat com.sun.tools.javac.code.Symbol$ClassSymbol.flags(Symbol.java:973)\n"
+ "\tat com.sun.tools.javac.code.Symbol$ClassSymbol.getKind(Symbol.java:1101)\n"
+ "\tat com.sun.tools.javac.code.Kinds.kindName(Kinds.java:162)\n"
+ "\tat com.sun.tools.javac.comp.Check.duplicateError(Check.java:329)\n"
+ "\tat com.sun.tools.javac.comp.Check.checkUnique(Check.java:3435)\n"
+ "\tat com.sun.tools.javac.comp.Enter.visitTypeParameter(Enter.java:454)\n"
+ "\tat com.sun.tools.javac.tree.JCTree$JCTypeParameter.accept(JCTree.java:2224)\n"
+ "\tat com.sun.tools.javac.comp.Enter.classEnter(Enter.java:258)\n"
+ "\tat com.sun.tools.javac.comp.Enter.classEnter(Enter.java:272)\n"
+ "\tat com.sun.tools.javac.comp.Enter.visitClassDef(Enter.java:418)\n"
+ "\tat com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:693)\n"
+ "\tat com.sun.tools.javac.comp.Enter.classEnter(Enter.java:258)\n"
+ "\tat com.sun.tools.javac.comp.Enter.classEnter(Enter.java:272)\n"
+ "\tat com.sun.tools.javac.comp.Enter.visitTopLevel(Enter.java:334)\n"
+ "\tat com.sun.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:518)\n"
+ "\tat com.sun.tools.javac.comp.Enter.classEnter(Enter.java:258)\n"
+ "\tat com.sun.tools.javac.comp.Enter.classEnter(Enter.java:272)\n"
+ "\tat com.sun.tools.javac.comp.Enter.complete(Enter.java:486)\n"
+ "\tat com.sun.tools.javac.comp.Enter.main(Enter.java:471)\n"
+ "\tat com.sun.tools.javac.main.JavaCompiler.enterTrees(JavaCompiler.java:982)\n"
+ "\tat com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:857)\n"
+ "\tat com.sun.tools.javac.main.Main.compile(Main.java:523)\n"
+ "\tat com.sun.tools.javac.main.Main.compile(Main.java:381)\n"
+ "\tat com.sun.tools.javac.main.Main.compile(Main.java:370)\n"
+ "\tat com.sun.tools.javac.main.Main.compile(Main.java:361)\n"
+ "\tat com.sun.tools.javac.Main.compile(Main.java:56)\n"
+ "\tat com.sun.tools.javac.Main.main(Main.java:42)\n";

List<CompilerMessage> compilerErrors =
JavacCompiler.parseModernStream(4, new BufferedReader(new StringReader(out)));

assertThat(compilerErrors, notNullValue());

assertThat(compilerErrors.size(), is(1));
// Parser retains stack trace header
assertThat(compilerErrors.get(0).getMessage(), startsWith(stackTraceHeader));
}

private static Stream<Arguments> testBugParade_args() {
return Stream.of(
Arguments.of(
"JDK 8 English",
"An exception has occurred in the compiler ({0}). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you."),
Arguments.of(
"JDK 8 Japanese",
"コンパイラで例外が発生しました({0})。Bug Paradeで重複がないかをご確認のうえ、Java Developer Connection (http://java.sun.com/webapps/bugreport)でbugの登録をお願いいたします。レポートには、そのプログラムと下記の診断内容を含めてください。ご協力ありがとうございます。"),
Arguments.of(
"JDK 8 Chinese",
"编译器 ({0}) 中出现异常错误。 如果在 Bug Parade 中没有找到该错误, 请在 Java Developer Connection (http://java.sun.com/webapps/bugreport) 中建立 Bug。请在报告中附上您的程序和以下诊断信息。谢谢。"),
Arguments.of(
"JDK 21 English",
"An exception has occurred in the compiler ({0}). Please file a bug against the Java compiler via the Java bug reporting page (https://bugreport.java.com) after checking the Bug Database (https://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you."),
Arguments.of(
"JDK 21 Japanese",
"コンパイラで例外が発生しました({0})。バグ・データベース(https://bugs.java.com)で重複がないかをご確認のうえ、Javaのバグ・レポート・ページ(https://bugreport.java.com)から、Javaコンパイラに対するバグの登録をお願いいたします。レポートには、該当のプログラム、次の診断内容、およびJavaコンパイラに渡されたパラメータをご入力ください。ご協力ありがとうございます。"),
Arguments.of(
"JDK 21 Chinese",
"编译器 ({0}) 中出现异常错误。如果在 Bug Database (https://bugs.java.com) 中没有找到有关该错误的 Java 编译器 Bug,请通过 Java Bug 报告页 (https://bugreport.java.com) 提交 Java 编译器 Bug。请在报告中附上您的程序、以下诊断信息以及传递到 Java 编译器的参数。谢谢。"),
Arguments.of(
"JDK 21 German",
"Im Compiler ({0}) ist eine Ausnahme aufgetreten. Erstellen Sie auf der Java-Seite zum Melden von Bugs (https://bugreport.java.com) einen Bugbericht, nachdem Sie die Bugdatenbank (https://bugs.java.com) auf Duplikate geprüft haben. Geben Sie in Ihrem Bericht Ihr Programm, die folgende Diagnose und die Parameter an, die Sie dem Java-Compiler übergeben haben. Vielen Dank."));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.stream.Stream;

import org.codehaus.plexus.compiler.CompilerMessage;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.core.StringStartsWith.startsWith;

/*
* Licensed to the Apache Software Foundation (ASF) under one
Expand Down Expand Up @@ -41,11 +46,11 @@ public void setUp() {
setForceJavacCompilerUse(true);
}

@Test
void parseModernStream_withAnnotationProcessingErrors() throws IOException {
String input = "\n" + "\n"
+ "An annotation processor threw an uncaught exception.\n"
+ "Consult the following stack trace for details.\n"
@ParameterizedTest(name = "{0}")
@MethodSource("parseModernStream_withAnnotationProcessingErrors_args")
void parseModernStream_withAnnotationProcessingErrors(String jdkAndLocale, String stackTraceHeader)
throws IOException {
String input = stackTraceHeader
+ "java.lang.IllegalAccessError: class lombok.javac.apt.LombokProcessor (in unnamed module @0x1da51a35) cannot access class com.sun.tools.javac.processing.JavacProcessingEnvironment (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.processing to unnamed module @0x1da51a35\n"
+ "\tat lombok.javac.apt.LombokProcessor.getJavacProcessingEnvironment(LombokProcessor.java:433)\n"
+ "\tat lombok.javac.apt.LombokProcessor.init(LombokProcessor.java:92)\n"
Expand All @@ -68,5 +73,24 @@ void parseModernStream_withAnnotationProcessingErrors() throws IOException {
JavacCompiler.parseModernStream(1, new BufferedReader(new StringReader(input)));

assertThat(compilerMessages, hasSize(1));
// Parser does not retain stack trace header, because it is hard to identify in a locale-independent way
assertThat(compilerMessages.get(0).getMessage(), not(startsWith(stackTraceHeader)));
}

private static Stream<Arguments> parseModernStream_withAnnotationProcessingErrors_args() {
return Stream.of(
Arguments.of(
"JDK 8 English",
"\n\nAn annotation processor threw an uncaught exception.\nConsult the following stack trace for details.\n"),
Arguments.of("JDK 8 Japanese", "\n\n注釈処理で捕捉されない例外がスローされました。\n詳細は次のスタック・トレースで調査してください。\n"),
Arguments.of("JDK 8 Chinese", "\n\n注释处理程序抛出未捕获的异常错误。\n有关详细信息, 请参阅以下堆栈跟踪。\n"),
Arguments.of(
"JDK 21 English",
"\n\nAn annotation processor threw an uncaught exception.\nConsult the following stack trace for details.\n"),
Arguments.of("JDK 21 Japanese", "\n\n注釈処理で捕捉されない例外がスローされました。\n詳細は次のスタックトレースで調査してください。\n"),
Arguments.of("JDK 21 Chinese", "\n\n批注处理程序抛出未捕获的异常错误。\n有关详细信息, 请参阅以下堆栈跟踪。\n"),
Arguments.of(
"JDK 21 German",
"\n\nEin Annotationsprozessor hat eine nicht abgefangene Ausnahme ausgelöst.\nDetails finden Sie im folgenden Stacktrace.\n"));
}
}

0 comments on commit 32ba367

Please sign in to comment.