Skip to content

Commit 0a6e8ca

Browse files
authored
Expose ExceptionTracker as public API - closes #36 (#37)
1 parent 9ad3044 commit 0a6e8ca

3 files changed

Lines changed: 105 additions & 70 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package net.openhft.chronicle.testframework.exception;
2+
3+
import net.openhft.chronicle.testframework.internal.VanillaExceptionTracker;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
import java.util.Map;
7+
import java.util.function.Function;
8+
import java.util.function.Predicate;
9+
10+
/**
11+
* A test utility class for recording and executing assertions about the presence (or absence) of exceptions
12+
*
13+
* @param <T> The class used to represent thrown exceptions
14+
*/
15+
public interface ExceptionTracker<T> {
16+
17+
/**
18+
* Create an exception tracker
19+
*
20+
* @param messageExtractor The function used to extract the String message or description from T
21+
* @param throwableExtractor The function used to extract the Throwable from T
22+
* @param resetRunnable A Runnable that will be called at the end of {@link #checkExceptions()}
23+
* @param exceptions A map that will be populated with T as the key and the count of occurrences of T as a value
24+
* @param ignorePredicate A predicate that will exclude T's from consideration
25+
* @param exceptionRenderer A function to render T as a String (used when dumping exceptions)
26+
*/
27+
static <T> ExceptionTracker<T> create(@NotNull final Function<T, String> messageExtractor,
28+
@NotNull final Function<T, Throwable> throwableExtractor,
29+
@NotNull final Runnable resetRunnable,
30+
@NotNull final Map<T, Integer> exceptions,
31+
@NotNull final Predicate<T> ignorePredicate,
32+
@NotNull final Function<T, String> exceptionRenderer) {
33+
return new VanillaExceptionTracker<>(
34+
messageExtractor,
35+
throwableExtractor,
36+
resetRunnable,
37+
exceptions,
38+
ignorePredicate,
39+
exceptionRenderer);
40+
}
41+
42+
/**
43+
* Require than an exception containing the specified string is thrown during the test
44+
*
45+
* @param message The string to require
46+
*/
47+
void expectException(String message);
48+
49+
/**
50+
* Require that an exception matching the specified predicate is thrown
51+
*
52+
* @param predicate The predicate used to match exceptions
53+
* @param description The description of the exceptions being required
54+
*/
55+
void expectException(Predicate<T> predicate, String description);
56+
57+
/**
58+
* Ignore exceptions containing the specified string
59+
*
60+
* @param message The string to ignore
61+
*/
62+
void ignoreException(String message);
63+
64+
/**
65+
* Ignore exceptions matching the specified predicate
66+
*
67+
* @param predicate The predicate to match the exception
68+
* @param description The description of the exceptions being ignored
69+
*/
70+
void ignoreException(Predicate<T> predicate, String description);
71+
72+
/**
73+
* Determine if the tracker contains an exception matching the predicate
74+
*
75+
* @param predicate The predicate to match the exception
76+
*/
77+
boolean hasException(Predicate<T> predicate);
78+
79+
/**
80+
* Call this in @After to ensure
81+
* <ul>
82+
* <li>No non-ignored exceptions were thrown</li>
83+
* <li>There is an exception matching each of the expected predicates</li>
84+
* </ul>
85+
* <p>
86+
* Implementations should throw an exception and print a summary of the assertion(s) violated
87+
*/
88+
void checkExceptions();
89+
}

src/main/java/net/openhft/chronicle/testframework/internal/ExceptionTracker.java

Lines changed: 13 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -9,80 +9,24 @@
99
/**
1010
* A test utility class for recording and executing assertions about the presence (or absence) of exceptions
1111
*
12+
* @deprecated use {@link net.openhft.chronicle.testframework.exception.ExceptionTracker}
13+
*
1214
* @param <T> The class used to represent thrown exceptions
1315
*/
14-
public interface ExceptionTracker<T> {
16+
@Deprecated(/* To be removed in x.25 */)
17+
public interface ExceptionTracker<T> extends net.openhft.chronicle.testframework.exception.ExceptionTracker<T> {
1518

1619
/**
17-
* Create an exception tracker
18-
*
19-
* @param messageExtractor The function used to extract the String message or description from T
20-
* @param throwableExtractor The function used to extract the Throwable from T
21-
* @param resetRunnable A Runnable that will be called at the end of {@link #checkExceptions()}
22-
* @param exceptions A map that will be populated with T as the key and the count of occurrences of T as a value
23-
* @param ignorePredicate A predicate that will exclude T's from consideration
24-
* @param exceptionRenderer A function to render T as a String (used when dumping exceptions)
20+
* @deprecated use {@link net.openhft.chronicle.testframework.exception.ExceptionTracker#create(Function, Function, Runnable, Map, Predicate, Function)}
2521
*/
22+
@Deprecated(/* To be removed in x.25 */)
2623
static <T> ExceptionTracker<T> create(@NotNull final Function<T, String> messageExtractor,
27-
@NotNull final Function<T, Throwable> throwableExtractor,
28-
@NotNull final Runnable resetRunnable,
29-
@NotNull final Map<T, Integer> exceptions,
30-
@NotNull final Predicate<T> ignorePredicate,
31-
@NotNull final Function<T, String> exceptionRenderer) {
32-
return new VanillaExceptionTracker<>(
33-
messageExtractor,
34-
throwableExtractor,
35-
resetRunnable,
36-
exceptions,
37-
ignorePredicate,
38-
exceptionRenderer);
24+
@NotNull final Function<T, Throwable> throwableExtractor,
25+
@NotNull final Runnable resetRunnable,
26+
@NotNull final Map<T, Integer> exceptions,
27+
@NotNull final Predicate<T> ignorePredicate,
28+
@NotNull final Function<T, String> exceptionRenderer) {
29+
return (ExceptionTracker<T>) net.openhft.chronicle.testframework.exception.ExceptionTracker.create(
30+
messageExtractor, throwableExtractor, resetRunnable, exceptions, ignorePredicate, exceptionRenderer);
3931
}
40-
41-
/**
42-
* Require than an exception containing the specified string is thrown during the test
43-
*
44-
* @param message The string to require
45-
*/
46-
void expectException(String message);
47-
48-
/**
49-
* Require that an exception matching the specified predicate is thrown
50-
*
51-
* @param predicate The predicate used to match exceptions
52-
* @param description The description of the exceptions being required
53-
*/
54-
void expectException(Predicate<T> predicate, String description);
55-
56-
/**
57-
* Ignore exceptions containing the specified string
58-
*
59-
* @param message The string to ignore
60-
*/
61-
void ignoreException(String message);
62-
63-
/**
64-
* Ignore exceptions matching the specified predicate
65-
*
66-
* @param predicate The predicate to match the exception
67-
* @param description The description of the exceptions being ignored
68-
*/
69-
void ignoreException(Predicate<T> predicate, String description);
70-
71-
/**
72-
* Determine if the tracker contains an exception matching the predicate
73-
*
74-
* @param predicate The predicate to match the exception
75-
*/
76-
boolean hasException(Predicate<T> predicate);
77-
78-
/**
79-
* Call this in @After to ensure
80-
* <ul>
81-
* <li>No non-ignored exceptions were thrown</li>
82-
* <li>There is an exception matching each of the expected predicates</li>
83-
* </ul>
84-
* <p>
85-
* Implementations should throw an exception and print a summary of the assertion(s) violated
86-
*/
87-
void checkExceptions();
8832
}

src/main/java/net/openhft/chronicle/testframework/internal/VanillaExceptionTracker.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
import java.util.function.Function;
1212
import java.util.function.Predicate;
1313
import java.util.stream.Collectors;
14+
import net.openhft.chronicle.testframework.exception.ExceptionTracker;
1415

1516
/**
1617
* A test utility class for recording and executing assertions about the presence (or absence) of exceptions
1718
*
1819
* @param <T> The class used to represent thrown exceptions
1920
*/
20-
public final class VanillaExceptionTracker<T> implements ExceptionTracker<T> {
21+
@SuppressWarnings("deprecation")
22+
public final class VanillaExceptionTracker<T> implements ExceptionTracker<T>, net.openhft.chronicle.testframework.internal.ExceptionTracker<T> {
2123

2224
private static final Logger LOGGER = LoggerFactory.getLogger(VanillaExceptionTracker.class);
2325

0 commit comments

Comments
 (0)