Example usage for java.lang AssertionError AssertionError

List of usage examples for java.lang AssertionError AssertionError

Introduction

In this page you can find the example usage for java.lang AssertionError AssertionError.

Prototype

public AssertionError(double detailMessage) 

Source Link

Document

Constructs an AssertionError with its detail message derived from the specified double, which is converted to a string as defined in section 15.18.1.1 of The Java™ Language Specification.

Usage

From source file:Main.java

public static Map<String, List<String>> getQueryParams(String url) {
    try {/*from  w  w w . j  a va 2s .co  m*/
        Map<String, List<String>> params = new HashMap<String, List<String>>();
        String[] urlParts = url.split("\\?");
        if (urlParts.length > 1) {
            String query = urlParts[1];
            for (String param : query.split("&")) {
                String[] pair = param.split("=");
                String key = URLDecoder.decode(pair[0], "UTF-8");
                String value = "";
                if (pair.length > 1) {
                    value = URLDecoder.decode(pair[1], "UTF-8");
                }

                List<String> values = params.get(key);
                if (values == null) {
                    values = new ArrayList<String>();
                    params.put(key, values);
                }
                values.add(value);
            }
        }

        return params;
    } catch (UnsupportedEncodingException ex) {
        throw new AssertionError(ex);
    }
}

From source file:Main.java

public static void awaitThreadState(Thread thread, long maxWaitMillis, Thread.State first,
        Thread.State... rest) {
    EnumSet<Thread.State> set = EnumSet.of(first, rest);
    long deadline = maxWaitMillis + System.currentTimeMillis();
    Thread.State currentState;
    do {/*  ww  w.j av a  2 s  .  c om*/
        currentState = thread.getState();
        if (System.currentTimeMillis() > deadline) {
            throw new AssertionError("Timed out waiting for thread state of <" + set + ">: " + thread
                    + " (state = " + thread.getState() + ")");
        }
    } while (!set.contains(currentState));
}

From source file:Main.java

/** Returns a 32 character string containing a hash of {@code s}. */
public static String hash(String s) {
    try {//w ww .  ja  va  2s.  c  o  m
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] md5bytes = messageDigest.digest(s.getBytes("UTF-8"));
        return bytesToHexString(md5bytes);
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    } catch (UnsupportedEncodingException e) {
        throw new AssertionError(e);
    }
}

From source file:Main.java

/**
 * Start an intent normally. Assert that the intent can't be opened inside this app.
 *///from   w  w  w . ja  va 2s  .  c o m
public static void startActivityOutsideApp(Context context, Intent intent) {
    final boolean isPlatformDebugBuild = Build.TYPE.equals("eng") || Build.TYPE.equals("userdebug");
    if (isPlatformDebugBuild) {
        if (getIntentInAppIfExists(context, intent) != null) {
            throw new AssertionError("startActivityOutsideApp() was called for an intent"
                    + " that can be handled inside the app");
        }
    }
    context.startActivity(intent);
}

From source file:Main.java

/**
 * Helper to get the Activity result code.    This must only be called on the main thread.
 *
 * @param activity Activity whose result code is to be obtained.
 * @return Result code of the Activity./*from  w  ww.ja v  a2  s  .c  o m*/
 */
public static int getActivityResultCode(@NonNull final Activity activity) {
    if (activity == null) {
        throw new IllegalArgumentException("activity cannot be null"); //$NON-NLS-1$
    }

    if (Looper.getMainLooper() != Looper.myLooper()) {
        throw new AssertionError("Must only be called on the main thread");
    }

    /*
     * This is a hack to obtain the Activity result code. There is no official way to check this using the Android
     * testing frameworks, so accessing the internals of the Activity object is the only way. This could
     * break on newer versions of Android.
     */

    try {
        final Field resultCodeField = Activity.class.getDeclaredField("mResultCode"); //$NON-NLS-1$
        resultCodeField.setAccessible(true);
        return ((Integer) resultCodeField.get(activity)).intValue();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void closeAll(Closeable a, Closeable b) throws IOException {
    Throwable thrown = null;/*from  w w w  .  j  a  v a  2  s .co  m*/
    try {
        a.close();
    } catch (Throwable e) {
        thrown = e;
    }
    try {
        b.close();
    } catch (Throwable e) {
        if (thrown == null)
            thrown = e;
    }
    if (thrown == null)
        return;
    if (thrown instanceof IOException)
        throw (IOException) thrown;
    if (thrown instanceof RuntimeException)
        throw (RuntimeException) thrown;
    if (thrown instanceof Error)
        throw (Error) thrown;
    throw new AssertionError(thrown);
}

From source file:Main.java

/**
 * Helper to get the Activity result Intent.  This must only be called on the main thread.
 *
 * @param activity Activity whose result Intent is to be obtained.
 * @return Result Intent of the Activity.
 *//*from ww w.j a  v a2 s.c  o  m*/
@Nullable
public static Intent getActivityResultData(@NonNull final Activity activity) {
    if (activity == null) {
        throw new IllegalArgumentException("activity cannot be null"); //$NON-NLS-1$
    }

    if (Looper.getMainLooper() != Looper.myLooper()) {
        throw new AssertionError("Must only be called on the main thread");
    }

    /*
     * This is a hack to obtain the Activity result data. There is no official way to check this using the Android
     * testing frameworks, so accessing the internals of the Activity object is the only way. This could
     * break on newer versions of Android.
     */

    try {
        final Field resultIntentField = Activity.class.getDeclaredField("mResultData"); //$NON-NLS-1$
        resultIntentField.setAccessible(true);
        return ((Intent) resultIntentField.get(activity));
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.sonar.sslr.test.minic.MiniCParser.java

public static AstNode parseFile(String filePath) {
    File file = FileUtils.toFile(MiniCParser.class.getResource(filePath));
    if (file == null || !file.exists()) {
        throw new AssertionError("The file \"" + filePath + "\" does not exist.");
    }/*from   ww w .  j  a  va  2  s .c o  m*/

    return P.parse(file);
}

From source file:Main.java

/**
 * Checks whether the expected XML element is contained in the actual
 * element. I.e., every attribute of the expected element must occur in the
 * actual element, with the same value, and all child nodes in the expected
 * element must occur in the actual element; if multiple child nodes with
 * the same name are found in the expected element, the first found
 * identifying attribute in the expected child element is used to identify
 * the child in the actual element./*from w ww  .  j  a v a 2  s.co  m*/
 * 
 * @param expected
 * @param actual
 * @return
 */
public static void assertContains(Element expected, Element actual, String messagePrefix,
        String... identifyingAttributes) {
    if (!expected.getTagName().equals(actual.getTagName())) {
        throw new AssertionError(messagePrefix + "\nExpected tagname differs from actual tagname (expected '"
                + printNodeOnly(expected) + "', found " + printNodeOnly(actual) + ")");
    }

    // Compare attributes
    NamedNodeMap expectedAttributes = expected.getAttributes();
    for (int i = expectedAttributes.getLength() - 1; i >= 0; i--) {
        String expectedAttributeName = expectedAttributes.item(i).getNodeName();
        String expectedAttributeValue = expectedAttributes.item(i).getNodeValue();

        String actualValue = actual.getAttribute(expectedAttributeName);
        if (actualValue == null || actualValue.isEmpty()) {
            throw new AssertionError(messagePrefix + "\nThe attribute '" + expectedAttributeName
                    + "' with value '" + expectedAttributeValue + "' was not found in the result "
                    + printNodeOnly(actual));
        }
        if (!expectedAttributeValue.equals(actualValue)) {
            throw new AssertionError(messagePrefix + "\nThe attribute '" + expectedAttributeName
                    + "' has value '" + actualValue + "' instead of '" + expectedAttributeValue
                    + "' in the result " + printNodeOnly(actual));
        }
    }

    // Compare child elements
    Node child = expected.getFirstChild();
    while (child != null) {
        if (child instanceof Element) {
            assertContainsChildElement((Element) child, actual, messagePrefix, identifyingAttributes);
        }
        child = child.getNextSibling();
    }
}

From source file:Main.java

public static void runOnMainSync(final @NonNull Runnable runnable) {
    if (isMainThread()) {
        runnable.run();//w w w . j  av  a2 s . c om
    } else {
        final CountDownLatch sync = new CountDownLatch(1);
        runOnMain(new Runnable() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } finally {
                    sync.countDown();
                }
            }
        });
        try {
            sync.await();
        } catch (InterruptedException ie) {
            throw new AssertionError(ie);
        }
    }
}