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