Android example source code file (MoreAsserts.java)
This example Android source code file (MoreAsserts.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Android by Example" TM.
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
import com.google.android.collect.Lists;
import junit.framework.Assert;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Contains additional assertion methods not found in JUnit.
*/
public final class MoreAsserts {
private MoreAsserts() { }
/**
* Asserts that the class {@code expected} is assignable from the object
* {@code actual}. This verifies {@code expected} is a parent class or a
* interface that {@code actual} implements.
*/
public static void assertAssignableFrom(Class<?> expected, Object actual) {
assertAssignableFrom(expected, actual.getClass());
}
/**
* Asserts that class {@code expected} is assignable from the class
* {@code actual}. This verifies {@code expected} is a parent class or a
* interface that {@code actual} implements.
*/
public static void assertAssignableFrom(Class<?> expected, Class> actual) {
Assert.assertTrue(
"Expected " + expected.getCanonicalName() +
" to be assignable from actual class " + actual.getCanonicalName(),
expected.isAssignableFrom(actual));
}
/**
* Asserts that {@code actual} is not equal {@code unexpected}, according
* to both {@code ==} and {@link Object#equals}.
*/
public static void assertNotEqual(
String message, Object unexpected, Object actual) {
if (equal(unexpected, actual)) {
failEqual(message, unexpected);
}
}
/**
* Variant of {@link #assertNotEqual(String,Object,Object)} using a
* generic message.
*/
public static void assertNotEqual(Object unexpected, Object actual) {
assertNotEqual(null, unexpected, actual);
}
/**
* Asserts that array {@code actual} is the same size and every element equals
* those in array {@code expected}. On failure, message indicates specific
* element mismatch.
*/
public static void assertEquals(
String message, byte[] expected, byte[] actual) {
if (expected.length != actual.length) {
failWrongLength(message, expected.length, actual.length);
}
for (int i = 0; i < expected.length; i++) {
if (expected[i] != actual[i]) {
failWrongElement(message, i, expected[i], actual[i]);
}
}
}
/**
* Asserts that array {@code actual} is the same size and every element equals
* those in array {@code expected}. On failure, message indicates specific
* element mismatch.
*/
public static void assertEquals(byte[] expected, byte[] actual) {
assertEquals(null, expected, actual);
}
/**
* Asserts that array {@code actual} is the same size and every element equals
* those in array {@code expected}. On failure, message indicates first
* specific element mismatch.
*/
public static void assertEquals(
String message, int[] expected, int[] actual) {
if (expected.length != actual.length) {
failWrongLength(message, expected.length, actual.length);
}
for (int i = 0; i < expected.length; i++) {
if (expected[i] != actual[i]) {
failWrongElement(message, i, expected[i], actual[i]);
}
}
}
/**
* Asserts that array {@code actual} is the same size and every element equals
* those in array {@code expected}. On failure, message indicates first
* specific element mismatch.
*/
public static void assertEquals(int[] expected, int[] actual) {
assertEquals(null, expected, actual);
}
/**
* Asserts that array {@code actual} is the same size and every element equals
* those in array {@code expected}. On failure, message indicates first
* specific element mismatch.
*/
public static void assertEquals(
String message, double[] expected, double[] actual) {
if (expected.length != actual.length) {
failWrongLength(message, expected.length, actual.length);
}
for (int i = 0; i < expected.length; i++) {
if (expected[i] != actual[i]) {
failWrongElement(message, i, expected[i], actual[i]);
}
}
}
/**
* Asserts that array {@code actual} is the same size and every element equals
* those in array {@code expected}. On failure, message indicates first
* specific element mismatch.
*/
public static void assertEquals(double[] expected, double[] actual) {
assertEquals(null, expected, actual);
}
/**
* Asserts that array {@code actual} is the same size and every element
* is the same as those in array {@code expected}. Note that this uses
* {@code equals()} instead of {@code ==} to compare the objects.
* {@code null} will be considered equal to {code null} (unlike SQL).
* On failure, message indicates first specific element mismatch.
*/
public static void assertEquals(
String message, Object[] expected, Object[] actual) {
if (expected.length != actual.length) {
failWrongLength(message, expected.length, actual.length);
}
for (int i = 0; i < expected.length; i++) {
Object exp = expected[i];
Object act = actual[i];
// The following borrowed from java.util.equals(Object[], Object[]).
if (!((exp==null) ? act==null : exp.equals(act))) {
failWrongElement(message, i, exp, act);
}
}
}
/**
* Asserts that array {@code actual} is the same size and every element
* is the same as those in array {@code expected}. Note that this uses
* {@code ==} instead of {@code equals()} to compare the objects.
* On failure, message indicates first specific element mismatch.
*/
public static void assertEquals(Object[] expected, Object[] actual) {
assertEquals(null, expected, actual);
}
/** Asserts that two sets contain the same elements. */
public static void assertEquals(
String message, Set<? extends Object> expected, Set extends Object> actual) {
Set<Object> onlyInExpected = new HashSet
Other Android examples (source code examples)
Here is a short list of links related to this Android MoreAsserts.java source code file: