Skip to content

Commit c0000ec

Browse files
committed
code review and added cross references to spec; updated TCK annotaton processor
git-svn-id: https://svn.jboss.org/repos/hibernate/validator/trunk@15745 1b8cb986-b30d-0410-93ca-fae66ebed9b2
1 parent debaf50 commit c0000ec

File tree

10 files changed

+285
-104
lines changed

10 files changed

+285
-104
lines changed

hibernate-validator/src/test/java/org/hibernate/validation/constraints/LengthConstraintTest.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,22 @@
2323
import static org.junit.Assert.assertTrue;
2424
import static org.junit.Assert.fail;
2525
import org.junit.Test;
26+
import org.junit.Before;
27+
28+
import org.hibernate.tck.annotations.SpecAssertion;
2629

2730
/**
31+
* Tests the <code>LengthConstraint</code>.
32+
*
2833
* @author Hardy Ferentschik
2934
*/
3035
public class LengthConstraintTest {
31-
@Test
32-
public void testIsValid() {
33-
LengthConstraint constraint = new LengthConstraint();
36+
37+
LengthConstraint constraint;
38+
39+
@Before
40+
public void init() {
41+
constraint = new LengthConstraint();
3442
constraint.initialize(
3543
new Length() {
3644

@@ -55,6 +63,10 @@ public Class<? extends Annotation> annotationType() {
5563
}
5664
}
5765
);
66+
}
67+
68+
@Test
69+
public void testIsValid() {
5870

5971
assertTrue( constraint.isValid( null, null ) );
6072
assertFalse( constraint.isValid( "", null ) );
@@ -63,6 +75,11 @@ public Class<? extends Annotation> annotationType() {
6375
assertTrue( constraint.isValid( "foo", null ) );
6476
assertFalse( constraint.isValid( "foobar", null ) );
6577

78+
}
79+
80+
@Test
81+
@SpecAssertion( section = "2.1", note="Incompatible type cause runtime error")
82+
public void testIncompatibleType() {
6683
try {
6784
constraint.isValid( new Object(), null );
6885
fail();

hibernate-validator/src/test/java/org/hibernate/validation/constraints/NotEmptyConstraintTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,41 @@
1717
*/
1818
package org.hibernate.validation.constraints;
1919

20+
import java.lang.annotation.Annotation;
21+
2022
import static org.junit.Assert.assertFalse;
2123
import static org.junit.Assert.assertTrue;
2224
import static org.junit.Assert.fail;
2325
import org.junit.Test;
26+
import org.junit.Before;
27+
28+
import org.hibernate.tck.annotations.SpecAssertion;
2429

2530
/**
2631
* @author Hardy Ferentschik
2732
*/
2833
public class NotEmptyConstraintTest {
2934

35+
NotEmptyConstraint constraint;
36+
37+
@Before
38+
public void init() {
39+
constraint = new NotEmptyConstraint();
40+
}
41+
3042
@Test
3143
public void testIsValid() {
32-
NotEmptyConstraint constraint = new NotEmptyConstraint();
3344

3445
assertTrue( constraint.isValid( null, null ) );
3546
assertTrue( constraint.isValid( "foo", null ) );
3647
assertTrue( constraint.isValid( " ", null ) );
3748

3849
assertFalse( constraint.isValid( "", null ) );
50+
}
3951

52+
@Test
53+
@SpecAssertion(section = "2.1", note = "Incompatible type cause runtime error")
54+
public void testIncompatibleType() {
4055
try {
4156
constraint.isValid( new Object(), null );
4257
fail();

hibernate-validator/src/test/java/org/hibernate/validation/constraints/PatternConstraintTest.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,20 @@
2323
import static org.junit.Assert.assertTrue;
2424
import static org.junit.Assert.fail;
2525
import org.junit.Test;
26+
import org.junit.Before;
27+
28+
import org.hibernate.tck.annotations.SpecAssertion;
2629

2730
/**
2831
* @author Hardy Ferentschik
2932
*/
3033
public class PatternConstraintTest {
31-
@Test
32-
public void testIsValid() {
33-
PatternConstraint constraint = new PatternConstraint();
34+
35+
PatternConstraint constraint;
36+
37+
@Before
38+
public void init() {
39+
constraint = new PatternConstraint();
3440
constraint.initialize(
3541
new Pattern() {
3642

@@ -55,6 +61,10 @@ public Class<? extends Annotation> annotationType() {
5561
}
5662
}
5763
);
64+
}
65+
66+
@Test
67+
public void testIsValid() {
5868

5969
assertTrue( constraint.isValid( null, null ) );
6070
assertFalse( constraint.isValid( "", null ) );
@@ -69,4 +79,16 @@ public Class<? extends Annotation> annotationType() {
6979
// success
7080
}
7181
}
82+
83+
@Test
84+
@SpecAssertion(section = "2.1", note = "Incompatible type cause runtime error")
85+
public void testIncompatibleType() {
86+
try {
87+
constraint.isValid( new Object(), null );
88+
fail();
89+
}
90+
catch ( IllegalArgumentException e ) {
91+
// success
92+
}
93+
}
7294
}

hibernate-validator/src/test/java/org/hibernate/validation/util/ReflectionHelperTest.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.hibernate.validation.eg.Order;
4242
import org.hibernate.validation.eg.constraint.NoGroups;
4343
import org.hibernate.validation.eg.constraint.NoMessage;
44+
import org.hibernate.tck.annotations.SpecAssertion;
4445

4546
/**
4647
* Tests for the <code>ReflectionHelper</code>.
@@ -49,7 +50,7 @@
4950
*/
5051
public class ReflectionHelperTest {
5152
@Test
52-
public void testGetIndexedValueFormMap() {
53+
public void testGetIndexedValueForMap() {
5354
Map<String, Object> map = new HashMap<String, Object>();
5455
Object testObject = new Object();
5556
String key = "key";
@@ -129,10 +130,8 @@ public Class<? extends Annotation> annotationType() {
129130
}
130131
}
131132

132-
/**
133-
* JSR 303: Constraint definition properties - message (2.1.1.1)
134-
*/
135133
@Test
134+
@SpecAssertion(section = "2.1.1.2", note = "constraint annotation must specify a groups element")
136135
public void testConstraintWithNoMessage() {
137136
Annotation annotation = new NoGroups() {
138137
public String message() {
@@ -148,10 +147,8 @@ public Class<? extends Annotation> annotationType() {
148147
);
149148
}
150149

151-
/**
152-
* JSR 303: Constraint definition properties - groups (2.1.1.2)
153-
*/
154150
@Test
151+
@SpecAssertion(section = "2.1.1.1", note = "constraint annotation must specify a groups element")
155152
public void testConstraintWithNoGroups() {
156153
Annotation annotation = new NoMessage() {
157154
public String[] groups() {
@@ -192,6 +189,6 @@ public void testGetMultiValueConstraints() throws Exception {
192189
annotation = fields[0].getAnnotation( NotNull.class );
193190
assertNotNull( annotation );
194191
multiValueConstraintAnnotations = ReflectionHelper.getMultiValueConstraints( annotation );
195-
assertTrue( "There should be two constraint annotations", multiValueConstraintAnnotations.size() == 0 );
192+
assertTrue( "There should be two constraint annotations", multiValueConstraintAnnotations.size() == 0 );
196193
}
197194
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
<artifactId>maven-surefire-plugin</artifactId>
129129
<configuration>
130130
<forkMode>always</forkMode>
131+
<redirectTestOutputToFile>true</redirectTestOutputToFile>
131132
</configuration>
132133
</plugin>
133134
<plugin>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.List;
21+
22+
/**
23+
* @author Hardy Ferentschik
24+
*/
25+
public class HtmlTckReportGenerator implements TCKReportGenerator {
26+
private final String[] tableHeaders = new String[] { "Section", "Class", "Method", "Note" };
27+
private StringBuffer out;
28+
29+
public String generateReport(List<JSRReference> references) {
30+
out = new StringBuffer();
31+
writeHeader();
32+
writeContents( references );
33+
writeFooter();
34+
return out.toString();
35+
}
36+
37+
private void writeFooter() {
38+
out.append( "</body></html>" );
39+
}
40+
41+
private void writeHeader() {
42+
out.append( "<html><head></head><body>" );
43+
}
44+
45+
private void writeTableHeader() {
46+
out.append( "<table border=\"1\"><tr>" );
47+
for ( String s : tableHeaders ) {
48+
out.append( "<th>" ).append( s ).append( "</th>" );
49+
}
50+
out.append( "</tr>" );
51+
}
52+
53+
private void writeTableFooter() {
54+
out.append( "</table>" );
55+
}
56+
57+
private void writeContents(List<JSRReference> references) {
58+
writeTableHeader();
59+
String currentReference = "";
60+
boolean sameReference;
61+
String currentClass = "";
62+
for ( JSRReference reference : references ) {
63+
out.append( "<tr>" );
64+
65+
if ( currentReference.equals( reference.jsrSectionReference ) ) {
66+
sameReference = true;
67+
out.append( "<td></td>" );
68+
}
69+
else {
70+
currentReference = reference.jsrSectionReference;
71+
sameReference = false;
72+
out.append( "<td>" ).append( reference.jsrSectionReference ).append( "</td>" );
73+
}
74+
75+
if ( sameReference && currentClass.equals( reference.className ) ) {
76+
out.append( "<td></td>" );
77+
}
78+
else {
79+
currentClass = reference.className;
80+
out.append( "<td><a href=\"" )
81+
.append( reference.getSourceLink() )
82+
.append( "\">" )
83+
.append( reference.className )
84+
.append( "</a></td>" );
85+
}
86+
87+
out.append( "<td>" ).append( reference.methodName ).append( "</td>" );
88+
89+
out.append( "<td>" ).append( reference.note ).append( "</td>" );
90+
out.append( "</tr>" );
91+
}
92+
writeTableFooter();
93+
}
94+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
/**
21+
* @author Hardy Ferentschik
22+
*/
23+
public class JSRReference implements Comparable {
24+
/**
25+
* The JSR section this instance references.
26+
*/
27+
String jsrSectionReference;
28+
29+
/**
30+
* The name of the class which references the JSR.
31+
*/
32+
String className;
33+
34+
/**
35+
* The method which references the JSR.
36+
*/
37+
String methodName;
38+
39+
/**
40+
* Optional note specified on the specification reference
41+
*/
42+
String note = "";
43+
44+
JSRReference(String reference, String className, String methodName) {
45+
this.jsrSectionReference = reference;
46+
this.className = className;
47+
this.methodName = methodName;
48+
}
49+
50+
public String getSourceLink() {
51+
StringBuilder builder = new StringBuilder();
52+
builder.append( "xref-test/" );
53+
builder.append( className.replace( '.', '/' ) );
54+
builder.append( ".html" );
55+
return builder.toString();
56+
}
57+
58+
public String getJsrSectionReference() {
59+
return jsrSectionReference;
60+
}
61+
62+
public String getClassName() {
63+
return className;
64+
}
65+
66+
public String getMethodName() {
67+
return methodName;
68+
}
69+
70+
public String getNote() {
71+
return note;
72+
}
73+
74+
public int compareTo(Object o) {
75+
return jsrSectionReference.compareTo( ( ( JSRReference ) o ).jsrSectionReference );
76+
}
77+
}

0 commit comments

Comments
 (0)