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.findbugs;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.api.utils.SonarException;
29 import java.net.URISyntaxException;
30 import java.util.List;
32 import static org.fest.assertions.Assertions.assertThat;
34 public class FindbugsXmlReportParserTest {
37 public ExpectedException thrown = ExpectedException.none();
39 private List<FindbugsXmlReportParser.XmlBugInstance> violations;
43 File findbugsXmlReport = getFile("/org/sonar/plugins/findbugs/findbugsReport.xml");
44 FindbugsXmlReportParser xmlParser = new FindbugsXmlReportParser(findbugsXmlReport);
45 violations = xmlParser.getBugInstances();
49 public void createFindbugsXmlReportParserWithUnexistedReportFile() {
50 File xmlReport = new File("doesntExist.xml");
51 thrown.expect(SonarException.class);
52 thrown.expectMessage("The findbugs XML report can't be found at '" + xmlReport.getAbsolutePath() + "'");
53 new FindbugsXmlReportParser(xmlReport);
57 public void testGetViolations() {
58 assertThat(violations.size()).isEqualTo(2);
60 FindbugsXmlReportParser.XmlBugInstance fbViolation = violations.get(0);
61 assertThat(fbViolation.getType()).isEqualTo("AM_CREATES_EMPTY_ZIP_FILE_ENTRY");
62 assertThat(fbViolation.getLongMessage()).isEqualTo("Empty zip file entry created in org.sonar.commons.ZipUtils._zip(String, File, ZipOutputStream)");
64 FindbugsXmlReportParser.XmlSourceLineAnnotation sourceLine = fbViolation.getPrimarySourceLine();
65 assertThat(sourceLine.getStart()).isEqualTo(107);
66 assertThat(sourceLine.getEnd()).isEqualTo(107);
67 assertThat(sourceLine.getClassName()).isEqualTo("org.sonar.commons.ZipUtils");
71 public void testGetSonarJavaFileKey() {
72 FindbugsXmlReportParser.XmlSourceLineAnnotation sourceLine = new FindbugsXmlReportParser.XmlSourceLineAnnotation();
73 sourceLine.className = "org.sonar.batch.Sensor";
74 assertThat(sourceLine.getSonarJavaFileKey()).isEqualTo("org.sonar.batch.Sensor");
75 sourceLine.className = "Sensor";
76 assertThat(sourceLine.getSonarJavaFileKey()).isEqualTo("Sensor");
77 sourceLine.className = "org.sonar.batch.Sensor$1";
78 assertThat(sourceLine.getSonarJavaFileKey()).isEqualTo("org.sonar.batch.Sensor");
81 private final File getFile(String filename) {
83 return new File(getClass().getResource(filename).toURI());
84 } catch (URISyntaxException e) {
85 throw new SonarException("Unable to open file " + filename, e);