11
11
import java .util .ArrayList ;
12
12
import java .util .List ;
13
13
import java .util .Set ;
14
+
14
15
import javax .validation .BootstrapConfiguration ;
15
16
import javax .validation .Configuration ;
16
17
import javax .validation .ConstraintViolation ;
27
28
import org .hibernate .validator .cfg .ConstraintMapping ;
28
29
import org .hibernate .validator .cfg .defs .SizeDef ;
29
30
import org .hibernate .validator .testutil .TestForIssue ;
31
+ import org .hibernate .validator .testutil .ValidationXmlTestHelper ;
30
32
import org .hibernate .validator .testutil .ValidatorUtil ;
33
+ import org .testng .annotations .BeforeClass ;
31
34
import org .testng .annotations .Test ;
32
35
33
36
import static org .hibernate .validator .internal .util .CollectionHelper .asSet ;
41
44
*/
42
45
public class XmlMappingTest {
43
46
47
+ private static ValidationXmlTestHelper validationXmlTestHelper ;
48
+
49
+ @ BeforeClass
50
+ public static void setupValidationXmlTestHelper () {
51
+ validationXmlTestHelper = new ValidationXmlTestHelper ( XmlMappingTest .class );
52
+ }
53
+
44
54
@ Test
45
55
@ TestForIssue (jiraKey = "HV-214" )
46
56
public void testConstraintInheritanceWithXmlConfiguration () {
@@ -182,7 +192,7 @@ public void shouldFailToLoadConstraintMappingWithUnsupportedVersion() {
182
192
183
193
@ Test
184
194
public void testParameterNameProviderConfiguration () {
185
- runWithCustomValidationXml (
195
+ validationXmlTestHelper . runWithCustomValidationXml (
186
196
"parameter-name-provider-validation.xml" , new Runnable () {
187
197
188
198
@ Override
@@ -211,7 +221,7 @@ public void run() {
211
221
@ Test
212
222
@ TestForIssue (jiraKey = "HV-707" )
213
223
public void shouldReturnDefaultExecutableTypesForValidationXmlWithoutTypesGiven () {
214
- runWithCustomValidationXml (
224
+ validationXmlTestHelper . runWithCustomValidationXml (
215
225
"bv-1.0-validation.xml" , new Runnable () {
216
226
217
227
@ Override
@@ -248,7 +258,7 @@ public void shouldReturnDefaultExecutableTypesIfNoValidationXmlIsGiven() {
248
258
249
259
@ Test
250
260
public void testLoadingOfBv10ValidationXml () {
251
- runWithCustomValidationXml (
261
+ validationXmlTestHelper . runWithCustomValidationXml (
252
262
"bv-1.0-validation.xml" , new Runnable () {
253
263
254
264
@ Override
@@ -273,7 +283,7 @@ public void run() {
273
283
expectedExceptionsMessageRegExp = "HV000122: Unsupported schema version for META-INF/validation.xml: 2\\ .0\\ ."
274
284
)
275
285
public void shouldFailToLoadValidationXmlWithUnsupportedVersion () {
276
- runWithCustomValidationXml (
286
+ validationXmlTestHelper . runWithCustomValidationXml (
277
287
"unsupported-validation.xml" , new Runnable () {
278
288
279
289
@ Override
@@ -289,7 +299,7 @@ public void run() {
289
299
expectedExceptionsMessageRegExp = "HV000100: Unable to parse META-INF/validation.xml."
290
300
)
291
301
public void shouldFailToLoad10ValidationXmlWithParameterNameProvider () {
292
- runWithCustomValidationXml (
302
+ validationXmlTestHelper . runWithCustomValidationXml (
293
303
"invalid-bv-1.0-validation.xml" , new Runnable () {
294
304
295
305
@ Override
@@ -316,34 +326,4 @@ public void testCascadedValidation() {
316
326
assertEquals ( violations .size (), 1 );
317
327
assertCorrectConstraintTypes ( violations , NotNull .class );
318
328
}
319
-
320
- /**
321
- * Executes the given runnable, using the specified file as replacement for
322
- * {@code META-INF/validation.xml}.
323
- *
324
- * @param validationXmlName The file to be used as validation.xml file.
325
- * @param runnable The runnable to execute.
326
- */
327
- private void runWithCustomValidationXml (final String validationXmlName , Runnable runnable ) {
328
- ClassLoader previousContextCl = Thread .currentThread ().getContextClassLoader ();
329
-
330
- try {
331
- Thread .currentThread ().setContextClassLoader (
332
- new ClassLoader ( previousContextCl ) {
333
- @ Override
334
- public InputStream getResourceAsStream (String name ) {
335
- if ( name .equals ( "META-INF/validation.xml" ) ) {
336
- return XmlMappingTest .class .getResourceAsStream ( validationXmlName );
337
- }
338
-
339
- return super .getResourceAsStream ( name );
340
- }
341
- }
342
- );
343
- runnable .run ();
344
- }
345
- finally {
346
- Thread .currentThread ().setContextClassLoader ( previousContextCl );
347
- }
348
- }
349
329
}
0 commit comments