Skip to content

Commit ab7a33c

Browse files
Carbon14-48romani
authored andcommitted
Issue #16361: Refactor testNewCtor
1 parent 70cab66 commit ab7a33c

1 file changed

Lines changed: 46 additions & 41 deletions

File tree

src/test/java/com/puppycrawl/tools/checkstyle/DefaultLoggerTest.java

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.io.IOException;
2727
import java.io.OutputStream;
2828
import java.nio.charset.StandardCharsets;
29-
import java.text.MessageFormat;
3029
import java.util.Arrays;
3130
import java.util.Locale;
3231
import java.util.ResourceBundle;
@@ -284,47 +283,53 @@ public void testCountryIsValid() {
284283

285284
@Test
286285
public void testNewCtor() throws Exception {
287-
final ResourceBundle bundle = ResourceBundle.getBundle(
288-
Definitions.CHECKSTYLE_BUNDLE, Locale.ENGLISH);
289-
final String auditStartedMessage = bundle.getString(DefaultLogger.AUDIT_STARTED_MESSAGE);
290-
final String auditFinishedMessage = bundle.getString(DefaultLogger.AUDIT_FINISHED_MESSAGE);
291-
final String addExceptionMessage = new MessageFormat(bundle.getString(
292-
DefaultLogger.ADD_EXCEPTION_MESSAGE), Locale.ENGLISH).format(new String[] {"myfile"}
293-
);
294-
final String infoOutput;
295-
final String errorOutput;
296-
try (MockByteArrayOutputStream infoStream = new MockByteArrayOutputStream()) {
297-
try (MockByteArrayOutputStream errorStream = new MockByteArrayOutputStream()) {
298-
final DefaultLogger dl = new DefaultLogger(
299-
infoStream, OutputStreamOptions.CLOSE,
300-
errorStream, OutputStreamOptions.CLOSE);
301-
dl.auditStarted(null);
302-
dl.addException(new AuditEvent(5000, "myfile"),
303-
new IllegalStateException("oops"));
304-
dl.auditFinished(new AuditEvent(6000, "myfile"));
305-
infoOutput = infoStream.toString(StandardCharsets.UTF_8);
306-
errorOutput = errorStream.toString(StandardCharsets.UTF_8);
307-
308-
assertWithMessage("Info stream should be closed")
309-
.that(infoStream.closedCount)
310-
.isGreaterThan(0);
311-
assertWithMessage("Error stream should be closed")
312-
.that(errorStream.closedCount)
313-
.isGreaterThan(0);
314-
}
286+
final String inputFile = "InputDefaultLoggerTestException.java";
287+
final String expectedInfoFile = "ExpectedDefaultLoggerInfoDefaultOutput.txt";
288+
final String expectedErrorFile = "ExpectedDefaultLoggerErrorsTestException.txt";
289+
290+
try (MockByteArrayOutputStream infoStream = new MockByteArrayOutputStream();
291+
MockByteArrayOutputStream errorStream = new MockByteArrayOutputStream()) {
292+
final DefaultLogger dl = new DefaultLogger(
293+
infoStream, OutputStreamOptions.CLOSE,
294+
errorStream, OutputStreamOptions.CLOSE);
295+
296+
verifyWithInlineConfigParserAndDefaultLogger(
297+
getNonCompilablePath(inputFile),
298+
getPath(expectedInfoFile),
299+
getPath(expectedErrorFile),
300+
dl, infoStream, errorStream);
301+
302+
assertWithMessage("Info stream should be closed")
303+
.that(infoStream.closedCount)
304+
.isGreaterThan(0);
305+
assertWithMessage("Error stream should be closed")
306+
.that(errorStream.closedCount)
307+
.isGreaterThan(0);
308+
}
309+
}
310+
311+
@Test
312+
public void testAddException() throws Exception {
313+
try (MockByteArrayOutputStream errorStream = new MockByteArrayOutputStream()) {
314+
final DefaultLogger dl = new DefaultLogger(
315+
new ByteArrayOutputStream(), OutputStreamOptions.CLOSE,
316+
errorStream, OutputStreamOptions.CLOSE);
317+
318+
dl.addException(new AuditEvent(5000, "myfile"),
319+
new IllegalStateException("oops"));
320+
321+
final String errorOutput = errorStream.toString(StandardCharsets.UTF_8);
322+
323+
assertWithMessage("Exception output should contain filename")
324+
.that(errorOutput)
325+
.contains("myfile");
326+
assertWithMessage("Exception output should contain exception type")
327+
.that(errorOutput)
328+
.contains("java.lang.IllegalStateException");
329+
assertWithMessage("Exception output should contain exception message")
330+
.that(errorOutput)
331+
.contains("oops");
315332
}
316-
assertWithMessage("Violation should contain message `audit started`")
317-
.that(infoOutput)
318-
.contains(auditStartedMessage);
319-
assertWithMessage("Violation should contain message `audit finished`")
320-
.that(infoOutput)
321-
.contains(auditFinishedMessage);
322-
assertWithMessage("Violation should contain exception info")
323-
.that(errorOutput)
324-
.contains(addExceptionMessage);
325-
assertWithMessage("Violation should contain exception message")
326-
.that(errorOutput)
327-
.contains("java.lang.IllegalStateException: oops");
328333
}
329334

330335
@Test

0 commit comments

Comments
 (0)