Skip to content

Commit debaf50

Browse files
committed
added an apt based tool to process TCK related annotations.
git-svn-id: https://svn.jboss.org/repos/hibernate/validator/trunk@15705 1b8cb986-b30d-0410-93ca-fae66ebed9b2
1 parent 6bdd65a commit debaf50

File tree

9 files changed

+362
-2
lines changed

9 files changed

+362
-2
lines changed

hibernate-validator/pom.xml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
<artifactId>junit</artifactId>
3939
<scope>test</scope>
4040
</dependency>
41+
<dependency>
42+
<groupId>org.hibernate</groupId>
43+
<artifactId>tck-utils</artifactId>
44+
<scope>test</scope>
45+
</dependency>
4146
</dependencies>
4247
<build>
4348
<resources>
@@ -46,6 +51,22 @@
4651
<filtering>true</filtering>
4752
</resource>
4853
</resources>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.codehaus.mojo</groupId>
57+
<artifactId>apt-maven-plugin</artifactId>
58+
<executions>
59+
<execution>
60+
<goals>
61+
<goal>test-process</goal>
62+
</goals>
63+
</execution>
64+
</executions>
65+
<configuration>
66+
<testOutputDirectory>${project.build.directory}/site</testOutputDirectory>
67+
</configuration>
68+
</plugin>
69+
</plugins>
4970
</build>
5071
<reporting>
5172
<plugins>
@@ -80,9 +101,9 @@
80101
<additionalparam>
81102
-d ${project.build.directory}/site
82103
</additionalparam>
83-
<!-- Other dir than apidocs -->
104+
<!--Other dir than apidocs-->
84105
<destDir>tck</destDir>
85-
<!-- For the project-reports page-->
106+
<!--For the project-reports page-->
86107
<name>JSR Tests</name>
87108
<description>Cross references unit tests to JSR 303 specification.</description>
88109
</configuration>

hibernate-validator/src/test/java/org/hibernate/validation/engine/ValidatorImplTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.hibernate.validation.eg.Last;
4848
import org.hibernate.validation.eg.DefaultAlias;
4949
import org.hibernate.validation.HibernateValidatorFactoryBuilder;
50+
import org.hibernate.tck.annotations.SpecAssertion;
5051

5152
/**
5253
* Tests for the implementation of <code>Validator</code>.
@@ -73,6 +74,7 @@ private Validator getHibernateValidator() {
7374
* JSR 303: Requirements on classes to be validates (3.1)
7475
* @jsr 3.1
7576
*/
77+
@SpecAssertion( section = {"3.1"} )
7678
@Test
7779
public void testWrongMethodName() {
7880
try {

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<module>validation-api</module>
5050
<module>hibernate-validator</module>
5151
<module>hibernate-validator-legacy</module>
52+
<module>tck-utils</module>
5253
</modules>
5354

5455
<dependencyManagement>
@@ -78,6 +79,11 @@
7879
<artifactId>junit</artifactId>
7980
<version>4.4</version>
8081
</dependency>
82+
<dependency>
83+
<groupId>org.hibernate</groupId>
84+
<artifactId>tck-utils</artifactId>
85+
<version>${version}</version>
86+
</dependency>
8187
</dependencies>
8288
</dependencyManagement>
8389

tck-utils/pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>org.hibernate</groupId>
9+
<artifactId>hibernate-validator-parent</artifactId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
<relativePath>../pom.xml</relativePath>
12+
</parent>
13+
14+
<groupId>org.hibernate</groupId>
15+
<artifactId>tck-utils</artifactId>
16+
<packaging>jar</packaging>
17+
<name>TCK Utils</name>
18+
19+
<distributionManagement>
20+
<site>
21+
<id>site</id>
22+
<url>file:///Users/hardy/Sites/${artifactId}</url>
23+
</site>
24+
</distributionManagement>
25+
</project>
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// $Id$
2+
/*
3+
* JBoss, Home of Professional Open Source
4+
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
5+
* by the @authors tag. See the copyright.txt in the distribution for a
6+
* full listing of individual contributors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.hibernate.tck;
19+
20+
import java.io.BufferedWriter;
21+
import java.io.File;
22+
import java.io.FileWriter;
23+
import java.io.IOException;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
import com.sun.mirror.apt.AnnotationProcessor;
28+
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
29+
import com.sun.mirror.declaration.AnnotationTypeDeclaration;
30+
import com.sun.mirror.declaration.Declaration;
31+
import com.sun.mirror.declaration.MethodDeclaration;
32+
import static com.sun.mirror.util.DeclarationVisitors.NO_OP;
33+
import static com.sun.mirror.util.DeclarationVisitors.getDeclarationScanner;
34+
import com.sun.mirror.util.SimpleDeclarationVisitor;
35+
36+
import org.hibernate.tck.annotations.SpecAssertion;
37+
38+
/**
39+
* @author Hardy Ferentschik
40+
*/
41+
public class TCKAnnotationProcessor implements AnnotationProcessor {
42+
43+
private static final String OUTDIR_OPTION_NAME = "-s";
44+
private static final String REPORT_FILE_NAME = "tck.html";
45+
46+
private final AnnotationProcessorEnvironment env;
47+
private final String[] tableHeaders = new String[] { "Section", "Class", "Method" };
48+
private final StringBuffer out = new StringBuffer();
49+
private final List<JSRReference> references = new ArrayList<JSRReference>();
50+
private final File baseDir;
51+
52+
public TCKAnnotationProcessor(AnnotationProcessorEnvironment annotationProcessorEnvironment) {
53+
this.env = annotationProcessorEnvironment;
54+
String baseDirName = env.getOptions().get( OUTDIR_OPTION_NAME );
55+
baseDir = new File( baseDirName );
56+
baseDir.mkdirs();
57+
}
58+
59+
public void process() {
60+
61+
62+
AnnotationTypeDeclaration annType = ( AnnotationTypeDeclaration ) env.getTypeDeclaration(
63+
SpecAssertion.class.getCanonicalName()
64+
);
65+
for ( Declaration d : env.getDeclarationsAnnotatedWith( annType ) ) {
66+
d.accept(
67+
getDeclarationScanner(
68+
new DoNothingVisitor(),
69+
NO_OP
70+
)
71+
);
72+
}
73+
74+
75+
writeHeader();
76+
writeContents();
77+
writeFooter();
78+
79+
writeReporttoFile();
80+
}
81+
82+
private void writeReporttoFile() {
83+
try {
84+
File report = new File( baseDir, REPORT_FILE_NAME );
85+
BufferedWriter writer = new BufferedWriter( new FileWriter( report ) );
86+
writer.write( out.toString() );
87+
writer.close();
88+
}
89+
catch ( IOException e ) {
90+
System.err.println( "Error writing report." );
91+
}
92+
}
93+
94+
private void writeFooter() {
95+
out.append( "</body></html>" );
96+
}
97+
98+
private void writeHeader() {
99+
out.append( "<html><head></head><body>" );
100+
}
101+
102+
private void writeTableHeader() {
103+
out.append( "<table border=\"1\"><tr>" );
104+
for ( String s : tableHeaders ) {
105+
out.append( "<th>" ).append( s ).append( "</th>" );
106+
}
107+
out.append( "</tr>" );
108+
}
109+
110+
private void writeTableFooter() {
111+
out.append( "</table>" );
112+
}
113+
114+
private void writeContents() {
115+
writeTableHeader();
116+
for ( JSRReference reference : references ) {
117+
out.append( "<tr>" );
118+
out.append( "<td>" ).append( reference.jsrSectionReference ).append( "</td>" );
119+
out.append( "<td><a href=\"" )
120+
.append( reference.getSourceLink() )
121+
.append( "\">" )
122+
.append( reference.className )
123+
.append( "</a></td>" );
124+
out.append( "<td>" ).append( reference.methodName ).append( "</td>" );
125+
out.append( "</tr>" );
126+
}
127+
writeTableFooter();
128+
}
129+
130+
private class DoNothingVisitor extends SimpleDeclarationVisitor {
131+
public void visitMethodDeclaration(MethodDeclaration d) {
132+
SpecAssertion annotation = d.getAnnotation( SpecAssertion.class );
133+
JSRReference ref = new JSRReference(
134+
annotation.section()[0], d.getDeclaringType().getQualifiedName(), d.getSimpleName()
135+
);
136+
references.add( ref );
137+
}
138+
}
139+
140+
private static class JSRReference implements Comparable {
141+
/**
142+
* The JSR section this instance references.
143+
*/
144+
String jsrSectionReference;
145+
146+
/**
147+
* The name of the class which references the JSR.
148+
*/
149+
String className;
150+
151+
/**
152+
* The method which references the JSR.
153+
*/
154+
String methodName;
155+
156+
/**
157+
* @todo Add some validation
158+
*/
159+
JSRReference(String reference, String className, String methodName) {
160+
this.jsrSectionReference = reference;
161+
this.className = className;
162+
this.methodName = methodName;
163+
}
164+
165+
public String getSourceLink() {
166+
StringBuilder builder = new StringBuilder();
167+
builder.append( "xref-test/" );
168+
builder.append( className.replace( '.', '/' ) );
169+
builder.append( ".html" );
170+
return builder.toString();
171+
}
172+
173+
public int compareTo(Object o) {
174+
return jsrSectionReference.compareTo( ( ( JSRReference ) o ).jsrSectionReference );
175+
}
176+
}
177+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// $Id$
2+
/*
3+
* JBoss, Home of Professional Open Source
4+
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
5+
* by the @authors tag. See the copyright.txt in the distribution for a
6+
* full listing of individual contributors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.hibernate.tck;
19+
20+
import java.util.Arrays;
21+
import java.util.Collection;
22+
import static java.util.Collections.emptySet;
23+
import static java.util.Collections.unmodifiableCollection;
24+
import java.util.Set;
25+
26+
import com.sun.mirror.apt.AnnotationProcessor;
27+
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
28+
import com.sun.mirror.apt.AnnotationProcessorFactory;
29+
import com.sun.mirror.declaration.AnnotationTypeDeclaration;
30+
31+
import org.hibernate.tck.annotations.SpecAssertion;
32+
import org.hibernate.tck.annotations.SpecVersion;
33+
34+
35+
/**
36+
* @author Hardy Ferentschik
37+
*/
38+
public class TCKAnnotationProcessorFactory implements AnnotationProcessorFactory {
39+
40+
// Process any set of annotations
41+
private static final Collection<String> supportedAnnotations
42+
= unmodifiableCollection(
43+
Arrays.asList(
44+
SpecAssertion.class.getCanonicalName(),
45+
SpecVersion.class.getCanonicalName()
46+
)
47+
);
48+
49+
// No supported options
50+
private static final Collection<String> supportedOptions = emptySet();
51+
52+
53+
public Collection<String> supportedOptions() {
54+
return supportedOptions;
55+
}
56+
57+
public Collection<String> supportedAnnotationTypes() {
58+
return supportedAnnotations;
59+
}
60+
61+
public AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> annotationTypeDeclarations, AnnotationProcessorEnvironment annotationProcessorEnvironment) {
62+
return new TCKAnnotationProcessor( annotationProcessorEnvironment );
63+
64+
}
65+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// $Id$
2+
/*
3+
* JBoss, Home of Professional Open Source
4+
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
5+
* by the @authors tag. See the copyright.txt in the distribution for a
6+
* full listing of individual contributors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.hibernate.tck.annotations;
19+
20+
import java.lang.annotation.Documented;
21+
import java.lang.annotation.ElementType;
22+
import java.lang.annotation.Target;
23+
24+
@Target(ElementType.METHOD)
25+
@Documented
26+
public @interface SpecAssertion {
27+
28+
public String[] section();
29+
30+
public String note() default "";
31+
32+
}
33+

0 commit comments

Comments
 (0)