Skip to content

Commit d2b4fe6

Browse files
committed
Add test for 2.x annotations
We add a separate equivalent test for 2.x annotations.
1 parent 8abc774 commit d2b4fe6

File tree

23 files changed

+456
-25
lines changed

23 files changed

+456
-25
lines changed

log4j-docgen/pom.xml

+31-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232

3333
<!-- Dependency versions -->
3434
<asciidoctorj-api.version>3.0.0-alpha.2</asciidoctorj-api.version>
35+
<!-- We explicitly test against `log4j-core` 2.x -->
36+
<log4j-core.version>2.22.1</log4j-core.version>
37+
<log4j-plugins.version>3.0.0-beta1</log4j-plugins.version>
3538
</properties>
3639

3740
<dependencies>
@@ -76,9 +79,23 @@
7679
<scope>test</scope>
7780
</dependency>
7881

82+
<dependency>
83+
<groupId>org.junit.jupiter</groupId>
84+
<artifactId>junit-jupiter-params</artifactId>
85+
<scope>test</scope>
86+
</dependency>
87+
88+
<dependency>
89+
<groupId>org.apache.logging.log4j</groupId>
90+
<artifactId>log4j-core</artifactId>
91+
<version>${log4j-core.version}</version>
92+
<scope>test</scope>
93+
</dependency>
94+
7995
<dependency>
8096
<groupId>org.apache.logging.log4j</groupId>
8197
<artifactId>log4j-plugins</artifactId>
98+
<version>${log4j-plugins.version}</version>
8299
<scope>test</scope>
83100
</dependency>
84101

@@ -93,14 +110,14 @@
93110
<build>
94111
<plugins>
95112

96-
<!--
97-
~ We split compilation in two executions, so we can fail on warnings for our sources,
98-
~ but be more tolerant for generated sources.
99-
-->
100113
<plugin>
101114
<groupId>org.apache.maven.plugins</groupId>
102115
<artifactId>maven-compiler-plugin</artifactId>
103116
<executions>
117+
<!--
118+
~ We split compilation in two executions, so we can fail on warnings for our sources,
119+
~ but be more tolerant for generated sources.
120+
-->
104121
<execution>
105122
<id>default-compile</id>
106123
<phase>none</phase>
@@ -136,6 +153,16 @@
136153
<failOnWarning>true</failOnWarning>
137154
</configuration>
138155
</execution>
156+
<!-- Remove test sources from compilation -->
157+
<execution>
158+
<id>default-test-compile</id>
159+
<configuration>
160+
<!-- Classes used only for annotation processing -->
161+
<excludes>
162+
<exclude>example/**</exclude>
163+
</excludes>
164+
</configuration>
165+
</execution>
139166
</executions>
140167
</plugin>
141168

log4j-docgen/src/main/java/org/apache/logging/log4j/docgen/processor/DocGenProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
import org.jspecify.annotations.Nullable;
8383

8484
@ServiceProvider(value = Processor.class, resolution = Resolution.OPTIONAL)
85-
@SupportedAnnotationTypes("org.apache.logging.log4j.plugins.*")
85+
@SupportedAnnotationTypes({"org.apache.logging.log4j.core.config.plugins.*", "org.apache.logging.log4j.plugins.*"})
8686
@NullMarked
8787
public class DocGenProcessor extends AbstractProcessor {
8888

log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/processor/AsciidocConverterTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ void convertToAsciidoc() throws Exception {
6767
final StandardJavaFileManager fileManager = tool.getStandardFileManager(null, Locale.ROOT, UTF_8);
6868

6969
final Path basePath = Paths.get(System.getProperty("basedir", "."));
70-
final Path sourcePath = basePath.resolve("src/test/it/example/JavadocExample.java");
70+
final Path sourcePath = Paths.get(AsciidocConverterTest.class
71+
.getResource("/processor/asciidoc/example/JavadocExample.java")
72+
.toURI());
7173
final Iterable<? extends JavaFileObject> sources = fileManager.getJavaFileObjects(sourcePath);
7274

7375
final Path destPath = basePath.resolve("target/test-site");

log4j-docgen/src/test/java/org/apache/logging/log4j/docgen/processor/DocGenProcessorTest.java

+14-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2121

22-
import java.io.IOException;
2322
import java.net.URL;
2423
import java.nio.charset.StandardCharsets;
2524
import java.nio.file.Files;
@@ -40,17 +39,23 @@
4039
import javax.tools.StandardLocation;
4140
import javax.tools.ToolProvider;
4241
import org.apache.logging.log4j.docgen.xsd.SchemaGenerator;
43-
import org.junit.jupiter.api.Test;
42+
import org.junit.jupiter.params.ParameterizedTest;
43+
import org.junit.jupiter.params.provider.MethodSource;
4444
import org.xmlunit.assertj3.XmlAssert;
4545

4646
public class DocGenProcessorTest {
4747

48-
@Test
49-
void descriptorGenerationSucceeds() {
48+
static Stream<String> descriptorGenerationSucceeds() {
49+
return Stream.of("v2", "v3");
50+
}
51+
52+
@ParameterizedTest
53+
@MethodSource
54+
void descriptorGenerationSucceeds(final String version) {
5055
final Path basePath = Paths.get(System.getProperty("basedir", "."));
5156
final Path schema = basePath.resolve("target/generated-site/resources/xsd/plugins-0.1.0.xsd");
5257
final URL expected = SchemaGenerator.class.getResource("/expected/processor/META-INF/log4j/plugins.xml");
53-
final Path actual = assertDoesNotThrow(DocGenProcessorTest::generateDescriptor);
58+
final Path actual = assertDoesNotThrow(() -> generateDescriptor(version));
5459
XmlAssert.assertThat(actual)
5560
.isValidAgainst(schema)
5661
.and(expected)
@@ -59,21 +64,22 @@ void descriptorGenerationSucceeds() {
5964
.areIdentical();
6065
}
6166

62-
private static Path generateDescriptor() throws IOException {
67+
private static Path generateDescriptor(final String version) throws Exception {
6368
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
6469
final DiagnosticCollector<JavaFileObject> ds = new DiagnosticCollector<>();
6570
final StandardJavaFileManager fileManager =
6671
compiler.getStandardFileManager(null, Locale.ROOT, StandardCharsets.UTF_8);
6772

6873
final Path basePath = Paths.get(System.getProperty("basedir", "."));
69-
final Path sourcePath = basePath.resolve("src/test/it");
74+
final Path sourcePath = Paths.get(
75+
DocGenProcessorTest.class.getResource("/processor/" + version).toURI());
7076
final Iterable<? extends JavaFileObject> sources;
7177
try (final Stream<Path> files = Files.walk(sourcePath)) {
7278
sources = fileManager.getJavaFileObjects(
7379
files.filter(Files::isRegularFile).toArray(Path[]::new));
7480
}
7581

76-
final Path destPath = basePath.resolve("target/test-site/processor");
82+
final Path destPath = basePath.resolve("target/test-site/processor/" + version);
7783
Files.createDirectories(destPath);
7884
fileManager.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Set.of(destPath));
7985

log4j-docgen/src/test/resources/expected/processor/META-INF/log4j/plugins.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ It also implements:
114114
* xref:Appender.adoc[],
115115
* xref:BaseAppender.adoc[]</description>
116116
</plugin>
117-
<plugin name="MyOldLayout" className="example.MyOldLayout">
117+
<plugin name="MyLayout" className="example.MyOldLayout">
118118
<supertypes>
119119
<supertype>example.Layout</supertype>
120120
<supertype>java.lang.Object</supertype>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package example;
18+
19+
import java.util.List;
20+
import java.util.Set;
21+
import javax.lang.model.element.TypeElement;
22+
import org.apache.logging.log4j.core.config.plugins.Plugin;
23+
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
24+
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
25+
import org.apache.logging.log4j.core.config.plugins.PluginElement;
26+
import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
27+
28+
/**
29+
* Example plugin
30+
* <p>
31+
* This is an example plugin. It has the following characteristics:
32+
* </p>
33+
* <ol>
34+
* <li>Plugin name: {@code MyPlugin},</li>
35+
* <li>Namespace: default (i.e. {@code Core}).</li>
36+
* </ol>
37+
* <p>
38+
* It also implements:
39+
* </p>
40+
* <ul>
41+
* <li>{@link Appender},</li>
42+
* <li>{@link BaseAppender}</li>
43+
* </ul>
44+
*/
45+
@Plugin(name = "MyAppender", category = "namespace")
46+
public final class MyAppender extends AbstractAppender implements Appender {
47+
48+
/**
49+
* Parent builder with some private fields that are not returned by
50+
* {@link javax.lang.model.util.Elements#getAllMembers(TypeElement)}.
51+
*/
52+
public static class ParentBuilder {
53+
54+
/**
55+
* A {@code char} attribute.
56+
*/
57+
@PluginBuilderAttribute
58+
private char charAtt = 'L';
59+
60+
/**
61+
* An {@code int} attribute.
62+
*/
63+
@PluginBuilderAttribute
64+
private int intAtt = 4242;
65+
66+
/**
67+
* An element with multiplicity 1.
68+
*/
69+
@PluginElement("layout")
70+
private Layout layout;
71+
}
72+
73+
public static final class Builder extends ParentBuilder
74+
implements org.apache.logging.log4j.plugins.util.Builder<MyAppender> {
75+
76+
/**
77+
* A {@code short} attribute annotated on type.
78+
*/
79+
private @PluginBuilderAttribute short shortAtt = 42;
80+
81+
/**
82+
* A {@code long} attribute annotated on type.
83+
*/
84+
private @PluginBuilderAttribute long longAtt = 424242L;
85+
86+
/**
87+
* A {@code String} attribute.
88+
*/
89+
@PluginBuilderAttribute
90+
@Required
91+
private String stringAtt;
92+
93+
/**
94+
* An attribute whose name differs from the field name.
95+
*/
96+
@PluginBuilderAttribute("anotherName")
97+
private String origName;
98+
99+
/**
100+
* An attribute that is an enumeration annotated on type.
101+
*/
102+
private @PluginBuilderAttribute MyEnum enumAtt;
103+
104+
/**
105+
* An attribute of type {@code float}.
106+
*/
107+
private @PluginBuilderAttribute float floatAtt;
108+
109+
/**
110+
* An attribute of type {@code double}.
111+
*/
112+
private @PluginBuilderAttribute double aDouble;
113+
114+
private Object notAnAttribute;
115+
116+
/**
117+
* A collection element.
118+
*/
119+
@PluginElement("appenderList")
120+
private List<Appender2> appenderList;
121+
122+
/**
123+
* A set of layouts
124+
*/
125+
@PluginElement("layoutSet")
126+
private LayoutSet layoutSet;
127+
128+
/**
129+
* A {@code boolean} attribute with annotated type.
130+
*/
131+
public Builder setBooleanAtt(final @PluginBuilderAttribute boolean booleanAtt) {
132+
return this;
133+
}
134+
135+
/**
136+
* A {@code byte} attribute with annotated parameter.
137+
*/
138+
public Builder setByteAtt(@PluginBuilderAttribute final byte byteAtt) {
139+
return this;
140+
}
141+
142+
/**
143+
* An element with multiplicity n with annotated setter.
144+
*/
145+
public Builder setFilters(final @PluginElement("filters") Filter[] filters) {
146+
return this;
147+
}
148+
149+
/**
150+
* An element that is not an interface with annotated parameter.
151+
*/
152+
public Builder setAbstractElement(@PluginElement("abstractAppender") final AbstractAppender abstractAppender) {
153+
return this;
154+
}
155+
156+
/**
157+
* An element with an annotated type.
158+
*/
159+
public Builder setNestedAppender(final @PluginElement("nestedAppender") Appender nestedAppender) {
160+
return this;
161+
}
162+
163+
/**
164+
* A setter with a varargs type.
165+
*/
166+
public Builder setVarargs(@PluginElement("layouts") final Layout3... layouts) {
167+
return this;
168+
}
169+
170+
@Override
171+
public MyAppender build() {
172+
return null;
173+
}
174+
}
175+
176+
@PluginBuilderFactory
177+
public static Builder newBuilder() {
178+
return new Builder();
179+
}
180+
181+
public static interface Appender2 {}
182+
183+
public static interface Layout2 {}
184+
185+
public static interface Layout3 {}
186+
187+
public abstract static class LayoutSet implements Set<Layout2> {}
188+
}

0 commit comments

Comments
 (0)