Skip to content

Commit

Permalink
Issue checkstyle#3110: Modified UTs to support localized messages fro…
Browse files Browse the repository at this point in the history
…m DefaultLogger
  • Loading branch information
subkrish committed Jun 30, 2017
1 parent 7e828cf commit 8f212e3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@
import org.junit.Test;

import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;

public class DefaultLoggerTest {

@Test
public void testCtor() throws UnsupportedEncodingException {
final LocalizedMessage addExceptionMessage = new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, "DefaultLogger.addException",
new String[] {"myfile"}, null,
LocalizedMessage.class, null);
final OutputStream infoStream = new ByteArrayOutputStream();
final ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
final DefaultLogger dl = new DefaultLogger(infoStream, true, errorStream, true);
dl.addException(new AuditEvent(5000, "myfile"), new IllegalStateException("upsss"));
dl.auditFinished(new AuditEvent(6000, "myfile"));
final String output = errorStream.toString("UTF-8");

assertTrue(output.contains("Error auditing myfile"));
assertTrue(output.contains(addExceptionMessage.getMessage()));
assertTrue(output.contains("java.lang.IllegalStateException: upsss"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import java.util.List;
import java.util.Map;

import com.puppycrawl.tools.checkstyle.*;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import org.apache.commons.io.FileUtils;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
Expand All @@ -56,11 +58,6 @@
import org.powermock.modules.junit4.PowerMockRunner;

import com.google.common.io.Closeables;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultLogger;
import com.puppycrawl.tools.checkstyle.PackageNamesLoader;
import com.puppycrawl.tools.checkstyle.TestRootModuleChecker;
import com.puppycrawl.tools.checkstyle.XMLLogger;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;

@RunWith(PowerMockRunner.class)
Expand Down Expand Up @@ -290,6 +287,14 @@ public final void testOverrideProperty() throws IOException {

@Test
public final void testExecuteIgnoredModules() throws IOException {
final LocalizedMessage auditStartedMessage = new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, "DefaultLogger.auditStarted",
null, null,
LocalizedMessage.class, null);
final LocalizedMessage auditFinishedMessage = new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, "DefaultLogger.auditFinished",
null, null,
LocalizedMessage.class, null);
final CheckstyleAntTask antTask = getCheckstyleAntTask();
antTask.setFile(new File(getPath(VIOLATED_INPUT)));
antTask.setFailOnViolation(false);
Expand All @@ -308,7 +313,7 @@ public final void testExecuteIgnoredModules() throws IOException {

final List<String> output = FileUtils.readLines(outputFile);
final String errorMessage = "Content of file with violations differs from expected";
assertEquals(errorMessage, "Starting audit...", output.get(0));
assertEquals(errorMessage, auditStartedMessage.getMessage(), output.get(0));
assertTrue(errorMessage, output.get(1).startsWith("[WARN]"));
assertTrue(errorMessage, output.get(1).endsWith("InputCheckstyleAntTaskError.java:4: "
+ "@incomplete=Some javadoc [WriteTag]"));
Expand All @@ -318,7 +323,7 @@ public final void testExecuteIgnoredModules() throws IOException {
assertTrue(errorMessage, output.get(3).startsWith("[ERROR]"));
assertTrue(errorMessage, output.get(3).endsWith("InputCheckstyleAntTaskError.java:9: "
+ "Line is longer than 70 characters (found 81). [LineLength]"));
assertEquals(errorMessage, "Audit done.", output.get(4));
assertEquals(errorMessage, auditFinishedMessage.getMessage(), output.get(4));
}

@Test
Expand Down Expand Up @@ -536,6 +541,14 @@ public void testCheckerException() throws IOException {

@Test
public final void testExecuteLogOutput() throws Exception {
final LocalizedMessage auditStartedMessage = new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, "DefaultLogger.auditStarted",
null, null,
LocalizedMessage.class, null);
final LocalizedMessage auditFinishedMessage = new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, "DefaultLogger.auditFinished",
null, null,
LocalizedMessage.class, null);
final CheckstyleAntTaskLogStub antTask = new CheckstyleAntTaskLogStub();
final URL url = new File(getPath(CONFIG_FILE)).toURI().toURL();
antTask.setProject(new Project());
Expand All @@ -554,8 +567,8 @@ public final void testExecuteLogOutput() throws Exception {
new MessageLevelPair("To locate the files took 0 ms.", Project.MSG_VERBOSE),
new MessageLevelPair("Running Checkstyle ", Project.MSG_INFO),
new MessageLevelPair("Using configuration ", Project.MSG_VERBOSE),
new MessageLevelPair("Starting audit", Project.MSG_DEBUG),
new MessageLevelPair("Audit done.", Project.MSG_DEBUG),
new MessageLevelPair(auditStartedMessage.getMessage(), Project.MSG_DEBUG),
new MessageLevelPair(auditFinishedMessage.getMessage(), Project.MSG_DEBUG),
new MessageLevelPair("To process the files took 0 ms.", Project.MSG_VERBOSE),
new MessageLevelPair("Total execution took 0 ms.", Project.MSG_VERBOSE)
);
Expand Down

0 comments on commit 8f212e3

Please sign in to comment.