From f2f539eb1725cadcb71c977eb0d199c65c0cbc88 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Fri, 6 May 2011 00:53:53 +0400 Subject: [PATCH] Fix cobertura plugin to remove warning about lineId --- .../checkstyle/CheckstyleAuditListener.java | 14 +++--- .../CheckstyleAuditListenerTest.java | 50 +++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleAuditListenerTest.java diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java index 64b0ece2253..37a1f002f2b 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java @@ -84,7 +84,7 @@ public class CheckstyleAuditListener implements AuditListener, BatchExtension { } } - private String getRuleKey(AuditEvent event) { + static String getRuleKey(AuditEvent event) { String key = null; try { key = event.getModuleId(); @@ -101,7 +101,7 @@ public class CheckstyleAuditListener implements AuditListener, BatchExtension { return key; } - private String getMessage(AuditEvent event) { + static String getMessage(AuditEvent event) { try { return event.getMessage(); @@ -111,13 +111,15 @@ public class CheckstyleAuditListener implements AuditListener, BatchExtension { } } - private int getLineId(AuditEvent event) { + static Integer getLineId(AuditEvent event) { try { - return event.getLine(); + int line = event.getLine(); + // checkstyle returns 0 if there is no relation to a file content, but we use null + return line == 0 ? null : line; } catch (Exception e) { - // checkstyle can throw a NullPointer if the message is not set - return 0; + // checkstyle can throw a NullPointerException if the message is not set + return null; } } diff --git a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleAuditListenerTest.java b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleAuditListenerTest.java new file mode 100644 index 00000000000..e19cf6ae3cb --- /dev/null +++ b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleAuditListenerTest.java @@ -0,0 +1,50 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.checkstyle; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; + +import com.puppycrawl.tools.checkstyle.api.AuditEvent; +import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; +import org.junit.Test; + +public class CheckstyleAuditListenerTest { + @Test + public void testUtilityMethods() { + AuditEvent event; + + event = new AuditEvent(this, "", new LocalizedMessage(0, "", "", null, "", CheckstyleAuditListenerTest.class, "msg")); + assertThat(CheckstyleAuditListener.getLineId(event), nullValue()); + assertThat(CheckstyleAuditListener.getMessage(event), is("msg")); + assertThat(CheckstyleAuditListener.getRuleKey(event), is(CheckstyleAuditListenerTest.class.getName())); + + event = new AuditEvent(this, "", new LocalizedMessage(1, "", "", null, "", CheckstyleAuditListenerTest.class, "msg")); + assertThat(CheckstyleAuditListener.getLineId(event), is(1)); + assertThat(CheckstyleAuditListener.getMessage(event), is("msg")); + assertThat(CheckstyleAuditListener.getRuleKey(event), is(CheckstyleAuditListenerTest.class.getName())); + + event = new AuditEvent(this); + assertThat(CheckstyleAuditListener.getLineId(event), nullValue()); + assertThat(CheckstyleAuditListener.getMessage(event), nullValue()); + assertThat(CheckstyleAuditListener.getRuleKey(event), nullValue()); + } +} -- 2.39.5