| 1 | package fr.sii.ogham.testing.extension.common; | |
| 2 | ||
| 3 | import static fr.sii.ogham.testing.extension.common.TestInformationLogger.Characters.BOTTOM_LEFT; | |
| 4 | import static fr.sii.ogham.testing.extension.common.TestInformationLogger.Characters.BOTTOM_RIGHT; | |
| 5 | import static fr.sii.ogham.testing.extension.common.TestInformationLogger.Characters.HORIZONTAL; | |
| 6 | import static fr.sii.ogham.testing.extension.common.TestInformationLogger.Characters.TOP_LEFT; | |
| 7 | import static fr.sii.ogham.testing.extension.common.TestInformationLogger.Characters.TOP_RIGHT; | |
| 8 | import static fr.sii.ogham.testing.extension.common.TestInformationLogger.Characters.VERTICAL; | |
| 9 | import static fr.sii.ogham.testing.extension.common.TestInformationLogger.Characters.VERTICAL_LEFT; | |
| 10 | import static fr.sii.ogham.testing.extension.common.TestInformationLogger.Characters.VERTICAL_RIGHT; | |
| 11 | ||
| 12 | import java.nio.charset.Charset; | |
| 13 | import java.nio.charset.StandardCharsets; | |
| 14 | import java.util.Arrays; | |
| 15 | import java.util.List; | |
| 16 | import java.util.StringJoiner; | |
| 17 | ||
| 18 | import org.apache.commons.lang3.StringUtils; | |
| 19 | ||
| 20 | import com.github.jknack.handlebars.internal.text.WordUtils; | |
| 21 | ||
| 22 | /** | |
| 23 | * Write information about test. This is useful when there are many tests: | |
| 24 | * <ul> | |
| 25 | * <li>To quickly find the logs for the test</li> | |
| 26 | * <li>To quickly know if the test has failed or succeeded</li> | |
| 27 | * <li>To quickly identify the test failure</li> | |
| 28 | * <li>To quickly find failed tests</li> | |
| 29 | * </ul> | |
| 30 | * | |
| 31 | * @author Aurélien Baudet | |
| 32 | * | |
| 33 | */ | |
| 34 | @SuppressWarnings("squid:S1312") | |
| 35 | public class TestInformationLogger { | |
| 36 | public static final int DEFAULT_MAX_LENGTH = 100; | |
| 37 | public static final String DEFAULT_MARKER = "test-info"; | |
| 38 | private static final String[] SINGLE_UTF8 = { "┌", "┐", "└", "┘", "─", "│", "├", "┤" }; | |
| 39 | private static final String[] DOUBLE_UTF8 = { "╔", "╗", "╚", "╝", "═", "║", "╠", "╣" }; | |
| 40 | private static final String[] SINGLE_ASCII = { "+", "+", "+", "+", "-", "|", "+", "+" }; | |
| 41 | private static final String[] DOUBLE_ASCII = { "+", "+", "+", "+", "=", "|", "+", "+" }; | |
| 42 | ||
| 43 | private final int maxLength; | |
| 44 | private final Printer printer; | |
| 45 | private final String marker; | |
| 46 | private final String[] singleChars; | |
| 47 | private final String[] doubleChars; | |
| 48 | ||
| 49 | /** | |
| 50 | * Initializes with the default max line length (100), uses this logger as | |
| 51 | * printer and mark logs with "test-info" marker | |
| 52 | */ | |
| 53 | public TestInformationLogger() { | |
| 54 | this(DEFAULT_MAX_LENGTH); | |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * Initializes with the provided max line length. | |
| 59 | * | |
| 60 | * Uses this logger as printer and default marker ("test-info"). | |
| 61 | * | |
| 62 | * @param maxLength | |
| 63 | * the length of each line | |
| 64 | */ | |
| 65 | public TestInformationLogger(int maxLength) { | |
| 66 | this(maxLength, DEFAULT_MARKER); | |
| 67 | } | |
| 68 | ||
| 69 | /** | |
| 70 | * Initializes with the provided max line length and marker. | |
| 71 | * | |
| 72 | * Uses this logger as printer. | |
| 73 | * | |
| 74 | * @param maxLength | |
| 75 | * the length of each line | |
| 76 | * @param marker | |
| 77 | * the marker for logs | |
| 78 | */ | |
| 79 | public TestInformationLogger(int maxLength, String marker) { | |
| 80 | this(maxLength, marker, new Slf4jPrinter()); | |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * | |
| 85 | * @param maxLength | |
| 86 | * the length of each line | |
| 87 | * @param marker | |
| 88 | * the marker for logs | |
| 89 | * @param printer | |
| 90 | * the printer | |
| 91 | */ | |
| 92 | public TestInformationLogger(int maxLength, String marker, Printer printer) { | |
| 93 | super(); | |
| 94 | this.maxLength = maxLength; | |
| 95 | this.printer = printer; | |
| 96 | this.marker = marker; | |
| 97 |
3
1. <init> : negated conditional → SURVIVED 2. <init> : negated conditional → TIMED_OUT 3. <init> : negated conditional → KILLED |
this.singleChars = Charset.defaultCharset().contains(StandardCharsets.UTF_8) ? SINGLE_UTF8 : SINGLE_ASCII; |
| 98 |
3
1. <init> : negated conditional → SURVIVED 2. <init> : negated conditional → TIMED_OUT 3. <init> : negated conditional → KILLED |
this.doubleChars = Charset.defaultCharset().contains(StandardCharsets.UTF_8) ? DOUBLE_UTF8 : DOUBLE_ASCII; |
| 99 | } | |
| 100 | ||
| 101 | /** | |
| 102 | * Write the name of the test. The name is boxed to quickly see the test | |
| 103 | * name in long logs. | |
| 104 | * | |
| 105 | * @param testName | |
| 106 | * the name of the test | |
| 107 | */ | |
| 108 | public void writeStart(String testName) { | |
| 109 | // @formatter:off | |
| 110 |
3
1. writeStart : removed call to fr/sii/ogham/testing/extension/common/Printer::printHeader → SURVIVED 2. writeStart : removed call to fr/sii/ogham/testing/extension/common/Printer::printHeader → TIMED_OUT 3. writeStart : removed call to fr/sii/ogham/testing/extension/common/Printer::printHeader → KILLED |
printer.printHeader(marker, |
| 111 | borderTop(doubleChars)+"\n"+ | |
| 112 | format(testName, doubleChars)+"\n"+ | |
| 113 | borderBottom(doubleChars)); | |
| 114 | // @formatter:on | |
| 115 | } | |
| 116 | ||
| 117 | /** | |
| 118 | * Write the name of the test and "SUCCESS" message. The name is boxed to | |
| 119 | * quickly see the test name in long logs. | |
| 120 | * | |
| 121 | * @param testName | |
| 122 | * the name of the test | |
| 123 | */ | |
| 124 | public void writeSuccess(String testName) { | |
| 125 | // @formatter:off | |
| 126 |
3
1. writeSuccess : removed call to fr/sii/ogham/testing/extension/common/Printer::printSucess → SURVIVED 2. writeSuccess : removed call to fr/sii/ogham/testing/extension/common/Printer::printSucess → TIMED_OUT 3. writeSuccess : removed call to fr/sii/ogham/testing/extension/common/Printer::printSucess → KILLED |
printer.printSucess(marker, |
| 127 | borderTop(singleChars)+"\n"+ | |
| 128 | format(testName, singleChars)+"\n"+ | |
| 129 | borderMiddle(singleChars)+"\n"+ | |
| 130 | format("SUCCESS", singleChars)+"\n"+ | |
| 131 | borderBottom(singleChars)); | |
| 132 | // @formatter:on | |
| 133 | } | |
| 134 | ||
| 135 | /** | |
| 136 | * Write the name of the test, "FAILED" message and failure information. The | |
| 137 | * name is boxed to quickly see the test name in long logs. | |
| 138 | * | |
| 139 | * @param testName | |
| 140 | * the name of the test | |
| 141 | * @param e | |
| 142 | * the thrown exception | |
| 143 | */ | |
| 144 | @SuppressWarnings("squid:S4142") | |
| 145 | public void writeFailure(String testName, Throwable e) { | |
| 146 | // @formatter:off | |
| 147 |
2
1. writeFailure : removed call to fr/sii/ogham/testing/extension/common/Printer::printFailure → NO_COVERAGE 2. writeFailure : removed call to fr/sii/ogham/testing/extension/common/Printer::printFailure → KILLED |
printer.printFailure(marker, |
| 148 | borderTop(singleChars)+"\n"+ | |
| 149 | format(testName, singleChars)+"\n"+ | |
| 150 | borderMiddle(singleChars)+"\n"+ | |
| 151 | format("FAILED", singleChars)+"\n"+ | |
| 152 | borderMiddle(singleChars)+"\n"+ | |
| 153 | format(e.toString(), singleChars)+"\n"+ | |
| 154 | borderBottom(singleChars), e); | |
| 155 | // @formatter:on | |
| 156 | } | |
| 157 | ||
| 158 | private String borderTop(String[] characters) { | |
| 159 |
3
1. borderTop : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger::borderTop → SURVIVED 2. borderTop : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger::borderTop → TIMED_OUT 3. borderTop : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger::borderTop → KILLED |
return TOP_LEFT.character(characters) + dashLine(HORIZONTAL.character(characters)) + TOP_RIGHT.character(characters); |
| 160 | } | |
| 161 | ||
| 162 | private String borderMiddle(String[] characters) { | |
| 163 |
3
1. borderMiddle : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger::borderMiddle → SURVIVED 2. borderMiddle : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger::borderMiddle → TIMED_OUT 3. borderMiddle : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger::borderMiddle → KILLED |
return VERTICAL_LEFT.character(characters) + dashLine(HORIZONTAL.character(characters)) + VERTICAL_RIGHT.character(characters); |
| 164 | } | |
| 165 | ||
| 166 | private String borderBottom(String[] characters) { | |
| 167 |
3
1. borderBottom : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger::borderBottom → SURVIVED 2. borderBottom : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger::borderBottom → TIMED_OUT 3. borderBottom : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger::borderBottom → KILLED |
return BOTTOM_LEFT.character(characters) + dashLine(HORIZONTAL.character(characters)) + BOTTOM_RIGHT.character(characters); |
| 168 | } | |
| 169 | ||
| 170 | private String dashLine(String character) { | |
| 171 |
6
1. dashLine : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger::dashLine → SURVIVED 2. dashLine : Replaced integer subtraction with addition → SURVIVED 3. dashLine : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger::dashLine → TIMED_OUT 4. dashLine : Replaced integer subtraction with addition → TIMED_OUT 5. dashLine : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger::dashLine → KILLED 6. dashLine : Replaced integer subtraction with addition → KILLED |
return StringUtils.repeat(character, maxLength - 2); |
| 172 | } | |
| 173 | ||
| 174 | private String format(String text, String[] characters) { | |
| 175 | String vertical = VERTICAL.character(characters); | |
| 176 | StringJoiner joiner = new StringJoiner(vertical + "\n" + vertical, vertical, vertical); | |
| 177 | for (String line : wrap(text)) { | |
| 178 |
3
1. format : Replaced integer subtraction with addition → SURVIVED 2. format : Replaced integer subtraction with addition → TIMED_OUT 3. format : Replaced integer subtraction with addition → KILLED |
joiner.add(StringUtils.rightPad(line, maxLength - 2)); |
| 179 | } | |
| 180 |
3
1. format : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger::format → SURVIVED 2. format : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger::format → TIMED_OUT 3. format : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger::format → KILLED |
return joiner.toString(); |
| 181 | } | |
| 182 | ||
| 183 | private List<String> wrap(String text) { | |
| 184 |
5
1. wrap : replaced return value with Collections.emptyList for fr/sii/ogham/testing/extension/common/TestInformationLogger::wrap → SURVIVED 2. wrap : Replaced integer subtraction with addition → SURVIVED 3. wrap : replaced return value with Collections.emptyList for fr/sii/ogham/testing/extension/common/TestInformationLogger::wrap → TIMED_OUT 4. wrap : Replaced integer subtraction with addition → TIMED_OUT 5. wrap : replaced return value with Collections.emptyList for fr/sii/ogham/testing/extension/common/TestInformationLogger::wrap → KILLED |
return Arrays.asList(WordUtils.wrap(text.replace("\t", " "), maxLength - 3, "\n", true).split("\n")); |
| 185 | } | |
| 186 | ||
| 187 | enum Characters { | |
| 188 | TOP_LEFT(0), | |
| 189 | TOP_RIGHT(1), | |
| 190 | BOTTOM_LEFT(2), | |
| 191 | BOTTOM_RIGHT(3), | |
| 192 | HORIZONTAL(4), | |
| 193 | VERTICAL(5), | |
| 194 | VERTICAL_LEFT(6), | |
| 195 | VERTICAL_RIGHT(7); | |
| 196 | ||
| 197 | private final int pos; | |
| 198 | ||
| 199 | Characters(int pos) { | |
| 200 | this.pos = pos; | |
| 201 | } | |
| 202 | ||
| 203 | public String character(String[] characters) { | |
| 204 |
3
1. character : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger$Characters::character → SURVIVED 2. character : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger$Characters::character → TIMED_OUT 3. character : replaced return value with "" for fr/sii/ogham/testing/extension/common/TestInformationLogger$Characters::character → KILLED |
return characters[pos]; |
| 205 | } | |
| 206 | } | |
| 207 | } | |
Mutations | ||
| 97 |
1.1 2.2 3.3 |
|
| 98 |
1.1 2.2 3.3 |
|
| 110 |
1.1 2.2 3.3 |
|
| 126 |
1.1 2.2 3.3 |
|
| 147 |
1.1 2.2 |
|
| 159 |
1.1 2.2 3.3 |
|
| 163 |
1.1 2.2 3.3 |
|
| 167 |
1.1 2.2 3.3 |
|
| 171 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
| 178 |
1.1 2.2 3.3 |
|
| 180 |
1.1 2.2 3.3 |
|
| 184 |
1.1 2.2 3.3 4.4 5.5 |
|
| 204 |
1.1 2.2 3.3 |