diff --git a/src/main/java/org/sonar/plugins/scala/language/ScalaFile.java b/src/main/java/org/sonar/plugins/scala/language/ScalaFile.java deleted file mode 100644 index 91caf44..0000000 --- a/src/main/java/org/sonar/plugins/scala/language/ScalaFile.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Sonar Scala Plugin - * Copyright (C) 2011 - 2014 All contributors - * dev@sonar.codehaus.org - * - * 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 02 - */ -package org.sonar.plugins.scala.language; - -import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.resources.Language; -import org.sonar.api.resources.Qualifiers; -import org.sonar.api.resources.Resource; -import org.sonar.api.resources.Scopes; -import org.sonar.api.utils.WildcardPattern; - -/** - * This class implements a Scala source file for Sonar. - * - * @author Felix Müller - * @since 0.1 - */ -public class ScalaFile extends Resource { - - private final boolean isUnitTest; - private final String filename; - private final String longName; - private final ScalaPackage parent; - - public ScalaFile(String packageKey, String className, boolean isUnitTest) { - super(); - this.isUnitTest = isUnitTest; - filename = className.trim(); - - String key; - if (StringUtils.isBlank(packageKey)) { - packageKey = ScalaPackage.DEFAULT_PACKAGE_NAME; - key = new StringBuilder().append(packageKey).append(".").append(this.filename).toString(); - longName = filename; - } else { - packageKey = packageKey.trim(); - key = new StringBuilder().append(packageKey).append(".").append(this.filename).toString(); - longName = key; - } - parent = new ScalaPackage(packageKey); - setKey(key); - } - - @Override - public String getName() { - return filename; - } - - @Override - public String getLongName() { - return longName; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public Language getLanguage() { - return null; - } - - @Override - public String getScope() { - return Scopes.FILE; - } - - @Override - public String getQualifier() { - return isUnitTest ? Qualifiers.UNIT_TEST_FILE : Qualifiers.FILE; - } - - @Override - public ScalaPackage getParent() { - return parent; - } - - @Override - public boolean matchFilePattern(String antPattern) { - final String patternWithoutFileSuffix = StringUtils.substringBeforeLast(antPattern, "."); - final WildcardPattern matcher = WildcardPattern.create(patternWithoutFileSuffix, "."); - return matcher.match(getKey()); - } - - public boolean isUnitTest() { - return isUnitTest; - } - - /** - * Shortcut for {@link #fromInputFile(InputFile, boolean)} for source files. - */ - public static ScalaFile fromInputFile(InputFile inputFile) { - return ScalaFile.fromInputFile(inputFile, false); - } - - /** - * Creates a {@link ScalaFile} from a file in the source directories. - * - * @param inputFile the file object with relative path - * @param isUnitTest whether it is a unit test file or a source file - * @return the {@link ScalaFile} created if exists, null otherwise - */ - public static ScalaFile fromInputFile(InputFile inputFile, boolean isUnitTest) { - if (inputFile == null || inputFile.file() == null || inputFile.relativePath() == null) { - return null; - } - - String packageName = PackageResolver.resolvePackageNameOfFile(inputFile.absolutePath()); - String className = resolveClassName(inputFile); - - if (isPackageObjectInFirstLevelPackage(packageName, className)) { - String lastFolderName = extractLastFolderName(inputFile); - return new ScalaFile(StringUtils.EMPTY, lastFolderName + "." + className, isUnitTest); - } - - return new ScalaFile(packageName, className, isUnitTest); - } - - private static boolean isPackageObjectInFirstLevelPackage(String packageName, String className) { - return "".equalsIgnoreCase(packageName) && "package".equalsIgnoreCase(className); - } - - private static String extractLastFolderName(InputFile inputFile) { - String absolutePath = inputFile.absolutePath(); - int lastPathSeparator = absolutePath.lastIndexOf("/"); - return absolutePath.substring(absolutePath.lastIndexOf("/", lastPathSeparator - 1) + 1, lastPathSeparator); - } - - private static String resolveClassName(InputFile inputFile) { - String classname = inputFile.relativePath(); - if (inputFile.relativePath().indexOf('/') >= 0) { - classname = StringUtils.substringAfterLast(inputFile.relativePath(), "/"); - } - return StringUtils.substringBeforeLast(classname, "."); - } - - @Override - public String toString() { - return new ToStringBuilder(this) - .append("key", getKey()) - .append("fileName", filename) - .append("isUnitTest", isUnitTest) - .append("longName", longName) - .append("parent", parent) - .toString(); - } -} diff --git a/src/main/java/org/sonar/plugins/scala/language/ScalaPackage.java b/src/main/java/org/sonar/plugins/scala/language/ScalaPackage.java deleted file mode 100644 index c833e06..0000000 --- a/src/main/java/org/sonar/plugins/scala/language/ScalaPackage.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Sonar Scala Plugin - * Copyright (C) 2011 - 2014 All contributors - * dev@sonar.codehaus.org - * - * 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 02 - */ -package org.sonar.plugins.scala.language; - -import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.sonar.api.resources.Language; -import org.sonar.api.resources.Qualifiers; -import org.sonar.api.resources.Resource; -import org.sonar.api.resources.Scopes; -import org.sonar.api.utils.WildcardPattern; - -/** - * This class implements a logical Scala package. - * - * @author Felix Müller - * @since 0.1 - */ -@SuppressWarnings("rawtypes") -public class ScalaPackage extends Resource { - - public static final String DEFAULT_PACKAGE_NAME = "[default]"; - - public ScalaPackage(String key) { - super(); - setKey(StringUtils.defaultIfEmpty(StringUtils.trim(key), DEFAULT_PACKAGE_NAME)); - } - - @Override - public String getName() { - return getKey(); - } - - @Override - public String getLongName() { - return null; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public Language getLanguage() { - return null; - } - - @Override - public String getScope() { - return Scopes.DIRECTORY; - } - - @Override - public String getQualifier() { - return Qualifiers.PACKAGE; - } - - @Override - public Resource getParent() { - return null; - } - - @Override - public boolean matchFilePattern(String antPattern) { - String patternWithoutFileSuffix = StringUtils.substringBeforeLast(antPattern, "."); - WildcardPattern matcher = WildcardPattern.create(patternWithoutFileSuffix, "."); - return matcher.match(getKey()); - } - - @Override - public String toString() { - return new ToStringBuilder(this) - .append("key", getKey()) - .toString(); - } -} diff --git a/src/test/java/org/sonar/plugins/scala/language/ScalaFileTest.java b/src/test/java/org/sonar/plugins/scala/language/ScalaFileTest.java deleted file mode 100644 index 33ddc3d..0000000 --- a/src/test/java/org/sonar/plugins/scala/language/ScalaFileTest.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Sonar Scala Plugin - * Copyright (C) 2011 - 2014 All contributors - * dev@sonar.codehaus.org - * - * 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 02 - */ -package org.sonar.plugins.scala.language; - -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.io.File; - -import org.junit.Test; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.resources.Qualifiers; - -public class ScalaFileTest { - - @Test - public void shouldHaveFileQualifierForSourceFile() { - assertThat(new ScalaFile("package", "Class", false).getQualifier(), - equalTo(Qualifiers.FILE)); - } - - @Test - public void shouldHaveTestFileQualifierForTestFile() { - assertThat(new ScalaFile("package", "Class", true).getQualifier(), - equalTo(Qualifiers.UNIT_TEST_FILE)); - } - -// @Test -// public void shouldCreateScalaFileWithCorrectAttributes() { -// InputFile inputFile = FileTestUtils.getInputFiles("/scalaFile/", "ScalaFile", 1).get(0); -// ScalaFile scalaFile = ScalaFile.fromInputFile(inputFile); -// -// assertThat(scalaFile.getLanguage().getKey(), is(Scala.INSTANCE.getKey())); -// assertThat(scalaFile.getName(), is("ScalaFile1")); -// assertThat(scalaFile.getLongName(), is("scalaFile.ScalaFile1")); -// assertThat(scalaFile.getParent().getName(), is("scalaFile")); -// assertThat(scalaFile.isUnitTest(), is(false)); -// } -// -// @Test -// public void shouldCreateScalaTestFileWithCorrectAttributes() { -// InputFile inputFile = FileTestUtils.getInputFiles("/scalaFile/", "ScalaTestFile", 1).get(0); -// ScalaFile scalaFile = ScalaFile.fromInputFile(inputFile, true); -// -// assertThat(scalaFile.getLanguage().getKey(), is(Scala.INSTANCE.getKey())); -// assertThat(scalaFile.getName(), is("ScalaTestFile1")); -// assertThat(scalaFile.getLongName(), is("scalaFile.ScalaTestFile1")); -// assertThat(scalaFile.getParent().getName(), is("scalaFile")); -// assertThat(scalaFile.isUnitTest(), is(true)); -// } -// -// @Test -// public void shouldHandlePackeObjectsInFirstLevelProperly() { -// InputFile inputFile = InputFileUtils.create(new File("src/test/resources/"), "scalaSourceImporter/package.scala"); -// ScalaFile scalaFile = ScalaFile.fromInputFile(inputFile, false); -// -// assertThat(scalaFile.getName(), is("scalaSourceImporter.package")); -// assertThat(scalaFile.getLongName(), is(scalaFile.getName())); -// assertThat(scalaFile.getKey(), is("[default].scalaSourceImporter.package")); -// } - - @Test - public void shouldNotCreateScalaFileIfInputFileIsNull() { - assertNull(ScalaFile.fromInputFile(null)); - } - - @Test - public void shouldNotCreateScalaFileIfFileIsNull() { - InputFile inputFile = mock(InputFile.class); - when(inputFile.file()).thenReturn(null); - assertNull(ScalaFile.fromInputFile(inputFile)); - } - - @Test - public void shouldNotCreateScalaFileIfRelativePathIsNull() { - InputFile inputFile = mock(InputFile.class); - when(inputFile.file()).thenReturn(new File("")); - when(inputFile.relativePath()).thenReturn(null); - assertNull(ScalaFile.fromInputFile(inputFile)); - } -} diff --git a/src/test/java/org/sonar/plugins/scala/sensor/FakeSensorContext.java b/src/test/java/org/sonar/plugins/scala/sensor/FakeSensorContext.java index 3320105..9427536 100644 --- a/src/test/java/org/sonar/plugins/scala/sensor/FakeSensorContext.java +++ b/src/test/java/org/sonar/plugins/scala/sensor/FakeSensorContext.java @@ -19,7 +19,11 @@ */ package org.sonar.plugins.scala.sensor; -import com.google.common.collect.Sets; +import java.io.Serializable; +import java.util.Collection; +import java.util.Date; +import java.util.List; +import java.util.Set; import org.sonar.api.batch.Event; import org.sonar.api.batch.SensorContext; @@ -32,26 +36,18 @@ import org.sonar.api.resources.Resource; import org.sonar.api.rules.Violation; -import java.io.Serializable; -import java.util.Collection; -import java.util.Date; -import java.util.List; -import java.util.Set; - /** * This fake helps to detect duplicate resources in tests. */ class FakeSensorContext implements SensorContext { - private final Set resources = Sets.newHashSet(); - + + @Deprecated public void saveSource(Resource reference, String source) { - if (resources.contains(reference)) { - throw new RuntimeException("Duplicate resources in sensor context are not allowed!"); - } - resources.add(reference); + } + @Deprecated public boolean index(Resource resource) { return false; }