[hibernate-commits] Hibernate SVN: r15745 - in validator/trunk: hibernate-validator/src/test/java/org/hibernate/validation/constraints and 3 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Jan 5 10:13:00 EST 2009


Author: hardy.ferentschik
Date: 2009-01-05 10:12:59 -0500 (Mon, 05 Jan 2009)
New Revision: 15745

Added:
   validator/trunk/tck-utils/src/main/java/org/hibernate/tck/HtmlTckReportGenerator.java
   validator/trunk/tck-utils/src/main/java/org/hibernate/tck/JSRReference.java
   validator/trunk/tck-utils/src/main/java/org/hibernate/tck/TCKReportGenerator.java
Modified:
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/LengthConstraintTest.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NotEmptyConstraintTest.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PatternConstraintTest.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ReflectionHelperTest.java
   validator/trunk/pom.xml
   validator/trunk/tck-utils/src/main/java/org/hibernate/tck/TCKAnnotationProcessor.java
   validator/trunk/validation-api/src/main/java/javax/validation/ConstraintValidator.java
Log:
code review and added cross references to spec; updated TCK annotaton processor

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/LengthConstraintTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/LengthConstraintTest.java	2008-12-31 11:29:37 UTC (rev 15744)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/LengthConstraintTest.java	2009-01-05 15:12:59 UTC (rev 15745)
@@ -23,14 +23,22 @@
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import org.junit.Test;
+import org.junit.Before;
 
+import org.hibernate.tck.annotations.SpecAssertion;
+
 /**
+ * Tests the <code>LengthConstraint</code>.
+ *
  * @author Hardy Ferentschik
  */
 public class LengthConstraintTest {
-	@Test
-	public void testIsValid() {
-		LengthConstraint constraint = new LengthConstraint();
+
+	LengthConstraint constraint;
+
+	@Before
+	public void init() {
+		constraint = new LengthConstraint();
 		constraint.initialize(
 				new Length() {
 
@@ -55,7 +63,11 @@
 					}
 				}
 		);
+	}
 
+	@Test
+	public void testIsValid() {
+
 		assertTrue( constraint.isValid( null, null ) );
 		assertFalse( constraint.isValid( "", null ) );
 		assertTrue( constraint.isValid( "f", null ) );
@@ -63,6 +75,11 @@
 		assertTrue( constraint.isValid( "foo", null ) );
 		assertFalse( constraint.isValid( "foobar", null ) );
 
+	}
+
+	@Test
+	@SpecAssertion( section = "2.1", note="Incompatible type cause runtime error")
+	public void testIncompatibleType() {
 		try {
 			constraint.isValid( new Object(), null );
 			fail();

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NotEmptyConstraintTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NotEmptyConstraintTest.java	2008-12-31 11:29:37 UTC (rev 15744)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/NotEmptyConstraintTest.java	2009-01-05 15:12:59 UTC (rev 15745)
@@ -17,26 +17,41 @@
 */
 package org.hibernate.validation.constraints;
 
+import java.lang.annotation.Annotation;
+
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import org.junit.Test;
+import org.junit.Before;
 
+import org.hibernate.tck.annotations.SpecAssertion;
+
 /**
  * @author Hardy Ferentschik
  */
 public class NotEmptyConstraintTest {
 
+	NotEmptyConstraint constraint;
+
+	@Before
+	public void init() {
+		constraint = new NotEmptyConstraint();
+	}
+
 	@Test
 	public void testIsValid() {
-		NotEmptyConstraint constraint = new NotEmptyConstraint();
 
 		assertTrue( constraint.isValid( null, null ) );
 		assertTrue( constraint.isValid( "foo", null ) );
 		assertTrue( constraint.isValid( "  ", null ) );
 
 		assertFalse( constraint.isValid( "", null ) );
+	}
 
+	@Test
+	@SpecAssertion(section = "2.1", note = "Incompatible type cause runtime error")
+	public void testIncompatibleType() {
 		try {
 			constraint.isValid( new Object(), null );
 			fail();

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PatternConstraintTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PatternConstraintTest.java	2008-12-31 11:29:37 UTC (rev 15744)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/PatternConstraintTest.java	2009-01-05 15:12:59 UTC (rev 15745)
@@ -23,14 +23,20 @@
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import org.junit.Test;
+import org.junit.Before;
 
+import org.hibernate.tck.annotations.SpecAssertion;
+
 /**
  * @author Hardy Ferentschik
  */
 public class PatternConstraintTest {
-	@Test
-	public void testIsValid() {
-		PatternConstraint constraint = new PatternConstraint();
+
+	PatternConstraint constraint;
+
+	@Before
+	public void init() {
+		constraint = new PatternConstraint();
 		constraint.initialize(
 				new Pattern() {
 
@@ -55,7 +61,11 @@
 					}
 				}
 		);
+	}
 
+	@Test
+	public void testIsValid() {
+
 		assertTrue( constraint.isValid( null, null ) );
 		assertFalse( constraint.isValid( "", null ) );
 		assertFalse( constraint.isValid( "bla bla", null ) );
@@ -69,4 +79,16 @@
 			// success
 		}
 	}
+
+	@Test
+	@SpecAssertion(section = "2.1", note = "Incompatible type cause runtime error")
+	public void testIncompatibleType() {
+		try {
+			constraint.isValid( new Object(), null );
+			fail();
+		}
+		catch ( IllegalArgumentException e ) {
+			// success
+		}
+	}
 }

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ReflectionHelperTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ReflectionHelperTest.java	2008-12-31 11:29:37 UTC (rev 15744)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ReflectionHelperTest.java	2009-01-05 15:12:59 UTC (rev 15745)
@@ -41,6 +41,7 @@
 import org.hibernate.validation.eg.Order;
 import org.hibernate.validation.eg.constraint.NoGroups;
 import org.hibernate.validation.eg.constraint.NoMessage;
+import org.hibernate.tck.annotations.SpecAssertion;
 
 /**
  * Tests for the <code>ReflectionHelper</code>.
@@ -49,7 +50,7 @@
  */
 public class ReflectionHelperTest {
 	@Test
-	public void testGetIndexedValueFormMap() {
+	public void testGetIndexedValueForMap() {
 		Map<String, Object> map = new HashMap<String, Object>();
 		Object testObject = new Object();
 		String key = "key";
@@ -129,10 +130,8 @@
 		}
 	}
 
-	/**
-	 * JSR 303: Constraint definition properties - message (2.1.1.1)
-	 */
 	@Test
+	@SpecAssertion(section = "2.1.1.2", note = "constraint annotation must specify a groups element")
 	public void testConstraintWithNoMessage() {
 		Annotation annotation = new NoGroups() {
 			public String message() {
@@ -148,10 +147,8 @@
 		);
 	}
 
-	/**
-	 * JSR 303: Constraint definition properties - groups (2.1.1.2)
-	 */
 	@Test
+	@SpecAssertion(section = "2.1.1.1", note = "constraint annotation must specify a groups element")
 	public void testConstraintWithNoGroups() {
 		Annotation annotation = new NoMessage() {
 			public String[] groups() {
@@ -192,6 +189,6 @@
 		annotation = fields[0].getAnnotation( NotNull.class );
 		assertNotNull( annotation );
 		multiValueConstraintAnnotations = ReflectionHelper.getMultiValueConstraints( annotation );
-		assertTrue( "There should be two constraint annotations", multiValueConstraintAnnotations.size() == 0 );			
+		assertTrue( "There should be two constraint annotations", multiValueConstraintAnnotations.size() == 0 );
 	}
 }

Modified: validator/trunk/pom.xml
===================================================================
--- validator/trunk/pom.xml	2008-12-31 11:29:37 UTC (rev 15744)
+++ validator/trunk/pom.xml	2009-01-05 15:12:59 UTC (rev 15745)
@@ -128,6 +128,7 @@
                     <artifactId>maven-surefire-plugin</artifactId>
                     <configuration>
                         <forkMode>always</forkMode>
+                        <redirectTestOutputToFile>true</redirectTestOutputToFile>
                     </configuration>
                 </plugin>
                 <plugin>

Added: validator/trunk/tck-utils/src/main/java/org/hibernate/tck/HtmlTckReportGenerator.java
===================================================================
--- validator/trunk/tck-utils/src/main/java/org/hibernate/tck/HtmlTckReportGenerator.java	                        (rev 0)
+++ validator/trunk/tck-utils/src/main/java/org/hibernate/tck/HtmlTckReportGenerator.java	2009-01-05 15:12:59 UTC (rev 15745)
@@ -0,0 +1,94 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.tck;
+
+import java.util.List;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class HtmlTckReportGenerator implements TCKReportGenerator {
+	private final String[] tableHeaders = new String[] { "Section", "Class", "Method", "Note" };
+	private StringBuffer out;
+
+	public String generateReport(List<JSRReference> references) {
+		out = new StringBuffer();
+		writeHeader();
+		writeContents( references );
+		writeFooter();
+		return out.toString();
+	}
+
+	private void writeFooter() {
+		out.append( "</body></html>" );
+	}
+
+	private void writeHeader() {
+		out.append( "<html><head></head><body>" );
+	}
+
+	private void writeTableHeader() {
+		out.append( "<table border=\"1\"><tr>" );
+		for ( String s : tableHeaders ) {
+			out.append( "<th>" ).append( s ).append( "</th>" );
+		}
+		out.append( "</tr>" );
+	}
+
+	private void writeTableFooter() {
+		out.append( "</table>" );
+	}
+
+	private void writeContents(List<JSRReference> references) {
+		writeTableHeader();
+		String currentReference = "";
+		boolean sameReference;
+		String currentClass = "";
+		for ( JSRReference reference : references ) {
+			out.append( "<tr>" );
+
+			if ( currentReference.equals( reference.jsrSectionReference ) ) {
+				sameReference = true;
+				out.append( "<td></td>" );
+			}
+			else {
+				currentReference = reference.jsrSectionReference;
+				sameReference = false;
+				out.append( "<td>" ).append( reference.jsrSectionReference ).append( "</td>" );
+			}
+
+			if ( sameReference && currentClass.equals( reference.className ) ) {
+				out.append( "<td></td>" );
+			}
+			else {
+				currentClass = reference.className;
+				out.append( "<td><a href=\"" )
+						.append( reference.getSourceLink() )
+						.append( "\">" )
+						.append( reference.className )
+						.append( "</a></td>" );
+			}
+
+			out.append( "<td>" ).append( reference.methodName ).append( "</td>" );
+
+			out.append( "<td>" ).append( reference.note ).append( "</td>" );
+			out.append( "</tr>" );
+		}
+		writeTableFooter();
+	}
+}


Property changes on: validator/trunk/tck-utils/src/main/java/org/hibernate/tck/HtmlTckReportGenerator.java
___________________________________________________________________
Name: svn:keywords
   + Id

Added: validator/trunk/tck-utils/src/main/java/org/hibernate/tck/JSRReference.java
===================================================================
--- validator/trunk/tck-utils/src/main/java/org/hibernate/tck/JSRReference.java	                        (rev 0)
+++ validator/trunk/tck-utils/src/main/java/org/hibernate/tck/JSRReference.java	2009-01-05 15:12:59 UTC (rev 15745)
@@ -0,0 +1,77 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.tck;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class JSRReference implements Comparable {
+	/**
+	 * The JSR  section this instance references.
+	 */
+	String jsrSectionReference;
+
+	/**
+	 * The name of the class which references the JSR.
+	 */
+	String className;
+
+	/**
+	 * The method which references the JSR.
+	 */
+	String methodName;
+
+	/**
+	 * Optional note specified on the specification reference
+	 */
+	String note = "";
+
+	JSRReference(String reference, String className, String methodName) {
+		this.jsrSectionReference = reference;
+		this.className = className;
+		this.methodName = methodName;
+	}
+
+	public String getSourceLink() {
+		StringBuilder builder = new StringBuilder();
+		builder.append( "xref-test/" );
+		builder.append( className.replace( '.', '/' ) );
+		builder.append( ".html" );
+		return builder.toString();
+	}
+
+	public String getJsrSectionReference() {
+		return jsrSectionReference;
+	}
+
+	public String getClassName() {
+		return className;
+	}
+
+	public String getMethodName() {
+		return methodName;
+	}
+
+	public String getNote() {
+		return note;
+	}
+
+	public int compareTo(Object o) {
+		return jsrSectionReference.compareTo( ( ( JSRReference ) o ).jsrSectionReference );
+	}
+}


Property changes on: validator/trunk/tck-utils/src/main/java/org/hibernate/tck/JSRReference.java
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: validator/trunk/tck-utils/src/main/java/org/hibernate/tck/TCKAnnotationProcessor.java
===================================================================
--- validator/trunk/tck-utils/src/main/java/org/hibernate/tck/TCKAnnotationProcessor.java	2008-12-31 11:29:37 UTC (rev 15744)
+++ validator/trunk/tck-utils/src/main/java/org/hibernate/tck/TCKAnnotationProcessor.java	2009-01-05 15:12:59 UTC (rev 15745)
@@ -23,6 +23,7 @@
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Collections;
 
 import com.sun.mirror.apt.AnnotationProcessor;
 import com.sun.mirror.apt.AnnotationProcessorEnvironment;
@@ -36,6 +37,8 @@
 import org.hibernate.tck.annotations.SpecAssertion;
 
 /**
+ * An APT annotation processor for creating a TCK coverage report.
+ *
  * @author Hardy Ferentschik
  */
 public class TCKAnnotationProcessor implements AnnotationProcessor {
@@ -44,8 +47,6 @@
 	private static final String REPORT_FILE_NAME = "tck.html";
 
 	private final AnnotationProcessorEnvironment env;
-	private final String[] tableHeaders = new String[] { "Section", "Class", "Method" };
-	private final StringBuffer out = new StringBuffer();
 	private final List<JSRReference> references = new ArrayList<JSRReference>();
 	private final File baseDir;
 
@@ -65,25 +66,23 @@
 		for ( Declaration d : env.getDeclarationsAnnotatedWith( annType ) ) {
 			d.accept(
 					getDeclarationScanner(
-							new DoNothingVisitor(),
+							new CreateReferenceVisitor(),
 							NO_OP
 					)
 			);
 		}
 
-
-		writeHeader();
-		writeContents();
-		writeFooter();
-
-		writeReporttoFile();
+		Collections.sort( references );
+		TCKReportGenerator generator = new HtmlTckReportGenerator();
+		String report = generator.generateReport( references );
+		writeReportFile( report );
 	}
 
-	private void writeReporttoFile() {
+	private void writeReportFile(String report) {
 		try {
-			File report = new File( baseDir, REPORT_FILE_NAME );
-			BufferedWriter writer = new BufferedWriter( new FileWriter( report ) );
-			writer.write( out.toString() );
+			File reportFile = new File( baseDir, REPORT_FILE_NAME );
+			BufferedWriter writer = new BufferedWriter( new FileWriter( reportFile ) );
+			writer.write( report );
 			writer.close();
 		}
 		catch ( IOException e ) {
@@ -91,87 +90,16 @@
 		}
 	}
 
-	private void writeFooter() {
-		out.append( "</body></html>" );
-	}
-
-	private void writeHeader() {
-		out.append( "<html><head></head><body>" );
-	}
-
-	private void writeTableHeader() {
-		out.append( "<table border=\"1\"><tr>" );
-		for ( String s : tableHeaders ) {
-			out.append( "<th>" ).append( s ).append( "</th>" );
-		}
-		out.append( "</tr>" );
-	}
-
-	private void writeTableFooter() {
-		out.append( "</table>" );
-	}
-
-	private void writeContents() {
-		writeTableHeader();
-		for ( JSRReference reference : references ) {
-			out.append( "<tr>" );
-			out.append( "<td>" ).append( reference.jsrSectionReference ).append( "</td>" );
-			out.append( "<td><a href=\"" )
-					.append( reference.getSourceLink() )
-					.append( "\">" )
-					.append( reference.className )
-					.append( "</a></td>" );
-			out.append( "<td>" ).append( reference.methodName ).append( "</td>" );
-			out.append( "</tr>" );
-		}
-		writeTableFooter();
-	}
-
-	private class DoNothingVisitor extends SimpleDeclarationVisitor {
+	private class CreateReferenceVisitor extends SimpleDeclarationVisitor {
 		public void visitMethodDeclaration(MethodDeclaration d) {
 			SpecAssertion annotation = d.getAnnotation( SpecAssertion.class );
 			JSRReference ref = new JSRReference(
 					annotation.section()[0], d.getDeclaringType().getQualifiedName(), d.getSimpleName()
 			);
+			if ( annotation.note().length() > 0 ) {
+				ref.note = annotation.note();
+			}
 			references.add( ref );
 		}
 	}
-
-	private static class JSRReference implements Comparable {
-		/**
-		 * The JSR  section this instance references.
-		 */
-		String jsrSectionReference;
-
-		/**
-		 * The name of the class which references the JSR.
-		 */
-		String className;
-
-		/**
-		 * The method which references the JSR.
-		 */
-		String methodName;
-
-		/**
-		 * @todo Add some validation
-		 */
-		JSRReference(String reference, String className, String methodName) {
-			this.jsrSectionReference = reference;
-			this.className = className;
-			this.methodName = methodName;
-		}
-
-		public String getSourceLink() {
-			StringBuilder builder = new StringBuilder();
-			builder.append( "xref-test/" );
-			builder.append( className.replace( '.', '/' ) );
-			builder.append( ".html" );
-			return builder.toString();
-		}
-
-		public int compareTo(Object o) {
-			return jsrSectionReference.compareTo( ( ( JSRReference ) o ).jsrSectionReference );
-		}
-	}
 }

Added: validator/trunk/tck-utils/src/main/java/org/hibernate/tck/TCKReportGenerator.java
===================================================================
--- validator/trunk/tck-utils/src/main/java/org/hibernate/tck/TCKReportGenerator.java	                        (rev 0)
+++ validator/trunk/tck-utils/src/main/java/org/hibernate/tck/TCKReportGenerator.java	2009-01-05 15:12:59 UTC (rev 15745)
@@ -0,0 +1,29 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.tck;
+
+import java.util.List;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public interface TCKReportGenerator {
+
+	public String generateReport(List<JSRReference> references);
+
+}


Property changes on: validator/trunk/tck-utils/src/main/java/org/hibernate/tck/TCKReportGenerator.java
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: validator/trunk/validation-api/src/main/java/javax/validation/ConstraintValidator.java
===================================================================
--- validator/trunk/validation-api/src/main/java/javax/validation/ConstraintValidator.java	2008-12-31 11:29:37 UTC (rev 15744)
+++ validator/trunk/validation-api/src/main/java/javax/validation/ConstraintValidator.java	2009-01-05 15:12:59 UTC (rev 15745)
@@ -39,7 +39,8 @@
 @Retention(RUNTIME)
 public @interface ConstraintValidator {
 	/**
-	 * Constraint validation implementation
+	 * @return The class implementing the constraint validation logic for the constraint annotation this annotation
+	 * is used on.
 	 */
 	public abstract Class<? extends Constraint> value();
 }
\ No newline at end of file




More information about the hibernate-commits mailing list