Skip to content

Commit 6bdd65a

Browse files
committed
Added a doclet which allows linking unit tests to the corresponding sections of the JSR 303. Run 'mvn package sire' to generate reports.
git-svn-id: https://svn.jboss.org/repos/hibernate/validator/trunk@15698 1b8cb986-b30d-0410-93ca-fae66ebed9b2
1 parent a223f18 commit 6bdd65a

File tree

5 files changed

+241
-3
lines changed

5 files changed

+241
-3
lines changed

hibernate-validator/pom.xml

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<name>Hibernate Validator</name>
1515
<distributionManagement>
1616
<site>
17-
<id>local</id>
17+
<id>site</id>
1818
<url>file:///Users/hardy/Sites/${artifactId}</url>
1919
</site>
2020
</distributionManagement>
@@ -47,4 +47,51 @@
4747
</resource>
4848
</resources>
4949
</build>
50+
<reporting>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-project-info-reports-plugin</artifactId>
55+
<version>2.0.1</version>
56+
</plugin>
57+
<plugin>
58+
<artifactId>maven-javadoc-plugin</artifactId>
59+
<reportSets>
60+
<reportSet>
61+
<id>html</id>
62+
<configuration>
63+
<tags>
64+
<tag>
65+
<name>todo</name>
66+
<placement>a</placement>
67+
<head>ToDo:</head>
68+
</tag>
69+
</tags>
70+
</configuration>
71+
<reports>
72+
<report>javadoc</report>
73+
</reports>
74+
</reportSet>
75+
<reportSet>
76+
<id>specCheck</id>
77+
<configuration>
78+
<doclet>org.hibernate.javadoc.JSRDoclet</doclet>
79+
<docletPath>${basedir}/target/test-classes</docletPath>
80+
<additionalparam>
81+
-d ${project.build.directory}/site
82+
</additionalparam>
83+
<!-- Other dir than apidocs -->
84+
<destDir>tck</destDir>
85+
<!-- For the project-reports page-->
86+
<name>JSR Tests</name>
87+
<description>Cross references unit tests to JSR 303 specification.</description>
88+
</configuration>
89+
<reports>
90+
<report>test-javadoc</report>
91+
</reports>
92+
</reportSet>
93+
</reportSets>
94+
</plugin>
95+
</plugins>
96+
</reporting>
5097
</project>
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// $Id:$
2+
// $Id$
3+
/*
4+
* JBoss, Home of Professional Open Source
5+
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
6+
* by the @authors tag. See the copyright.txt in the distribution for a
7+
* full listing of individual contributors.
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.hibernate.javadoc;
20+
21+
import java.io.BufferedWriter;
22+
import java.io.File;
23+
import java.io.FileWriter;
24+
import java.io.IOException;
25+
import java.util.ArrayList;
26+
import java.util.Collections;
27+
import java.util.List;
28+
29+
import com.sun.javadoc.ClassDoc;
30+
import com.sun.javadoc.DocErrorReporter;
31+
import com.sun.javadoc.MethodDoc;
32+
import com.sun.javadoc.RootDoc;
33+
import com.sun.javadoc.Tag;
34+
import com.sun.tools.doclets.standard.Standard;
35+
36+
/**
37+
* This doclet writes a report for all junit tests marked documented with <i>jsr</i>.
38+
* Tests documented with this tag are referencing sections of the Bean Validation spec they
39+
* are testing/validating.
40+
*
41+
* @author Hardy Ferentschik
42+
*/
43+
public class JSRDoclet {
44+
45+
public static final String TAG_NAME = "jsr";
46+
private static final String[] tableHeaders = new String[] { "Bean Validation Specification", "Class", "Method" };
47+
private static StringBuffer out = new StringBuffer();
48+
49+
50+
public static boolean start(RootDoc root) {
51+
String outDirName = readOptions( root.options() );
52+
File outDir = new File( outDirName );
53+
outDir.mkdirs();
54+
File specCheckReport = new File( outDir, "index.html" );
55+
56+
List<JSRReference> references = processClasses( root.classes() );
57+
Collections.sort( references );
58+
59+
writeHeader();
60+
writeContents( references );
61+
writeFooter();
62+
63+
64+
try {
65+
BufferedWriter writer = new BufferedWriter( new FileWriter( specCheckReport ) );
66+
writer.write( out.toString() );
67+
writer.close();
68+
}
69+
catch ( IOException e ) {
70+
System.err.println( "Error writing tck report." );
71+
}
72+
return true;
73+
}
74+
75+
public static int optionLength(String option) {
76+
if ( option.equals( "-d" ) ) {
77+
return 2;
78+
}
79+
else {
80+
return Standard.optionLength( option );
81+
}
82+
}
83+
84+
public static boolean validOptions(String options[][], DocErrorReporter reporter) {
85+
return true;
86+
}
87+
88+
private static void writeFooter() {
89+
out.append( "</body></html>" );
90+
}
91+
92+
private static void writeHeader() {
93+
out.append( "<html><head></head><body>" );
94+
}
95+
96+
private static void writeTableHeader() {
97+
out.append( "<table border=\"1\"><tr>" );
98+
for ( String s : tableHeaders ) {
99+
out.append( "<th>" ).append( s ).append( "</th>" );
100+
}
101+
out.append( "</tr>" );
102+
}
103+
104+
private static void writeTableFooter() {
105+
out.append( "</table>" );
106+
}
107+
108+
private static String readOptions(String[][] options) {
109+
String tagName = null;
110+
for ( String[] opt : options ) {
111+
if ( opt[0].equals( "-d" ) ) {
112+
tagName = opt[1];
113+
}
114+
}
115+
return tagName;
116+
}
117+
118+
private static void writeContents(List<JSRReference> references) {
119+
writeTableHeader();
120+
for ( JSRReference reference : references ) {
121+
out.append( "<tr>" );
122+
out.append( "<td>" ).append( reference.jsrReference ).append( "</td>" );
123+
out.append( "<td><a href=\"" )
124+
.append( reference.getSourceLink() )
125+
.append( "\">" )
126+
.append( reference.className )
127+
.append( "</a></td>" );
128+
out.append( "<td>" ).append( reference.methodName ).append( "</td>" );
129+
out.append( "</tr>" );
130+
}
131+
writeTableFooter();
132+
}
133+
134+
private static List<JSRReference> processClasses(ClassDoc[] classDocs) {
135+
ArrayList<JSRReference> references = new ArrayList<JSRReference>();
136+
for ( ClassDoc aClass : classDocs ) {
137+
MethodDoc[] methods = aClass.methods();
138+
for ( MethodDoc method : methods ) {
139+
Tag[] tags = method.tags( TAG_NAME );
140+
if ( tags.length > 0 ) {
141+
for ( Tag tag : tags ) {
142+
JSRReference reference = new JSRReference( tag.text(), aClass.qualifiedName(), method.name() );
143+
references.add( reference );
144+
}
145+
}
146+
}
147+
}
148+
return references;
149+
}
150+
151+
static class JSRReference implements Comparable {
152+
/**
153+
* The JSR 303 section this instance references.
154+
*/
155+
String jsrReference;
156+
157+
/**
158+
* The name of the class which references the JSR.
159+
*/
160+
String className;
161+
162+
/**
163+
* The method which references the JSR.
164+
*/
165+
String methodName;
166+
167+
/**
168+
* @todo Add some validation
169+
*/
170+
JSRReference(String reference, String className, String methodName) {
171+
this.jsrReference = reference;
172+
this.className = className;
173+
this.methodName = methodName;
174+
}
175+
176+
public String getSourceLink() {
177+
StringBuilder builder = new StringBuilder();
178+
builder.append( "../xref-test/" );
179+
builder.append( className.replace( '.', '/' ) );
180+
builder.append( ".html" );
181+
return builder.toString();
182+
}
183+
184+
public int compareTo(Object o) {
185+
return jsrReference.compareTo( ( ( JSRReference ) o ).jsrReference );
186+
}
187+
}
188+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ private Validator getHibernateValidator() {
7171

7272
/**
7373
* JSR 303: Requirements on classes to be validates (3.1)
74+
* @jsr 3.1
7475
*/
7576
@Test
7677
public void testWrongMethodName() {
@@ -339,6 +340,7 @@ public void testValidateList() {
339340

340341
/**
341342
* JSR 303: Multi-valued constraints (2.2)
343+
* @jsr 2.2
342344
*/
343345
@Test
344346
public void testMultiValueConstraint() {
@@ -361,6 +363,7 @@ public void testMultiValueConstraint() {
361363

362364
/**
363365
* JSR 303: Object graph validation (3.5.1)
366+
* @jsr 3.5.1
364367
*/
365368
@Test
366369
public void testGraphValidation() {

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
<url>dav:https://snapshots.jboss.org/maven2</url>
178178
</snapshotRepository>
179179
<site>
180-
<id>local</id>
180+
<id>site</id>
181181
<url>file:///Users/hardy/Sites</url>
182182
</site>
183183
</distributionManagement>

validation-api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<distributionManagement>
2020
<site>
21-
<id>local</id>
21+
<id>site</id>
2222
<url>file:///Users/hardy/Sites/${artifactId}</url>
2323
</site>
2424
</distributionManagement>

0 commit comments

Comments
 (0)