Skip to content

Commit 935ad0c

Browse files
committed
BVTCK-32 Adding tests for ExecutableValidator#validateReturnValue() and validateConstructorReturnValue()
1 parent 6e116b6 commit 935ad0c

File tree

6 files changed

+732
-0
lines changed

6 files changed

+732
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.hibernate.beanvalidation.tck.tests.methodvalidation;
18+
19+
import java.util.Date;
20+
import javax.validation.constraints.NotNull;
21+
import javax.validation.constraints.Pattern;
22+
import javax.validation.constraints.Size;
23+
24+
import org.hibernate.beanvalidation.tck.tests.methodvalidation.constraint.ValidCustomer;
25+
26+
/**
27+
* @author Gunnar Morling
28+
*/
29+
public class Customer {
30+
31+
private String name;
32+
33+
public interface Basic {
34+
}
35+
36+
public interface Extended {
37+
}
38+
39+
//testOneViolation
40+
@NotNull
41+
public Address getAddress() {
42+
return null;
43+
}
44+
45+
@ValidCustomer
46+
public Customer() {
47+
}
48+
49+
//testTwoViolations
50+
//testNoViolations
51+
@Size(min = 3)
52+
@Pattern(regexp = "aaa")
53+
public String getFirstName(String s) {
54+
return null;
55+
}
56+
57+
@NotNull
58+
@ValidCustomer
59+
public Customer(String name) {
60+
this.name = name;
61+
}
62+
63+
//testValidationWithGroup
64+
@Size(min = 3, groups = Extended.class)
65+
public String getLastName(long l) {
66+
return null;
67+
}
68+
69+
@NotNull(groups = Extended.class)
70+
public Customer(long l) {
71+
}
72+
73+
//testTwoConstraintsOfSameType
74+
@Size.List({
75+
@Size(min = 3),
76+
@Size(min = 6)
77+
})
78+
public String getLastName(CharSequence lastName) {
79+
return null;
80+
}
81+
82+
@ValidCustomer.List({
83+
@ValidCustomer(message = "1"),
84+
@ValidCustomer(message = "2")
85+
})
86+
public Customer(CharSequence lastName) {
87+
}
88+
89+
//testValidationWithSeveralGroups
90+
@Size(min = 3, groups = Extended.class)
91+
@Pattern(regexp = "aaa", groups = Basic.class)
92+
public String getAllData(Date dateOfBirth) {
93+
return null;
94+
}
95+
96+
@NotNull(groups = Extended.class)
97+
@ValidCustomer(groups = Basic.class)
98+
public Customer(Date dateOfBirth) {
99+
}
100+
101+
public String getName() {
102+
return name;
103+
}
104+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.hibernate.beanvalidation.tck.tests.methodvalidation;
18+
19+
import javax.validation.constraints.Past;
20+
21+
/**
22+
* @author Gunnar Morling
23+
*/
24+
public class Email {
25+
26+
@Past
27+
public Email() {
28+
}
29+
30+
@Past
31+
public String getValue() {
32+
return null;
33+
}
34+
}
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.hibernate.beanvalidation.tck.tests.methodvalidation;
18+
19+
import java.lang.reflect.Constructor;
20+
import java.util.Date;
21+
import java.util.Set;
22+
import javax.validation.ConstraintViolation;
23+
import javax.validation.MethodValidator;
24+
import javax.validation.ValidationException;
25+
import javax.validation.constraints.NotNull;
26+
import javax.validation.metadata.ElementDescriptor.Kind;
27+
28+
import org.jboss.arquillian.container.test.api.Deployment;
29+
import org.jboss.arquillian.testng.Arquillian;
30+
import org.jboss.shrinkwrap.api.spec.WebArchive;
31+
import org.jboss.test.audit.annotations.SpecAssertion;
32+
import org.jboss.test.audit.annotations.SpecAssertions;
33+
import org.jboss.test.audit.annotations.SpecVersion;
34+
import org.testng.annotations.BeforeMethod;
35+
import org.testng.annotations.Test;
36+
37+
import org.hibernate.beanvalidation.tck.tests.methodvalidation.Customer.Basic;
38+
import org.hibernate.beanvalidation.tck.tests.methodvalidation.Customer.Extended;
39+
import org.hibernate.beanvalidation.tck.tests.methodvalidation.constraint.MyCrossParameterConstraint;
40+
import org.hibernate.beanvalidation.tck.tests.methodvalidation.constraint.ValidCustomer;
41+
import org.hibernate.beanvalidation.tck.util.Groups;
42+
import org.hibernate.beanvalidation.tck.util.TestUtil;
43+
import org.hibernate.beanvalidation.tck.util.shrinkwrap.WebArchiveBuilder;
44+
45+
import static org.hibernate.beanvalidation.tck.util.TestUtil.assertCorrectConstraintTypes;
46+
import static org.hibernate.beanvalidation.tck.util.TestUtil.assertCorrectNumberOfViolations;
47+
import static org.hibernate.beanvalidation.tck.util.TestUtil.assertCorrectPathDescriptorKinds;
48+
import static org.hibernate.beanvalidation.tck.util.TestUtil.assertCorrectPathNodeNames;
49+
import static org.hibernate.beanvalidation.tck.util.TestUtil.kinds;
50+
import static org.hibernate.beanvalidation.tck.util.TestUtil.names;
51+
52+
/**
53+
* @author Gunnar Morling
54+
*/
55+
@SpecVersion(spec = "beanvalidation", version = "1.1.0")
56+
public class ValidateConstructorReturnValueTest extends Arquillian {
57+
58+
private MethodValidator executableValidator;
59+
60+
@Deployment
61+
public static WebArchive createTestArchive() {
62+
return new WebArchiveBuilder()
63+
.withTestClass( ValidateConstructorReturnValueTest.class )
64+
.withPackage( MyCrossParameterConstraint.class.getPackage() )
65+
.withClass( Address.class )
66+
.withClass( Customer.class )
67+
.withClass( Email.class )
68+
.build();
69+
}
70+
71+
@BeforeMethod
72+
public void setupValidator() {
73+
executableValidator = TestUtil.getValidatorUnderTest().forMethods();
74+
}
75+
76+
//fails on RI due to wrong return value node name
77+
@Test(groups = Groups.FAILING_IN_RI)
78+
@SpecAssertions({
79+
@SpecAssertion(section = "5.1.2", id = "g"),
80+
@SpecAssertion(section = "5.1.2", id = "h")
81+
})
82+
public void testOneViolation() throws Exception {
83+
Constructor<Customer> constructor = Customer.class.getConstructor();
84+
Customer returnValue = new Customer();
85+
86+
Set<ConstraintViolation<Customer>> violations = executableValidator.validateConstructorReturnValue(
87+
constructor,
88+
returnValue
89+
);
90+
91+
assertCorrectNumberOfViolations( violations, 1 );
92+
93+
assertCorrectConstraintTypes( violations, ValidCustomer.class );
94+
assertCorrectPathNodeNames( violations, names( "Customer", null ) );
95+
assertCorrectPathDescriptorKinds(
96+
violations,
97+
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE )
98+
);
99+
}
100+
101+
//fails on RI due to wrong return value node name
102+
@Test(groups = Groups.FAILING_IN_RI)
103+
@SpecAssertion(section = "5.1.2", id = "h")
104+
public void testTwoViolations() throws Exception {
105+
Constructor<Customer> constructor = Customer.class.getConstructor( String.class );
106+
Customer returnValue = null;
107+
108+
Set<ConstraintViolation<Customer>> violations = executableValidator.validateConstructorReturnValue(
109+
constructor,
110+
returnValue
111+
);
112+
113+
assertCorrectNumberOfViolations( violations, 2 );
114+
115+
assertCorrectConstraintTypes( violations, NotNull.class, ValidCustomer.class );
116+
assertCorrectPathNodeNames(
117+
violations,
118+
names( "Customer", null ),
119+
names( "Customer", null )
120+
);
121+
assertCorrectPathDescriptorKinds(
122+
violations,
123+
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE ),
124+
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE )
125+
);
126+
}
127+
128+
//fails on RI due to wrong return value node name
129+
@Test(groups = Groups.FAILING_IN_RI)
130+
@SpecAssertion(section = "5.1.2", id = "h")
131+
public void testTwoConstraintsOfSameType() throws Exception {
132+
Constructor<Customer> constructor = Customer.class.getConstructor( CharSequence.class );
133+
Customer returnValue = new Customer();
134+
135+
Set<ConstraintViolation<Customer>> violations = executableValidator.validateConstructorReturnValue(
136+
constructor,
137+
returnValue
138+
);
139+
140+
assertCorrectNumberOfViolations( violations, 2 );
141+
142+
assertCorrectConstraintTypes( violations, ValidCustomer.class, ValidCustomer.class );
143+
assertCorrectPathNodeNames(
144+
violations,
145+
names( "Customer", null ),
146+
names( "Customer", null )
147+
);
148+
assertCorrectPathDescriptorKinds(
149+
violations,
150+
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE ),
151+
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE )
152+
);
153+
}
154+
155+
@Test
156+
@SpecAssertion(section = "5.1.2", id = "h")
157+
public void testNoViolations() throws Exception {
158+
Constructor<Customer> constructor = Customer.class.getConstructor();
159+
Customer returnValue = new Customer( "Bob" );
160+
161+
Set<ConstraintViolation<Customer>> violations = executableValidator.validateConstructorReturnValue(
162+
constructor,
163+
returnValue
164+
);
165+
166+
assertCorrectNumberOfViolations( violations, 0 );
167+
}
168+
169+
//fails on RI due to wrong return value node name
170+
@Test(groups = Groups.FAILING_IN_RI)
171+
@SpecAssertion(section = "5.1.2", id = "h")
172+
public void testValidationWithGroup() throws Exception {
173+
Constructor<Customer> constructor = Customer.class.getConstructor( long.class );
174+
Customer returnValue = null;
175+
176+
Set<ConstraintViolation<Customer>> violations = executableValidator.validateConstructorReturnValue(
177+
constructor,
178+
returnValue
179+
);
180+
181+
assertCorrectNumberOfViolations( violations, 0 );
182+
183+
violations = executableValidator.validateConstructorReturnValue(
184+
constructor,
185+
returnValue,
186+
Extended.class
187+
);
188+
189+
assertCorrectConstraintTypes( violations, NotNull.class );
190+
assertCorrectPathNodeNames( violations, names( "Customer", null ) );
191+
assertCorrectPathDescriptorKinds(
192+
violations,
193+
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE )
194+
);
195+
}
196+
197+
//fails on RI due to wrong return value node name
198+
@Test(groups = Groups.FAILING_IN_RI)
199+
@SpecAssertion(section = "5.1.2", id = "h")
200+
public void testValidationWithSeveralGroups() throws Exception {
201+
Constructor<Customer> constructor = Customer.class.getConstructor( Date.class );
202+
Customer returnValue = null;
203+
204+
Set<ConstraintViolation<Customer>> violations = executableValidator.validateConstructorReturnValue(
205+
constructor,
206+
returnValue
207+
);
208+
209+
assertCorrectNumberOfViolations( violations, 0 );
210+
211+
violations = executableValidator.validateConstructorReturnValue(
212+
constructor,
213+
returnValue,
214+
Basic.class,
215+
Extended.class
216+
);
217+
218+
assertCorrectConstraintTypes( violations, NotNull.class, ValidCustomer.class );
219+
assertCorrectPathNodeNames(
220+
violations,
221+
names( "Customer", null ),
222+
names( "Customer", null )
223+
);
224+
assertCorrectPathDescriptorKinds(
225+
violations,
226+
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE ),
227+
kinds( Kind.CONSTRUCTOR, Kind.RETURN_VALUE )
228+
);
229+
}
230+
231+
@Test(expectedExceptions = ValidationException.class)
232+
@SpecAssertion(section = "5.1.2", id = "g")
233+
public void testUnexpectedType() throws Exception {
234+
Constructor<Email> constructor = Email.class.getConstructor();
235+
Email returnValue = new Email();
236+
237+
executableValidator.validateConstructorReturnValue( constructor, returnValue );
238+
}
239+
}

0 commit comments

Comments
 (0)