Add test demo for LOG4J2-3609 release-2.x
authorMatt Sicker <mattsicker@apache.org>
Sat, 1 Oct 2022 22:24:36 +0000 (17:24 -0500)
committerMatt Sicker <mattsicker@apache.org>
Sat, 1 Oct 2022 22:24:36 +0000 (17:24 -0500)
Signed-off-by: Matt Sicker <mattsicker@apache.org>
log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/Compiler.java [new file with mode: 0644]
log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/AnnotationProcessorCompilerErrorTest.java [new file with mode: 0644]
log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/util/PluginManagerPackagesTest.java
log4j-core-test/src/test/resources/LOG4J2-3609/MyAnnotatedClass.java.source [new file with mode: 0644]
log4j-core-test/src/test/resources/LOG4J2-3609/MyAnnotation.java.source [new file with mode: 0644]
log4j-core-test/src/test/resources/LOG4J2-3609/MyAnnotationProcessor.java.source [new file with mode: 0644]
log4j-core-test/src/test/resources/LOG4J2-3609/MyEmptySubClass.java.source [new file with mode: 0644]

diff --git a/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/Compiler.java b/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/Compiler.java
new file mode 100644 (file)
index 0000000..8b6361d
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.test;
+
+import javax.tools.Diagnostic;
+import javax.tools.DiagnosticCollector;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class Compiler {
+    public static void compile(final File source, final String... compilerOptions) throws IOException {
+        compile(Collections.singletonList(source), compilerOptions);
+    }
+
+    public static void compile(final Iterable<? extends File> sources, final String... compilerOptions) throws IOException {
+        final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
+        final List<String> errors = new ArrayList<>();
+        try (final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) {
+            final Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(sources);
+            final List<String> options = Arrays.asList(compilerOptions);
+            compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits).call();
+
+            // check we don't have any compilation errors
+            for (final Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
+                if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
+                    errors.add(diagnostic.toString());
+                }
+            }
+        }
+        assertThat(errors).isEmpty();
+    }
+}
diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/AnnotationProcessorCompilerErrorTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/AnnotationProcessorCompilerErrorTest.java
new file mode 100644 (file)
index 0000000..96da35a
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.config.plugins.processor;
+
+import org.apache.logging.log4j.core.test.Compiler;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
+import org.junitpioneer.jupiter.Issue;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+@Issue("LOG4J2-3609")
+// only enabled for explicit testing of what may be a javac bug
+// mvn test -Dtest=AnnotationProcessorCompilerErrorTest'#'testMissingExternalAnnotationIsSupported
+@EnabledIfSystemProperty(named = "test", matches = "AnnotationProcessorCompilerErrorTest#testMissingExternalAnnotationIsSupported")
+class AnnotationProcessorCompilerErrorTest {
+    @Test
+    void testMissingExternalAnnotationIsSupported() throws Exception {
+        final Path sourceDirectory = Paths.get("target", "test-classes", "LOG4J2-3609");
+        // set up classpath directories
+        final Path annotationDirectory = Files.createTempDirectory("annotation");
+        final Path coreDirectory = Files.createTempDirectory("core");
+        final Path bugDirectory = Files.createTempDirectory("bug");
+        final Path processorDirectory = Files.createTempDirectory("processor");
+
+        // Create a stub annotation processor
+        final Path originalProcessor = sourceDirectory.resolve("MyAnnotationProcessor.java.source");
+        final Path myProcessor = sourceDirectory.resolve("MyAnnotationProcessor.java");
+        Files.move(originalProcessor, myProcessor);
+        try {
+            Compiler.compile(myProcessor.toFile(), "-proc:none", "-d", processorDirectory.toString());
+        } finally {
+            Files.move(myProcessor, originalProcessor);
+        }
+
+        // Compile the annotation and keep the class in a dedicated directory.
+        // javac -d target/annotation src/MyAnnotation.java
+        final Path originalAnnotation = sourceDirectory.resolve("MyAnnotation.java.source");
+        final Path myAnnotation = sourceDirectory.resolve("MyAnnotation.java");
+        Files.move(originalAnnotation, myAnnotation);
+        try {
+            Compiler.compile(myAnnotation.toFile(), "-d", annotationDirectory.toString(), "-processorpath", processorDirectory.toString(), "-processor", "MyAnnotationProcessor");
+        } finally {
+            Files.move(myAnnotation, originalAnnotation);
+        }
+
+        // Compile the annotated class and save it in a dedicated directory.
+        // javac -d target/core -cp target/annotation src/MyAnnotatedClass.java
+        final Path originalAnnotatedClass = sourceDirectory.resolve("MyAnnotatedClass.java.source");
+        final Path myAnnotatedClass = sourceDirectory.resolve("MyAnnotatedClass.java");
+        Files.move(originalAnnotatedClass, myAnnotatedClass);
+        try {
+            Compiler.compile(myAnnotatedClass.toFile(), "-d", coreDirectory.toString(), "-cp", annotationDirectory.toString(), "-processorpath", processorDirectory.toString(), "-processor", "MyAnnotationProcessor");
+        } finally {
+            Files.move(myAnnotatedClass, originalAnnotatedClass);
+        }
+
+        // Compile the empty subclass, which depends transitively on MyAnnotation,
+        // without including MyAnnotation.class in the classpath.
+        // javac -d target/bug -cp target/core:target/log4j/* src/MyEmptySubClass.java
+        final Path originalSubclass = sourceDirectory.resolve("MyEmptySubClass.java.source");
+        final Path mySubclass = sourceDirectory.resolve("MyEmptySubClass.java");
+        Files.move(originalSubclass, mySubclass);
+        try {
+            Compiler.compile(mySubclass.toFile(), "-d", bugDirectory.toString(), "-cp", coreDirectory.toString(), "-processorpath", processorDirectory.toString(), "-processor", "MyAnnotationProcessor");
+        } finally {
+            Files.move(mySubclass, originalSubclass);
+        }
+    }
+}
index a35564790e487d9ca167ef4c0b7f5f288067f5e7..69bc8241d7230abaf4f73510e45df5423d5c5330 100644 (file)
  */
 package org.apache.logging.log4j.core.config.plugins.util;
 
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Locale;
-
-import javax.tools.Diagnostic;
-import javax.tools.DiagnosticCollector;
-import javax.tools.JavaCompiler;
-import javax.tools.JavaFileObject;
-import javax.tools.StandardJavaFileManager;
-import javax.tools.ToolProvider;
-
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.ConfigurationFactory;
 import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.logging.log4j.core.test.Compiler;
 import org.apache.logging.log4j.core.test.appender.ListAppender;
 import org.apache.logging.log4j.status.StatusLogger;
 import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.Test;
 
-import static org.junit.jupiter.api.Assertions.*;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class PluginManagerPackagesTest {
     private static Configuration config;
@@ -85,26 +77,8 @@ public class PluginManagerPackagesTest {
     }
 
     static void compile(final File f) throws IOException {
-        // set up compiler
-        final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
-        final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
-        final List<String> errors = new ArrayList<>();
-        try (final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) {
-            final Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(
-                    Collections.singletonList(f));
-
-            // compile generated source
-            // (switch off annotation processing: no need to create Log4j2Plugins.dat)
-            final List<String> options = Collections.singletonList("-proc:none");
-            compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits).call();
-
-            // check we don't have any compilation errors
-            for (final Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
-                if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
-                    errors.add(String.format("Compile error: %s%n", diagnostic.getMessage(Locale.getDefault())));
-                }
-            }
-        }
-        assertTrue(errors.isEmpty(), errors.toString());
+        // compile generated source
+        // (switch off annotation processing: no need to create Log4j2Plugins.dat)
+        Compiler.compile(f, "-proc:none");
     }
 }
diff --git a/log4j-core-test/src/test/resources/LOG4J2-3609/MyAnnotatedClass.java.source b/log4j-core-test/src/test/resources/LOG4J2-3609/MyAnnotatedClass.java.source
new file mode 100644 (file)
index 0000000..f9efde3
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+@MyAnnotation
+public class MyAnnotatedClass {
+}
diff --git a/log4j-core-test/src/test/resources/LOG4J2-3609/MyAnnotation.java.source b/log4j-core-test/src/test/resources/LOG4J2-3609/MyAnnotation.java.source
new file mode 100644 (file)
index 0000000..9f37b5a
--- /dev/null
@@ -0,0 +1,19 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public @interface MyAnnotation {
+}
diff --git a/log4j-core-test/src/test/resources/LOG4J2-3609/MyAnnotationProcessor.java.source b/log4j-core-test/src/test/resources/LOG4J2-3609/MyAnnotationProcessor.java.source
new file mode 100644 (file)
index 0000000..dbd0fb8
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.RoundEnvironment;
+import javax.lang.model.element.TypeElement;
+import java.util.Set;
+
+public class MyAnnotationProcessor extends AbstractProcessor {
+    @Override
+    public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
+        // trivial annotation processor claims nothing and does nothing
+        return false;
+    }
+}
diff --git a/log4j-core-test/src/test/resources/LOG4J2-3609/MyEmptySubClass.java.source b/log4j-core-test/src/test/resources/LOG4J2-3609/MyEmptySubClass.java.source
new file mode 100644 (file)
index 0000000..377e47f
--- /dev/null
@@ -0,0 +1,19 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class MyEmptySubClass extends MyAnnotatedClass {
+}