1   package groovy.xml;
2   
3   import junit.framework.TestCase;
4   
5   import javax.xml.parsers.ParserConfigurationException;
6   import java.security.PrivilegedExceptionAction;
7   import java.security.PrivilegedActionException;
8   
9   public class FactorySupportTest extends TestCase {
10      private static final PrivilegedActionException PRIVILEGED_ACTION_EXCEPTION = new PrivilegedActionException(new IllegalStateException());
11      private static final ParserConfigurationException PARSER_CONFIGURATION_EXCEPTION = new ParserConfigurationException();
12  
13      public void testCreatesFactories() throws Exception {
14          assertNotNull(FactorySupport.createDocumentBuilderFactory());
15          assertNotNull(FactorySupport.createSaxParserFactory());
16      }
17  
18      public void testParserConfigurationExceptionNotWrapped() throws ParserConfigurationException {
19          try {
20              FactorySupport.createFactory(new PrivilegedExceptionAction(){
21                  public Object run() throws Exception {
22                      throw PARSER_CONFIGURATION_EXCEPTION;
23                  }
24              });
25              fail("Exception was not caught");
26          } catch (Throwable t) {
27              assertSame(PARSER_CONFIGURATION_EXCEPTION, t);
28          }
29      }
30  
31      public void testOtherExceptionsWrappedAsUnchecked() throws ParserConfigurationException {
32          try {
33              FactorySupport.createFactory(new PrivilegedExceptionAction(){
34                  public Object run() throws Exception {
35                      throw PRIVILEGED_ACTION_EXCEPTION;
36                  }
37              });
38              fail("Exception was not caught");
39          } catch (RuntimeException re) {
40              assertSame(PRIVILEGED_ACTION_EXCEPTION, re.getCause());
41          } catch (Throwable t) {
42              fail("Exception was not wrapped as runtime");
43          }
44      }
45  }