Java tutorial
/* * SonarQube JavaScript Plugin * Copyright (C) 2012-2016 SonarSource SA * mailto:contact 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 com.sonar.javascript.it.plugin; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import org.apache.commons.io.FileUtils; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl; import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile; import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue; import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration; import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneGlobalConfiguration; import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneSonarLintEngine; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; public class SonarLintTest { @ClassRule public static TemporaryFolder temp = new TemporaryFolder(); private static StandaloneSonarLintEngine sonarlintEngine; private static File baseDir; @BeforeClass public static void prepare() throws Exception { StandaloneGlobalConfiguration sonarLintConfig = StandaloneGlobalConfiguration.builder() .addPlugin(Tests.JAVASCRIPT_PLUGIN_LOCATION.getFile().toURI().toURL()) .setSonarLintUserHome(temp.newFolder().toPath()).setLogOutput((formattedMessage, level) -> { /* Don't pollute logs */ }) .build(); sonarlintEngine = new StandaloneSonarLintEngineImpl(sonarLintConfig); baseDir = temp.newFolder(); } @AfterClass public static void stop() { sonarlintEngine.stop(); } @Test public void should_raise_three_issues() throws IOException { ClientInputFile inputFile = prepareInputFile("foo.js", "function foo() { \n" + " var a; \n" + " var b = 1; \n" + "} \n", false); List<Issue> issues = new ArrayList<>(); sonarlintEngine.analyze(new StandaloneAnalysisConfiguration(baseDir.toPath(), temp.newFolder().toPath(), Arrays.asList(inputFile), new HashMap<String, String>()), issues::add); assertThat(issues).extracting("ruleKey", "startLine", "inputFile.path", "severity").containsOnly( tuple("javascript:UnusedVariable", 2, inputFile.getPath(), "MINOR"), tuple("javascript:UnusedVariable", 3, inputFile.getPath(), "MINOR"), tuple("javascript:S1854", 3, inputFile.getPath(), "MAJOR")); } private ClientInputFile prepareInputFile(String relativePath, String content, final boolean isTest) throws IOException { File file = new File(baseDir, relativePath); FileUtils.write(file, content, StandardCharsets.UTF_8); return createInputFile(file.toPath(), isTest); } private ClientInputFile createInputFile(final Path path, final boolean isTest) { return new ClientInputFile() { @Override public String getPath() { return path.toString(); } @Override public boolean isTest() { return isTest; } @Override public Charset getCharset() { return StandardCharsets.UTF_8; } @Override public <G> G getClientObject() { return null; } @Override public String contents() throws IOException { return new String(Files.readAllBytes(path), StandardCharsets.UTF_8); } @Override public InputStream inputStream() throws IOException { return Files.newInputStream(path); } }; } }