You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rj...@apache.org on 2016/02/03 21:20:23 UTC
lucene-solr git commit: LUCENE-7009: Add expectThrows utility to
LuceneTestCase
Repository: lucene-solr
Updated Branches:
refs/heads/master 61fae32f5 -> 732b8fb3b
LUCENE-7009: Add expectThrows utility to LuceneTestCase
Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/732b8fb3
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/732b8fb3
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/732b8fb3
Branch: refs/heads/master
Commit: 732b8fb3b9d0e1a215c0f02b99a0c8847ecf7039
Parents: 61fae32
Author: Ryan Ernst <ry...@iernst.net>
Authored: Wed Feb 3 11:02:32 2016 -0800
Committer: Ryan Ernst <ry...@iernst.net>
Committed: Wed Feb 3 12:18:12 2016 -0800
----------------------------------------------------------------------
lucene/CHANGES.txt | 6 ++++++
.../org/apache/lucene/util/LuceneTestCase.java | 22 ++++++++++++++++++++
2 files changed, 28 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/732b8fb3/lucene/CHANGES.txt
----------------------------------------------------------------------
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index db58f4d..5b23146 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -110,6 +110,12 @@ Changes in Runtime Behavior
and codec components are no longer allowed to use this extension
(Robert Muir, Mike McCandless)
+Tests
+
+* LUCENE-7009: Add expectThrows utility to LuceneTestCase. This uses a lambda
+ expression to encapsulate a statement that is expected to throw an exception.
+ (Ryan Ernst)
+
======================= Lucene 5.5.0 =======================
New Features
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/732b8fb3/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
----------------------------------------------------------------------
diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
index 7175837..c7c45c4 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
@@ -64,6 +64,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
+import junit.framework.AssertionFailedError;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
@@ -2597,6 +2598,27 @@ public abstract class LuceneTestCase extends Assert {
}
}
+ /** A runnable that can throw any checked exception. */
+ @FunctionalInterface
+ public interface ThrowingRunnable {
+ void run() throws Throwable;
+ }
+
+ /** Checks a specific exception class is thrown by the given runnable, and returns it. */
+ public static <T extends Throwable> T expectThrows(Class<T> expectedType, ThrowingRunnable runnable) {
+ try {
+ runnable.run();
+ } catch (Throwable e) {
+ if (expectedType.isInstance(e)) {
+ return expectedType.cast(e);
+ }
+ AssertionFailedError assertion = new AssertionFailedError("Unexpected exception type, expected " + expectedType.getSimpleName());
+ assertion.initCause(e);
+ throw assertion;
+ }
+ throw new AssertionFailedError("Expected exception " + expectedType.getSimpleName());
+ }
+
/** Returns true if the file exists (can be opened), false
* if it cannot be opened, and (unlike Java's
* File.exists) throws IOException if there's some