import org.apache.commons.lang.ClassUtils;
import org.sonar.api.BatchExtension;
import org.sonar.api.batch.CheckProject;
-import org.sonar.api.batch.Sensor;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.resources.Project;
private boolean shouldKeep(Class type, Object extension, Project project, ExtensionMatcher matcher) {
boolean keep = ClassUtils.isAssignable(extension.getClass(), type) && (matcher == null || matcher.accept(extension));
- // For Sensors we no longer filter on shouldExecuteOnProject
- if (keep && project != null && ClassUtils.isAssignable(extension.getClass(), CheckProject.class)
- && !ClassUtils.isAssignable(extension.getClass(), Sensor.class)) {
+ if (keep && project != null && ClassUtils.isAssignable(extension.getClass(), CheckProject.class)) {
keep = ((CheckProject) extension).shouldExecuteOnProject(project);
}
return keep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.BatchComponent;
-import org.sonar.api.CoreProperties;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.maven.DependsUponMavenPlugin;
import org.sonar.api.batch.maven.MavenPluginHandler;
import org.sonar.api.database.DatabaseSession;
-import org.sonar.api.resources.Language;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.TimeProfiler;
import org.sonar.batch.bootstrap.BatchExtensionDictionnary;
import org.sonar.batch.events.EventBus;
import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
-import org.sonar.batch.scan.language.DefaultModuleLanguages;
import org.sonar.batch.scan.maven.MavenPluginExecutor;
import java.util.Collection;
private BatchExtensionDictionnary selector;
private final DatabaseSession session;
private final SensorMatcher sensorMatcher;
- private final DefaultModuleLanguages moduleLanguages;
public SensorsExecutor(BatchExtensionDictionnary selector, Project project, DefaultModuleFileSystem fs, MavenPluginExecutor mavenExecutor, EventBus eventBus,
- DatabaseSession session, SensorMatcher sensorMatcher, DefaultModuleLanguages moduleLanguages) {
+ DatabaseSession session, SensorMatcher sensorMatcher) {
this.selector = selector;
this.mavenExecutor = mavenExecutor;
this.eventBus = eventBus;
this.fs = fs;
this.session = session;
this.sensorMatcher = sensorMatcher;
- this.moduleLanguages = moduleLanguages;
}
public void execute(SensorContext context) {
// SONAR-2965 In case the sensor takes too much time we close the session to not face a timeout
session.commitAndClose();
- if (sensor.shouldExecuteOnProject(module)) {
- executeSensor(context, sensor);
- } else {
- // For backward compatibility try to execute Sensor for each language until it is executed once (or never)
- String oldLanguageKey = module.getLanguageKey();
- Language oldLanguage = module.getLanguage();
- for (Language language : moduleLanguages.languages()) {
- module.setLanguage(language);
- module.getConfiguration().setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, language.getKey());
- if (sensor.shouldExecuteOnProject(module)) {
- LOG.warn("Sensor {} should be updated to not depends on deprecated Project::getLanguage or Project::getLanguageKey", sensor);
- executeSensor(context, sensor);
- break;
- }
- }
- // Restore module language
- module.setLanguage(oldLanguage);
- module.getConfiguration().setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, oldLanguageKey);
- }
+ executeSensor(context, sensor);
}
eventBus.fireEvent(new SensorsPhaseEvent(Lists.newArrayList(sensors), false));
import org.sonar.api.batch.SonarIndex;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.File;
-import org.sonar.api.resources.Java;
-import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
for (InputFile inputFile : inputFiles) {
String languageKey = inputFile.attribute(InputFile.ATTRIBUTE_LANGUAGE);
boolean unitTest = InputFile.TYPE_TEST.equals(inputFile.attribute(InputFile.ATTRIBUTE_TYPE));
- Resource sonarFile;
String pathFromSourceDir = inputFile.attribute(DefaultInputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH);
if (pathFromSourceDir == null) {
pathFromSourceDir = inputFile.path();
}
- if (Java.KEY.equals(languageKey)) {
- sonarFile = JavaFile.create(inputFile.path(), pathFromSourceDir, unitTest);
- } else {
- sonarFile = File.create(inputFile.path(), pathFromSourceDir, languages.get(languageKey), unitTest);
+ Resource sonarFile = File.create(inputFile.path(), pathFromSourceDir, languages.get(languageKey), unitTest);
+ String deprecatedKey = inputFile.attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_DEPRECATED_KEY);
+ if (deprecatedKey != null) {
+ sonarFile.setDeprecatedKey(deprecatedKey);
}
if (sonarFile != null) {
moduleLanguages.addLanguage(languageKey);
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
import org.sonar.api.scan.filesystem.FileQuery;
-import org.sonar.api.scan.filesystem.InputDir;
import org.sonar.api.scan.filesystem.InputFile;
import org.sonar.api.scan.filesystem.ModuleFileSystem;
import org.sonar.api.scan.filesystem.internal.InputFiles;
return index.inputFile(this, ioFile);
}
- @Override
- public InputDir inputDir(File ioFile) {
- if (!ioFile.isDirectory()) {
- throw new SonarException(ioFile.getAbsolutePath() + "is not a directory");
- }
- return index.inputDir(this, ioFile);
- }
-
@Override
public List<File> files(FileQuery query) {
return InputFiles.toFiles(inputFiles(query));
package org.sonar.batch.scan.filesystem;
import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.HiddenFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.slf4j.LoggerFactory;
import org.sonar.api.BatchComponent;
import org.sonar.api.resources.Project;
-import org.sonar.api.scan.filesystem.InputDir;
import org.sonar.api.scan.filesystem.InputFile;
-import org.sonar.api.scan.filesystem.PathResolver;
-import org.sonar.api.scan.filesystem.internal.DefaultInputDir;
import org.sonar.api.scan.filesystem.InputFileFilter;
-import org.sonar.api.utils.PathUtils;
+import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.SonarException;
import java.io.File;
private final InputFileBuilderFactory inputFileBuilderFactory;
public FileIndex(List<InputFileFilter> filters, ExclusionFilters exclusionFilters, InputFileBuilderFactory inputFileBuilderFactory,
- InputFileCache cache, PathResolver pathResolver, Project project) {
+ InputFileCache cache, PathResolver pathResolver, Project project) {
this.filters = filters;
this.exclusionFilters = exclusionFilters;
this.inputFileBuilderFactory = inputFileBuilderFactory;
if (path == null) {
LoggerFactory.getLogger(getClass()).warn(String.format(
"File '%s' is not declared in module basedir %s", sourceFile.getAbsoluteFile(), fileSystem.baseDir()
- ));
+ ));
} else {
if (exclusionFilters.accept(sourceFile, path, type)) {
indexFile(inputFileBuilder, fileSystem, progress, sourceFile, path, type);
return fileCache.byPath(fileSystem.moduleKey(), path);
}
- InputDir inputDir(DefaultModuleFileSystem fileSystem, File ioFile) {
- String path = computeFilePath(fileSystem, ioFile);
- DefaultInputDir inputDir = new DefaultInputDir(FilenameUtils.normalize(ioFile.getAbsolutePath(), true), path);
- String resourceKey = PathUtils.sanitize(path);
- inputDir.setKey(module.getEffectiveKey() + ":" + resourceKey);
- return inputDir;
- }
-
private void indexDirectory(InputFileBuilder inputFileBuilder, DefaultModuleFileSystem fileSystem, Progress status, File dirToIndex) {
Collection<File> files = FileUtils.listFiles(dirToIndex, FILE_FILTER, DIR_FILTER);
for (File sourceFile : files) {
if (path == null) {
LoggerFactory.getLogger(getClass()).warn(String.format(
"File '%s' is not declared in module basedir %s", sourceFile.getAbsoluteFile(), fileSystem.baseDir()
- ));
+ ));
} else {
if (exclusionFilters.accept(sourceFile, path, InputFile.TYPE_MAIN)) {
indexFile(inputFileBuilder, fileSystem, status, sourceFile, path, InputFile.TYPE_MAIN);
return pathResolver.relativePath(fileSystem.baseDir(), file);
}
-
private boolean accept(InputFile inputFile) {
// InputFileFilter extensions
for (InputFileFilter filter : filters) {
import org.sonar.api.config.Settings;
import org.sonar.api.resources.AbstractLanguage;
import org.sonar.api.resources.Java;
-import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Qualifiers;
mock(ResourceDao.class), mock(InputFileCache.class));
indexer.execute(fs);
- verify(sonarIndex).index(JavaFile.create("src/main/java/foo/bar/Foo.java", "foo/bar/Foo.java", false));
- verify(sonarIndex).index(JavaFile.create("src/main/java2/foo/bar/Foo.java", "foo/bar/Foo.java", false));
- verify(sonarIndex).index(argThat(new ArgumentMatcher<JavaFile>() {
+ verify(sonarIndex).index(org.sonar.api.resources.File.create("src/main/java/foo/bar/Foo.java", "foo/bar/Foo.java", Java.INSTANCE, false));
+ verify(sonarIndex).index(org.sonar.api.resources.File.create("src/main/java2/foo/bar/Foo.java", "foo/bar/Foo.java", Java.INSTANCE, false));
+ verify(sonarIndex).index(argThat(new ArgumentMatcher<org.sonar.api.resources.File>() {
@Override
public boolean matches(Object arg0) {
- JavaFile javaFile = (JavaFile) arg0;
- return javaFile.getKey().equals("src/test/java/foo/bar/FooTest.java") && javaFile.getDeprecatedKey().equals("foo.bar.FooTest")
+ org.sonar.api.resources.File javaFile = (org.sonar.api.resources.File) arg0;
+ return javaFile.getKey().equals("src/test/java/foo/bar/FooTest.java")
&& javaFile.getPath().equals("src/test/java/foo/bar/FooTest.java")
&& javaFile.getQualifier().equals(Qualifiers.UNIT_TEST_FILE);
}
mock(ResourceDao.class), mock(InputFileCache.class));
indexer.execute(fs);
- Resource sonarFile = JavaFile.create("src/main/java/foo/bar/Foo.java", "foo/bar/Foo.java", false);
+ Resource sonarFile = org.sonar.api.resources.File.create("src/main/java/foo/bar/Foo.java", "foo/bar/Foo.java", Java.INSTANCE, false);
verify(sonarIndex).index(sonarFile);
verify(sonarIndex).setSource(sonarFile, "sample code");
}
mock(ResourceDao.class), mock(InputFileCache.class));
indexer.execute(fs);
- Resource sonarFile = JavaFile.create("src/main/java/foo/bar/Foo.java", "foo/bar/Foo.java", false);
+ Resource sonarFile = org.sonar.api.resources.File.create("src/main/java/foo/bar/Foo.java", "foo/bar/Foo.java", Java.INSTANCE, false);
verify(sonarIndex).setSource(eq(sonarFile), argThat(new ArgumentMatcher<String>() {
@Override
mock(ResourceDao.class), mock(InputFileCache.class));
indexer.execute(fs);
- Resource sonarFile = JavaFile.create("/src/main/java/foo/bar/Foo.java", "foo/bar/Foo.java", false);
+ Resource sonarFile = org.sonar.api.resources.File.create("/src/main/java/foo/bar/Foo.java", "foo/bar/Foo.java", Java.INSTANCE, false);
verify(sonarIndex).setSource(eq(sonarFile), argThat(new ArgumentMatcher<String>() {
@Override
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.api.batch;
+
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.JavaPackage;
+
+public final class SquidUtils {
+
+ private SquidUtils() {
+ // only static methods
+ }
+
+ /**
+ * @deprecated since 4.2 JavaFile is deprecated
+ */
+ @Deprecated
+ public static JavaFile convertJavaFileKeyFromSquidFormat(String key) {
+ String extension = StringUtils.lowerCase(FilenameUtils.getExtension(key));
+ boolean isJavaFile = "jav".equals(extension) || "java".equals(extension);
+ if (isJavaFile) {
+ key = key.substring(0, key.length() - extension.length() - 1);
+ }
+
+ String convertedKey = key.replace('/', '.');
+ if (convertedKey.indexOf('.') == -1 && !"".equals(convertedKey)) {
+ convertedKey = "[default]." + convertedKey;
+
+ } else if (convertedKey.indexOf('.') == -1) {
+ convertedKey = "[default]";
+ }
+
+ return new JavaFile(convertedKey);
+ }
+
+ /**
+ * @deprecated since 4.2 JavaPackage is deprecated
+ */
+ @Deprecated
+ public static JavaPackage convertJavaPackageKeyFromSquidFormat(String key) {
+ return new JavaPackage(key);
+ }
+
+ public static String convertToSquidKeyFormat(JavaFile file) {
+ throw new UnsupportedOperationException("Not supported since v4.0. Was badly implemented");
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.api.resources;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.sonar.api.scan.filesystem.PathResolver;
+import org.sonar.api.utils.WildcardPattern;
+
+import java.io.File;
+import java.util.List;
+
+/**
+ * A class that represents a Java class. This class can either be a Test class or source class
+ *
+ * @since 1.10
+ * @deprecated since 4.2 use {@link org.sonar.api.resources.File}
+ */
+@Deprecated
+public class JavaFile extends Resource {
+
+ private static final String JAVA_SUFFIX = ".java";
+ private static final String JAV_SUFFIX = ".jav";
+ private String className;
+ private String filename;
+ private String fullyQualifiedName;
+ private String packageFullyQualifiedName;
+ private boolean unitTest;
+ private JavaPackage parent;
+
+ private JavaFile() {
+ // Default constructor
+ }
+
+ /**
+ * Creates a JavaFile that is not a class of test based on package and file names
+ */
+ public JavaFile(String packageName, String className) {
+ this(packageName, className, false);
+ }
+
+ /**
+ * Creates a JavaFile that can be of any type based on package and file names
+ *
+ * @param unitTest whether it is a unit test file or a source file
+ */
+ public JavaFile(String packageKey, String className, boolean unitTest) {
+ if (className == null) {
+ throw new IllegalArgumentException("Java filename can not be null");
+ }
+ this.className = StringUtils.trim(className);
+ String deprecatedKey;
+ if (StringUtils.isBlank(packageKey)) {
+ this.packageFullyQualifiedName = JavaPackage.DEFAULT_PACKAGE_NAME;
+ this.fullyQualifiedName = this.className;
+ deprecatedKey = new StringBuilder().append(this.packageFullyQualifiedName).append(".").append(this.className).toString();
+ } else {
+ this.packageFullyQualifiedName = packageKey.trim();
+ deprecatedKey = new StringBuilder().append(this.packageFullyQualifiedName).append(".").append(this.className).toString();
+ this.fullyQualifiedName = deprecatedKey;
+ }
+ setDeprecatedKey(deprecatedKey);
+ this.unitTest = unitTest;
+ }
+
+ /**
+ * Creates a source file from its key
+ */
+ public JavaFile(String deprecatedKey) {
+ this(deprecatedKey, false);
+ }
+
+ /**
+ * Creates any JavaFile from its key
+ *
+ * @param unitTest whether it is a unit test file or a source file
+ */
+ public JavaFile(String deprecatedKey, boolean unitTest) {
+ if (deprecatedKey == null) {
+ throw new IllegalArgumentException("Java filename can not be null");
+ }
+ String realKey = StringUtils.trim(deprecatedKey);
+ this.unitTest = unitTest;
+
+ if (realKey.contains(".")) {
+ this.className = StringUtils.substringAfterLast(realKey, ".");
+ this.packageFullyQualifiedName = StringUtils.substringBeforeLast(realKey, ".");
+ this.fullyQualifiedName = realKey;
+
+ } else {
+ this.className = realKey;
+ this.fullyQualifiedName = realKey;
+ this.packageFullyQualifiedName = JavaPackage.DEFAULT_PACKAGE_NAME;
+ realKey = new StringBuilder().append(JavaPackage.DEFAULT_PACKAGE_NAME).append(".").append(realKey).toString();
+ }
+ setDeprecatedKey(realKey);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public JavaPackage getParent() {
+ if (parent == null) {
+ parent = new JavaPackage(packageFullyQualifiedName);
+ }
+ return parent;
+ }
+
+ /**
+ * @return null
+ */
+ @Override
+ public String getDescription() {
+ return null;
+ }
+
+ /**
+ * @return Java
+ */
+ @Override
+ public Language getLanguage() {
+ return Java.INSTANCE;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getName() {
+ return StringUtils.isNotBlank(filename) ? filename : (className + JAVA_SUFFIX);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getLongName() {
+ return fullyQualifiedName;
+ }
+
+ /**
+ * @return SCOPE_ENTITY
+ */
+ @Override
+ public String getScope() {
+ return Scopes.FILE;
+ }
+
+ /**
+ * @return QUALIFIER_UNIT_TEST_CLASS or QUALIFIER_FILE depending whether it is a unit test class
+ */
+ @Override
+ public String getQualifier() {
+ return unitTest ? Qualifiers.UNIT_TEST_FILE : Qualifiers.FILE;
+ }
+
+ /**
+ * @return whether the JavaFile is a unit test class or not
+ */
+ public boolean isUnitTest() {
+ return unitTest;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean matchFilePattern(String antPattern) {
+ WildcardPattern matcher = WildcardPattern.create(antPattern, Directory.SEPARATOR);
+ return matcher.match(getKey());
+ }
+
+ public static JavaFile fromIOFile(File file, Project module, boolean unitTest) {
+ if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), JAVA_SUFFIX)) {
+ return null;
+ }
+ PathResolver.RelativePath relativePath = new PathResolver().relativePath(
+ unitTest ? module.getFileSystem().getTestDirs() : module.getFileSystem().getSourceDirs(),
+ file);
+ if (relativePath != null) {
+ JavaFile sonarFile = fromRelativePath(relativePath.path(), unitTest);
+ sonarFile.setPath(new PathResolver().relativePath(module.getFileSystem().getBasedir(), file));
+ return sonarFile;
+ }
+ return null;
+ }
+
+ /**
+ * For internal use only.
+ */
+ public static JavaFile create(String relativePathFromBasedir) {
+ JavaFile javaFile = new JavaFile();
+ String normalizedPath = normalize(relativePathFromBasedir);
+ javaFile.setKey(normalizedPath);
+ javaFile.setPath(normalizedPath);
+ javaFile.parent = new JavaPackage();
+ String directoryPath;
+ if (normalizedPath.contains(Directory.SEPARATOR)) {
+ directoryPath = StringUtils.substringBeforeLast(normalizedPath, Directory.SEPARATOR);
+ } else {
+ directoryPath = Directory.SEPARATOR;
+ }
+ String normalizedParentPath = normalize(directoryPath);
+ javaFile.parent.setKey(normalizedParentPath);
+ javaFile.parent.setPath(normalizedParentPath);
+ return javaFile;
+ }
+
+ /**
+ * For internal use only.
+ */
+ public static JavaFile create(String relativePathFromBasedir, String relativePathFromSourceDir, boolean unitTest) {
+ JavaFile javaFile = JavaFile.create(relativePathFromBasedir);
+ if (relativePathFromSourceDir.contains(Directory.SEPARATOR)) {
+ javaFile.packageFullyQualifiedName = StringUtils.substringBeforeLast(relativePathFromSourceDir, Directory.SEPARATOR);
+ javaFile.packageFullyQualifiedName = StringUtils.replace(javaFile.packageFullyQualifiedName, Directory.SEPARATOR, ".");
+ javaFile.filename = StringUtils.substringAfterLast(relativePathFromSourceDir, Directory.SEPARATOR);
+ if (javaFile.filename.endsWith(JAVA_SUFFIX)) {
+ javaFile.className = StringUtils.removeEndIgnoreCase(javaFile.filename, JAVA_SUFFIX);
+ } else if (javaFile.filename.endsWith(JAV_SUFFIX)) {
+ javaFile.className = StringUtils.removeEndIgnoreCase(javaFile.filename, JAV_SUFFIX);
+ }
+ javaFile.fullyQualifiedName = javaFile.packageFullyQualifiedName + "." + javaFile.className;
+ javaFile.setDeprecatedKey(javaFile.fullyQualifiedName);
+ javaFile.parent.setDeprecatedKey(Directory.parseKey(StringUtils.substringBeforeLast(relativePathFromSourceDir, Directory.SEPARATOR)));
+ } else {
+ javaFile.packageFullyQualifiedName = JavaPackage.DEFAULT_PACKAGE_NAME;
+ javaFile.className = StringUtils.removeEndIgnoreCase(relativePathFromSourceDir, JAVA_SUFFIX);
+ javaFile.fullyQualifiedName = javaFile.className;
+ javaFile.setDeprecatedKey(JavaPackage.DEFAULT_PACKAGE_NAME + "." + javaFile.className);
+ javaFile.parent.setDeprecatedKey(Directory.ROOT);
+ }
+ javaFile.unitTest = unitTest;
+ return javaFile;
+ }
+
+ /**
+ * @deprecated since 4.2 use {@link #create(String, String, boolean)}
+ */
+ @Deprecated
+ public static JavaFile fromRelativePath(String relativePath, boolean unitTest) {
+ if (relativePath != null) {
+ String pacname = null;
+ String classname = relativePath;
+
+ if (relativePath.indexOf('/') >= 0) {
+ pacname = StringUtils.substringBeforeLast(relativePath, "/");
+ pacname = StringUtils.replace(pacname, "/", ".");
+ classname = StringUtils.substringAfterLast(relativePath, "/");
+ }
+ classname = StringUtils.substringBeforeLast(classname, ".");
+ return new JavaFile(pacname, classname, unitTest);
+ }
+ return null;
+ }
+
+ /**
+ * Creates a JavaFile from a file in the source directories
+ *
+ * @return the JavaFile created if exists, null otherwise
+ * @deprecated since 4.2 use {@link #create(String, String, boolean)}
+ */
+ @Deprecated
+ public static JavaFile fromIOFile(File file, List<File> sourceDirs, boolean unitTest) {
+ if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), JAVA_SUFFIX)) {
+ return null;
+ }
+ PathResolver.RelativePath relativePath = new PathResolver().relativePath(sourceDirs, file);
+ if (relativePath != null) {
+ return fromRelativePath(relativePath.path(), unitTest);
+ }
+ return null;
+ }
+
+ /**
+ * Shortcut to fromIOFile with an abolute path
+ * @deprecated since 4.2 use {@link #create(String, String, boolean)}
+ */
+ @Deprecated
+ public static JavaFile fromAbsolutePath(String path, List<File> sourceDirs, boolean unitTest) {
+ if (path == null) {
+ return null;
+ }
+ return fromIOFile(new File(path), sourceDirs, unitTest);
+ }
+
+ @Override
+ public String toString() {
+ return new ToStringBuilder(this)
+ .append("key", getKey())
+ .append("deprecatedKey", getDeprecatedKey())
+ .append("path", getPath())
+ .append("filename", className)
+ .toString();
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.api.resources;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.builder.ToStringBuilder;
+
+/**
+ * A class that represents a Java package in Sonar
+ *
+ * @since 1.10
+ * @deprecated since 4.2 use {@link Directory} instead
+ */
+@Deprecated
+public class JavaPackage extends Resource {
+
+ /**
+ * Default package name for classes without package definition
+ */
+ public static final String DEFAULT_PACKAGE_NAME = "[default]";
+
+ /**
+ * Default constructor
+ * @deprecated since 4.2 use {@link #create(String, String)}
+ */
+ @Deprecated
+ public JavaPackage() {
+ this(null);
+ }
+
+ /**
+ * Creates a JavaPackage from its key.
+ * @deprecated since 4.2 use {@link #create(String, String)}
+ */
+ @Deprecated
+ public JavaPackage(String deprecatedKey) {
+ if (DEFAULT_PACKAGE_NAME.equals(deprecatedKey)) {
+ deprecatedKey = Directory.ROOT;
+ }
+ String deprecatedDirectoryKey = StringUtils.trimToEmpty(deprecatedKey);
+ deprecatedDirectoryKey = deprecatedDirectoryKey.replaceAll("\\.", Directory.SEPARATOR);
+ setDeprecatedKey(StringUtils.defaultIfEmpty(deprecatedDirectoryKey, Directory.ROOT));
+ }
+
+ /**
+ * @return whether the JavaPackage key is the default key
+ */
+ public boolean isDefault() {
+ return StringUtils.equals(getDeprecatedKey(), Directory.ROOT);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean matchFilePattern(String antPattern) {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getDescription() {
+ return null;
+ }
+
+ /**
+ * @return SCOPE_SPACE
+ */
+ @Override
+ public String getScope() {
+ return Scopes.DIRECTORY;
+ }
+
+ /**
+ * @return QUALIFIER_PACKAGE
+ */
+ @Override
+ public String getQualifier() {
+ return Qualifiers.DIRECTORY;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getName() {
+ return getKey();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Resource getParent() {
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getLongName() {
+ return null;
+ }
+
+ /**
+ * @return null
+ */
+ @Override
+ public Language getLanguage() {
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ return new ToStringBuilder(this)
+ .append("id", getId())
+ .append("key", getKey())
+ .append("deprecatedKey", getDeprecatedKey())
+ .toString();
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.api.batch;
+
+import org.junit.Test;
+import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.JavaPackage;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class SquidUtilsTest {
+
+ @Test
+ public void convertJavaFileKeyFromSquidFormat() {
+ assertThat(new JavaFile("java.lang.String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("java/lang/String"));
+ assertThat(new JavaFile("java.lang.String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("java/lang/String.java"));
+ assertThat(new JavaFile("java.lang.String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("java/lang/String.jav"));
+ assertThat(new JavaFile("java.lang.String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("java/lang/String.JAVA"));
+ assertThat(new JavaFile("java.lang.String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("java/lang/String.JAV"));
+ assertThat(new JavaFile("String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("String.java"));
+ assertThat(new JavaFile("String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("String.JAVA"));
+ assertThat(new JavaFile("String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("String.JAV"));
+ assertThat(new JavaFile("String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("String"));
+ }
+
+ @Test
+ public void shouldConvertJavaPackageKeyFromSquidFormat() {
+ assertThat(new JavaPackage("java/lang")).isEqualTo(SquidUtils.convertJavaPackageKeyFromSquidFormat("java/lang"));
+ assertThat(new JavaPackage("")).isEqualTo(SquidUtils.convertJavaPackageKeyFromSquidFormat(""));
+ assertThat(new JavaPackage("singlepackage")).isEqualTo(SquidUtils.convertJavaPackageKeyFromSquidFormat("singlepackage"));
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void shouldConvertToSquidKeyFormat() {
+ SquidUtils.convertToSquidKeyFormat(new JavaFile("com.foo.Bar"));
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.api.resources;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+public class JavaFileTest {
+
+ @Rule
+ public TemporaryFolder tempFolder = new TemporaryFolder();
+
+ @Test
+ public void testNewClass() {
+ JavaFile javaClass = JavaFile.create("src/main/java/org/foo/bar/Hello.java", "org/foo/bar/Hello.java", false);
+ assertThat(javaClass.getKey()).isEqualTo("src/main/java/org/foo/bar/Hello.java");
+ assertThat(javaClass.getDeprecatedKey(), is("org.foo.bar.Hello"));
+ assertThat(javaClass.getName(), is("Hello.java"));
+ assertThat(javaClass.getLongName(), is("org.foo.bar.Hello"));
+ assertThat(javaClass.getParent().getKey(), is("src/main/java/org/foo/bar"));
+ assertThat(javaClass.getParent().getDeprecatedKey(), is("org/foo/bar"));
+ }
+
+ @Test
+ public void testNewClassByDeprecatedKey() {
+ JavaFile javaClass = new JavaFile("org.foo.bar.Hello", false);
+ assertThat(javaClass.getDeprecatedKey(), is("org.foo.bar.Hello"));
+ assertThat(javaClass.getName(), is("Hello.java"));
+ assertThat(javaClass.getLongName(), is("org.foo.bar.Hello"));
+ assertThat(javaClass.getParent().getDeprecatedKey(), is("org/foo/bar"));
+ }
+
+ @Test
+ public void testNewClassWithExplicitPackage() {
+ JavaFile javaClass = new JavaFile("org.foo.bar", "Hello", false);
+ assertThat(javaClass.getDeprecatedKey(), is("org.foo.bar.Hello"));
+ assertThat(javaClass.getName(), is("Hello.java"));
+ assertThat(javaClass.getLongName(), is("org.foo.bar.Hello"));
+ assertThat(javaClass.getParent().getDeprecatedKey(), is("org/foo/bar"));
+ }
+
+ @Test
+ public void shouldAcceptFilenamesWithDollars() {
+ // $ is not used only for inner classes !!!
+ JavaFile javaFile = new JavaFile("org.foo.bar", "Hello$Bar");
+ assertThat(javaFile.getDeprecatedKey(), is("org.foo.bar.Hello$Bar"));
+ }
+
+ @Test
+ public void testNewClassWithEmptyPackage() {
+ JavaFile javaClass = JavaFile.create("src/main/java/Hello.java", "Hello.java", false);
+ assertThat(javaClass.getKey()).isEqualTo("src/main/java/Hello.java");
+ assertThat(javaClass.getDeprecatedKey(), is(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello"));
+ assertThat(javaClass.getName(), is("Hello.java"));
+ assertThat(javaClass.getLongName(), is("Hello"));
+ assertThat(javaClass.getParent().getKey()).isEqualTo("src/main/java");
+ assertThat(javaClass.getParent().getDeprecatedKey()).isEqualTo(Directory.ROOT);
+ assertThat(javaClass.getParent().isDefault()).isTrue();
+ }
+
+ @Test
+ public void testNewClassInRootFolder() {
+ JavaFile javaClass = JavaFile.create("Hello.java", "Hello.java", false);
+ assertThat(javaClass.getKey()).isEqualTo("Hello.java");
+ assertThat(javaClass.getDeprecatedKey(), is(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello"));
+ assertThat(javaClass.getName(), is("Hello.java"));
+ assertThat(javaClass.getLongName(), is("Hello"));
+ assertThat(javaClass.getParent().getKey()).isEqualTo("/");
+ assertThat(javaClass.getParent().getDeprecatedKey()).isEqualTo(Directory.ROOT);
+ assertThat(javaClass.getParent().isDefault()).isTrue();
+ }
+
+ @Test
+ public void testNewClassWithEmptyPackageDeprecatedConstructor() {
+ JavaFile javaClass = new JavaFile("", "Hello", false);
+ assertThat(javaClass.getDeprecatedKey(), is(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello"));
+ assertThat(javaClass.getName(), is("Hello.java"));
+ assertThat(javaClass.getLongName(), is("Hello"));
+ assertThat(javaClass.getParent().isDefault(), is(true));
+ }
+
+ @Test
+ public void testNewClassWithNullPackageDeprecatedConstructor() {
+ JavaFile javaClass = new JavaFile(null, "Hello", false);
+ assertThat(javaClass.getDeprecatedKey(), is(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello"));
+ assertThat(javaClass.getName(), is("Hello.java"));
+ assertThat(javaClass.getLongName(), is("Hello"));
+ assertThat((javaClass.getParent()).isDefault(), is(true));
+ }
+
+ @Test
+ public void shouldBeDefaultPackageIfNoPackage() {
+ JavaFile javaClass = new JavaFile("Hello", false);
+ assertEquals(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello", javaClass.getDeprecatedKey());
+ assertThat(javaClass.getName(), is("Hello.java"));
+ assertThat(javaClass.getLongName(), is("Hello"));
+ assertThat(javaClass.getParent().isDefault(), is(true));
+ }
+
+ @Test
+ public void aClassShouldBeNamedJava() {
+ JavaFile javaClass = new JavaFile("org.foo.bar.Java", false);
+ assertThat(javaClass.getDeprecatedKey(), is("org.foo.bar.Java"));
+ assertThat(javaClass.getLongName(), is("org.foo.bar.Java"));
+ assertThat(javaClass.getName(), is("Java.java"));
+ JavaPackage parent = javaClass.getParent();
+ assertEquals("org/foo/bar", parent.getDeprecatedKey());
+ }
+
+ @Test
+ public void shouldTrimClasses() {
+ JavaFile clazz = new JavaFile(" org.foo.bar.Hello ", false);
+ assertThat(clazz.getDeprecatedKey(), is("org.foo.bar.Hello"));
+ assertThat(clazz.getLongName(), is("org.foo.bar.Hello"));
+ assertThat(clazz.getName(), is("Hello.java"));
+ JavaPackage parent = clazz.getParent();
+ assertThat(parent.getDeprecatedKey(), is("org/foo/bar"));
+ }
+
+ @Test
+ public void testEqualsOnClasses() {
+ JavaFile class1 = new JavaFile("foo.bar", "Hello", false);
+ JavaFile class2 = new JavaFile("foo.bar.Hello", false);
+ assertThat(class1).isEqualTo(class2);
+
+ class1 = new JavaFile("NoPackage", false);
+ class2 = new JavaFile("NoPackage", false);
+ assertThat(class1).isEqualTo(class2);
+ assertThat(class1).isEqualTo(class1);
+ }
+
+ @Test
+ public void oneLevelPackage() {
+ JavaFile clazz = new JavaFile("onelevel.MyFile");
+ assertEquals("onelevel.MyFile", clazz.getDeprecatedKey());
+ assertEquals("onelevel", clazz.getParent().getDeprecatedKey());
+
+ clazz = new JavaFile("onelevel", "MyFile");
+ assertEquals("onelevel.MyFile", clazz.getDeprecatedKey());
+ assertEquals("onelevel", clazz.getParent().getDeprecatedKey());
+
+ File sourceDir = newDir("sources");
+ List<File> sources = Arrays.asList(sourceDir);
+ JavaFile javaFile = JavaFile.fromAbsolutePath(absPath(sourceDir, "onelevel/MyFile.java"), sources, false);
+ assertEquals("onelevel.MyFile", javaFile.getDeprecatedKey());
+ assertEquals("MyFile.java", javaFile.getName());
+ assertEquals("onelevel", javaFile.getParent().getDeprecatedKey());
+ assertThat(javaFile.getParent().isDefault(), is(false));
+ }
+
+ @Test
+ public void shouldResolveClassFromAbsolutePath() {
+ File sources1 = newDir("source1");
+ File sources2 = newDir("source2");
+ List<File> sources = Arrays.asList(sources1, sources2);
+ JavaFile javaFile = JavaFile.fromAbsolutePath(absPath(sources2, "foo/bar/MyFile.java"), sources, false);
+ assertThat("foo.bar.MyFile", is(javaFile.getDeprecatedKey()));
+ assertThat(javaFile.getLongName(), is("foo.bar.MyFile"));
+ assertThat(javaFile.getName(), is("MyFile.java"));
+ assertThat(javaFile.getParent().getDeprecatedKey(), is("foo/bar"));
+ }
+
+ @Test
+ public void shouldResolveFromAbsolutePathEvenIfDefaultPackage() {
+ File source1 = newDir("source1");
+ File source2 = newDir("source2");
+ List<File> sources = Arrays.asList(source1, source2);
+
+ JavaFile javaClass = JavaFile.fromAbsolutePath(absPath(source1, "MyClass.java"), sources, false);
+ assertEquals(JavaPackage.DEFAULT_PACKAGE_NAME + ".MyClass", javaClass.getDeprecatedKey());
+ assertEquals("MyClass.java", javaClass.getName());
+
+ assertThat((javaClass.getParent()).isDefault()).isEqualTo(true);
+ }
+
+ @Test
+ public void shouldResolveOnlyJavaFromAbsolutePath() {
+ File source1 = newDir("source1");
+ List<File> sources = Arrays.asList(source1);
+ assertThat(JavaFile.fromAbsolutePath(absPath(source1, "foo/bar/my_file.sql"), sources, false)).isNull();
+ }
+
+ @Test
+ public void shouldNotFailWhenResolvingUnknownClassFromAbsolutePath() {
+ File source1 = newDir("source1");
+ List<File> sources = Arrays.asList(source1);
+ assertThat(JavaFile.fromAbsolutePath("/home/other/src/main/java/foo/bar/MyClass.java", sources, false)).isNull();
+ }
+
+ @Test
+ public void shouldMatchFilePatterns() {
+ JavaFile clazz = JavaFile.create("src/main/java/org/sonar/commons/Foo.java", "org/sonar/commons/Foo.java", false);
+ assertThat(clazz.matchFilePattern("**/commons/**/*.java")).isTrue();
+ assertThat(clazz.matchFilePattern("/**/commons/**/*.java")).isTrue();
+ assertThat(clazz.matchFilePattern("/**/commons/**/*.*")).isTrue();
+ assertThat(clazz.matchFilePattern("/**/sonar/*.java")).isFalse();
+ assertThat(clazz.matchFilePattern("src/main/java/org/*/commons/**/*.java")).isTrue();
+ assertThat(clazz.matchFilePattern("src/main/java/org/sonar/commons/*")).isTrue();
+ assertThat(clazz.matchFilePattern("src/main/java/org/sonar/**/*.java")).isTrue();
+ assertThat(clazz.matchFilePattern("src/main/java/org/sonar/*")).isFalse();
+ assertThat(clazz.matchFilePattern("src/main/java/org/sonar*/*")).isFalse();
+ assertThat(clazz.matchFilePattern("src/main/java/org/**")).isTrue();
+ assertThat(clazz.matchFilePattern("*src/main/java/org/sona?/co??ons/**.*")).isTrue();
+ assertThat(clazz.matchFilePattern("src/main/java/org/sonar/core/**")).isFalse();
+ assertThat(clazz.matchFilePattern("src/main/java/org/sonar/commons/Foo.java")).isTrue();
+ assertThat(clazz.matchFilePattern("**/*Foo.java")).isTrue();
+ assertThat(clazz.matchFilePattern("**/*Foo.*")).isTrue();
+ assertThat(clazz.matchFilePattern("src/main/java/org/*/*/Foo.java")).isTrue();
+ assertThat(clazz.matchFilePattern("src/main/java/org/**/**/Foo.java")).isTrue();
+ assertThat(clazz.matchFilePattern("**/commons/**/*")).isTrue();
+ assertThat(clazz.matchFilePattern("**/*")).isTrue();
+ }
+
+ // SONAR-4397
+ @Test
+ public void shouldMatchFilePatternsWhenNoPackage() {
+ JavaFile clazz = JavaFile.create("src/main/java/Foo.java", "Foo.java", false);
+ assertThat(clazz.matchFilePattern("**/*Foo.java")).isTrue();
+ assertThat(clazz.matchFilePattern("**/*Foo.*")).isTrue();
+ assertThat(clazz.matchFilePattern("**/*")).isTrue();
+ assertThat(clazz.matchFilePattern("src/main/java/Foo*.*")).isTrue();
+ }
+
+ /**
+ * See http://jira.codehaus.org/browse/SONAR-1449
+ */
+ @Test
+ public void doNotMatchAPattern() {
+ JavaFile file = JavaFile.create("src/main/java/org/sonar/commons/Foo.java", "org/sonar/commons/Foo.java", false);
+ assertThat(file.matchFilePattern("**/*.aj")).isFalse();
+ assertThat(file.matchFilePattern("**/*.java")).isTrue();
+ }
+
+ @Test
+ public void should_exclude_test_files() {
+ JavaFile unitTest = JavaFile.create("src/main/java/org/sonar/commons/Foo.java", "org/sonar/commons/Foo.java", true);
+ assertThat(unitTest.matchFilePattern("**/*")).isTrue();
+ }
+
+ private File newDir(String dirName) {
+ return tempFolder.newFolder(dirName);
+ }
+
+ private String absPath(File dir, String filePath) {
+ return new File(dir, filePath).getPath();
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.api.resources;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+
+public class JavaPackageTest {
+ @Test
+ public void defaultPackageDeprecatedConstructor() {
+ assertEquals(new JavaPackage(), new JavaPackage());
+ assertEquals(Directory.ROOT, new JavaPackage(null).getDeprecatedKey());
+ assertEquals(Directory.ROOT, new JavaPackage("").getDeprecatedKey());
+ assertThat(new JavaPackage(null).isDefault(), is(true));
+ }
+
+ @Test
+ public void testNewPackageDeprecatedConstructor() {
+ assertEquals(new JavaPackage(" foo.bar "), new JavaPackage("foo.bar"));
+ JavaPackage pac = new JavaPackage("foo.bar");
+ assertEquals("foo/bar", pac.getDeprecatedKey());
+ }
+
+ @Test
+ public void singleLevelPackageDeprecatedConstructor() {
+ assertEquals(new JavaPackage("foo"), new JavaPackage("foo"));
+ JavaPackage pac = new JavaPackage("foo");
+ assertEquals("foo", pac.getDeprecatedKey());
+ }
+
+ @Test
+ public void shouldNotMatchFilePatterns() {
+ JavaPackage pac = new JavaPackage("org.sonar.commons");
+ assertFalse(pac.matchFilePattern("**"));
+ }
+
+}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.api.batch;
-
-import org.apache.commons.io.FilenameUtils;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.resources.Directory;
-import org.sonar.api.resources.JavaFile;
-import org.sonar.api.resources.JavaPackage;
-
-public final class SquidUtils {
-
- private SquidUtils() {
- // only static methods
- }
-
- /**
- * @deprecated since 4.2 JavaFile is deprecated
- */
- @Deprecated
- public static JavaFile convertJavaFileKeyFromSquidFormat(String key) {
- String extension = StringUtils.lowerCase(FilenameUtils.getExtension(key));
- boolean isJavaFile = "jav".equals(extension) || "java".equals(extension);
- if (isJavaFile) {
- key = key.substring(0, key.length() - extension.length() - 1);
- }
-
- String convertedKey = key.replace('/', '.');
- if (convertedKey.indexOf('.') == -1 && !"".equals(convertedKey)) {
- convertedKey = "[default]." + convertedKey;
-
- } else if (convertedKey.indexOf('.') == -1) {
- convertedKey = "[default]";
- }
-
- return new JavaFile(convertedKey);
- }
-
- /**
- * @deprecated since 4.2 JavaPackage is deprecated
- */
- @Deprecated
- public static JavaPackage convertJavaPackageKeyFromSquidFormat(String key) {
- return new Directory(key);
- }
-
- public static String convertToSquidKeyFormat(JavaFile file) {
- throw new UnsupportedOperationException("Not supported since v4.0. Was badly implemented");
- }
-}
/**
* @since 1.10
- * Extends JavaPackage to allow smooth migration from JavaPackage to Directory
*/
-public class Directory extends JavaPackage {
+public class Directory extends Resource {
public static final String SEPARATOR = "/";
public static final String ROOT = "[root]";
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.api.resources;
-
-import org.apache.commons.lang.StringUtils;
-import org.apache.commons.lang.builder.ToStringBuilder;
-import org.sonar.api.scan.filesystem.PathResolver;
-import org.sonar.api.utils.WildcardPattern;
-
-import java.io.File;
-import java.util.List;
-
-/**
- * A class that represents a Java class. This class can either be a Test class or source class
- *
- * @since 1.10
- * @deprecated since 4.2 use {@link org.sonar.api.resources.File}
- */
-@Deprecated
-public class JavaFile extends Resource {
-
- private static final String JAVA_SUFFIX = ".java";
- private static final String JAV_SUFFIX = ".jav";
- private String className;
- private String filename;
- private String fullyQualifiedName;
- private String packageFullyQualifiedName;
- private boolean unitTest;
- private JavaPackage parent;
-
- private JavaFile() {
- // Default constructor
- }
-
- /**
- * Creates a JavaFile that is not a class of test based on package and file names
- */
- public JavaFile(String packageName, String className) {
- this(packageName, className, false);
- }
-
- /**
- * Creates a JavaFile that can be of any type based on package and file names
- *
- * @param unitTest whether it is a unit test file or a source file
- */
- public JavaFile(String packageKey, String className, boolean unitTest) {
- if (className == null) {
- throw new IllegalArgumentException("Java filename can not be null");
- }
- this.className = StringUtils.trim(className);
- String deprecatedKey;
- if (StringUtils.isBlank(packageKey)) {
- this.packageFullyQualifiedName = JavaPackage.DEFAULT_PACKAGE_NAME;
- this.fullyQualifiedName = this.className;
- deprecatedKey = new StringBuilder().append(this.packageFullyQualifiedName).append(".").append(this.className).toString();
- } else {
- this.packageFullyQualifiedName = packageKey.trim();
- deprecatedKey = new StringBuilder().append(this.packageFullyQualifiedName).append(".").append(this.className).toString();
- this.fullyQualifiedName = deprecatedKey;
- }
- setDeprecatedKey(deprecatedKey);
- this.unitTest = unitTest;
- }
-
- /**
- * Creates a source file from its key
- */
- public JavaFile(String deprecatedKey) {
- this(deprecatedKey, false);
- }
-
- /**
- * Creates any JavaFile from its key
- *
- * @param unitTest whether it is a unit test file or a source file
- */
- public JavaFile(String deprecatedKey, boolean unitTest) {
- if (deprecatedKey == null) {
- throw new IllegalArgumentException("Java filename can not be null");
- }
- String realKey = StringUtils.trim(deprecatedKey);
- this.unitTest = unitTest;
-
- if (realKey.contains(".")) {
- this.className = StringUtils.substringAfterLast(realKey, ".");
- this.packageFullyQualifiedName = StringUtils.substringBeforeLast(realKey, ".");
- this.fullyQualifiedName = realKey;
-
- } else {
- this.className = realKey;
- this.fullyQualifiedName = realKey;
- this.packageFullyQualifiedName = JavaPackage.DEFAULT_PACKAGE_NAME;
- realKey = new StringBuilder().append(JavaPackage.DEFAULT_PACKAGE_NAME).append(".").append(realKey).toString();
- }
- setDeprecatedKey(realKey);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public JavaPackage getParent() {
- if (parent == null) {
- parent = new JavaPackage(packageFullyQualifiedName);
- }
- return parent;
- }
-
- /**
- * @return null
- */
- @Override
- public String getDescription() {
- return null;
- }
-
- /**
- * @return Java
- */
- @Override
- public Language getLanguage() {
- return Java.INSTANCE;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String getName() {
- return StringUtils.isNotBlank(filename) ? filename : (className + JAVA_SUFFIX);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String getLongName() {
- return fullyQualifiedName;
- }
-
- /**
- * @return SCOPE_ENTITY
- */
- @Override
- public String getScope() {
- return Scopes.FILE;
- }
-
- /**
- * @return QUALIFIER_UNIT_TEST_CLASS or QUALIFIER_FILE depending whether it is a unit test class
- */
- @Override
- public String getQualifier() {
- return unitTest ? Qualifiers.UNIT_TEST_FILE : Qualifiers.FILE;
- }
-
- /**
- * @return whether the JavaFile is a unit test class or not
- */
- public boolean isUnitTest() {
- return unitTest;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean matchFilePattern(String antPattern) {
- WildcardPattern matcher = WildcardPattern.create(antPattern, Directory.SEPARATOR);
- return matcher.match(getKey());
- }
-
- public static JavaFile fromIOFile(File file, Project module, boolean unitTest) {
- if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), JAVA_SUFFIX)) {
- return null;
- }
- PathResolver.RelativePath relativePath = new PathResolver().relativePath(
- unitTest ? module.getFileSystem().getTestDirs() : module.getFileSystem().getSourceDirs(),
- file);
- if (relativePath != null) {
- JavaFile sonarFile = fromRelativePath(relativePath.path(), unitTest);
- sonarFile.setPath(new PathResolver().relativePath(module.getFileSystem().getBasedir(), file));
- return sonarFile;
- }
- return null;
- }
-
- /**
- * For internal use only.
- */
- public static JavaFile create(String relativePathFromBasedir) {
- JavaFile javaFile = new JavaFile();
- String normalizedPath = normalize(relativePathFromBasedir);
- javaFile.setKey(normalizedPath);
- javaFile.setPath(normalizedPath);
- javaFile.parent = new Directory();
- String directoryPath;
- if (normalizedPath.contains(Directory.SEPARATOR)) {
- directoryPath = StringUtils.substringBeforeLast(normalizedPath, Directory.SEPARATOR);
- } else {
- directoryPath = Directory.SEPARATOR;
- }
- String normalizedParentPath = normalize(directoryPath);
- javaFile.parent.setKey(normalizedParentPath);
- javaFile.parent.setPath(normalizedParentPath);
- return javaFile;
- }
-
- /**
- * For internal use only.
- */
- public static JavaFile create(String relativePathFromBasedir, String relativePathFromSourceDir, boolean unitTest) {
- JavaFile javaFile = JavaFile.create(relativePathFromBasedir);
- if (relativePathFromSourceDir.contains(Directory.SEPARATOR)) {
- javaFile.packageFullyQualifiedName = StringUtils.substringBeforeLast(relativePathFromSourceDir, Directory.SEPARATOR);
- javaFile.packageFullyQualifiedName = StringUtils.replace(javaFile.packageFullyQualifiedName, Directory.SEPARATOR, ".");
- javaFile.filename = StringUtils.substringAfterLast(relativePathFromSourceDir, Directory.SEPARATOR);
- if (javaFile.filename.endsWith(JAVA_SUFFIX)) {
- javaFile.className = StringUtils.removeEndIgnoreCase(javaFile.filename, JAVA_SUFFIX);
- } else if (javaFile.filename.endsWith(JAV_SUFFIX)) {
- javaFile.className = StringUtils.removeEndIgnoreCase(javaFile.filename, JAV_SUFFIX);
- }
- javaFile.fullyQualifiedName = javaFile.packageFullyQualifiedName + "." + javaFile.className;
- javaFile.setDeprecatedKey(javaFile.fullyQualifiedName);
- javaFile.parent.setDeprecatedKey(Directory.parseKey(StringUtils.substringBeforeLast(relativePathFromSourceDir, Directory.SEPARATOR)));
- } else {
- javaFile.packageFullyQualifiedName = JavaPackage.DEFAULT_PACKAGE_NAME;
- javaFile.className = StringUtils.removeEndIgnoreCase(relativePathFromSourceDir, JAVA_SUFFIX);
- javaFile.fullyQualifiedName = javaFile.className;
- javaFile.setDeprecatedKey(JavaPackage.DEFAULT_PACKAGE_NAME + "." + javaFile.className);
- javaFile.parent.setDeprecatedKey(Directory.ROOT);
- }
- javaFile.unitTest = unitTest;
- return javaFile;
- }
-
- /**
- * @deprecated since 4.2 use {@link #create(String, String, boolean)}
- */
- @Deprecated
- public static JavaFile fromRelativePath(String relativePath, boolean unitTest) {
- if (relativePath != null) {
- String pacname = null;
- String classname = relativePath;
-
- if (relativePath.indexOf('/') >= 0) {
- pacname = StringUtils.substringBeforeLast(relativePath, "/");
- pacname = StringUtils.replace(pacname, "/", ".");
- classname = StringUtils.substringAfterLast(relativePath, "/");
- }
- classname = StringUtils.substringBeforeLast(classname, ".");
- return new JavaFile(pacname, classname, unitTest);
- }
- return null;
- }
-
- /**
- * Creates a JavaFile from a file in the source directories
- *
- * @return the JavaFile created if exists, null otherwise
- * @deprecated since 4.2 use {@link #create(String, String, boolean)}
- */
- @Deprecated
- public static JavaFile fromIOFile(File file, List<File> sourceDirs, boolean unitTest) {
- if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), JAVA_SUFFIX)) {
- return null;
- }
- PathResolver.RelativePath relativePath = new PathResolver().relativePath(sourceDirs, file);
- if (relativePath != null) {
- return fromRelativePath(relativePath.path(), unitTest);
- }
- return null;
- }
-
- /**
- * Shortcut to fromIOFile with an abolute path
- * @deprecated since 4.2 use {@link #create(String, String, boolean)}
- */
- @Deprecated
- public static JavaFile fromAbsolutePath(String path, List<File> sourceDirs, boolean unitTest) {
- if (path == null) {
- return null;
- }
- return fromIOFile(new File(path), sourceDirs, unitTest);
- }
-
- @Override
- public String toString() {
- return new ToStringBuilder(this)
- .append("key", getKey())
- .append("deprecatedKey", getDeprecatedKey())
- .append("path", getPath())
- .append("filename", className)
- .toString();
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.api.resources;
-
-import org.apache.commons.lang.StringUtils;
-import org.apache.commons.lang.builder.ToStringBuilder;
-
-/**
- * A class that represents a Java package in Sonar
- *
- * @since 1.10
- * @deprecated since 4.2 use {@link Directory} instead
- */
-@Deprecated
-public class JavaPackage extends Resource {
-
- /**
- * Default package name for classes without package definition
- */
- public static final String DEFAULT_PACKAGE_NAME = "[default]";
-
- /**
- * Default constructor
- * @deprecated since 4.2 use {@link #create(String, String)}
- */
- @Deprecated
- public JavaPackage() {
- this(null);
- }
-
- /**
- * Creates a JavaPackage from its key.
- * @deprecated since 4.2 use {@link #create(String, String)}
- */
- @Deprecated
- public JavaPackage(String deprecatedKey) {
- if (DEFAULT_PACKAGE_NAME.equals(deprecatedKey)) {
- deprecatedKey = Directory.ROOT;
- }
- String deprecatedDirectoryKey = StringUtils.trimToEmpty(deprecatedKey);
- deprecatedDirectoryKey = deprecatedDirectoryKey.replaceAll("\\.", Directory.SEPARATOR);
- setDeprecatedKey(StringUtils.defaultIfEmpty(deprecatedDirectoryKey, Directory.ROOT));
- }
-
- /**
- * @return whether the JavaPackage key is the default key
- */
- public boolean isDefault() {
- return StringUtils.equals(getDeprecatedKey(), Directory.ROOT);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean matchFilePattern(String antPattern) {
- return false;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String getDescription() {
- return null;
- }
-
- /**
- * @return SCOPE_SPACE
- */
- @Override
- public String getScope() {
- return Scopes.DIRECTORY;
- }
-
- /**
- * @return QUALIFIER_PACKAGE
- */
- @Override
- public String getQualifier() {
- return Qualifiers.DIRECTORY;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String getName() {
- return getKey();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Resource getParent() {
- return null;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String getLongName() {
- return null;
- }
-
- /**
- * @return null
- */
- @Override
- public Language getLanguage() {
- return null;
- }
-
- @Override
- public String toString() {
- return new ToStringBuilder(this)
- .append("id", getId())
- .append("key", getKey())
- .append("deprecatedKey", getDeprecatedKey())
- .toString();
- }
-}
if (o == null) {
return false;
}
- if (getClass() != o.getClass()
- // JavaPackage and Directory are considered equivalent since 4.2
- // JavaFile and File are considered equivalent since 4.2
- && !isEquivalentTo(o)) {
+ if (getClass() != o.getClass()) {
return false;
}
}
}
- private boolean isPackageOrDirectory(Object o) {
- return o instanceof JavaPackage;
- }
-
- private boolean isJavaFileOrFile(Object o) {
- return o instanceof JavaFile || o instanceof File;
- }
-
- private boolean isEquivalentTo(Object o) {
- return isPackageOrDirectory(this) && isPackageOrDirectory(o) || isJavaFileOrFile(this) && isJavaFileOrFile(o);
- }
-
@Override
public int hashCode() {
return key != null ? key.hashCode() : deprecatedKey.hashCode();
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.api.scan.filesystem;
-
-import javax.annotation.CheckForNull;
-
-import java.io.File;
-import java.io.Serializable;
-import java.util.Map;
-
-/**
- * @since 4.2
- */
-public interface InputDir extends Serializable {
-
- /**
- * Path is relative from module base directory. Path is unique and identifies file
- * within given <code>{@link org.sonar.api.scan.filesystem.ModuleFileSystem}</code>.
- * File separator is the forward slash ('/'), even on MSWindows.
- * <p/>
- * Returns <code>src/main/java/com</code> if module base dir is
- * <code>/absolute/path/to/module</code> and if directory is
- * <code>/absolute/path/to/module/src/main/java/com</code>.
- * <p/>
- * Returned path is never null.
- */
- String path();
-
- /**
- * Not-null canonical path. File separator is forward slash ('/'), even on MSWindows.
- */
- String absolutePath();
-
- File file();
-
- /**
- * Not-null directory name
- */
- String name();
-
- /**
- * Does the given attribute have the given value ?
- */
- boolean has(String attribute, String value);
-
- /**
- * See list of attribute keys in constants starting with ATTRIBUTE_.
- */
- @CheckForNull
- String attribute(String key);
-
- Map<String, String> attributes();
-}
@CheckForNull
InputFile inputFile(File ioFile);
- /**
- * Search for input directory corresponding to the given java.io.File.
- * @since 4.2
- */
- @CheckForNull
- InputDir inputDir(File ioFile);
-
/**
* Default charset for files of the module. If it's not defined, then
* return the platform default charset. When trying to read an input file it is better to rely on
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.api.scan.filesystem.internal;
-
-import com.google.common.collect.Maps;
-import org.apache.commons.io.FilenameUtils;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.scan.filesystem.InputDir;
-import org.sonar.api.utils.PathUtils;
-
-import javax.annotation.CheckForNull;
-
-import java.io.File;
-import java.util.Map;
-
-/**
- * PLUGINS MUST NOT USE THIS CLASS, EVEN FOR UNIT TESTING.
- *
- * @since 4.2
- */
-public class DefaultInputDir implements InputDir {
-
- /**
- * We're not sure that this is the correct way, so not in API yet.
- */
- public static final String ATTRIBUTE_COMPONENT_KEY = "CMP_KEY";
-
- private final String absolutePath;
- private final String path;
- private final Map<String, String> attributes;
-
- private DefaultInputDir(File file, String path, Map<String, String> attributes) {
- this.absolutePath = PathUtils.canonicalPath(file);
- this.path = FilenameUtils.separatorsToUnix(path);
- this.attributes = attributes;
- }
-
- public DefaultInputDir(String absolutePath, String path) {
- this.absolutePath = absolutePath;
- this.path = path;
- this.attributes = Maps.newHashMap();
- }
-
- /**
- * Plugins must not build their own instances of {@link InputDir}.
- * {@link org.sonar.api.scan.filesystem.ModuleFileSystem} must be used to search for inputDir.
- */
- public static DefaultInputDir create(File file, String path, Map<String, String> attributes) {
- return new DefaultInputDir(file, path, attributes);
- }
-
- @Override
- public String path() {
- return path;
- }
-
- @Override
- public String absolutePath() {
- return absolutePath;
- }
-
- @Override
- public File file() {
- return new File(absolutePath);
- }
-
- @Override
- public String name() {
- return path();
- }
-
- @Override
- public boolean has(String attribute, String value) {
- return StringUtils.equals(attributes.get(attribute), value);
- }
-
- @Override
- @CheckForNull
- public String attribute(String key) {
- return attributes.get(key);
- }
-
- @Override
- public Map<String, String> attributes() {
- return attributes;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
- DefaultInputDir other = (DefaultInputDir) o;
- return absolutePath.equals(other.absolutePath);
- }
-
- @Override
- public int hashCode() {
- return absolutePath.hashCode();
- }
-
- @Override
- public String toString() {
- return String.format("[%s]", path);
- }
-
- public DefaultInputDir setKey(String s) {
- attributes.put(ATTRIBUTE_COMPONENT_KEY, s);
- return this;
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.api.batch;
-
-import org.junit.Test;
-import org.sonar.api.resources.JavaFile;
-import org.sonar.api.resources.JavaPackage;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class SquidUtilsTest {
-
- @Test
- public void convertJavaFileKeyFromSquidFormat() {
- assertThat(new JavaFile("java.lang.String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("java/lang/String"));
- assertThat(new JavaFile("java.lang.String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("java/lang/String.java"));
- assertThat(new JavaFile("java.lang.String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("java/lang/String.jav"));
- assertThat(new JavaFile("java.lang.String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("java/lang/String.JAVA"));
- assertThat(new JavaFile("java.lang.String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("java/lang/String.JAV"));
- assertThat(new JavaFile("String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("String.java"));
- assertThat(new JavaFile("String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("String.JAVA"));
- assertThat(new JavaFile("String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("String.JAV"));
- assertThat(new JavaFile("String")).isEqualTo(SquidUtils.convertJavaFileKeyFromSquidFormat("String"));
- }
-
- @Test
- public void shouldConvertJavaPackageKeyFromSquidFormat() {
- assertThat(new JavaPackage("java/lang")).isEqualTo(SquidUtils.convertJavaPackageKeyFromSquidFormat("java/lang"));
- assertThat(new JavaPackage("")).isEqualTo(SquidUtils.convertJavaPackageKeyFromSquidFormat(""));
- assertThat(new JavaPackage("singlepackage")).isEqualTo(SquidUtils.convertJavaPackageKeyFromSquidFormat("singlepackage"));
- }
-
- @Test(expected = UnsupportedOperationException.class)
- public void shouldConvertToSquidKeyFormat() {
- SquidUtils.convertToSquidKeyFormat(new JavaFile("com.foo.Bar"));
- }
-}
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.SensorContext;
-import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.File;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.Violation;
private SensorContext sensorContext = mock(SensorContext.class);
NoSonarFilter filter = new NoSonarFilter(sensorContext);
- private JavaFile javaFile;
+ private File javaFile;
@Before
public void prepare() {
- javaFile = new JavaFile("org.foo.Bar");
+ javaFile = new File("org.foo.Bar");
when(sensorContext.getResource(javaFile)).thenReturn(javaFile);
}
import org.junit.Before;
import org.junit.Test;
-import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.File;
import java.util.List;
public void test_calculation_for_file() {
when(data.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 60.0));
when(data.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 20.0));
- when(context.getResource()).thenReturn(new JavaFile("foo"));
+ when(context.getResource()).thenReturn(new File("foo"));
Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
assertThat(measure.getValue()).isEqualTo(3.0);
when(data.getMeasure(CoreMetrics.COMPLEXITY_IN_FUNCTIONS)).thenReturn(null);
when(data.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 60.0));
when(data.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 20.0));
- when(context.getResource()).thenReturn(new JavaFile("foo"));
+ when(context.getResource()).thenReturn(new File("foo"));
Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, CoreMetrics.FUNCTIONS)
- .setFallbackForMainMetric(CoreMetrics.COMPLEXITY)
- .calculate(data, context);
+ .setFallbackForMainMetric(CoreMetrics.COMPLEXITY)
+ .calculate(data, context);
assertThat(measure.getValue()).isEqualTo(3.0);
}
when(data.getMeasure(CoreMetrics.COMPLEXITY_IN_FUNCTIONS)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 60.0));
when(data.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 42.0));
when(data.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 20.0));
- when(context.getResource()).thenReturn(new JavaFile("foo"));
+ when(context.getResource()).thenReturn(new File("foo"));
Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, CoreMetrics.FUNCTIONS)
- .setFallbackForMainMetric(CoreMetrics.COMPLEXITY)
- .calculate(data, context);
+ .setFallbackForMainMetric(CoreMetrics.COMPLEXITY)
+ .calculate(data, context);
assertThat(measure.getValue()).isEqualTo(3.0);
}
when(data.getChildren()).thenReturn(childrenData);
Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, CoreMetrics.FUNCTIONS)
- .setFallbackForMainMetric(CoreMetrics.COMPLEXITY)
- .calculate(data, context);
+ .setFallbackForMainMetric(CoreMetrics.COMPLEXITY)
+ .calculate(data, context);
assertThat(measure.getValue()).isEqualTo(2.5);
}
import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
+import org.sonar.api.resources.Directory;
import org.sonar.api.resources.File;
-import org.sonar.api.resources.JavaFile;
-import org.sonar.api.resources.JavaPackage;
import org.sonar.api.resources.Scopes;
import java.util.Collections;
@Test
public void testWhenGetChildrenReturnsEmpty() {
when(context.getTargetMetric()).thenReturn(new Metric("foo"));
- when(data.getChildrenMeasures(new Metric("foo"))).thenReturn(Collections.<Measure> emptyList());
+ when(data.getChildrenMeasures(new Metric("foo"))).thenReturn(Collections.<Measure>emptyList());
assertNull(formula.calculate(data, context));
}
List<Measure> list = Lists.newArrayList(
new Measure(m, "1=0;2=2;5=0;10=10;20=2"),
new Measure(m, "1=0;2=2;5=0;10=10;30=3")
- );
+ );
when(data.getChildrenMeasures(new Metric("foo"))).thenReturn(list);
assertThat(formula.calculate(data, context), nullValue());
}
List<Measure> list = Lists.newArrayList(
new Measure(m, "1=0;2=2;5=0;10=10;20=2"),
new Measure(m, "1=3;2=2;5=3;10=12;20=0")
- );
+ );
when(data.getChildrenMeasures(new Metric("foo"))).thenReturn(list);
assertThat(formula.calculate(data, context).getData(), is("1=3;2=4;5=3;10=22;20=2"));
}
@Test
public void shouldNotPersistWhenScopeLowerThanMinimun() throws Exception {
- when(context.getResource()).thenReturn(JavaFile.fromRelativePath("org/Foo.java", false));
+ when(context.getResource()).thenReturn(new File("org/Foo.java"));
initContextWithChildren();
formula.setMinimumScopeToPersist(Scopes.DIRECTORY);
@Test
public void shouldPersistWhenScopeEqualsMinimun() throws Exception {
- when(context.getResource()).thenReturn(JavaFile.fromRelativePath("org/Foo.java", false));
+ when(context.getResource()).thenReturn(new File("org/Foo.java"));
initContextWithChildren();
formula.setMinimumScopeToPersist(Scopes.FILE);
@Test
public void shouldPersistWhenScopeHigherThanMinimun() throws Exception {
- when(context.getResource()).thenReturn(new JavaPackage("org.foo"));
+ when(context.getResource()).thenReturn(new Directory("org/foo"));
initContextWithChildren();
formula.setMinimumScopeToPersist(Scopes.FILE);
Metric m = new Metric("foo", Metric.ValueType.DATA);
when(context.getTargetMetric()).thenReturn(m);
List<Measure> list = Lists.newArrayList(
- new Measure(m, "0.5=0;2.5=2"),
- new Measure(m, "0.5=3;2.5=4")
- );
+ new Measure(m, "0.5=0;2.5=2"),
+ new Measure(m, "0.5=3;2.5=4")
+ );
when(data.getChildrenMeasures(new Metric("foo"))).thenReturn(list);
}
}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.api.resources;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-
-import java.io.File;
-import java.util.Arrays;
-import java.util.List;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThat;
-
-public class JavaFileTest {
-
- @Rule
- public TemporaryFolder tempFolder = new TemporaryFolder();
-
- @Test
- public void testNewClass() {
- JavaFile javaClass = JavaFile.create("src/main/java/org/foo/bar/Hello.java", "org/foo/bar/Hello.java", false);
- assertThat(javaClass.getKey()).isEqualTo("src/main/java/org/foo/bar/Hello.java");
- assertThat(javaClass.getDeprecatedKey(), is("org.foo.bar.Hello"));
- assertThat(javaClass.getName(), is("Hello.java"));
- assertThat(javaClass.getLongName(), is("org.foo.bar.Hello"));
- assertThat(javaClass.getParent().getKey(), is("src/main/java/org/foo/bar"));
- assertThat(javaClass.getParent().getDeprecatedKey(), is("org/foo/bar"));
- }
-
- @Test
- public void testNewClassByDeprecatedKey() {
- JavaFile javaClass = new JavaFile("org.foo.bar.Hello", false);
- assertThat(javaClass.getDeprecatedKey(), is("org.foo.bar.Hello"));
- assertThat(javaClass.getName(), is("Hello.java"));
- assertThat(javaClass.getLongName(), is("org.foo.bar.Hello"));
- assertThat(javaClass.getParent().getDeprecatedKey(), is("org/foo/bar"));
- }
-
- @Test
- public void testNewClassWithExplicitPackage() {
- JavaFile javaClass = new JavaFile("org.foo.bar", "Hello", false);
- assertThat(javaClass.getDeprecatedKey(), is("org.foo.bar.Hello"));
- assertThat(javaClass.getName(), is("Hello.java"));
- assertThat(javaClass.getLongName(), is("org.foo.bar.Hello"));
- assertThat(javaClass.getParent().getDeprecatedKey(), is("org/foo/bar"));
- }
-
- @Test
- public void shouldAcceptFilenamesWithDollars() {
- // $ is not used only for inner classes !!!
- JavaFile javaFile = new JavaFile("org.foo.bar", "Hello$Bar");
- assertThat(javaFile.getDeprecatedKey(), is("org.foo.bar.Hello$Bar"));
- }
-
- @Test
- public void testNewClassWithEmptyPackage() {
- JavaFile javaClass = JavaFile.create("src/main/java/Hello.java", "Hello.java", false);
- assertThat(javaClass.getKey()).isEqualTo("src/main/java/Hello.java");
- assertThat(javaClass.getDeprecatedKey(), is(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello"));
- assertThat(javaClass.getName(), is("Hello.java"));
- assertThat(javaClass.getLongName(), is("Hello"));
- assertThat(javaClass.getParent().getKey()).isEqualTo("src/main/java");
- assertThat(javaClass.getParent().getDeprecatedKey()).isEqualTo(Directory.ROOT);
- assertThat(javaClass.getParent().isDefault()).isTrue();
- }
-
- @Test
- public void testNewClassInRootFolder() {
- JavaFile javaClass = JavaFile.create("Hello.java", "Hello.java", false);
- assertThat(javaClass.getKey()).isEqualTo("Hello.java");
- assertThat(javaClass.getDeprecatedKey(), is(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello"));
- assertThat(javaClass.getName(), is("Hello.java"));
- assertThat(javaClass.getLongName(), is("Hello"));
- assertThat(javaClass.getParent().getKey()).isEqualTo("/");
- assertThat(javaClass.getParent().getDeprecatedKey()).isEqualTo(Directory.ROOT);
- assertThat(javaClass.getParent().isDefault()).isTrue();
- }
-
- @Test
- public void testNewClassWithEmptyPackageDeprecatedConstructor() {
- JavaFile javaClass = new JavaFile("", "Hello", false);
- assertThat(javaClass.getDeprecatedKey(), is(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello"));
- assertThat(javaClass.getName(), is("Hello.java"));
- assertThat(javaClass.getLongName(), is("Hello"));
- assertThat(javaClass.getParent().isDefault(), is(true));
- }
-
- @Test
- public void testNewClassWithNullPackageDeprecatedConstructor() {
- JavaFile javaClass = new JavaFile(null, "Hello", false);
- assertThat(javaClass.getDeprecatedKey(), is(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello"));
- assertThat(javaClass.getName(), is("Hello.java"));
- assertThat(javaClass.getLongName(), is("Hello"));
- assertThat((javaClass.getParent()).isDefault(), is(true));
- }
-
- @Test
- public void shouldBeDefaultPackageIfNoPackage() {
- JavaFile javaClass = new JavaFile("Hello", false);
- assertEquals(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello", javaClass.getDeprecatedKey());
- assertThat(javaClass.getName(), is("Hello.java"));
- assertThat(javaClass.getLongName(), is("Hello"));
- assertThat(javaClass.getParent().isDefault(), is(true));
- }
-
- @Test
- public void aClassShouldBeNamedJava() {
- JavaFile javaClass = new JavaFile("org.foo.bar.Java", false);
- assertThat(javaClass.getDeprecatedKey(), is("org.foo.bar.Java"));
- assertThat(javaClass.getLongName(), is("org.foo.bar.Java"));
- assertThat(javaClass.getName(), is("Java.java"));
- JavaPackage parent = javaClass.getParent();
- assertEquals("org/foo/bar", parent.getDeprecatedKey());
- }
-
- @Test
- public void shouldTrimClasses() {
- JavaFile clazz = new JavaFile(" org.foo.bar.Hello ", false);
- assertThat(clazz.getDeprecatedKey(), is("org.foo.bar.Hello"));
- assertThat(clazz.getLongName(), is("org.foo.bar.Hello"));
- assertThat(clazz.getName(), is("Hello.java"));
- JavaPackage parent = clazz.getParent();
- assertThat(parent.getDeprecatedKey(), is("org/foo/bar"));
- }
-
- @Test
- public void testEqualsOnClasses() {
- JavaFile class1 = new JavaFile("foo.bar", "Hello", false);
- JavaFile class2 = new JavaFile("foo.bar.Hello", false);
- assertThat(class1).isEqualTo(class2);
-
- class1 = new JavaFile("NoPackage", false);
- class2 = new JavaFile("NoPackage", false);
- assertThat(class1).isEqualTo(class2);
- assertThat(class1).isEqualTo(class1);
- }
-
- @Test
- public void javaFilesAreEquivalentToFiles() {
- JavaFile javaFile = new JavaFile("foo");
- javaFile.setKey("someKey");
- org.sonar.api.resources.File file = new org.sonar.api.resources.File("bar");
- file.setKey("someKey");
- assertThat(javaFile).isEqualTo(file);
- }
-
- @Test
- public void oneLevelPackage() {
- JavaFile clazz = new JavaFile("onelevel.MyFile");
- assertEquals("onelevel.MyFile", clazz.getDeprecatedKey());
- assertEquals("onelevel", clazz.getParent().getDeprecatedKey());
-
- clazz = new JavaFile("onelevel", "MyFile");
- assertEquals("onelevel.MyFile", clazz.getDeprecatedKey());
- assertEquals("onelevel", clazz.getParent().getDeprecatedKey());
-
- File sourceDir = newDir("sources");
- List<File> sources = Arrays.asList(sourceDir);
- JavaFile javaFile = JavaFile.fromAbsolutePath(absPath(sourceDir, "onelevel/MyFile.java"), sources, false);
- assertEquals("onelevel.MyFile", javaFile.getDeprecatedKey());
- assertEquals("MyFile.java", javaFile.getName());
- assertEquals("onelevel", javaFile.getParent().getDeprecatedKey());
- assertThat(javaFile.getParent().isDefault(), is(false));
- }
-
- @Test
- public void shouldResolveClassFromAbsolutePath() {
- File sources1 = newDir("source1");
- File sources2 = newDir("source2");
- List<File> sources = Arrays.asList(sources1, sources2);
- JavaFile javaFile = JavaFile.fromAbsolutePath(absPath(sources2, "foo/bar/MyFile.java"), sources, false);
- assertThat("foo.bar.MyFile", is(javaFile.getDeprecatedKey()));
- assertThat(javaFile.getLongName(), is("foo.bar.MyFile"));
- assertThat(javaFile.getName(), is("MyFile.java"));
- assertThat(javaFile.getParent().getDeprecatedKey(), is("foo/bar"));
- }
-
- @Test
- public void shouldResolveFromAbsolutePathEvenIfDefaultPackage() {
- File source1 = newDir("source1");
- File source2 = newDir("source2");
- List<File> sources = Arrays.asList(source1, source2);
-
- JavaFile javaClass = JavaFile.fromAbsolutePath(absPath(source1, "MyClass.java"), sources, false);
- assertEquals(JavaPackage.DEFAULT_PACKAGE_NAME + ".MyClass", javaClass.getDeprecatedKey());
- assertEquals("MyClass.java", javaClass.getName());
-
- assertThat((javaClass.getParent()).isDefault()).isEqualTo(true);
- }
-
- @Test
- public void shouldResolveOnlyJavaFromAbsolutePath() {
- File source1 = newDir("source1");
- List<File> sources = Arrays.asList(source1);
- assertThat(JavaFile.fromAbsolutePath(absPath(source1, "foo/bar/my_file.sql"), sources, false)).isNull();
- }
-
- @Test
- public void shouldNotFailWhenResolvingUnknownClassFromAbsolutePath() {
- File source1 = newDir("source1");
- List<File> sources = Arrays.asList(source1);
- assertThat(JavaFile.fromAbsolutePath("/home/other/src/main/java/foo/bar/MyClass.java", sources, false)).isNull();
- }
-
- @Test
- public void shouldMatchFilePatterns() {
- JavaFile clazz = JavaFile.create("src/main/java/org/sonar/commons/Foo.java", "org/sonar/commons/Foo.java", false);
- assertThat(clazz.matchFilePattern("**/commons/**/*.java")).isTrue();
- assertThat(clazz.matchFilePattern("/**/commons/**/*.java")).isTrue();
- assertThat(clazz.matchFilePattern("/**/commons/**/*.*")).isTrue();
- assertThat(clazz.matchFilePattern("/**/sonar/*.java")).isFalse();
- assertThat(clazz.matchFilePattern("src/main/java/org/*/commons/**/*.java")).isTrue();
- assertThat(clazz.matchFilePattern("src/main/java/org/sonar/commons/*")).isTrue();
- assertThat(clazz.matchFilePattern("src/main/java/org/sonar/**/*.java")).isTrue();
- assertThat(clazz.matchFilePattern("src/main/java/org/sonar/*")).isFalse();
- assertThat(clazz.matchFilePattern("src/main/java/org/sonar*/*")).isFalse();
- assertThat(clazz.matchFilePattern("src/main/java/org/**")).isTrue();
- assertThat(clazz.matchFilePattern("*src/main/java/org/sona?/co??ons/**.*")).isTrue();
- assertThat(clazz.matchFilePattern("src/main/java/org/sonar/core/**")).isFalse();
- assertThat(clazz.matchFilePattern("src/main/java/org/sonar/commons/Foo.java")).isTrue();
- assertThat(clazz.matchFilePattern("**/*Foo.java")).isTrue();
- assertThat(clazz.matchFilePattern("**/*Foo.*")).isTrue();
- assertThat(clazz.matchFilePattern("src/main/java/org/*/*/Foo.java")).isTrue();
- assertThat(clazz.matchFilePattern("src/main/java/org/**/**/Foo.java")).isTrue();
- assertThat(clazz.matchFilePattern("**/commons/**/*")).isTrue();
- assertThat(clazz.matchFilePattern("**/*")).isTrue();
- }
-
- // SONAR-4397
- @Test
- public void shouldMatchFilePatternsWhenNoPackage() {
- JavaFile clazz = JavaFile.create("src/main/java/Foo.java", "Foo.java", false);
- assertThat(clazz.matchFilePattern("**/*Foo.java")).isTrue();
- assertThat(clazz.matchFilePattern("**/*Foo.*")).isTrue();
- assertThat(clazz.matchFilePattern("**/*")).isTrue();
- assertThat(clazz.matchFilePattern("src/main/java/Foo*.*")).isTrue();
- }
-
- /**
- * See http://jira.codehaus.org/browse/SONAR-1449
- */
- @Test
- public void doNotMatchAPattern() {
- JavaFile file = JavaFile.create("src/main/java/org/sonar/commons/Foo.java", "org/sonar/commons/Foo.java", false);
- assertThat(file.matchFilePattern("**/*.aj")).isFalse();
- assertThat(file.matchFilePattern("**/*.java")).isTrue();
- }
-
- @Test
- public void should_exclude_test_files() {
- JavaFile unitTest = JavaFile.create("src/main/java/org/sonar/commons/Foo.java", "org/sonar/commons/Foo.java", true);
- assertThat(unitTest.matchFilePattern("**/*")).isTrue();
- }
-
- private File newDir(String dirName) {
- return tempFolder.newFolder(dirName);
- }
-
- private String absPath(File dir, String filePath) {
- return new File(dir, filePath).getPath();
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.api.resources;
-
-import org.junit.Test;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertThat;
-
-public class JavaPackageTest {
- @Test
- public void defaultPackageDeprecatedConstructor() {
- assertEquals(new JavaPackage(), new JavaPackage());
- assertEquals(Directory.ROOT, new JavaPackage(null).getDeprecatedKey());
- assertEquals(Directory.ROOT, new JavaPackage("").getDeprecatedKey());
- assertThat(new JavaPackage(null).isDefault(), is(true));
- }
-
- @Test
- public void testNewPackageDeprecatedConstructor() {
- assertEquals(new JavaPackage(" foo.bar "), new JavaPackage("foo.bar"));
- JavaPackage pac = new JavaPackage("foo.bar");
- assertEquals("foo/bar", pac.getDeprecatedKey());
- }
-
- @Test
- public void singleLevelPackageDeprecatedConstructor() {
- assertEquals(new JavaPackage("foo"), new JavaPackage("foo"));
- JavaPackage pac = new JavaPackage("foo");
- assertEquals("foo", pac.getDeprecatedKey());
- }
-
- @Test
- public void shouldNotMatchFilePatterns() {
- JavaPackage pac = new JavaPackage("org.sonar.commons");
- assertFalse(pac.matchFilePattern("**"));
- }
-
- @Test
- public void packagesAreEquivalentToDirectories() {
- JavaPackage pac = new JavaPackage();
- pac.setKey("someKey");
- Directory dir = new Directory();
- dir.setKey("someKey");
- assertThat(pac).isEqualTo(dir);
- }
-
-}
public class ResourceUtilsTest {
@Test
- public void checkJavaClass() {
- JavaFile clazz = new JavaFile("hello.Foo");
- assertThat(ResourceUtils.isClass(clazz), is(true));
- assertThat(ResourceUtils.isPackage(clazz), is(false));
- assertThat(ResourceUtils.isModuleProject(clazz), is(false));
- assertThat(ResourceUtils.isSpace(clazz), is(false));
- assertThat(ResourceUtils.isEntity(clazz), is(true));
- assertThat(ResourceUtils.isSet(clazz), is(false));
- assertThat(ResourceUtils.isRootProject(clazz), is(false));
- assertThat(ResourceUtils.isUnitTestClass(clazz), is(false));
+ public void checkFile() {
+ File file = new File("hello.Foo");
+ assertThat(ResourceUtils.isClass(file), is(true));
+ assertThat(ResourceUtils.isPackage(file), is(false));
+ assertThat(ResourceUtils.isModuleProject(file), is(false));
+ assertThat(ResourceUtils.isSpace(file), is(false));
+ assertThat(ResourceUtils.isEntity(file), is(true));
+ assertThat(ResourceUtils.isSet(file), is(false));
+ assertThat(ResourceUtils.isRootProject(file), is(false));
+ assertThat(ResourceUtils.isUnitTestClass(file), is(false));
}
@Test
- public void checkJavaUnitTest() {
- JavaFile clazz = new JavaFile("hello.Foo", true);
- assertThat(ResourceUtils.isClass(clazz), is(false));
- assertThat(ResourceUtils.isPackage(clazz), is(false));
- assertThat(ResourceUtils.isModuleProject(clazz), is(false));
- assertThat(ResourceUtils.isSpace(clazz), is(false));
- assertThat(ResourceUtils.isEntity(clazz), is(true));
- assertThat(ResourceUtils.isSet(clazz), is(false));
- assertThat(ResourceUtils.isRootProject(clazz), is(false));
- assertThat(ResourceUtils.isUnitTestClass(clazz), is(true));
+ public void checkUnitTest() {
+ File utFile = new File("hello.Foo");
+ utFile.setQualifier(Qualifiers.UNIT_TEST_FILE);
+ assertThat(ResourceUtils.isClass(utFile), is(false));
+ assertThat(ResourceUtils.isPackage(utFile), is(false));
+ assertThat(ResourceUtils.isModuleProject(utFile), is(false));
+ assertThat(ResourceUtils.isSpace(utFile), is(false));
+ assertThat(ResourceUtils.isEntity(utFile), is(true));
+ assertThat(ResourceUtils.isSet(utFile), is(false));
+ assertThat(ResourceUtils.isRootProject(utFile), is(false));
+ assertThat(ResourceUtils.isUnitTestClass(utFile), is(true));
}
@Test
- public void checkJavaPackage() {
- JavaPackage pack = new JavaPackage("hello");
- assertThat(ResourceUtils.isClass(pack), is(false));
- assertThat(ResourceUtils.isPackage(pack), is(true));
- assertThat(ResourceUtils.isModuleProject(pack), is(false));
- assertThat(ResourceUtils.isSpace(pack), is(true));
- assertThat(ResourceUtils.isEntity(pack), is(false));
- assertThat(ResourceUtils.isSet(pack), is(false));
- assertThat(ResourceUtils.isRootProject(pack), is(false));
- assertThat(ResourceUtils.isUnitTestClass(pack), is(false));
+ public void checkDirectory() {
+ Directory dir = new Directory("hello");
+ assertThat(ResourceUtils.isClass(dir), is(false));
+ assertThat(ResourceUtils.isPackage(dir), is(true));
+ assertThat(ResourceUtils.isModuleProject(dir), is(false));
+ assertThat(ResourceUtils.isSpace(dir), is(true));
+ assertThat(ResourceUtils.isEntity(dir), is(false));
+ assertThat(ResourceUtils.isSet(dir), is(false));
+ assertThat(ResourceUtils.isRootProject(dir), is(false));
+ assertThat(ResourceUtils.isUnitTestClass(dir), is(false));
}
@Test