Example usage for junit.framework TestCase TestCase

List of usage examples for junit.framework TestCase TestCase

Introduction

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

Prototype

TestCase

Source Link

Usage

From source file:io.sightly.tck.tests.TestBuilder.java

private static TestCase buildTestCase(final Client client, final int expectedStatusCode, final String url,
        final String suite, final String groupName, final String method, final JSONObject testCase,
        final String expectedMarkupPath) {

    final String selector = testCase.getString(JSON_CASE_SELECTOR);

    return new TestCase() {
        @Override//from  w w w .j a v  a  2  s  .co m
        public String getName() {
            return suite + ": " + groupName + " - " + selector;
        }

        @Override
        protected void runTest() throws Throwable {
            if (!content.containsKey(url)) {
                content.put(url, client.getStringContent(url, expectedStatusCode));
            }
            String overriddenMethod = method;
            if (testCase.has(JSON_METHOD)) {
                overriddenMethod = testCase.getString(JSON_METHOD);
            }
            String output = content.get(url);
            if ("innerHTMLEquals".equals(overriddenMethod)) {
                String value = HTMLExtractor.innerHTML(url, output, selector);
                String expectedValue = testCase.getString(JSON_CASE_VALUE);
                assertTrue(String.format(
                        "Expected to find an element matching selector '%s'. Please check the expected markup "
                                + "from %s.",
                        selector, expectedMarkupPath), HTMLExtractor.exists(url, output, selector));
                assertTrue(String.format(
                        "Expected value '%s' for selector '%s'. Instead we got '%s'. Please check the expected markup from %s"
                                + ".",
                        expectedValue, selector, value, expectedMarkupPath), expectedValue.equals(value));
            } else if ("contains".equals(overriddenMethod)) {
                String expectedValue = testCase.getString(JSON_CASE_VALUE);
                boolean contains = HTMLExtractor.contains(url, output, selector, expectedValue);
                assertTrue(String.format(
                        "Expected to find an element matching selector '%s'. Please check the expected markup "
                                + "from %s.",
                        selector, expectedMarkupPath), HTMLExtractor.exists(url, output, selector));
                assertTrue(String.format(
                        "Missing content for selector '%s'. Please check the expected markup from %s.",
                        selector, expectedMarkupPath), contains);
            } else if ("exists".equals(overriddenMethod)) {
                boolean exists = true;
                if (testCase.has(JSON_CASE_POSITIVE)) {
                    exists = testCase.getBoolean(JSON_CASE_POSITIVE);
                }
                if (exists) {
                    assertTrue(String.format(
                            "Expected to find an element matching selector '%s'. Please check the expected markup "
                                    + "from %s.",
                            selector, expectedMarkupPath), HTMLExtractor.exists(url, output, selector));
                } else {
                    assertFalse(String.format(
                            "Did not expect to find an element matching selector '%s'. Please check the expected "
                                    + "markup from " + "%s.",
                            selector, expectedMarkupPath), HTMLExtractor.exists(url, output, selector));
                }
            } else if ("hasAttribute".equals(overriddenMethod)) {
                String attributeName = testCase.getString(JSON_CASE_ATTRIBUTE);
                String attributeValue = null;
                if (testCase.has(JSON_CASE_VALUE)) {
                    attributeValue = testCase.getString(JSON_CASE_VALUE);
                }
                boolean exists = true;
                if (testCase.has(JSON_CASE_POSITIVE)) {
                    exists = testCase.getBoolean(JSON_CASE_POSITIVE);
                }
                assertTrue(String.format(
                        "Expected to find an element matching selector '%s'. Please check the expected markup "
                                + "from %s.",
                        selector, expectedMarkupPath), HTMLExtractor.exists(url, output, selector));
                if (exists) {
                    assertTrue(String.format(
                            "Cannot find attribute '%s' on element matching selector '%s' or its actual value does "
                                    + "not match the expected value '%s'. Please check the expected markup from %s.",
                            attributeName, selector, attributeValue, expectedMarkupPath),
                            HTMLExtractor.hasAttribute(url, output, selector, exists, attributeName,
                                    attributeValue));
                } else {
                    assertFalse(String.format(
                            "Did not expect to find attribute '%s' on element matching selector '%s'. Please check "
                                    + "the expected markup from %s.",
                            attributeName, selector, expectedMarkupPath),
                            HTMLExtractor.hasAttribute(url, output, selector, exists, attributeName,
                                    attributeValue));
                }
            } else if ("hasChildren".equals(overriddenMethod)) {
                assertTrue(String.format(
                        "Expected to find an element matching selector '%s'. Please check the expected markup "
                                + "from %s.",
                        selector, expectedMarkupPath), HTMLExtractor.exists(url, output, selector));
                int expectedChildren = testCase.optInt(JSON_CASE_VALUE);
                assertTrue(String.format(
                        "Element matched by selector '%s' was expected to have %d children. Please check the "
                                + "expected markup from %s.",
                        selector, expectedChildren, expectedMarkupPath),
                        HTMLExtractor.hasChildren(url, output, selector, expectedChildren));
            } else {
                fail("Unknown test method: " + overriddenMethod);
            }
        }
    };
}