### Question:
Validation { public static ValidatorFactory buildDefaultValidatorFactory() { return byDefaultProvider().configure().buildValidatorFactory(); }  static ValidatorFactory buildDefaultValidatorFactory(); static GenericBootstrap byDefaultProvider(); static ProviderSpecificBootstrap<T> byProvider(Class<U> providerType);  }### Answer:
@Test public void testCurrentClassLoaderIsUsedInCaseContextClassLoaderCannotLoadServiceFile() { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader dummyClassLoader = new URLClassLoader( new URL[] { }, null ); Thread.currentThread().setContextClassLoader( dummyClassLoader ); try { Validation.buildDefaultValidatorFactory(); fail(); } catch ( ValidationException e ) { assertEquals( e.getMessage(), "Unable to load Bean Validation provider" ); } finally { Thread.currentThread().setContextClassLoader( contextClassLoader ); } }
@Test public void testCurrentClassLoaderIsUsedInCaseContextClassLoaderIsNull() { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader( null ); try { Validation.buildDefaultValidatorFactory(); fail(); } catch ( ValidationException e ) { assertEquals( e.getMessage(), "Unable to load Bean Validation provider" ); } finally { Thread.currentThread().setContextClassLoader( contextClassLoader ); } }
@Test public void testCachedProvidersCanBeGarbageCollected() { int LOOP_COUNT = 100; ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); try { for ( int i = 1; i <= LOOP_COUNT; i++ ) { Thread.currentThread().setContextClassLoader( new CustomValidationXmlClassLoader( "-2" ) ); Validation.buildDefaultValidatorFactory(); } int createdProviders = countInMemoryProviders(); assertTrue( createdProviders > 1, "There should be cached providers" ); try { byte[][] buf = new byte[1024][]; for ( int i = 0; i < buf.length; i++ ) { buf[i] = new byte[10 * 1024 * 1024]; } fail( "The byte array allocation should have triggered a OutOfMemoryError" ); } catch ( OutOfMemoryError ex ) { } assertEquals( countInMemoryProviders(), 0 ); } finally { Thread.currentThread().setContextClassLoader( contextClassLoader ); } }
@Test public void testMultipleProvidersWithOneUnknownProviderThrowsException() { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader( new CustomValidationXmlClassLoader( "-2", "-3" ) ); try { Validation.buildDefaultValidatorFactory(); fail(); } catch ( ValidationException e ) { assertEquals( e.getMessage(), "Unable to load Bean Validation provider" ); } finally { Thread.currentThread().setContextClassLoader( contextClassLoader ); } }