TestFramework.java

1
package com.renomad.minum.testing;
2
3
import com.renomad.minum.state.Constants;
4
import com.renomad.minum.state.Context;
5
import com.renomad.minum.logging.TestLogger;
6
import com.renomad.minum.queue.ActionQueueKiller;
7
import com.renomad.minum.utils.ThrowingRunnable;
8
import com.renomad.minum.web.FullSystem;
9
10
import java.util.Arrays;
11
import java.util.List;
12
import java.util.Properties;
13
import java.util.concurrent.Executors;
14
15
/**
16
 * These are utility functions for basic automated
17
 * testing.  It turns out you don't really need fancy tools
18
 * to do excellent testing.  Just a committment to
19
 * quality.  Don't let anyone tell you differently.
20
 */
21
public final class TestFramework {
22
23
    private TestFramework() {
24
        // making this private to be clearer it isn't supposed to be instantiated.
25
    }
26
27
    /**
28
     * assert that a particular chunk of code throws a particular
29
     * exception.
30
     * <p>
31
     *     Example usage:
32
     * </p>
33
     * <pre>
34
     *     <code>
35
     *         {@code assertThrows(TemplateRenderException.class, "Missing a value for key {missing_key}", () -> tp.renderTemplate(myMap));}
36
     *     </code>
37
     * </pre>
38
     */
39
    public static <T> T assertThrows(Class<T> myEx, ThrowingRunnable r) {
40 1 1. assertThrows : replaced return value with null for com/renomad/minum/testing/TestFramework::assertThrows → KILLED
        return assertThrows(myEx, null, r);
41
    }
42
43
    // quick note about the warning suppression - we already checked that the
44
    // case will be valid, when we checked if (!myEx.isInstance(ex)).
45
    @SuppressWarnings("unchecked")
46
    public static <T> T assertThrows(Class<T> myEx, String expectedMsg, ThrowingRunnable r) {
47
        try {
48 1 1. assertThrows : removed call to com/renomad/minum/utils/ThrowingRunnable::run → KILLED
            r.run();
49
            throw new TestFailureException("Failed to throw exception");
50
        } catch (Exception ex) {
51 1 1. assertThrows : negated conditional → KILLED
            if (!myEx.getTypeName().equals(ex.getClass().getTypeName())) {
52
                String msg = String.format("This did not throw the expected exception type (%s).  Instead, (%s) was thrown", myEx, ex);
53
                throw new TestFailureException(msg);
54
            }
55 2 1. assertThrows : negated conditional → KILLED
2. assertThrows : negated conditional → KILLED
            if (expectedMsg != null && !ex.getMessage().equals(expectedMsg)) {
56
                String msg = String.format("Did not get expected message (%s). Instead, got: %s", expectedMsg, ex.getMessage());
57
                throw new TestFailureException(msg);
58
            }
59 1 1. assertThrows : replaced return value with null for com/renomad/minum/testing/TestFramework::assertThrows → KILLED
            return (T) ex;
60
        }
61
62
    }
63
64
    /**
65
     * A helper for testing - assert two generics are equal.  If you
66
     * need to compare two byte arrays, see {@link #assertEqualByteArray(byte[], byte[])}
67
     */
68
    public static <T> void assertEquals(T left, T right) {
69 1 1. assertEquals : negated conditional → KILLED
        if (! left.equals(right)) {
70
            throw new TestFailureException("Not equal! %nleft:  %s %nright: %s".formatted(showWhiteSpace(left.toString()), showWhiteSpace(right.toString())));
71
        }
72
    }
73
74
    /**
75
     * Compares two byte arrays for equality
76
     */
77
    public static void assertEqualByteArray(byte[] left, byte[] right) {
78 2 1. assertEqualByteArray : negated conditional → KILLED
2. assertEqualByteArray : negated conditional → KILLED
        if (left == null || right == null) throw new TestFailureException("at least one of the inputs was null: left: %s right: %s".formatted(Arrays.toString(left), Arrays.toString(right)));
79 1 1. assertEqualByteArray : negated conditional → KILLED
        if (left.length != right.length) throw new TestFailureException("Not equal! left length: %d right length: %d".formatted(left.length, right.length));
80 2 1. assertEqualByteArray : negated conditional → KILLED
2. assertEqualByteArray : changed conditional boundary → KILLED
        for (int i = 0; i < left.length; i++) {
81 1 1. assertEqualByteArray : negated conditional → KILLED
            if (left[i] != right[i]) throw new TestFailureException("Not equal! at index %d left was: %d right was: %d".formatted(i, left[i], right[i]));
82
        }
83
    }
84
85
    public static void assertEqualByteArray(byte[] left, byte[] right, String failureMessage) {
86
        try {
87 1 1. assertEqualByteArray : removed call to com/renomad/minum/testing/TestFramework::assertEqualByteArray → KILLED
            assertEqualByteArray(left, right);
88
        } catch (TestFailureException ex) {
89
            throw new TestFailureException(ex.getMessage() + ". " + failureMessage);
90
        }
91
    }
92
93
    /**
94
     * asserts two lists are equal, ignoring the order.
95
     * For example, (a, b) is equal to (b, a)
96
     * <p>
97
     * Note that the lists must be of comparable objects, or else
98
     * a ClassCastException will be thrown
99
     */
100
    public static void assertEqualsDisregardOrder(List<? extends CharSequence> left, List<? extends CharSequence> right) {
101 1 1. assertEqualsDisregardOrder : negated conditional → KILLED
        if (left.size() != right.size()) {
102
            throw new TestFailureException(String.format("different sizes: left was %d, right was %d%n", left.size(), right.size()));
103
        }
104
        List<? extends CharSequence> orderedLeft = left.stream().sorted().toList();
105
        List<? extends CharSequence> orderedRight = right.stream().sorted().toList();
106
107 2 1. assertEqualsDisregardOrder : changed conditional boundary → KILLED
2. assertEqualsDisregardOrder : negated conditional → KILLED
        for (int i = 0; i < left.size(); i++) {
108 1 1. assertEqualsDisregardOrder : negated conditional → KILLED
            if (!orderedLeft.get(i).toString().contentEquals(orderedRight.get(i))) {
109
                throw new TestFailureException(
110
                        String.format(
111
                                "%n%ndifferent values:%n%nleft:  %s%nright: %s%n%nfull left:%n-----------%n%s%n%nfull right:%n-----------%n%s%n",
112
                                orderedLeft.get(i),
113
                                orderedRight.get(i),
114
                                String.join("\n", showWhiteSpace(left.toString())),
115
                                String.join("\n", showWhiteSpace(right.toString()))));
116
            }
117
        }
118
    }
119
120
    public static void assertEqualsDisregardOrder(List<? extends CharSequence> left, List<? extends CharSequence> right, String failureMessage) {
121
        try {
122 1 1. assertEqualsDisregardOrder : removed call to com/renomad/minum/testing/TestFramework::assertEqualsDisregardOrder → KILLED
            assertEqualsDisregardOrder(left, right);
123
        } catch (TestFailureException ex) {
124
            throw new TestFailureException(ex.getMessage() + ". " + failureMessage);
125
        }
126
    }
127
128
    /**
129
     * asserts that two lists are equal in value and order.
130
     * <br><br>
131
     * For example, (a, b) is equal to (a, b)
132
     * Does not expect null as an input value.
133
     * Two empty lists are considered equal.
134
     */
135
    public static <T> void assertEquals(List<T> left, List<T> right) {
136 1 1. assertEquals : removed call to com/renomad/minum/testing/TestFramework::assertEquals → TIMED_OUT
       assertEquals(left, right, "");
137
    }
138
139
    /**
140
     * asserts that two lists are equal in value and order.
141
     * <br><br>
142
     * For example, (a, b) is equal to (a, b)
143
     * Does not expect null as an input value.
144
     * Two empty lists are considered equal.
145
     * <br><br>
146
     * @param failureMessage a failureMessage that should be shown if this assertion fails
147
     */
148
    public static <T> void assertEquals(List<T> left, List<T> right, String failureMessage) {
149 1 1. assertEquals : negated conditional → KILLED
        if (left.size() != right.size()) {
150
            throw new TestFailureException(
151
                    String.format("different sizes: left was %d, right was %d. %s", left.size(), right.size(), failureMessage));
152
        }
153 2 1. assertEquals : negated conditional → KILLED
2. assertEquals : changed conditional boundary → KILLED
        for (int i = 0; i < left.size(); i++) {
154 1 1. assertEquals : negated conditional → KILLED
            if (!left.get(i).equals(right.get(i))) {
155
                throw new TestFailureException(
156
                        String.format("different values - left: \"%s\" right: \"%s\". %s", showWhiteSpace(left.get(i).toString()), showWhiteSpace(right.get(i).toString()), failureMessage));
157
            }
158
        }
159
    }
160
161
    public static void assertTrue(boolean value) {
162 1 1. assertTrue : removed call to com/renomad/minum/testing/TestFramework::assertTrue → KILLED
      assertTrue(value, "");
163
    }
164
165
    /**
166
     * Assert that something is true, and show a message if it fails. This
167
     * is also handy for including a kind of documentation in your test
168
     * code.  So, please carefully note this example of its use, because
169
     * there's a certain subtlety at play:
170
     * <p>
171
     *     <pre>
172
     *      {@code assertTrue(foo == true, "foo must be true");}
173
     *     </pre>
174
     * </p>
175
     * <p>
176
     * Notice something here: The message is a statement about what *should*
177
     * be true.  Sometimes, I see people who do it wrong here - they
178
     * add a message like *foo was wrong*, but that's a disconcerting
179
     * thing to see in a test.  Do it like the example above, instead.
180
     * </p>
181
     * <p>
182
     *     One other detail to mention: If this test fails, it doesn't really
183
     *     give you much help about what the value should have been, it merely
184
     *     insists it be true.  In some cases, like where you are
185
     *     asserting that a string contains a substring, it is handy to include
186
     *     what you were looking for and what the string was as part of the
187
     *     failure message.
188
     * </p>
189
     */
190
    public static void assertTrue(boolean value, String failureMessage) {
191 1 1. assertTrue : negated conditional → KILLED
        if (!value) {
192
            throw new TestFailureException(failureMessage);
193
        }
194
    }
195
196
    public static void assertFalse(boolean value) {
197 1 1. assertFalse : negated conditional → KILLED
        if (value) {
198
            throw new TestFailureException("value was unexpectedly true");
199
        }
200
    }
201
202
    public static void assertFalse(boolean value, String failureMessage) {
203 1 1. assertFalse : negated conditional → KILLED
        if (value) {
204
            throw new TestFailureException(failureMessage);
205
        }
206
    }
207
208
    /**
209
     * Given a string that may have whitespace chars,
210
     * render it in a way we can see.
211
     * <p>
212
     *     More specifically, it will replace tabs with (TAB),
213
     *     newlines with (NEWLINE), carriage returns with (RETURN).
214
     *     Also, if the entire text is empty (it's got a 0 length), you'll
215
     *     get back (EMPTY), and if blank (it's full of whitespace),
216
     *     you'll get back (BLANK).
217
     * </p>
218
     * <p>
219
     *     Note that this method is not very performant.  It carries out
220
     *     its work through multiple string replacements, so it's
221
     *     basically O(3*n) (that is, it scans through
222
     *     the whole string three times).
223
     * </p>
224
     */
225
    static String showWhiteSpace(String msg) {
226 2 1. showWhiteSpace : replaced return value with "" for com/renomad/minum/testing/TestFramework::showWhiteSpace → KILLED
2. showWhiteSpace : negated conditional → KILLED
        if (msg == null) return "(NULL)";
227 2 1. showWhiteSpace : replaced return value with "" for com/renomad/minum/testing/TestFramework::showWhiteSpace → KILLED
2. showWhiteSpace : negated conditional → KILLED
        if (msg.isEmpty()) return "(EMPTY)";
228
229
        // if we have tabs, returns, newlines in the text, show them
230
        String text = msg
231
                .replace("\t", "\\t")
232
                .replace("\r", "\\r")
233
                .replace("\n", "\\n");
234
235 2 1. showWhiteSpace : replaced return value with "" for com/renomad/minum/testing/TestFramework::showWhiteSpace → KILLED
2. showWhiteSpace : negated conditional → KILLED
        if (text.isBlank()) return "(BLANK)";
236 1 1. showWhiteSpace : replaced return value with "" for com/renomad/minum/testing/TestFramework::showWhiteSpace → KILLED
        return text;
237
    }
238
239
    public static Context buildTestingContext(String loggerName) {
240 1 1. buildTestingContext : replaced return value with null for com/renomad/minum/testing/TestFramework::buildTestingContext → KILLED
        return buildTestingContext(loggerName, null);
241
    }
242
243
    /**
244
     * This builds a context very similarly to {@link FullSystem#buildContext()},
245
     * except that it uses {@link TestLogger} instead of {@link com.renomad.minum.logging.Logger}
246
     * @param loggerName this will assign a human-readable name to the logger's
247
     *                   LoggingActionQueue so we can distinguish it
248
     *                   when reviewing the threads
249
     * @param properties If you want, you can inject a properties object here, to have
250
     *                   greater control over your test.  Using a parameter of null here
251
     *                   will cause the system to obtain properties from the minum.config file
252
     */
253
    public static Context buildTestingContext(String loggerName, Properties properties) {
254
        var constants = new Constants(properties);
255
        var executorService = Executors.newVirtualThreadPerTaskExecutor();
256
        var logger = new TestLogger(constants, executorService, loggerName);
257
258
        var context = new Context(executorService, constants);
259
260 1 1. buildTestingContext : removed call to com/renomad/minum/state/Context::setLogger → KILLED
        context.setLogger(logger);
261
262 1 1. buildTestingContext : replaced return value with null for com/renomad/minum/testing/TestFramework::buildTestingContext → KILLED
        return context;
263
    }
264
265
    public static void shutdownTestingContext(Context context) {
266 1 1. shutdownTestingContext : removed call to com/renomad/minum/queue/ActionQueueKiller::killAllQueues → TIMED_OUT
            new ActionQueueKiller(context).killAllQueues();
267
            context.getLogger().stop();
268
            context.getExecutorService().shutdownNow();
269
    }
270
271
}

Mutations

40

1.1
Location : assertThrows
Killed by : com.renomad.minum.utils.InvariantsTests.test_MustBeTrue(com.renomad.minum.utils.InvariantsTests)
replaced return value with null for com/renomad/minum/testing/TestFramework::assertThrows → KILLED

48

1.1
Location : assertThrows
Killed by : com.renomad.minum.testing.TestFrameworkTests.test_assertEqualsByteArray_RightIsNull(com.renomad.minum.testing.TestFrameworkTests)
removed call to com/renomad/minum/utils/ThrowingRunnable::run → KILLED

51

1.1
Location : assertThrows
Killed by : com.renomad.minum.testing.TestFrameworkTests.test_assertEqualsByteArray_RightIsNull(com.renomad.minum.testing.TestFrameworkTests)
negated conditional → KILLED

55

1.1
Location : assertThrows
Killed by : com.renomad.minum.testing.TestFrameworkTests.test_assertEqualsByteArray_RightIsNull(com.renomad.minum.testing.TestFrameworkTests)
negated conditional → KILLED

2.2
Location : assertThrows
Killed by : com.renomad.minum.utils.InvariantsTests.test_MustBeTrue(com.renomad.minum.utils.InvariantsTests)
negated conditional → KILLED

59

1.1
Location : assertThrows
Killed by : com.renomad.minum.utils.InvariantsTests.test_MustBeTrue(com.renomad.minum.utils.InvariantsTests)
replaced return value with null for com/renomad/minum/testing/TestFramework::assertThrows → KILLED

69

1.1
Location : assertEquals
Killed by : com.renomad.minum.htmlparsing.HtmlParseNodeTests.test_recursiveTreeWalk(com.renomad.minum.htmlparsing.HtmlParseNodeTests)
negated conditional → KILLED

78

1.1
Location : assertEqualByteArray
Killed by : com.renomad.minum.testing.TestFrameworkTests.test_assertEqualsByteArray_CustomError_ButValidComparison(com.renomad.minum.testing.TestFrameworkTests)
negated conditional → KILLED

2.2
Location : assertEqualByteArray
Killed by : com.renomad.minum.testing.TestFrameworkTests.test_assertEqualsByteArray_CustomError_ButValidComparison(com.renomad.minum.testing.TestFrameworkTests)
negated conditional → KILLED

79

1.1
Location : assertEqualByteArray
Killed by : com.renomad.minum.testing.TestFrameworkTests.test_assertEqualsByteArray_CustomError_ButValidComparison(com.renomad.minum.testing.TestFrameworkTests)
negated conditional → KILLED

80

1.1
Location : assertEqualByteArray
Killed by : com.renomad.minum.utils.ByteUtilsTests.testConversionToArray(com.renomad.minum.utils.ByteUtilsTests)
negated conditional → KILLED

2.2
Location : assertEqualByteArray
Killed by : com.renomad.minum.testing.TestFrameworkTests.test_assertEqualsByteArray_CustomError_ButValidComparison(com.renomad.minum.testing.TestFrameworkTests)
changed conditional boundary → KILLED

81

1.1
Location : assertEqualByteArray
Killed by : com.renomad.minum.testing.TestFrameworkTests.test_assertEqualsByteArray_CustomError_ButValidComparison(com.renomad.minum.testing.TestFrameworkTests)
negated conditional → KILLED

87

1.1
Location : assertEqualByteArray
Killed by : com.renomad.minum.testing.TestFrameworkTests.test_assertEqualsByteArray_CustomError(com.renomad.minum.testing.TestFrameworkTests)
removed call to com/renomad/minum/testing/TestFramework::assertEqualByteArray → KILLED

101

1.1
Location : assertEqualsDisregardOrder
Killed by : com.renomad.minum.testing.TestFrameworkTests.test_assertEquals_ListsDifferentOrders(com.renomad.minum.testing.TestFrameworkTests)
negated conditional → KILLED

107

1.1
Location : assertEqualsDisregardOrder
Killed by : com.renomad.minum.testing.TestFrameworkTests.test_assertEquals_ListsDifferentOrders(com.renomad.minum.testing.TestFrameworkTests)
changed conditional boundary → KILLED

2.2
Location : assertEqualsDisregardOrder
Killed by : com.renomad.minum.testing.TestFrameworkTests.test_assertEquals_ListsDifferentOrders(com.renomad.minum.testing.TestFrameworkTests)
negated conditional → KILLED

108

1.1
Location : assertEqualsDisregardOrder
Killed by : com.renomad.minum.testing.TestFrameworkTests.test_assertEquals_ListsDifferentOrders(com.renomad.minum.testing.TestFrameworkTests)
negated conditional → KILLED

122

1.1
Location : assertEqualsDisregardOrder
Killed by : com.renomad.minum.testing.TestFrameworkTests.test_assertEquals_ListsDifferentOrders(com.renomad.minum.testing.TestFrameworkTests)
removed call to com/renomad/minum/testing/TestFramework::assertEqualsDisregardOrder → KILLED

136

1.1
Location : assertEquals
Killed by : none
removed call to com/renomad/minum/testing/TestFramework::assertEquals → TIMED_OUT

149

1.1
Location : assertEquals
Killed by : com.renomad.minum.state.ConstantsTests.testGetProps_Array(com.renomad.minum.state.ConstantsTests)
negated conditional → KILLED

153

1.1
Location : assertEquals
Killed by : com.renomad.minum.htmlparsing.HtmlParseNodeTests.testHappyPath(com.renomad.minum.htmlparsing.HtmlParseNodeTests)
negated conditional → KILLED

2.2
Location : assertEquals
Killed by : com.renomad.minum.state.ConstantsTests.testGetProps_Array(com.renomad.minum.state.ConstantsTests)
changed conditional boundary → KILLED

154

1.1
Location : assertEquals
Killed by : com.renomad.minum.state.ConstantsTests.testGetProps_Array(com.renomad.minum.state.ConstantsTests)
negated conditional → KILLED

162

1.1
Location : assertTrue
Killed by : com.renomad.minum.web.FullSystemTests.testFullSystem_WithRedirect(com.renomad.minum.web.FullSystemTests)
removed call to com/renomad/minum/testing/TestFramework::assertTrue → KILLED

191

1.1
Location : assertTrue
Killed by : com.renomad.minum.testing.RegexUtilsTests.test_isFound(com.renomad.minum.testing.RegexUtilsTests)
negated conditional → KILLED

197

1.1
Location : assertFalse
Killed by : com.renomad.minum.testing.RegexUtilsTests.test_isFound(com.renomad.minum.testing.RegexUtilsTests)
negated conditional → KILLED

203

1.1
Location : assertFalse
Killed by : com.renomad.minum.testing.TestFrameworkTests.test_assertFalse_WithMessage(com.renomad.minum.testing.TestFrameworkTests)
negated conditional → KILLED

226

1.1
Location : showWhiteSpace
Killed by : com.renomad.minum.testing.TestFrameworkTests.testShowWhiteSpace(com.renomad.minum.testing.TestFrameworkTests)
replaced return value with "" for com/renomad/minum/testing/TestFramework::showWhiteSpace → KILLED

2.2
Location : showWhiteSpace
Killed by : com.renomad.minum.testing.TestFrameworkTests.testShowWhiteSpace(com.renomad.minum.testing.TestFrameworkTests)
negated conditional → KILLED

227

1.1
Location : showWhiteSpace
Killed by : com.renomad.minum.testing.TestFrameworkTests.testShowWhiteSpace(com.renomad.minum.testing.TestFrameworkTests)
replaced return value with "" for com/renomad/minum/testing/TestFramework::showWhiteSpace → KILLED

2.2
Location : showWhiteSpace
Killed by : com.renomad.minum.testing.TestFrameworkTests.testShowWhiteSpace(com.renomad.minum.testing.TestFrameworkTests)
negated conditional → KILLED

235

1.1
Location : showWhiteSpace
Killed by : com.renomad.minum.testing.TestFrameworkTests.testShowWhiteSpace(com.renomad.minum.testing.TestFrameworkTests)
replaced return value with "" for com/renomad/minum/testing/TestFramework::showWhiteSpace → KILLED

2.2
Location : showWhiteSpace
Killed by : com.renomad.minum.testing.TestFrameworkTests.testShowWhiteSpace(com.renomad.minum.testing.TestFrameworkTests)
negated conditional → KILLED

236

1.1
Location : showWhiteSpace
Killed by : com.renomad.minum.testing.TestFrameworkTests.testShowWhiteSpace(com.renomad.minum.testing.TestFrameworkTests)
replaced return value with "" for com/renomad/minum/testing/TestFramework::showWhiteSpace → KILLED

240

1.1
Location : buildTestingContext
Killed by : com.renomad.minum.logging.TestLoggerTests.test_TestLogger_MaxLines(com.renomad.minum.logging.TestLoggerTests)
replaced return value with null for com/renomad/minum/testing/TestFramework::buildTestingContext → KILLED

260

1.1
Location : buildTestingContext
Killed by : com.renomad.minum.logging.TestLoggerTests.test_TestLogger_MaxLines(com.renomad.minum.logging.TestLoggerTests)
removed call to com/renomad/minum/state/Context::setLogger → KILLED

262

1.1
Location : buildTestingContext
Killed by : com.renomad.minum.logging.TestLoggerTests.test_TestLogger_MaxLines(com.renomad.minum.logging.TestLoggerTests)
replaced return value with null for com/renomad/minum/testing/TestFramework::buildTestingContext → KILLED

266

1.1
Location : shutdownTestingContext
Killed by : none
removed call to com/renomad/minum/queue/ActionQueueKiller::killAllQueues → TIMED_OUT

Active mutators

Tests examined


Report generated by PIT 1.17.0