Skip to content

Commit 43f60f9

Browse files
jrenaatgsmet
authored andcommitted
HV-1863 Add an annotation processor test for records
Signed-off-by: Jan Schatteman <jschatte@redhat.com>
1 parent 2253258 commit 43f60f9

File tree

6 files changed

+199
-0
lines changed

6 files changed

+199
-0
lines changed

annotation-processor/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,36 @@
155155
</plugins>
156156
</build>
157157
</profile>
158+
<profile>
159+
<id>testWithJdk17</id>
160+
<activation>
161+
<property>
162+
<name>java-version.test.release</name>
163+
<value>17</value>
164+
</property>
165+
</activation>
166+
<build>
167+
<plugins>
168+
<plugin>
169+
<groupId>org.codehaus.mojo</groupId>
170+
<artifactId>build-helper-maven-plugin</artifactId>
171+
<executions>
172+
<execution>
173+
<id>add-test-source</id>
174+
<phase>generate-test-sources</phase>
175+
<goals>
176+
<goal>add-test-source</goal>
177+
</goals>
178+
<configuration>
179+
<sources>
180+
<source>src/test/java17</source>
181+
</sources>
182+
</configuration>
183+
</execution>
184+
</executions>
185+
</plugin>
186+
</plugins>
187+
</build>
188+
</profile>
158189
</profiles>
159190
</project>

annotation-processor/src/test/java/org/hibernate/validator/ap/testutil/CompilerTestHelper.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,25 @@ public File getSourceFile(Class<?> clazz) {
106106
return new File( sourceBaseDir + sourceFileName );
107107
}
108108

109+
/**
110+
* Retrieves a file object containing the source of the given class.
111+
*
112+
* @param clazz The class of interest.
113+
* @param testSourceBase The test source base in which to look for the class of interest
114+
*
115+
* @return A file with the source of the given class.
116+
*/
117+
public File getSourceFile(Class<?> clazz, String testSourceBase) {
118+
if ( testSourceBase == null || testSourceBase.trim().isEmpty() ) {
119+
return getSourceFile( clazz );
120+
}
121+
122+
String sourceFileName = File.separator + clazz.getName().replace( ".", File.separator ) + ".java";
123+
String sourceBaseDir = BASE_DIR.getAbsolutePath() + testSourceBase;
124+
125+
return new File( sourceBaseDir + sourceFileName );
126+
}
127+
109128
/**
110129
* @see CompilerTestHelper#compile(Processor, DiagnosticCollector, Kind, Boolean, Boolean, EnumSet, File...)
111130
*/
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.ap.record;
8+
9+
import java.io.File;
10+
import java.util.EnumSet;
11+
12+
import javax.tools.Diagnostic;
13+
14+
import org.hibernate.validator.ap.ConstraintValidationProcessor;
15+
import org.hibernate.validator.ap.ConstraintValidationProcessorTestBase;
16+
import org.hibernate.validator.ap.testutil.CompilerTestHelper;
17+
import org.hibernate.validator.ap.util.DiagnosticExpectation;
18+
19+
import org.testng.annotations.Test;
20+
21+
import static org.hibernate.validator.ap.testutil.CompilerTestHelper.assertThatDiagnosticsMatch;
22+
import static org.testng.Assert.assertFalse;
23+
24+
/**
25+
* @author Jan Schatteman
26+
*/
27+
public class RecordConstraintValidationProcessorTest extends ConstraintValidationProcessorTestBase {
28+
29+
@Test
30+
public void testRecordWithInvalidConstraints() {
31+
32+
File sourceFile = compilerHelper.getSourceFile( RecordWithInvalidConstraints.class, "/src/test/java17" );
33+
34+
boolean compilationResult =
35+
compilerHelper.compile(
36+
new ConstraintValidationProcessor(),
37+
diagnostics,
38+
EnumSet.of( CompilerTestHelper.Library.VALIDATION_API ),
39+
sourceFile
40+
);
41+
42+
assertFalse( compilationResult );
43+
44+
assertThatDiagnosticsMatch(
45+
diagnostics,
46+
new DiagnosticExpectation( Diagnostic.Kind.ERROR, 15 )
47+
);
48+
}
49+
50+
@Test
51+
public void testRecordWithInvalidConstructorConstraints() {
52+
53+
File sourceFile = compilerHelper.getSourceFile( RecordWithInvalidConstructorConstraints.class, "/src/test/java17" );
54+
55+
boolean compilationResult =
56+
compilerHelper.compile(
57+
new ConstraintValidationProcessor(),
58+
diagnostics,
59+
EnumSet.of( CompilerTestHelper.Library.VALIDATION_API ),
60+
sourceFile
61+
);
62+
63+
assertFalse( compilationResult );
64+
65+
assertThatDiagnosticsMatch(
66+
diagnostics,
67+
new DiagnosticExpectation( Diagnostic.Kind.ERROR, 16 )
68+
);
69+
}
70+
71+
@Test
72+
public void testRecordWithInvalidMethodConstraints() {
73+
74+
File sourceFile = compilerHelper.getSourceFile( RecordWithInvalidMethodConstraints.class, "/src/test/java17" );
75+
76+
boolean compilationResult =
77+
compilerHelper.compile(
78+
new ConstraintValidationProcessor(),
79+
diagnostics,
80+
EnumSet.of( CompilerTestHelper.Library.VALIDATION_API ),
81+
sourceFile
82+
);
83+
84+
assertFalse( compilationResult );
85+
86+
assertThatDiagnosticsMatch(
87+
diagnostics,
88+
new DiagnosticExpectation( Diagnostic.Kind.ERROR, 19 )
89+
);
90+
}
91+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.ap.record;
8+
9+
import java.util.Date;
10+
import jakarta.validation.constraints.FutureOrPresent;
11+
12+
/**
13+
* @author Jan Schatteman
14+
*/
15+
public record RecordWithInvalidConstraints(/* Not allowed */ @FutureOrPresent String string, @FutureOrPresent Date date) {
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.ap.record;
8+
9+
import java.util.Date;
10+
import jakarta.validation.constraints.FutureOrPresent;
11+
12+
/**
13+
* @author Jan Schatteman
14+
*/
15+
public record RecordWithInvalidConstructorConstraints(String string, Date date) {
16+
public RecordWithInvalidConstructorConstraints(@FutureOrPresent String string, @FutureOrPresent Date date) {
17+
this.string = string;
18+
this.date = date;
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
6+
*/
7+
package org.hibernate.validator.ap.record;
8+
9+
import java.util.Date;
10+
import jakarta.validation.constraints.FutureOrPresent;
11+
import jakarta.validation.constraints.NotBlank;
12+
import jakarta.validation.constraints.Positive;
13+
14+
/**
15+
* @author Jan Schatteman
16+
*/
17+
public record RecordWithInvalidMethodConstraints(@NotBlank String string, @FutureOrPresent Date date) {
18+
19+
public void doNothing(@Positive String s) {
20+
//
21+
}
22+
}

0 commit comments

Comments
 (0)