/
IllegalMethodParameterConstraintsTest.java
173 lines (130 loc) · 5.01 KB
/
IllegalMethodParameterConstraintsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat, Inc. and/or its affiliates, 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.validator.test.engine.methodlevel;
import java.util.Set;
import javax.validation.ConstraintDefinitionException;
import javax.validation.ConstraintViolation;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.testng.annotations.Test;
import org.hibernate.validator.metadata.BeanMetaDataImpl;
import static org.hibernate.validator.test.util.TestUtil.assertCorrectConstraintViolationMessages;
import static org.hibernate.validator.test.util.TestUtil.getMethodValidator;
import static org.hibernate.validator.test.util.TestUtil.getValidator;
/**
* Integration test for {@link ValidatorImpl} and {@link BeanMetaDataImpl} which
* tests that illegal method parameter constraints are handled properly.
*
* @author Gunnar Morling
*/
public class IllegalMethodParameterConstraintsTest {
@Test(
expectedExceptions = ConstraintDefinitionException.class,
expectedExceptionsMessageRegExp = "Only the root method of an overridden method in an inheritance hierarchy may be annotated with parameter constraints\\. The following.*"
)
public void parameterConstraintsAddedInSubTypeCausesDefinitionException() {
getMethodValidator().validateAllParameters(
new FooExtImpl(), FooExtImpl.class.getDeclaredMethods()[0], new Object[] { }
);
}
@Test(
expectedExceptions = ConstraintDefinitionException.class,
expectedExceptionsMessageRegExp = "Only the root method of an overridden method in an inheritance hierarchy may be annotated with parameter constraints, but there are.*"
)
public void constraintStrengtheningInSubTypeCausesDefinitionException() {
getMethodValidator().validateAllParameters(
new BarExtImpl(), BarExtImpl.class.getDeclaredMethods()[0], new Object[] { }
);
}
@Test(
expectedExceptions = ConstraintDefinitionException.class,
expectedExceptionsMessageRegExp = "Only the root method of an overridden method in an inheritance hierarchy may be annotated with parameter constraints\\. The following.*"
)
public void parameterConstraintsInHierarchyWithMultipleRootMethodsCausesDefinitionException() {
getMethodValidator().validateAllParameters(
new BazImpl(), BazImpl.class.getDeclaredMethods()[0], new Object[] { }
);
}
@Test(
expectedExceptions = ConstraintDefinitionException.class,
expectedExceptionsMessageRegExp = "Only the root method of an overridden method in an inheritance hierarchy may be annotated with parameter constraints.*"
)
public void standardBeanValidationCanBePerformedOnTypeWithIllegalMethodParameterConstraints() {
QuxImpl qux = new QuxImpl();
//validating a property is fine
Set<ConstraintViolation<QuxImpl>> violations = getValidator().validate( qux );
assertCorrectConstraintViolationMessages( violations, "may not be null" );
//but method validation fails due to illegal parameter constraints being defined
getMethodValidator().validateAllParameters(
qux, QuxImpl.class.getDeclaredMethods()[0], new Object[] { }
);
}
public static interface Foo {
void foo(String s);
}
public static interface FooExt extends Foo {
/**
* Adds constraints to an un-constrained method from a super-type, which is not allowed.
*/
void foo(@NotNull String s);
}
public static class FooExtImpl implements FooExt {
public void foo(String s) {
}
}
public static interface Bar {
void bar(@NotNull String s);
}
public static interface BarExt extends Bar {
/**
* Adds constraints to a constrained method from a super-type, which is not allowed.
*/
void bar(@Size(min = 3) String s);
}
public static class BarExtImpl implements BarExt {
public void bar(String s) {
}
}
public static interface Baz1 {
void baz(String s);
}
public static interface Baz2 {
void baz(@Size(min = 3) String s);
}
public static class BazImpl implements Baz1, Baz2 {
/**
* Implements a method defined by two interfaces, with di a super-type, which is not allowed.
*/
public void baz(String s) {
}
}
public static interface Qux {
@NotNull
public String getQux();
public void qux(String s);
}
public static interface QuxExt extends Qux {
public void qux(@NotNull String s);
}
public static class QuxImpl implements QuxExt {
public String getQux() {
return null;
}
public void qux(String s) {
}
}
}