Skip to content

Commit 5374b6b

Browse files
author
Sabrina Lawrey
committed
added javadoc to function
1 parent 67b947f commit 5374b6b

6 files changed

Lines changed: 109 additions & 36 deletions

File tree

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

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,25 @@
88
import java.util.function.Predicate;
99

1010
/**
11-
* A test utility class for recording and executing assertions about the presence (or absence) of exceptions
11+
* The ExceptionTracker interface provides a set of methods to record, track, and assert exceptions
12+
* in a testing context. By defining rules to expect or ignore certain exceptions, it helps in
13+
* systematically verifying the correct exception handling behavior of code under test.
1214
*
1315
* @param <T> The class used to represent thrown exceptions
1416
*/
1517
public interface ExceptionTracker<T> {
1618

1719
/**
18-
* Create an exception tracker
20+
* Factory method to create an instance of the exception tracker. This method encapsulates
21+
* the construction of a concrete implementation of the ExceptionTracker interface.
1922
*
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)
23+
* @param messageExtractor Function to extract the String message or description from T
24+
* @param throwableExtractor Function to extract the Throwable from T
25+
* @param resetRunnable Runnable that will be called at the end of {@link #checkExceptions()}
26+
* @param exceptions Map to populate with T as the key and count of occurrences as value
27+
* @param ignorePredicate Predicate to exclude T's from consideration
28+
* @param exceptionRenderer Function to render T as a String (used when dumping exceptions)
29+
* @return An instance of ExceptionTracker
2630
*/
2731
static <T> ExceptionTracker<T> create(@NotNull final Function<T, String> messageExtractor,
2832
@NotNull final Function<T, Throwable> throwableExtractor,
@@ -40,50 +44,54 @@ static <T> ExceptionTracker<T> create(@NotNull final Function<T, String> message
4044
}
4145

4246
/**
43-
* Require than an exception containing the specified string is thrown during the test
47+
* Specifies that an exception containing the specified string must be thrown during the test.
48+
* Failing to do so will cause the test to fail.
4449
*
4550
* @param message The string to require
4651
*/
4752
void expectException(String message);
4853

4954
/**
50-
* Require that an exception matching the specified predicate is thrown
55+
* Specifies that an exception matching the specified predicate must be thrown.
56+
* Failing to match the exception will cause the test to fail.
5157
*
52-
* @param predicate The predicate used to match exceptions
53-
* @param description The description of the exceptions being required
58+
* @param predicate Predicate to match exceptions
59+
* @param description Description of the exceptions being required
5460
*/
5561
void expectException(Predicate<T> predicate, String description);
5662

5763
/**
58-
* Ignore exceptions containing the specified string
64+
* Ignores exceptions containing the specified string. Matching exceptions will not cause
65+
* the test to fail.
5966
*
6067
* @param message The string to ignore
6168
*/
6269
void ignoreException(String message);
6370

6471
/**
65-
* Ignore exceptions matching the specified predicate
72+
* Ignores exceptions matching the specified predicate. Matching exceptions will not cause
73+
* the test to fail.
6674
*
67-
* @param predicate The predicate to match the exception
68-
* @param description The description of the exceptions being ignored
75+
* @param predicate Predicate to match the exception
76+
* @param description Description of the exceptions being ignored
6977
*/
7078
void ignoreException(Predicate<T> predicate, String description);
7179

7280
/**
73-
* Determine if the tracker contains an exception matching the predicate
81+
* Determines if the tracker contains an exception matching the predicate.
7482
*
75-
* @param predicate The predicate to match the exception
83+
* @param predicate Predicate to match the exception
84+
* @return true if an exception matching the predicate is found; false otherwise
7685
*/
7786
boolean hasException(Predicate<T> predicate);
7887

7988
/**
80-
* Call this in @After to ensure
89+
* Call this method in a teardown (@After) phase of the test to:
8190
* <ul>
82-
* <li>No non-ignored exceptions were thrown</li>
83-
* <li>There is an exception matching each of the expected predicates</li>
91+
* <li>Verify no non-ignored exceptions were thrown</li>
92+
* <li>Assert there is an exception matching each of the expected predicates</li>
8493
* </ul>
85-
* <p>
86-
* Implementations should throw an exception and print a summary of the assertion(s) violated
94+
* Implementations should throw an exception and print a summary if the assertion(s) are violated.
8795
*/
8896
void checkExceptions();
8997
}

src/main/java/net/openhft/chronicle/testframework/function/HasName.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22

33
import org.jetbrains.annotations.NotNull;
44

5+
/**
6+
* This interface represents any object that has a name associated with it.
7+
* Implementing this interface allows a class to have a method that retrieves
8+
* a name string, providing a consistent way to manage named objects.
9+
*/
510
public interface HasName {
611

712
/**
813
* Returns the name of this object.
14+
* <p>
15+
* The name is an identifier that may be used to distinguish this object
16+
* from other objects in a collection or system. It is not meant to be unique
17+
* necessarily, but should provide meaningful information to the developers or
18+
* users.
919
*
10-
* @return name
20+
* @return name - the name associated with this object, must not be null.
1121
*/
1222
@NotNull
1323
String name();
14-
15-
}
24+
}

src/main/java/net/openhft/chronicle/testframework/function/NamedConsumer.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,24 @@
77

88
import static java.util.Objects.requireNonNull;
99

10+
/**
11+
* The NamedConsumer interface extends both HasName and Consumer interfaces,
12+
* thus providing a Consumer with an associated name.
13+
* <p>
14+
* NamedConsumer can be used in contexts where consumers are managed and identified by their names.
15+
*
16+
* @param <T> the type of the input to the consumer
17+
*/
1018
public interface NamedConsumer<T> extends HasName, Consumer<T> {
19+
20+
/**
21+
* Creates a NamedConsumer instance from the given consumer and name.
22+
*
23+
* @param consumer the Consumer instance
24+
* @param name the name to associate with the consumer
25+
* @return a NamedConsumer wrapping the given consumer and name
26+
* @throws NullPointerException if either consumer or name is null
27+
*/
1128
@NotNull
1229
static <T> NamedConsumer<T> of(@NotNull final Consumer<T> consumer,
1330
@NotNull final String name) {
@@ -16,6 +33,15 @@ static <T> NamedConsumer<T> of(@NotNull final Consumer<T> consumer,
1633
return new VanillaNamedConsumer<>(consumer, name);
1734
}
1835

36+
/**
37+
* Creates a NamedConsumer instance from the given ThrowingConsumer and name.
38+
* ThrowingConsumer is a special type of Consumer that can throw checked exceptions.
39+
*
40+
* @param consumer the ThrowingConsumer instance
41+
* @param name the name to associate with the consumer
42+
* @return a NamedConsumer wrapping the given ThrowingConsumer and name
43+
* @throws NullPointerException if either consumer or name is null
44+
*/
1945
@NotNull
2046
static <T> NamedConsumer<T> ofThrowing(@NotNull final ThrowingConsumer<T, ?> consumer,
2147
@NotNull final String name) {

src/main/java/net/openhft/chronicle/testframework/function/ThrowingConsumer.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010
/**
1111
* Represents an operation that accepts a single input argument and returns no
12-
* result and that can throw an Exception. Unlike most other functional interfaces, {@code Consumer} is expected
13-
* to operate via side-effects.
14-
*
15-
* <p>This is a <a href="package-summary.html">functional interface</a>
16-
* whose functional method is {@link #accept(Object)}.
12+
* result and that can throw an exception of type {@code X}. Unlike most other functional interfaces,
13+
* {@code ThrowingConsumer} is expected to operate via side-effects and may throw exceptions during execution.
14+
* <p>
15+
* This is a functional interface whose functional method is {@link #accept(Object)}.
1716
*
1817
* @param <T> the type of the input to the operation
18+
* @param <X> the type of the exception that can be thrown by this operation
1919
*/
2020
@FunctionalInterface
2121
public interface ThrowingConsumer<T, X extends Exception> {
@@ -24,16 +24,21 @@ public interface ThrowingConsumer<T, X extends Exception> {
2424
* Performs this operation on the given argument.
2525
*
2626
* @param t the input argument
27-
* @throws X if an exception occurs
27+
* @throws X if an exception of type {@code X} occurs during execution
2828
*/
2929
void accept(T t) throws X;
3030

3131
/**
3232
* Creates and returns a new Consumer that will wrap any exceptions thrown by the
33-
* provided {@code throwingConsumer} in a {@link ThrowingConsumerException}
34-
* @param throwingConsumer to wrap (non-null)
33+
* provided {@code throwingConsumer} in a {@link ThrowingConsumerException}.
34+
* <p>
35+
* This method allows the integration of a ThrowingConsumer into contexts where a regular
36+
* Consumer is expected, by translating checked exceptions into runtime exceptions.
37+
*
38+
* @param throwingConsumer the ThrowingConsumer to wrap, must not be null
3539
* @param <T> consumed type
36-
* @return a wrapped Consumer
40+
* @return a wrapped Consumer that translates checked exceptions into runtime exceptions
41+
* @throws NullPointerException if {@code throwingConsumer} is null
3742
*/
3843
@NotNull
3944
static <T> Consumer<T> of(@NotNull final ThrowingConsumer<T, ?> throwingConsumer) {
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
package net.openhft.chronicle.testframework.function;
22

3+
/**
4+
* ThrowingConsumerException is a specialized {@link RuntimeException} used to wrap exceptions
5+
* thrown by a {@link ThrowingConsumer}.
6+
* <p>
7+
* This exception allows checked exceptions to be propagated through the ThrowingConsumer interface,
8+
* and can be used to handle or log the underlying exceptions as needed.
9+
*/
310
public class ThrowingConsumerException extends RuntimeException {
411

12+
/**
13+
* The serial version UID for the serialization mechanism.
14+
*/
515
static final long serialVersionUID = -8237762538281151227L;
616

17+
/**
18+
* Constructs a new ThrowingConsumerException with the specified cause.
19+
* <p>
20+
* The cause is used to retain necessary information about the underlying exception that
21+
* triggered this ThrowingConsumerException, making it available for further inspection or logging.
22+
*
23+
* @param cause the underlying cause (usually a checked exception thrown by the ThrowingConsumer)
24+
*/
725
public ThrowingConsumerException(Throwable cause) {
826
super(cause);
927
}
10-
}
28+
}

src/main/java/net/openhft/chronicle/testframework/function/TriConsumer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
* result. This is the three-arity specialization of {@link Consumer}.
99
* Unlike most other functional interfaces, {@code TriConsumer} is expected
1010
* to operate via side-effects.
11+
* <p>
12+
* This can be used anywhere you need to apply an operation to three input parameters
13+
* and don't need to return a result, for example, in handling events with three attributes.
1114
*
1215
* <p>This is a <a href="package-summary.html">functional interface</a>
1316
* whose functional method is {@link #accept(Object, Object, Object)}.
@@ -37,6 +40,10 @@ interface TriConsumer<T, U, V> {
3740
* composed operation. If performing this operation throws an exception,
3841
* the {@code after} operation will not be performed.
3942
*
43+
* <p>This can be useful in scenarios where you have several operations that
44+
* must be performed on the same set of input parameters, possibly by different parts
45+
* of your code.
46+
*
4047
* @param after the operation to perform after this operation
4148
* @return a composed {@code TriConsumer} that performs in sequence this
4249
* operation followed by the {@code after} operation

0 commit comments

Comments
 (0)