Example usage for org.apache.commons.lang.exception ExceptionUtils getCause

List of usage examples for org.apache.commons.lang.exception ExceptionUtils getCause

Introduction

In this page you can find the example usage for org.apache.commons.lang.exception ExceptionUtils getCause.

Prototype

public static Throwable getCause(Throwable throwable) 

Source Link

Document

Introspects the Throwable to obtain the cause.

The method searches for methods with specific names that return a Throwable object.

Usage

From source file:com.switchfly.inputvalidation.canonicalizer.QueryStringCanonicalizerTest.java

@Test
public void testExecute() throws Exception {
    QueryStringCanonicalizer canonicalizer = new QueryStringCanonicalizer();

    assertEquals("\u00C1", canonicalizer.execute("A\u0301"));
    assertEquals("\u00F6", canonicalizer.execute("o\u0308"));
    assertEquals("\u00E1", canonicalizer.execute("a\u0301"));
    assertEquals(" ", canonicalizer.execute(" "));
    assertEquals("", canonicalizer.execute(""));
    assertEquals(" ", canonicalizer.execute("+"));
    assertEquals(null, canonicalizer.execute(null));
    assertEquals("<script", canonicalizer.execute("\u003Cscript"));
    assertEquals("&lt;script", canonicalizer.execute("&lt;script"));
    assertEquals("&#x3C;&#x73;&#x63;&#x72;&#x69;&#x70;&#x74;",
            canonicalizer.execute("&#x3C;&#x73;&#x63;&#x72;&#x69;&#x70;&#x74;"));
    assertEquals("<script", canonicalizer.execute("%3C%73%63%72%69%70%74"));

    try {/*  w  ww.j  a  v  a2 s. co  m*/
        canonicalizer.execute("data%3atext%2fht%%206dl%3bbase64%2cPHdoc2N%20oZWNrPg%3d%3d");
        fail("Should throw IllegalArgumentException");
    } catch (Exception e) {
        //noinspection ThrowableResultOfMethodCallIgnored
        assertTrue(ExceptionUtils.getCause(e) instanceof IllegalArgumentException);
    }
}

From source file:de.cosmocode.palava.core.lifecycle.LifecycleModuleTest.java

/**
 * Tests whether {@link Initializable}s get initialized.
 *//*from  www  .  j av  a  2 s.  c  o  m*/
@Test
public void initialize() {
    final Injector injector = Guice.createInjector(new LifecycleModule(), new DefaultRegistryModule());

    try {
        injector.getInstance(InitializableClass.class);
    } catch (ProvisionException e) {
        Assert.assertTrue(ExceptionUtils.getCause(e) instanceof SuccessException);
        return;
    }

    Assert.fail();
}

From source file:de.cosmocode.palava.core.lifecycle.LifecycleModuleTest.java

/**
 * Tests whether {@link AutoStartable}s get started.
 *//*from w  w w.j a  va2s.  c om*/
@Test
public void autostart() {
    final Injector injector = Guice.createInjector(new LifecycleModule(), new DefaultRegistryModule());

    try {
        injector.getInstance(AutoStartableClass.class);
    } catch (ProvisionException e) {
        Assert.assertTrue(ExceptionUtils.getCause(e) instanceof SuccessException);
        return;
    }

    Assert.fail();
}

From source file:net.sf.zekr.ui.QuranFormController.java

public void executeAction(MenuItem item, String name) {
    try {/*from   ww  w.  jav a  2s.  c o m*/
        Method method;
        try {
            method = getClass().getMethod(name, new Class<?>[0]);
            method.invoke(this);
        } catch (NoSuchMethodException nsme) {
            if (item != null) {
                method = getClass().getMethod(name, new Class<?>[] { boolean.class });
                method.invoke(this, item.getSelection());
            } else {
                throw nsme;
            }
        }
    } catch (Throwable th) {
        Throwable cause = ExceptionUtils.getCause(th);
        if (cause != null) {
            th = cause;
        }
        MessageBoxUtils.showError(String.format("Error running action: %s.\n%s", name, th));
        logger.error("Error calling action: " + name, th);
    }
}

From source file:net.sf.zekr.ui.QuranFormController.java

public ActionItem registerAction(final String name, final Class<?> parameterType, final Object methodParam) {
    Runnable runnable = new Runnable() {
        public void run() {
            try {
                Method method = QuranFormController.this.getClass().getMethod(name, parameterType);
                method.invoke(QuranFormController.this, methodParam);
            } catch (Throwable th) {
                Throwable cause = ExceptionUtils.getCause(th);
                if (cause != null) {
                    th = cause;//from   www . ja va 2  s .  co m
                }
                MessageBoxUtils.showError(String.format("Error running action: %s.\n%s", name, th));
                logger.error("Error calling action: " + name, th);
            }
        }
    };
    return new ActionItem(runnable, methodParam);
}

From source file:mitm.common.security.crlstore.hibernate.X509CRLStoreExtHibernateTest.java

@Test
public void testUniqueConstraint() throws Exception {
    // get CRLS does not have a @StartTransaction so auto commit should make no difference
    X509CRLStoreExt crlStore = new X509CRLStoreExtAutoCommitFactory(sessionSource, "test").create();

    try {/*  w ww  .  j  av a 2  s  . co  m*/
        addCRL("itrus.com.cn.crl", crlStore);
        addCRL("itrus.com.cn.crl", crlStore);

        fail();
    } catch (CRLStoreException e) {
        Throwable cause = ExceptionUtils.getCause(e);

        assertTrue(cause instanceof ConstraintViolationException);
    }
}

From source file:mitm.common.security.certpath.CertPathBuilderTest.java

@Test
public void testBuildPathCRLUnavailable() throws Exception {
    // add roots/*from w ww  .j  a v a  2s  . com*/
    addCertificates("windows-xp-all-roots.p7b", rootStoreParams.getCertStore());
    addCertificates("mitm-test-root.cer", rootStoreParams.getCertStore());

    addCertificates("windows-xp-all-intermediates.p7b", certStoreParams.getCertStore());
    addCertificates("mitm-test-ca.cer", certStoreParams.getCertStore());
    addCertificates("testCertificates.p7b", certStoreParams.getCertStore());

    trustAnchors = getTrustAnchors();

    X509CertSelector selector = new X509CertSelector();

    selector.setSerialNumber(BigIntegerUtils.hexDecode("115FD110A82F742D0AE14A71B651962"));
    selector.setIssuer("EMAILADDRESS=ca@example.com, CN=MITM Test CA, L=Amsterdam, ST=NH, C=NL");

    CertificatePathBuilder builder = new PKIXCertificatePathBuilder();

    builder.setTrustAnchors(trustAnchors);
    builder.addCertPathChecker(new SMIMEExtendedKeyUsageCertPathChecker());
    builder.addCertStore(certStore);
    builder.setRevocationEnabled(true);

    try {
        builder.buildPath(selector);

        fail();
    } catch (CertPathBuilderException e) {
        Throwable cause = ExceptionUtils.getCause(e);

        assertTrue(cause.getMessage().startsWith("No CRLs found"));
    }
}

From source file:mitm.common.security.certpath.CertPathBuilderTest.java

@Test
public void testBuildPathEndCertRevoked() throws Exception {
    // add roots//from  w ww. j av  a2 s.  co  m
    addCertificates("mitm-test-root.cer", rootStoreParams.getCertStore());

    addCertificates("mitm-test-ca.cer", certStoreParams.getCertStore());
    addCertificates("testCertificates.p7b", certStoreParams.getCertStore());

    addCRL("test-ca.crl", certStoreParams.getCRLStore());
    addCRL("test-root-ca-not-revoked.crl", certStoreParams.getCRLStore());

    trustAnchors = getTrustAnchors();

    X509CertSelector selector = new X509CertSelector();

    selector.setSerialNumber(BigIntegerUtils.hexDecode("115FCD741088707366E9727452C9770"));
    selector.setIssuer("EMAILADDRESS=ca@example.com, CN=MITM Test CA, L=Amsterdam, ST=NH, C=NL");

    CertificatePathBuilder builder = new PKIXCertificatePathBuilder();

    builder.setTrustAnchors(trustAnchors);
    builder.addCertPathChecker(new SMIMEExtendedKeyUsageCertPathChecker());
    builder.addCertStore(certStore);
    builder.setRevocationEnabled(true);

    try {
        builder.buildPath(selector);

        fail();
    } catch (CertPathBuilderException e) {
        // CertPathValidatorException should have been thrown because the certificate is revoked 
        Throwable cause = ExceptionUtils.getCause(e);

        assertTrue(cause.getMessage().startsWith("Certificate revocation after Fri Nov 30"));
        assertTrue(cause.getMessage().endsWith("2007, reason: privilegeWithdrawn"));
    }
}

From source file:mitm.common.security.certpath.CertPathBuilderTest.java

@Test
public void testBuildPathCACertRevoked() throws Exception {
    // add roots/*www  .  j a  v  a2s  .  com*/
    addCertificates("mitm-test-root.cer", rootStoreParams.getCertStore());

    addCertificates("mitm-test-ca.cer", certStoreParams.getCertStore());
    addCertificates("testCertificates.p7b", certStoreParams.getCertStore());

    addCRL("test-ca.crl", certStoreParams.getCRLStore());
    addCRL("test-root-ca-revoked.crl", certStoreParams.getCRLStore());

    trustAnchors = getTrustAnchors();

    X509CertSelector selector = new X509CertSelector();

    selector.setSerialNumber(BigIntegerUtils.hexDecode("115FD110A82F742D0AE14A71B651962"));
    selector.setIssuer("EMAILADDRESS=ca@example.com, CN=MITM Test CA, L=Amsterdam, ST=NH, C=NL");

    CertificatePathBuilder builder = new PKIXCertificatePathBuilder();

    builder.setTrustAnchors(trustAnchors);
    builder.addCertPathChecker(new SMIMEExtendedKeyUsageCertPathChecker());
    builder.addCertStore(certStore);
    builder.setRevocationEnabled(true);

    try {
        builder.buildPath(selector);

        fail();
    } catch (CertPathBuilderException e) {
        // CertPathValidatorException should have been thrown because the certificate has a 
        // key usage extension that is critical.
        Throwable cause = ExceptionUtils.getCause(e);

        assertTrue(cause.getMessage().startsWith("Certificate revocation after Fri Nov 30"));
        assertTrue(cause.getMessage().endsWith("2007, reason: cACompromise"));
    }
}

From source file:mitm.common.security.certpath.CertPathBuilderTest.java

@Test
public void testBuildPathEKUCriticalNoEmailProtection() throws Exception {
    // add roots//from   ww  w .j a v  a2s .  co  m
    addCertificates("mitm-test-root.cer", rootStoreParams.getCertStore());

    addCertificates("mitm-test-ca.cer", certStoreParams.getCertStore());
    addCertificates("testCertificates.p7b", certStoreParams.getCertStore());

    addCRL("test-ca.crl", certStoreParams.getCRLStore());
    addCRL("test-root-ca-not-revoked.crl", certStoreParams.getCRLStore());

    trustAnchors = getTrustAnchors();

    X509CertSelector selector = new X509CertSelector();

    selector.setSerialNumber(BigIntegerUtils.hexDecode("116A448F117FF69FE4F2D4D38F689D7"));
    selector.setIssuer("EMAILADDRESS=ca@example.com, CN=MITM Test CA, L=Amsterdam, ST=NH, C=NL");

    CertificatePathBuilder builder = new PKIXCertificatePathBuilder();

    builder.setTrustAnchors(trustAnchors);
    builder.addCertStore(certStore);
    builder.setRevocationEnabled(true);

    try {
        builder.buildPath(selector);

        fail();
    } catch (CertPathBuilderException e) {
        // CertPathValidatorException should have been thrown because the certificate has a 
        // key usage extension that is critical.
        Throwable cause = ExceptionUtils.getCause(e);

        assertTrue(cause instanceof CertPathValidatorException);
        assertNotNull(cause);
        assertEquals("Certificate has unsupported critical extension", cause.getMessage());
    }
}