Move backported JUnit file to third_party

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=177637931
This commit is contained in:
jianglai 2017-12-01 14:06:35 -08:00
parent 82382aaca6
commit 8ef4fbe9d5
22 changed files with 248 additions and 138 deletions

View file

@ -12,6 +12,7 @@ java_library(
srcs = glob(["*.java"]),
deps = [
"//java/google/registry/monitoring/metrics",
"//third_party/junit",
"@com_google_guava",
"@com_google_truth",
"@com_google_truth_extensions_truth_java8_extension",

View file

@ -15,7 +15,7 @@
package google.registry.monitoring.metrics;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.JUnitBackports.expectThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

View file

@ -15,7 +15,7 @@
package google.registry.monitoring.metrics;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.JUnitBackports.expectThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;

View file

@ -15,7 +15,7 @@
package google.registry.monitoring.metrics;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.JUnitBackports.expectThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableRangeMap;

View file

@ -15,7 +15,7 @@
package google.registry.monitoring.metrics;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.JUnitBackports.expectThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import org.junit.Test;
import org.junit.runner.RunWith;

View file

@ -15,7 +15,7 @@
package google.registry.monitoring.metrics;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.JUnitBackports.expectThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import com.google.common.collect.ImmutableList;
import org.junit.Test;

View file

@ -1,122 +0,0 @@
package google.registry.monitoring.metrics;
/**
* A testing utility class that contains backports of useful but not yet released JUnit methods.
*
* <p>All of this code was taken directly from
* https://github.com/junit-team/junit4/blob/a832c5afe5b0e7c2590d057a1a49a344d207f8a0/src/main/java/org/junit/Assert.java
*/
public class JUnitBackports {
// TODO(b/68257761): Delete these and switch over to JUnit 4.13 methods upon release.
/**
* This interface facilitates the use of expectThrows from Java 8. It allows method references to
* void methods (that declare checked exceptions) to be passed directly into expectThrows without
* wrapping. It is not meant to be implemented directly.
*
* @since 4.13
*/
public interface ThrowingRunnable {
void run() throws Throwable;
}
/**
* Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when
* executed. If it does not throw an exception, an {@link AssertionError} is thrown. If it throws
* the wrong type of exception, an {@code AssertionError} is thrown describing the mismatch; the
* exception that was actually thrown can be obtained by calling {@link AssertionError#getCause}.
*
* @param expectedThrowable the expected type of the exception
* @param runnable a function that is expected to throw an exception when executed
* @since 4.13
*/
public static void assertThrows(
Class<? extends Throwable> expectedThrowable, ThrowingRunnable runnable) {
expectThrows(expectedThrowable, runnable);
}
/**
* Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when
* executed. If it does, the exception object is returned. If it does not throw an exception, an
* {@link AssertionError} is thrown. If it throws the wrong type of exception, an {@code
* AssertionError} is thrown describing the mismatch; the exception that was actually thrown can
* be obtained by calling {@link AssertionError#getCause}.
*
* @param expectedThrowable the expected type of the exception
* @param runnable a function that is expected to throw an exception when executed
* @return the exception thrown by {@code runnable}
* @since 4.13
*/
public static <T extends Throwable> T expectThrows(
Class<T> expectedThrowable, ThrowingRunnable runnable) {
try {
runnable.run();
} catch (Throwable actualThrown) {
if (expectedThrowable.isInstance(actualThrown)) {
@SuppressWarnings("unchecked")
T retVal = (T) actualThrown;
return retVal;
} else {
String expected = formatClass(expectedThrowable);
Class<? extends Throwable> actualThrowable = actualThrown.getClass();
String actual = formatClass(actualThrowable);
if (expected.equals(actual)) {
// There must be multiple class loaders. Add the identity hash code so the message
// doesn't say "expected: java.lang.String<my.package.MyException> ..."
expected += "@" + Integer.toHexString(System.identityHashCode(expectedThrowable));
actual += "@" + Integer.toHexString(System.identityHashCode(actualThrowable));
}
String mismatchMessage = format("unexpected exception type thrown;", expected, actual);
// The AssertionError(String, Throwable) ctor is only available on JDK7.
AssertionError assertionError = new AssertionError(mismatchMessage);
assertionError.initCause(actualThrown);
throw assertionError;
}
}
String message =
String.format(
"expected %s to be thrown, but nothing was thrown", formatClass(expectedThrowable));
throw new AssertionError(message);
}
static String format(String message, Object expected, Object actual) {
String formatted = "";
if (message != null && !"".equals(message)) {
formatted = message + " ";
}
String expectedString = String.valueOf(expected);
String actualString = String.valueOf(actual);
if (equalsRegardingNull(expectedString, actualString)) {
return formatted
+ "expected: "
+ formatClassAndValue(expected, expectedString)
+ " but was: "
+ formatClassAndValue(actual, actualString);
} else {
return formatted + "expected:<" + expectedString + "> but was:<" + actualString + ">";
}
}
private static String formatClass(Class<?> value) {
String className = value.getCanonicalName();
return className == null ? value.getName() : className;
}
private static String formatClassAndValue(Object value, String valueString) {
String className = value == null ? "null" : value.getClass().getName();
return className + "<" + valueString + ">";
}
private static boolean equalsRegardingNull(Object expected, Object actual) {
if (expected == null) {
return actual == null;
}
return isEquals(expected, actual);
}
private static boolean isEquals(Object expected, Object actual) {
return expected.equals(actual);
}
}

View file

@ -15,7 +15,7 @@
package google.registry.monitoring.metrics;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.JUnitBackports.expectThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import org.junit.Test;
import org.junit.runner.RunWith;

View file

@ -15,7 +15,7 @@
package google.registry.monitoring.metrics;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.JUnitBackports.expectThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import org.junit.Test;
import org.junit.runner.RunWith;

View file

@ -15,7 +15,7 @@
package google.registry.monitoring.metrics;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.JUnitBackports.expectThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import static org.mockito.Mockito.mock;
import com.google.common.collect.ImmutableList;

View file

@ -15,7 +15,7 @@
package google.registry.monitoring.metrics;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.JUnitBackports.expectThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import com.google.common.collect.ImmutableSet;
import google.registry.monitoring.metrics.MetricSchema.Kind;

View file

@ -15,7 +15,7 @@
package google.registry.monitoring.metrics;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.JUnitBackports.expectThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import com.google.common.collect.ImmutableRangeMap;
import com.google.common.collect.ImmutableSet;

View file

@ -15,7 +15,7 @@
package google.registry.monitoring.metrics;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.JUnitBackports.expectThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

View file

@ -13,7 +13,7 @@ java_library(
deps = [
"//java/google/registry/monitoring/metrics",
"//java/google/registry/monitoring/metrics/contrib",
"//javatests/google/registry/monitoring/metrics",
"//third_party/junit",
"@com_google_guava",
"@com_google_truth",
"@com_google_truth_extensions_truth_java8_extension",

View file

@ -15,8 +15,8 @@
package google.registry.monitoring.metrics.contrib;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.JUnitBackports.expectThrows;
import static google.registry.monitoring.metrics.contrib.DistributionMetricSubject.assertThat;
import static google.registry.testing.JUnitBackports.expectThrows;
import com.google.common.collect.ImmutableSet;
import google.registry.monitoring.metrics.EventMetric;

View file

@ -15,8 +15,8 @@
package google.registry.monitoring.metrics.contrib;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.JUnitBackports.expectThrows;
import static google.registry.monitoring.metrics.contrib.LongMetricSubject.assertThat;
import static google.registry.testing.JUnitBackports.expectThrows;
import com.google.common.collect.ImmutableSet;
import google.registry.monitoring.metrics.IncrementableMetric;

View file

@ -13,7 +13,7 @@ java_library(
deps = [
"//java/google/registry/monitoring/metrics",
"//java/google/registry/monitoring/metrics/stackdriver",
"//javatests/google/registry/monitoring/metrics",
"//third_party/junit",
"@com_google_api_client",
"@com_google_apis_google_api_services_monitoring",
"@com_google_guava",

View file

@ -15,7 +15,7 @@
package google.registry.monitoring.metrics.stackdriver;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.JUnitBackports.assertThrows;
import static google.registry.testing.JUnitBackports.assertThrows;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;

View file

@ -19,6 +19,7 @@ java_library(
"//java/google/registry:env/common/default/WEB-INF/datastore-indexes.xml",
"//java/google/registry:env/common/default/WEB-INF/queue.xml",
] + glob(["*.csv"]) + glob(["testdata/*"]),
exports = ["//third_party/junit"],
deps = [
"//java/google/registry/config",
"//java/google/registry/dns:constants",

View file

@ -1,122 +0,0 @@
package google.registry.testing;
/**
* A testing utility class that contains backports of useful but not yet released JUnit methods.
*
* <p>All of this code was taken directly from
* https://github.com/junit-team/junit4/blob/a832c5afe5b0e7c2590d057a1a49a344d207f8a0/src/main/java/org/junit/Assert.java
*/
public class JUnitBackports {
// TODO(b/68257761): Delete these and switch over to JUnit 4.13 methods upon release.
/**
* This interface facilitates the use of expectThrows from Java 8. It allows method references to
* void methods (that declare checked exceptions) to be passed directly into expectThrows without
* wrapping. It is not meant to be implemented directly.
*
* @since 4.13
*/
public interface ThrowingRunnable {
void run() throws Throwable;
}
/**
* Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when
* executed. If it does not throw an exception, an {@link AssertionError} is thrown. If it throws
* the wrong type of exception, an {@code AssertionError} is thrown describing the mismatch; the
* exception that was actually thrown can be obtained by calling {@link AssertionError#getCause}.
*
* @param expectedThrowable the expected type of the exception
* @param runnable a function that is expected to throw an exception when executed
* @since 4.13
*/
public static void assertThrows(
Class<? extends Throwable> expectedThrowable, ThrowingRunnable runnable) {
expectThrows(expectedThrowable, runnable);
}
/**
* Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when
* executed. If it does, the exception object is returned. If it does not throw an exception, an
* {@link AssertionError} is thrown. If it throws the wrong type of exception, an {@code
* AssertionError} is thrown describing the mismatch; the exception that was actually thrown can
* be obtained by calling {@link AssertionError#getCause}.
*
* @param expectedThrowable the expected type of the exception
* @param runnable a function that is expected to throw an exception when executed
* @return the exception thrown by {@code runnable}
* @since 4.13
*/
public static <T extends Throwable> T expectThrows(
Class<T> expectedThrowable, ThrowingRunnable runnable) {
try {
runnable.run();
} catch (Throwable actualThrown) {
if (expectedThrowable.isInstance(actualThrown)) {
@SuppressWarnings("unchecked")
T retVal = (T) actualThrown;
return retVal;
} else {
String expected = formatClass(expectedThrowable);
Class<? extends Throwable> actualThrowable = actualThrown.getClass();
String actual = formatClass(actualThrowable);
if (expected.equals(actual)) {
// There must be multiple class loaders. Add the identity hash code so the message
// doesn't say "expected: java.lang.String<my.package.MyException> ..."
expected += "@" + Integer.toHexString(System.identityHashCode(expectedThrowable));
actual += "@" + Integer.toHexString(System.identityHashCode(actualThrowable));
}
String mismatchMessage = format("unexpected exception type thrown;", expected, actual);
// The AssertionError(String, Throwable) ctor is only available on JDK7.
AssertionError assertionError = new AssertionError(mismatchMessage);
assertionError.initCause(actualThrown);
throw assertionError;
}
}
String message =
String.format(
"expected %s to be thrown, but nothing was thrown", formatClass(expectedThrowable));
throw new AssertionError(message);
}
static String format(String message, Object expected, Object actual) {
String formatted = "";
if (message != null && !"".equals(message)) {
formatted = message + " ";
}
String expectedString = String.valueOf(expected);
String actualString = String.valueOf(actual);
if (equalsRegardingNull(expectedString, actualString)) {
return formatted
+ "expected: "
+ formatClassAndValue(expected, expectedString)
+ " but was: "
+ formatClassAndValue(actual, actualString);
} else {
return formatted + "expected:<" + expectedString + "> but was:<" + actualString + ">";
}
}
private static String formatClass(Class<?> value) {
String className = value.getCanonicalName();
return className == null ? value.getName() : className;
}
private static String formatClassAndValue(Object value, String valueString) {
String className = value == null ? "null" : value.getClass().getName();
return className + "<" + valueString + ">";
}
private static boolean equalsRegardingNull(Object expected, Object actual) {
if (expected == null) {
return actual == null;
}
return isEquals(expected, actual);
}
private static boolean isEquals(Object expected, Object actual) {
return expected.equals(actual);
}
}