aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-deprecated/src
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2014-02-12 11:33:13 +0100
committerJulien HENRY <julien.henry@sonarsource.com>2014-02-12 11:34:46 +0100
commit9cc87b7ac82818ae37dab4bbf8f296ff388b6d74 (patch)
tree051a5cf1a043f93104dc6b7651e80d111b5ba1df /sonar-deprecated/src
parent039316de80b7e71a349f4ed8f6b2730aa01c457e (diff)
downloadsonarqube-9cc87b7ac82818ae37dab4bbf8f296ff388b6d74.tar.gz
sonarqube-9cc87b7ac82818ae37dab4bbf8f296ff388b6d74.zip
SONAR-926 Move JavaFile/JavaPackage to sonar deprecated and remove all usages of them
Diffstat (limited to 'sonar-deprecated/src')
-rw-r--r--sonar-deprecated/src/main/java/org/sonar/api/batch/SquidUtils.java66
-rw-r--r--sonar-deprecated/src/main/java/org/sonar/api/resources/JavaFile.java315
-rw-r--r--sonar-deprecated/src/main/java/org/sonar/api/resources/JavaPackage.java141
-rw-r--r--sonar-deprecated/src/test/java/org/sonar/api/batch/SquidUtilsTest.java54
-rw-r--r--sonar-deprecated/src/test/java/org/sonar/api/resources/JavaFileTest.java274
-rw-r--r--sonar-deprecated/src/test/java/org/sonar/api/resources/JavaPackageTest.java58
6 files changed, 908 insertions, 0 deletions
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/batch/SquidUtils.java b/sonar-deprecated/src/main/java/org/sonar/api/batch/SquidUtils.java
new file mode 100644
index 00000000000..b8381af2819
--- /dev/null
+++ b/sonar-deprecated/src/main/java/org/sonar/api/batch/SquidUtils.java
@@ -0,0 +1,66 @@
+/*
+ * 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");
+ }
+}
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/resources/JavaFile.java b/sonar-deprecated/src/main/java/org/sonar/api/resources/JavaFile.java
new file mode 100644
index 00000000000..4bb59509364
--- /dev/null
+++ b/sonar-deprecated/src/main/java/org/sonar/api/resources/JavaFile.java
@@ -0,0 +1,315 @@
+/*
+ * 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();
+ }
+
+}
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/resources/JavaPackage.java b/sonar-deprecated/src/main/java/org/sonar/api/resources/JavaPackage.java
new file mode 100644
index 00000000000..356cf58a14e
--- /dev/null
+++ b/sonar-deprecated/src/main/java/org/sonar/api/resources/JavaPackage.java
@@ -0,0 +1,141 @@
+/*
+ * 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();
+ }
+}
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/batch/SquidUtilsTest.java b/sonar-deprecated/src/test/java/org/sonar/api/batch/SquidUtilsTest.java
new file mode 100644
index 00000000000..7313c4ca330
--- /dev/null
+++ b/sonar-deprecated/src/test/java/org/sonar/api/batch/SquidUtilsTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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"));
+ }
+}
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/resources/JavaFileTest.java b/sonar-deprecated/src/test/java/org/sonar/api/resources/JavaFileTest.java
new file mode 100644
index 00000000000..619dfff794d
--- /dev/null
+++ b/sonar-deprecated/src/test/java/org/sonar/api/resources/JavaFileTest.java
@@ -0,0 +1,274 @@
+/*
+ * 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();
+ }
+}
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/resources/JavaPackageTest.java b/sonar-deprecated/src/test/java/org/sonar/api/resources/JavaPackageTest.java
new file mode 100644
index 00000000000..9210a95ce5c
--- /dev/null
+++ b/sonar-deprecated/src/test/java/org/sonar/api/resources/JavaPackageTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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("**"));
+ }
+
+}