Skip to content

Commit

Permalink
HV-1863 Add an annotation processor test for records
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Schatteman <[email protected]>
  • Loading branch information
jrenaat authored and gsmet committed May 4, 2022
1 parent 46ab2b7 commit 2e0373e
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 0 deletions.
31 changes: 31 additions & 0 deletions annotation-processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,36 @@
</plugins>
</build>
</profile>
<profile>
<id>testWithJdk17</id>
<activation>
<property>
<name>java-version.test.release</name>
<value>17</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/java17</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ public File getSourceFile(Class<?> clazz) {
return new File( sourceBaseDir + sourceFileName );
}

/**
* Retrieves a file object containing the source of the given class.
*
* @param clazz The class of interest.
* @param testSourceBase The test source base in which to look for the class of interest
*
* @return A file with the source of the given class.
*/
public File getSourceFile(Class<?> clazz, String testSourceBase) {
if ( testSourceBase == null || testSourceBase.trim().isEmpty() ) {
return getSourceFile( clazz );
}

String sourceFileName = File.separator + clazz.getName().replace( ".", File.separator ) + ".java";
String sourceBaseDir = BASE_DIR.getAbsolutePath() + testSourceBase;

return new File( sourceBaseDir + sourceFileName );
}

/**
* @see CompilerTestHelper#compile(Processor, DiagnosticCollector, Kind, Boolean, Boolean, EnumSet, File...)
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap.record;

import java.io.File;
import java.util.EnumSet;

import javax.tools.Diagnostic;

import org.hibernate.validator.ap.ConstraintValidationProcessor;
import org.hibernate.validator.ap.ConstraintValidationProcessorTestBase;
import org.hibernate.validator.ap.testutil.CompilerTestHelper;
import org.hibernate.validator.ap.util.DiagnosticExpectation;

import org.testng.annotations.Test;

import static org.hibernate.validator.ap.testutil.CompilerTestHelper.assertThatDiagnosticsMatch;
import static org.testng.Assert.assertFalse;

/**
* @author Jan Schatteman
*/
public class RecordConstraintValidationProcessorTest extends ConstraintValidationProcessorTestBase {

@Test
public void testRecordWithInvalidConstraints() {

File sourceFile = compilerHelper.getSourceFile( RecordWithInvalidConstraints.class, "/src/test/java17" );

boolean compilationResult =
compilerHelper.compile(
new ConstraintValidationProcessor(),
diagnostics,
EnumSet.of( CompilerTestHelper.Library.VALIDATION_API ),
sourceFile
);

assertFalse( compilationResult );

assertThatDiagnosticsMatch(
diagnostics,
new DiagnosticExpectation( Diagnostic.Kind.ERROR, 15 )
);
}

@Test
public void testRecordWithInvalidConstructorConstraints() {

File sourceFile = compilerHelper.getSourceFile( RecordWithInvalidConstructorConstraints.class, "/src/test/java17" );

boolean compilationResult =
compilerHelper.compile(
new ConstraintValidationProcessor(),
diagnostics,
EnumSet.of( CompilerTestHelper.Library.VALIDATION_API ),
sourceFile
);

assertFalse( compilationResult );

assertThatDiagnosticsMatch(
diagnostics,
new DiagnosticExpectation( Diagnostic.Kind.ERROR, 16 )
);
}

@Test
public void testRecordWithInvalidMethodConstraints() {

File sourceFile = compilerHelper.getSourceFile( RecordWithInvalidMethodConstraints.class, "/src/test/java17" );

boolean compilationResult =
compilerHelper.compile(
new ConstraintValidationProcessor(),
diagnostics,
EnumSet.of( CompilerTestHelper.Library.VALIDATION_API ),
sourceFile
);

assertFalse( compilationResult );

assertThatDiagnosticsMatch(
diagnostics,
new DiagnosticExpectation( Diagnostic.Kind.ERROR, 19 )
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap.record;

import java.util.Date;
import javax.validation.constraints.FutureOrPresent;

/**
* @author Jan Schatteman
*/
public record RecordWithInvalidConstraints(/* Not allowed */ @FutureOrPresent String string, @FutureOrPresent Date date) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap.record;

import java.util.Date;
import javax.validation.constraints.FutureOrPresent;

/**
* @author Jan Schatteman
*/
public record RecordWithInvalidConstructorConstraints(String string, Date date) {
public RecordWithInvalidConstructorConstraints(@FutureOrPresent String string, @FutureOrPresent Date date) {
this.string = string;
this.date = date;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.validator.ap.record;

import java.util.Date;
import javax.validation.constraints.FutureOrPresent;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Positive;

/**
* @author Jan Schatteman
*/
public record RecordWithInvalidMethodConstraints(@NotBlank String string, @FutureOrPresent Date date) {

public void doNothing(@Positive String s) {
//
}
}

0 comments on commit 2e0373e

Please sign in to comment.