Skip to content

Commit 997374a

Browse files
gunnarmorlinghferentschik
authored andcommitted
HV-955 Moving runWithCustomValidationXml() to separate helper class
1 parent d2838fe commit 997374a

File tree

2 files changed

+74
-35
lines changed

2 files changed

+74
-35
lines changed

engine/src/test/java/org/hibernate/validator/test/internal/xml/XmlMappingTest.java

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.ArrayList;
1212
import java.util.List;
1313
import java.util.Set;
14+
1415
import javax.validation.BootstrapConfiguration;
1516
import javax.validation.Configuration;
1617
import javax.validation.ConstraintViolation;
@@ -27,7 +28,9 @@
2728
import org.hibernate.validator.cfg.ConstraintMapping;
2829
import org.hibernate.validator.cfg.defs.SizeDef;
2930
import org.hibernate.validator.testutil.TestForIssue;
31+
import org.hibernate.validator.testutil.ValidationXmlTestHelper;
3032
import org.hibernate.validator.testutil.ValidatorUtil;
33+
import org.testng.annotations.BeforeClass;
3134
import org.testng.annotations.Test;
3235

3336
import static org.hibernate.validator.internal.util.CollectionHelper.asSet;
@@ -41,6 +44,13 @@
4144
*/
4245
public class XmlMappingTest {
4346

47+
private static ValidationXmlTestHelper validationXmlTestHelper;
48+
49+
@BeforeClass
50+
public static void setupValidationXmlTestHelper() {
51+
validationXmlTestHelper = new ValidationXmlTestHelper( XmlMappingTest.class );
52+
}
53+
4454
@Test
4555
@TestForIssue(jiraKey = "HV-214")
4656
public void testConstraintInheritanceWithXmlConfiguration() {
@@ -182,7 +192,7 @@ public void shouldFailToLoadConstraintMappingWithUnsupportedVersion() {
182192

183193
@Test
184194
public void testParameterNameProviderConfiguration() {
185-
runWithCustomValidationXml(
195+
validationXmlTestHelper.runWithCustomValidationXml(
186196
"parameter-name-provider-validation.xml", new Runnable() {
187197

188198
@Override
@@ -211,7 +221,7 @@ public void run() {
211221
@Test
212222
@TestForIssue(jiraKey = "HV-707")
213223
public void shouldReturnDefaultExecutableTypesForValidationXmlWithoutTypesGiven() {
214-
runWithCustomValidationXml(
224+
validationXmlTestHelper.runWithCustomValidationXml(
215225
"bv-1.0-validation.xml", new Runnable() {
216226

217227
@Override
@@ -248,7 +258,7 @@ public void shouldReturnDefaultExecutableTypesIfNoValidationXmlIsGiven() {
248258

249259
@Test
250260
public void testLoadingOfBv10ValidationXml() {
251-
runWithCustomValidationXml(
261+
validationXmlTestHelper.runWithCustomValidationXml(
252262
"bv-1.0-validation.xml", new Runnable() {
253263

254264
@Override
@@ -273,7 +283,7 @@ public void run() {
273283
expectedExceptionsMessageRegExp = "HV000122: Unsupported schema version for META-INF/validation.xml: 2\\.0\\."
274284
)
275285
public void shouldFailToLoadValidationXmlWithUnsupportedVersion() {
276-
runWithCustomValidationXml(
286+
validationXmlTestHelper.runWithCustomValidationXml(
277287
"unsupported-validation.xml", new Runnable() {
278288

279289
@Override
@@ -289,7 +299,7 @@ public void run() {
289299
expectedExceptionsMessageRegExp = "HV000100: Unable to parse META-INF/validation.xml."
290300
)
291301
public void shouldFailToLoad10ValidationXmlWithParameterNameProvider() {
292-
runWithCustomValidationXml(
302+
validationXmlTestHelper.runWithCustomValidationXml(
293303
"invalid-bv-1.0-validation.xml", new Runnable() {
294304

295305
@Override
@@ -316,34 +326,4 @@ public void testCascadedValidation() {
316326
assertEquals( violations.size(), 1 );
317327
assertCorrectConstraintTypes( violations, NotNull.class );
318328
}
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-
}
349329
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.testutil;
8+
9+
import java.io.InputStream;
10+
11+
/**
12+
* Helps tests with using a specific file as {@code META-INF/validation.xml}.
13+
*
14+
* @author Gunnar Morling
15+
*/
16+
public class ValidationXmlTestHelper {
17+
18+
private final Class<?> clazz;
19+
20+
/**
21+
* Creates a new {@code ValidationXmlTestHelper}.
22+
*
23+
* @param clazz
24+
* A class through which the specified {@code validation.xml} stand-in will be loaded.
25+
*/
26+
public ValidationXmlTestHelper(Class<?> clazz) {
27+
this.clazz = clazz;
28+
}
29+
30+
/**
31+
* Executes the given runnable, using the specified file as replacement for
32+
* {@code META-INF/validation.xml}.
33+
*
34+
* @param validationXmlName The file to be used as validation.xml file.
35+
* @param runnable The runnable to execute.
36+
*/
37+
public void runWithCustomValidationXml(final String validationXmlName, Runnable runnable) {
38+
ClassLoader previousContextCl = Thread.currentThread().getContextClassLoader();
39+
40+
try {
41+
Thread.currentThread().setContextClassLoader(
42+
new ClassLoader( previousContextCl ) {
43+
@Override
44+
public InputStream getResourceAsStream(String name) {
45+
if ( name.equals( "META-INF/validation.xml" ) ) {
46+
return clazz.getResourceAsStream( validationXmlName );
47+
}
48+
49+
return super.getResourceAsStream( name );
50+
}
51+
}
52+
);
53+
runnable.run();
54+
}
55+
finally {
56+
Thread.currentThread().setContextClassLoader( previousContextCl );
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)