2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2012 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.checkstyle;
22 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
23 import org.apache.commons.io.FileUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.hamcrest.BaseMatcher;
26 import org.hamcrest.Description;
27 import org.junit.Test;
30 import java.nio.charset.Charset;
31 import java.util.Arrays;
32 import java.util.Locale;
34 import static org.hamcrest.core.Is.is;
35 import static org.junit.Assert.assertThat;
36 import static org.junit.internal.matchers.StringContains.containsString;
37 import static org.mockito.Matchers.anyObject;
38 import static org.mockito.Mockito.*;
40 public class CheckstyleExecutorTest {
43 public void execute() throws Exception {
44 CheckstyleConfiguration conf = mockConf();
45 CheckstyleAuditListener listener = mockListener();
46 CheckstyleExecutor executor = new CheckstyleExecutor(conf, listener, getClass().getClassLoader());
49 verify(listener, times(1)).auditStarted((AuditEvent) anyObject());
50 verify(listener, times(1)).auditFinished((AuditEvent) anyObject());
51 verify(listener, times(1)).fileStarted(argThat(newFilenameMatcher("Hello.java")));
52 verify(listener, times(1)).fileFinished(argThat(newFilenameMatcher("Hello.java")));
53 verify(listener, times(1)).fileStarted(argThat(newFilenameMatcher("World.java")));
54 verify(listener, times(1)).fileFinished(argThat(newFilenameMatcher("World.java")));
55 verify(listener, atLeast(1)).addError(argThat(newErrorMatcher("Hello.java", "com.puppycrawl.tools.checkstyle.checks.coding.EmptyStatementCheck")));
59 public void canGenerateXMLReport() throws Exception {
60 CheckstyleConfiguration conf = mockConf();
61 File report = new File("target/test-tmp/checkstyle-report.xml");
62 when(conf.getTargetXMLReport()).thenReturn(report);
63 CheckstyleAuditListener listener = mockListener();
64 CheckstyleExecutor executor = new CheckstyleExecutor(conf, listener, getClass().getClassLoader());
67 assertThat(report.exists(), is(true));
68 assertThat(FileUtils.readFileToString(report), containsString("<error"));
71 private BaseMatcher<AuditEvent> newErrorMatcher(final String filename, final String rule) {
72 return new BaseMatcher<AuditEvent>(){
73 public boolean matches(Object o) {
74 AuditEvent event = (AuditEvent)o;
75 return StringUtils.endsWith(event.getFileName(), filename) && StringUtils.equals(event.getSourceName(), rule);
78 public void describeTo(Description description) {
83 private BaseMatcher<AuditEvent> newFilenameMatcher(final String filename) {
84 return new BaseMatcher<AuditEvent>(){
85 public boolean matches(Object o) {
86 AuditEvent event = (AuditEvent)o;
87 return StringUtils.endsWith(event.getFileName(), filename);
90 public void describeTo(Description description) {
95 private CheckstyleAuditListener mockListener() {
96 return mock(CheckstyleAuditListener.class);
99 private CheckstyleConfiguration mockConf() throws Exception {
100 CheckstyleConfiguration conf = mock(CheckstyleConfiguration.class);
101 when(conf.getCharset()).thenReturn(Charset.defaultCharset());
102 when(conf.getCheckstyleConfiguration()).thenReturn(CheckstyleConfiguration.toCheckstyleConfiguration(new File("test-resources/checkstyle-conf.xml")));
103 when(conf.getSourceFiles()).thenReturn(Arrays.<File>asList(new File("test-resources/Hello.java"), new File("test-resources/World.java")));
104 when(conf.getLocale()).thenReturn(Locale.ENGLISH);