Example usage for junit.framework AssertionFailedError initCause

List of usage examples for junit.framework AssertionFailedError initCause

Introduction

In this page you can find the example usage for junit.framework AssertionFailedError initCause.

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:com.scvngr.levelup.ui.activity.TestIntentActivity.java

/**
 * Waits for {@link #onActivityResult} to be called, then returns the result.
 *
 * @return the result that was delivered from the other activity.
 * @throws AssertionFailedError if the callback isn't called or is interrupted.
 *///  w  w w .j ava 2 s .co m
@Nullable
public final Instrumentation.ActivityResult waitForActivityResult() throws AssertionFailedError {
    try {
        if (!mOnActivityResultLatch.await(RESULT_WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
            throw new AssertionFailedError("onActivityResult was not called.");
        }
    } catch (final InterruptedException e) {
        final AssertionFailedError assertionFailed = new AssertionFailedError("Latch interrupted.");
        assertionFailed.initCause(e);

        throw assertionFailed;
    }

    return mActivityResult;
}

From source file:org.eclipse.mylyn.commons.sdk.util.CommonTestUtil.java

public static CertificateCredentials getCertificateCredentials() {
    File keyStoreFile;//from  ww w.jav a2 s  . c  om
    try {
        keyStoreFile = CommonTestUtil.getFile(CommonTestUtil.class, "testdata/keystore");
        String password = CommonTestUtil.getUserCredentials().getPassword();
        return new CertificateCredentials(keyStoreFile.getAbsolutePath(), password, null);
    } catch (IOException cause) {
        AssertionFailedError e = new AssertionFailedError("Failed to load keystore file");
        e.initCause(cause);
        throw e;
    }
}

From source file:org.eclipse.mylyn.commons.sdk.util.CommonTestUtil.java

public static UserCredentials getCredentials(PrivilegeLevel level, String realm) {
    Properties properties = new Properties();
    try {//from  w  w w  . j a v  a2s  . c  om
        File file;
        String filename = System.getProperty(KEY_CREDENTIALS_FILE);
        if (filename != null) {
            // 1. use user specified file
            file = new File(filename);
        } else {
            // 2. check in home directory
            file = new File(new File(System.getProperty("user.home"), ".mylyn"), "credentials.properties");
            if (!file.exists()) {
                // 3. fall back to included credentials file
                file = getFile(CommonTestUtil.class, "testdata/credentials.properties");
            }
        }
        properties.load(new FileInputStream(file));
    } catch (Exception e) {
        AssertionFailedError error = new AssertionFailedError(
                "must define credentials in $HOME/.mylyn/credentials.properties");
        error.initCause(e);
        throw error;
    }

    String defaultPassword = properties.getProperty("pass");

    realm = (realm != null) ? realm + "." : "";
    switch (level) {
    case ANONYMOUS:
        return createCredentials(properties, realm + "anon.", "", "");
    case GUEST:
        return createCredentials(properties, realm + "guest.", "guest@mylyn.eclipse.org", defaultPassword);
    case USER:
        return createCredentials(properties, realm, "tests@mylyn.eclipse.org", defaultPassword);
    case READ_ONLY:
        return createCredentials(properties, realm, "read-only@mylyn.eclipse.org", defaultPassword);
    case ADMIN:
        return createCredentials(properties, realm + "admin.", "admin@mylyn.eclipse.org", null);
    }

    throw new AssertionFailedError("invalid privilege level");
}

From source file:org.eclipse.mylyn.commons.sdk.util.CommonTestUtil.java

public static File getFile(Object source, String filename) throws IOException {
    Class<?> clazz = (source instanceof Class<?>) ? (Class<?>) source : source.getClass();
    if (Platform.isRunning()) {
        ClassLoader classLoader = clazz.getClassLoader();
        try {//  ww w . j a  va2 s  .  c  o  m
            if (isOsgiVersion310orNewer(classLoader)) {
                return checkNotNull(getFileFromClassLoader4Luna(filename, classLoader));
            } else {
                return checkNotNull(getFileFromClassLoaderBeforeLuna(filename, classLoader));
            }
        } catch (Exception e) {
            AssertionFailedError exception = new AssertionFailedError(
                    NLS.bind("Could not locate {0} using classloader for {1}", filename, clazz));
            exception.initCause(e);
            throw exception;
        }
    } else {
        return getFileFromNotRunningPlatform(filename, clazz);
    }
}

From source file:org.intermine.modelviewer.jaxb.ConfigParserTest.java

/**
 * Test whether the model read from <code>src/test/(no)schema/core.xml</code>
 * is correct./*from w w  w  .  jav a 2 s .co m*/
 * 
 * @param model The core Model.
 * 
 * @param sourceFile The source file.
 */
private void coreCorrect(Model model, File sourceFile) {

    try {
        assertEquals("Model name wrong", "genomic", model.getName());
        assertEquals("Package wrong", "org.intermine.model.bio", model.getPackage());

        assertEquals("Wrong number of classes", 34, model.getClazz().size());

        Map<String, Class> classMap = new HashMap<String, Class>();
        for (Class c : model.getClazz()) {
            classMap.put(c.getName(), c);
        }

        Class relation = classMap.get("Relation");
        assertNotNull("Class 'Relation' not found", relation);

        assertEquals("Relation extends wrong", "SymmetricalRelation", relation.getExtends());
        assertTrue("Relation interface wrong", relation.isIsInterface());
        assertEquals("Relation should have no attributes", 0, relation.getAttribute().size());

        assertNotNull("Relation should have 2 references (list unset)", relation.getReference());
        assertEquals("Relation should have 2 references", 2, relation.getReference().size());
        ClassReference ref = relation.getReference().get(0);
        assertEquals("Reference name wrong", "subject", ref.getName());
        assertEquals("Reference type wrong", "BioEntity", ref.getReferencedType());
        assertEquals("Reference reverse wrong", "objects", ref.getReverseReference());

        assertNotNull("Relation should have 2 collections (list unset)", relation.getCollection());
        assertEquals("Relation should have 2 collections", 2, relation.getCollection().size());
        ClassReference col = relation.getCollection().get(0);
        assertEquals("Collection name wrong", "evidence", col.getName());
        assertEquals("Collection type wrong", "Evidence", col.getReferencedType());
        assertEquals("Collection reverse wrong", "relations", col.getReverseReference());

        Class comment = classMap.get("Comment");
        assertNotNull("Class 'Comment' not found", comment);

        assertNull("Comment extends wrong", comment.getExtends());
        assertTrue("Comment interface wrong", comment.isIsInterface());

        assertEquals("Comment should have 2 attributes", 2, comment.getAttribute().size());
        Attribute att = comment.getAttribute().get(0);
        assertEquals("Attribute name wrong", "text", att.getName());
        assertEquals("Attribute type wrong", String.class.getName(), att.getType());

        assertNotNull("Comment should have 1 reference (list unset)", comment.getReference());
        assertEquals("Comment should have 1 reference", 1, comment.getReference().size());
        ref = comment.getReference().get(0);
        assertEquals("Reference name wrong", "source", ref.getName());
        assertEquals("Reference type wrong", "InfoSource", ref.getReferencedType());
        assertNull("Reference reverse wrong", ref.getReverseReference());

        assertEquals("Comment should have 0 collections", 0, comment.getCollection().size());

    } catch (AssertionFailedError e) {
        AssertionFailedError addition = new AssertionFailedError(
                "Failure with file " + sourceFile.getAbsolutePath() + " :\n" + e.getMessage());
        addition.initCause(e.getCause());
        addition.setStackTrace(e.getStackTrace());
        throw addition;
    }
}

From source file:org.intermine.modelviewer.jaxb.ConfigParserTest.java

/**
 * Test whether the model read from <code>src/test/(no)schema/genomic_additions.xml</code>
 * is correct.//from  w w  w.  j  a v a  2 s.  c o  m
 * 
 * @param classes The top level Classes object.
 * 
 * @param sourceFile The source file.
 */
private void genomicAdditionsCorrect(Classes classes, File sourceFile) {
    try {

        assertEquals("Wrong number of classes", 23, classes.getClazz().size());

        Map<String, Class> classMap = new HashMap<String, Class>();
        for (Class c : classes.getClazz()) {
            classMap.put(c.getName(), c);
        }

        Class transcript = classMap.get("Transcript");
        assertNotNull("Class 'Transcript' not found", transcript);

        assertNull("Transcript extends wrong", transcript.getExtends());
        assertTrue("Transcript interface wrong", transcript.isIsInterface());

        assertEquals("Transcript should have 1 attribute", 1, transcript.getAttribute().size());
        Attribute att = transcript.getAttribute().get(0);
        assertEquals("Attribute name wrong", "exonCount", att.getName());
        assertEquals("Attribute type wrong", Integer.class.getName(), att.getType());

        assertEquals("Transcript should have 1 reference", 1, transcript.getReference().size());
        ClassReference ref = transcript.getReference().get(0);
        assertEquals("Reference name wrong", "protein", ref.getName());
        assertEquals("Reference type wrong", "Protein", ref.getReferencedType());
        assertEquals("Reference reverse wrong", "transcripts", ref.getReverseReference());

        assertNotNull("Transcript should have 2 collections (list unset)", transcript.getCollection());
        assertEquals("Transcript should have 2 collections", 2, transcript.getCollection().size());
        ClassReference col = transcript.getCollection().get(0);
        assertEquals("Collection name wrong", "introns", col.getName());
        assertEquals("Collection type wrong", "Intron", col.getReferencedType());
        assertEquals("Collection reverse wrong", "transcripts", col.getReverseReference());

    } catch (AssertionFailedError e) {
        AssertionFailedError addition = new AssertionFailedError(
                "Failure with file " + sourceFile.getAbsolutePath() + " :\n" + e.getMessage());
        addition.initCause(e.getCause());
        addition.setStackTrace(e.getStackTrace());
        throw addition;
    }
}

From source file:org.intermine.modelviewer.jaxb.ConfigParserTest.java

/**
 * Test whether the project read from <code>src/test/(no)schema/project.xml</code>
 * is correct./*from www  .  j a  v  a 2s  .c  o  m*/
 * 
 * @param project The Project.
 * 
 * @param sourceFile The source file.
 */
private void projectCorrect(Project project, File sourceFile) {

    try {
        assertEquals("Project type wrong", "bio", project.getType());

        assertEquals("Wrong number of project properties", 6, project.getProperty().size());

        // Ignore duplicate source.location
        Map<String, Property> propMap = new HashMap<String, Property>();
        for (Property p : project.getProperty()) {
            propMap.put(p.getName(), p);
        }

        Property propsFile = propMap.get("default.intermine.properties.file");
        assertNotNull("Property 'default.intermine.properties.file' missing", propsFile);
        assertEquals("'default.intermine.properties.file' location wrong",
                "../default.intermine.integrate.properties", propsFile.getLocation());
        assertNull("'default.intermine.properties.file' value set", propsFile.getValue());

        Property targetModel = propMap.get("target.model");
        assertNotNull("Property 'target.model' missing", targetModel);
        assertEquals("'target.model' value wrong", "genomic", targetModel.getValue());
        assertNull("'target.model' location set", targetModel.getLocation());

        assertEquals("Wrong number of project sources", 8, project.getSources().getSource().size());

        Map<String, Source> sourceMap = new HashMap<String, Source>();
        for (Source s : project.getSources().getSource()) {
            sourceMap.put(s.getName(), s);
        }

        Source chromoFasta = sourceMap.get("malaria-chromosome-fasta");
        assertNotNull("Source 'malaria-chromosome-fasta' missing", chromoFasta);
        assertEquals("'malaria-chromosome-fasta' type wrong", "fasta", chromoFasta.getType());
        assertEquals("'malaria-chromosome-fasta' dump wrong", Boolean.TRUE, chromoFasta.isDump());

        assertEquals("'malaria-chromosome-fasta' source has wrong number of properties", 6,
                chromoFasta.getProperty().size());

        propMap.clear();
        for (Property p : chromoFasta.getProperty()) {
            propMap.put(p.getName(), p);
        }

        Property srcDataDir = propMap.get("src.data.dir");
        assertNotNull("Property 'src.data.dir' missing from source 'malaria-chromosome-fasta'", srcDataDir);
        assertEquals("'src.data.dir' location wrong", "/home/richard/malaria/genome/fasta",
                srcDataDir.getLocation());
        assertNull("'src.data.dir' value set", srcDataDir.getValue());

        Property fastaTitle = propMap.get("fasta.dataSourceName");
        assertNotNull("Property 'fasta.dataSourceName' missing from source " + "'malaria-chromosome-fasta'",
                fastaTitle);
        assertEquals("'fasta.dataSourceName' value wrong", "PlasmoDB", fastaTitle.getValue());
        assertNull("'fasta.dataSourceName' location set", fastaTitle.getLocation());

        Source gff = sourceMap.get("malaria-gff");
        assertNotNull("Source 'malaria-gff' missing", gff);
        assertEquals("'malaria-gff' type wrong", "malaria-gff", gff.getType());
        assertEquals("'malaria-gff' dump wrong", Boolean.FALSE, gff.isDump());

        assertEquals("Wrong number of post processors", 5, project.getPostProcessing().getPostProcess().size());

        Map<String, PostProcess> postProcessMap = new HashMap<String, PostProcess>();
        for (PostProcess pp : project.getPostProcessing().getPostProcess()) {
            postProcessMap.put(pp.getName(), pp);
        }

        PostProcess transfer = postProcessMap.get("transfer-sequences");
        assertNotNull("Post processor 'transfer-sequences' missing", transfer);
        assertEquals("'transfer-sequences' dump flag wrong", Boolean.TRUE, transfer.isDump());

        PostProcess doSources = postProcessMap.get("do-sources");
        assertNotNull("Post processor 'do-sources' missing", doSources);
        assertEquals("'do-sources' dump flag wrong", Boolean.FALSE, doSources.isDump());

    } catch (AssertionFailedError e) {
        AssertionFailedError addition = new AssertionFailedError(
                "Failure with file " + sourceFile.getAbsolutePath() + " :\n" + e.getMessage());
        addition.initCause(e.getCause());
        addition.setStackTrace(e.getStackTrace());
        throw addition;
    }
}

From source file:org.smartfrog.test.SmartFrogTestManager.java

/**
 * assert that a string contains a substring
 *
 * @param source     source to scan//from w ww  . j  av a2 s. c o m
 * @param substring  string to look for
 * @param resultMessage configuration description
 * @param exception  any exception to look at can be null
 */
public void assertContains(String source, String substring, String resultMessage, Throwable exception) {
    assertNotNull("No string to look for [" + substring + "]", source);
    assertNotNull("No substring ", substring);
    final boolean contained = source.contains(substring);

    if (!contained) {
        String message = "- Did not find \n[" + substring + "] \nin \n[" + source + ']'
                + (resultMessage != null ? ("\n, Result:\n" + resultMessage) : "");
        if (exception != null) {
            log.error(message, exception);
        } else {
            log.error(message);
        }
        AssertionFailedError error = new AssertionFailedError(message);
        if (exception != null) {
            error.initCause(exception);
        }
        throw error;
    }
}

From source file:org.smartfrog.test.SmartFrogTestManager.java

/**
 * Fail if a condition is not met; the message raised includes the message and the string value of the event.
 *
 * @param test    condition to evaluate//from  w ww . j  av a 2 s .  c  om
 * @param message message to print
 * @param event   related event
 * @throws AssertionFailedError if the condition is true
 */
public void conditionalFail(boolean test, String message, String eventHistory, LifecycleEvent event) {
    if (test) {
        AssertionFailedError afe = new AssertionFailedError(message + "\nEvent is " + event
                + (eventHistory != null ? ("\nHistory:\n" + eventHistory) : ""));
        TerminationRecord tr = event.getStatus();
        if (tr != null) {
            afe.initCause(tr.getCause());
        }
        throw afe;
    }
}

From source file:org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests.java

/**
 * Runs a test via the supplied {@link TestExecutionCallback}, providing
 * support for the {@link ExpectedException &#064;ExpectedException} and
 * {@link Repeat &#064;Repeat} annotations.
 *
 * @param tec the test execution callback to run
 * @param testMethod the actual test method: used to retrieve the
 * {@link ExpectedException &#064;ExpectedException} and {@link Repeat
 * &#064;Repeat} annotations/*from  w  ww .  j a  va  2s .  com*/
 * @throws Throwable if any exception is thrown
 * @see ExpectedException
 * @see Repeat
 */
private void runTest(TestExecutionCallback tec, Method testMethod) throws Throwable {
    ExpectedException expectedExceptionAnnotation = testMethod.getAnnotation(ExpectedException.class);
    boolean exceptionIsExpected = (expectedExceptionAnnotation != null
            && expectedExceptionAnnotation.value() != null);
    Class<? extends Throwable> expectedException = (exceptionIsExpected ? expectedExceptionAnnotation.value()
            : null);

    Repeat repeat = testMethod.getAnnotation(Repeat.class);
    int runs = ((repeat != null) && (repeat.value() > 1)) ? repeat.value() : 1;

    for (int i = 0; i < runs; i++) {
        try {
            if (runs > 1 && this.logger.isInfoEnabled()) {
                this.logger.info("Repetition " + (i + 1) + " of test " + testMethod.getName());
            }
            tec.run();
            if (exceptionIsExpected) {
                fail("Expected exception: " + expectedException.getName());
            }
        } catch (Throwable ex) {
            if (!exceptionIsExpected) {
                throw ex;
            }
            if (!expectedException.isAssignableFrom(ex.getClass())) {
                // Wrap the unexpected throwable with an explicit message.
                AssertionFailedError assertionError = new AssertionFailedError(
                        "Unexpected exception, expected <" + expectedException.getName() + "> but was <"
                                + ex.getClass().getName() + ">");
                assertionError.initCause(ex);
                throw assertionError;
            }
        }
    }
}