123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- /*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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 this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- package org.sonar.scanner.scan.report;
-
- import java.io.File;
- import java.io.IOException;
- import java.io.StringWriter;
- import java.text.SimpleDateFormat;
- import java.util.Collections;
- import java.util.TimeZone;
- import org.apache.commons.io.IOUtils;
- import org.junit.Before;
- import org.junit.Rule;
- import org.junit.Test;
- import org.junit.rules.TemporaryFolder;
- import org.sonar.api.batch.bootstrap.ProjectDefinition;
- import org.sonar.api.batch.fs.InputFile;
- import org.sonar.api.batch.fs.internal.DefaultFileSystem;
- import org.sonar.api.batch.fs.internal.DefaultInputFile;
- import org.sonar.api.batch.fs.internal.DefaultInputProject;
- import org.sonar.api.batch.fs.internal.InputModuleHierarchy;
- import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
- import org.sonar.api.batch.rule.Rules;
- import org.sonar.api.batch.rule.internal.RulesBuilder;
- import org.sonar.api.config.internal.MapSettings;
- import org.sonar.api.issue.Issue;
- import org.sonar.api.platform.Server;
- import org.sonar.api.rule.RuleKey;
- import org.sonar.scanner.issue.IssueCache;
- import org.sonar.scanner.issue.tracking.TrackedIssue;
- import org.sonar.scanner.scan.branch.BranchConfiguration;
- import org.sonar.scanner.scan.filesystem.InputComponentStore;
-
- import static net.javacrumbs.jsonunit.assertj.JsonAssert.assertThatJson;
- import static org.assertj.core.api.Assertions.assertThat;
- import static org.mockito.Mockito.mock;
- import static org.mockito.Mockito.verifyZeroInteractions;
- import static org.mockito.Mockito.when;
-
- public class JSONReportTest {
-
- private SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- private JSONReport jsonReport;
- private DefaultFileSystem fs;
- private Server server = mock(Server.class);
- private Rules rules = mock(Rules.class);
- private MapSettings settings = new MapSettings();
- private IssueCache issueCache = mock(IssueCache.class);
- private InputModuleHierarchy moduleHierarchy;
-
- @Before
- public void before() throws Exception {
- moduleHierarchy = mock(InputModuleHierarchy.class);
- File projectBaseDir = temp.newFolder();
- fs = new DefaultFileSystem(projectBaseDir.toPath());
- SIMPLE_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+02:00"));
- when(server.getVersion()).thenReturn("3.6");
-
- ProjectDefinition def = ProjectDefinition.create().setBaseDir(projectBaseDir).setWorkDir(temp.newFolder()).setKey("struts");
- DefaultInputProject project = new DefaultInputProject(def, 1);
- InputComponentStore inputComponentStore = new InputComponentStore(mock(BranchConfiguration.class));
-
- DefaultInputFile inputFile = new TestInputFileBuilder("struts", "src/main/java/org/apache/struts/Action.java")
- .setModuleBaseDir(projectBaseDir.toPath()).build();
- inputFile.setStatus(InputFile.Status.CHANGED);
- inputFile.setPublished(true);
- inputComponentStore.put("struts", inputFile);
-
- RulesBuilder builder = new RulesBuilder();
- builder.add(RuleKey.of("squid", "AvoidCycles")).setName("Avoid Cycles");
- rules = builder.build();
- jsonReport = new JSONReport(settings.asConfig(), fs, server, rules, issueCache, project, inputComponentStore);
- }
-
- @Test
- public void should_write_json() throws Exception {
- TrackedIssue issue = new TrackedIssue();
- issue.setKey("200");
- issue.setComponentKey("struts:src/main/java/org/apache/struts/Action.java");
- issue.setRuleKey(RuleKey.of("squid", "AvoidCycles"));
- issue.setMessage("There are 2 cycles");
- issue.setSeverity("MINOR");
- issue.setStatus(Issue.STATUS_OPEN);
- issue.setResolution(null);
- issue.setStartLine(1);
- issue.setEndLine(2);
- issue.setStartLineOffset(3);
- issue.setEndLineOffset(4);
- issue.setGap(3.14);
- issue.setAssignee("simon");
- issue.setCreationDate(SIMPLE_DATE_FORMAT.parse("2013-04-24"));
- issue.setNew(false);
- when(issueCache.all()).thenReturn(Collections.singleton(issue));
-
- StringWriter writer = new StringWriter();
- jsonReport.writeJson(writer);
-
- assertThatJson(writer.toString()).isEqualTo(IOUtils.toString(this.getClass().getResource(this.getClass().getSimpleName() + "/report.json")));
- }
-
- @Test
- public void should_exclude_resolved_issues() throws Exception {
- RuleKey ruleKey = RuleKey.of("squid", "AvoidCycles");
- TrackedIssue issue = new TrackedIssue();
- issue.setKey("200");
- issue.setComponentKey("struts:src/main/java/org/apache/struts/Action.java");
- issue.setRuleKey(ruleKey);
- issue.setStatus(Issue.STATUS_CLOSED);
- issue.setResolution(Issue.RESOLUTION_FIXED);
- issue.setCreationDate(SIMPLE_DATE_FORMAT.parse("2013-04-24"));
- issue.setNew(false);
- when(issueCache.all()).thenReturn(Collections.singleton(issue));
-
- StringWriter writer = new StringWriter();
- jsonReport.writeJson(writer);
-
- assertThatJson(writer.toString()).isEqualTo(IOUtils.toString(this.getClass().getResource(this.getClass().getSimpleName() + "/report-without-resolved-issues.json")));
- }
-
- @Test
- public void should_not_export_by_default() throws IOException {
- File workDir = temp.newFolder("sonar");
- fs.setWorkDir(workDir.toPath());
-
- jsonReport.execute();
-
- verifyZeroInteractions(issueCache);
- }
-
- @Test
- public void should_export_issues_to_file() throws IOException {
- File workDir = temp.newFolder("sonar");
- fs.setWorkDir(workDir.toPath());
-
- when(issueCache.all()).thenReturn(Collections.<TrackedIssue>emptyList());
-
- settings.setProperty("sonar.report.export.path", "output.json");
-
- jsonReport.execute();
-
- assertThat(new File(workDir, "output.json")).exists();
- }
-
- }
|