diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-01-13 20:49:01 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-01-13 20:49:54 +0100 |
commit | 808e4f107fdd1659a41bb616660090fc04a9d7c0 (patch) | |
tree | e3b1c3fa12d35d90909dbe77f1c6722796b15885 /sonar-plugin-api | |
parent | 256c992dd006ded2b7e2a84e0a5a7dfb8be16fe7 (diff) | |
download | sonarqube-808e4f107fdd1659a41bb616660090fc04a9d7c0.tar.gz sonarqube-808e4f107fdd1659a41bb616660090fc04a9d7c0.zip |
SONAR-3024 Introduce deprecated key on resources to ease transition
Diffstat (limited to 'sonar-plugin-api')
13 files changed, 421 insertions, 213 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SquidUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SquidUtils.java index dec485a41a7..c26100b9825 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SquidUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SquidUtils.java @@ -34,7 +34,7 @@ public final class SquidUtils { 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); + key = key.substring(0, key.length() - extension.length() - 1); } String convertedKey = key.replace('/', '.'); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/ResourceModel.java b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/ResourceModel.java index e37f0714b22..e3e97d5f8ac 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/ResourceModel.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/ResourceModel.java @@ -75,9 +75,12 @@ public class ResourceModel extends BaseIdentifiable implements Cloneable { @Column(name = "qualifier", updatable = true, nullable = false, length = 10) private String qualifier; - @Column(name = "kee", updatable = false, nullable = false, length = KEY_SIZE) + @Column(name = "kee", updatable = true, nullable = false, length = KEY_SIZE) private String key; + @Column(name = "deprecated_kee", updatable = true, nullable = true, length = KEY_SIZE) + private String deprecatedKey; + @Column(name = "language", updatable = true, nullable = true, length = 20) private String languageKey; @@ -219,6 +222,10 @@ public class ResourceModel extends BaseIdentifiable implements Cloneable { return key; } + public String getDeprecatedKey() { + return deprecatedKey; + } + public String getLanguageKey() { return languageKey; } @@ -260,6 +267,16 @@ public class ResourceModel extends BaseIdentifiable implements Cloneable { this.key = key; } + /** + * @throws IllegalArgumentException if the key is longer than KEY_SIZE + */ + public void setDeprecatedKey(String deprecatedKey) { + if (deprecatedKey.length() > KEY_SIZE) { + throw new IllegalArgumentException("Resource deprecated key is too long, max is " + KEY_SIZE + " characters. Got : " + deprecatedKey); + } + this.deprecatedKey = deprecatedKey; + } + public Integer getRootId() { return rootId; } @@ -305,13 +322,6 @@ public class ResourceModel extends BaseIdentifiable implements Cloneable { return true; } ResourceModel other = (ResourceModel) obj; - if (StringUtils.isNotBlank(path)) { - return new EqualsBuilder() - .append(path, other.path) - .append(enabled, other.enabled) - .append(rootId, other.rootId) - .isEquals(); - } return new EqualsBuilder() .append(key, other.key) .append(enabled, other.enabled) @@ -321,13 +331,6 @@ public class ResourceModel extends BaseIdentifiable implements Cloneable { @Override public int hashCode() { - if (StringUtils.isNotBlank(path)) { - return new HashCodeBuilder(17, 37) - .append(path) - .append(enabled) - .append(rootId) - .toHashCode(); - } return new HashCodeBuilder(17, 37) .append(key) .append(enabled) @@ -340,6 +343,7 @@ public class ResourceModel extends BaseIdentifiable implements Cloneable { return new ToStringBuilder(this) .append("id", getId()) .append("key", key) + .append("deprecatedKey", deprecatedKey) .append("scope", scope) .append("qualifier", qualifier) .append("name", name) @@ -358,6 +362,7 @@ public class ResourceModel extends BaseIdentifiable implements Cloneable { public Object clone() { ResourceModel clone = new ResourceModel(getScope(), getKey(), getQualifier(), getRootId(), getPath(), getName()); clone.setDescription(getDescription()); + clone.setDeprecatedKey(getDeprecatedKey()); clone.setEnabled(getEnabled()); clone.setProjectLinks(getProjectLinks()); clone.setLanguageKey(getLanguageKey()); @@ -376,6 +381,7 @@ public class ResourceModel extends BaseIdentifiable implements Cloneable { model.setEnabled(Boolean.TRUE); model.setDescription(resource.getDescription()); model.setKey(resource.getKey()); + model.setDeprecatedKey(resource.getDeprecatedKey()); model.setPath(resource.getPath()); if (resource.getLanguage() != null) { model.setLanguageKey(resource.getLanguage().getKey()); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java index 98f9c386bf8..1bb04d84886 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java @@ -33,18 +33,30 @@ public class Directory extends Resource { private Language language; - public Directory(String key) { - this(key, null); + private Directory() { + // USed by factory } - public Directory(String key, Language language) { - setKey(parseKey(key)); + /** + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} + */ + @Deprecated + public Directory(String deprecatedKey) { + this(deprecatedKey, null); + } + + /** + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} + */ + @Deprecated + public Directory(String deprecatedKey, Language language) { + setDeprecatedKey(parseKey(deprecatedKey)); this.language = language; } @Override public String getName() { - return getKey(); + return getDeprecatedKey(); } @Override @@ -95,6 +107,15 @@ public class Directory extends Resource { return key; } + public static Directory create(String path, String directoryDeprecatedKey) { + Directory d = new Directory(); + String normalizedPath = normalize(path); + d.setKey(normalizedPath); + d.setDeprecatedKey(parseKey(directoryDeprecatedKey)); + d.setPath(normalizedPath); + return d; + } + @Override public String toString() { return new ToStringBuilder(this) @@ -103,4 +124,5 @@ public class Directory extends Resource { .append("language", language) .toString(); } + } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java index 84d2d34ce77..75adc4cb1b3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java @@ -35,58 +35,70 @@ public class File extends Resource { public static final String SCOPE = Scopes.FILE; - private String directoryKey; + private String directoryDeprecatedKey; private String filename; private Language language; private Directory parent; private String qualifier = Qualifiers.FILE; + private File() { + // Used by factory method + } + /** * File in project. Key is the path relative to project source directories. It is not the absolute path and it does not include the path * to source directories. Example : <code>new File("org/sonar/foo.sql")</code>. The absolute path may be * c:/myproject/src/main/sql/org/sonar/foo.sql. Project root is c:/myproject and source dir is src/main/sql. + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} */ - public File(String key) { - if (key == null) { + @Deprecated + public File(String deprecatedKey) { + if (deprecatedKey == null) { throw new IllegalArgumentException("File key is null"); } - String realKey = parseKey(key); + String realKey = parseKey(deprecatedKey); if (realKey.indexOf(Directory.SEPARATOR) >= 0) { - this.directoryKey = Directory.parseKey(StringUtils.substringBeforeLast(key, Directory.SEPARATOR)); + this.directoryDeprecatedKey = Directory.parseKey(StringUtils.substringBeforeLast(deprecatedKey, Directory.SEPARATOR)); this.filename = StringUtils.substringAfterLast(realKey, Directory.SEPARATOR); - realKey = new StringBuilder().append(this.directoryKey).append(Directory.SEPARATOR).append(filename).toString(); + realKey = new StringBuilder().append(this.directoryDeprecatedKey).append(Directory.SEPARATOR).append(filename).toString(); } else { - this.filename = key; + this.filename = deprecatedKey; } - setKey(realKey); + setDeprecatedKey(realKey); } /** * Creates a file from its containing directory and name + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} */ - public File(String directory, String filename) { + @Deprecated + public File(String deprecatedDirectoryKey, String filename) { this.filename = StringUtils.trim(filename); - if (StringUtils.isBlank(directory)) { - setKey(filename); + if (StringUtils.isBlank(deprecatedDirectoryKey)) { + setDeprecatedKey(filename); } else { - this.directoryKey = Directory.parseKey(directory); - setKey(new StringBuilder().append(directoryKey).append(Directory.SEPARATOR).append(this.filename).toString()); + this.directoryDeprecatedKey = Directory.parseKey(deprecatedDirectoryKey); + setDeprecatedKey(new StringBuilder().append(directoryDeprecatedKey).append(Directory.SEPARATOR).append(this.filename).toString()); } } /** * Creates a File from its language and its key + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} */ - public File(Language language, String key) { - this(key); + @Deprecated + public File(Language language, String deprecatedKey) { + this(deprecatedKey); this.language = language; } /** * Creates a File from language, directory and filename + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} */ + @Deprecated public File(Language language, String directory, String filename) { this(directory, filename); this.language = language; @@ -100,11 +112,7 @@ public class File extends Resource { @Override public Directory getParent() { if (parent == null) { - parent = new Directory(directoryKey); - String filePath = getPath(); - if (StringUtils.isNotBlank(filePath)) { - parent.setPath(StringUtils.substringBeforeLast(filePath, Directory.SEPARATOR)); - } + parent = new Directory(directoryDeprecatedKey); } return parent; } @@ -126,13 +134,15 @@ public class File extends Resource { */ @Override public boolean matchFilePattern(String antPattern) { - WildcardPattern matcher = WildcardPattern.create(antPattern, "/"); + WildcardPattern matcher = WildcardPattern.create(antPattern, Directory.SEPARATOR); return matcher.match(getKey()); } /** * Creates a File from an io.file and a list of sources directories + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} */ + @Deprecated public static File fromIOFile(java.io.File file, List<java.io.File> sourceDirs) { PathResolver.RelativePath relativePath = new PathResolver().relativePath(sourceDirs, file); if (relativePath != null) { @@ -143,7 +153,9 @@ public class File extends Resource { /** * Creates a File from its name and a project + * @deprecated since 4.2 use {@link #create(String, String, Language, boolean)} */ + @Deprecated public static File fromIOFile(java.io.File file, Project project) { return fromIOFile(file, project.getFileSystem().getSourceDirs()); } @@ -217,12 +229,35 @@ public class File extends Resource { this.qualifier = qualifier; } + public static File create(String relativePathFromBasedir, String relativePathFromSourceDir, Language language, boolean unitTest) { + File file = new File(); + String normalizedPath = normalize(relativePathFromBasedir); + file.setKey(normalizedPath); + file.setPath(normalizedPath); + String directoryKey = StringUtils.substringBeforeLast(normalizedPath, Directory.SEPARATOR); + file.setLanguage(language); + if (relativePathFromSourceDir.contains(Directory.SEPARATOR)) { + file.filename = StringUtils.substringAfterLast(relativePathFromSourceDir, Directory.SEPARATOR); + file.directoryDeprecatedKey = Directory.parseKey(StringUtils.substringBeforeLast(relativePathFromSourceDir, Directory.SEPARATOR)); + file.setDeprecatedKey(file.directoryDeprecatedKey + Directory.SEPARATOR + file.filename); + } else { + file.filename = relativePathFromSourceDir; + file.directoryDeprecatedKey = Directory.ROOT; + file.setDeprecatedKey(file.filename); + } + if (unitTest) { + file.setQualifier(Qualifiers.UNIT_TEST_FILE); + } + file.parent = Directory.create(directoryKey, file.directoryDeprecatedKey); + return file; + } + @Override public String toString() { return new ToStringBuilder(this) .append("key", getKey()) .append("path", getPath()) - .append("dir", directoryKey) + .append("dir", directoryDeprecatedKey) .append("filename", filename) .append("language", language) .toString(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaFile.java index d3cbf2f88ad..74fce50de45 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaFile.java @@ -35,15 +35,21 @@ import java.util.List; public class JavaFile extends Resource { private static final String JAVA_SUFFIX = ".java"; - private String filename; - private String longName; - private String packageKey; + private String className; + 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 + * @deprecated since 4.2 use {@link #create(String, String, boolean)} */ + @Deprecated public JavaFile(String packageName, String className) { this(packageName, className, false); } @@ -52,29 +58,33 @@ public class JavaFile extends Resource { * 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 + * @deprecated since 4.2 use {@link #create(String, String, boolean)} */ + @Deprecated public JavaFile(String packageKey, String className, boolean unitTest) { if (className == null) { throw new IllegalArgumentException("Java filename can not be null"); } - this.filename = StringUtils.trim(className); - String key; + this.className = StringUtils.trim(className); + String deprecatedKey; if (StringUtils.isBlank(packageKey)) { - this.packageKey = JavaPackage.DEFAULT_PACKAGE_NAME; - this.longName = this.filename; - key = new StringBuilder().append(this.packageKey).append(".").append(this.filename).toString(); + this.packageFullyQualifiedName = JavaPackage.DEFAULT_PACKAGE_NAME; + this.fullyQualifiedName = this.className; + deprecatedKey = new StringBuilder().append(this.packageFullyQualifiedName).append(".").append(this.className).toString(); } else { - this.packageKey = packageKey.trim(); - key = new StringBuilder().append(this.packageKey).append(".").append(this.filename).toString(); - this.longName = key; + this.packageFullyQualifiedName = packageKey.trim(); + deprecatedKey = new StringBuilder().append(this.packageFullyQualifiedName).append(".").append(this.className).toString(); + this.fullyQualifiedName = deprecatedKey; } - setKey(key); + setDeprecatedKey(deprecatedKey); this.unitTest = unitTest; } /** * Creates a source file from its key + * @deprecated since 4.2 use {@link #create(String, String, boolean)} */ + @Deprecated public JavaFile(String key) { this(key, false); } @@ -83,7 +93,9 @@ public class JavaFile extends Resource { * Creates any JavaFile from its key * * @param unitTest whether it is a unit test file or a source file + * @deprecated since 4.2 use {@link #create(String, String, boolean)} */ + @Deprecated public JavaFile(String key, boolean unitTest) { if (key == null) { throw new IllegalArgumentException("Java filename can not be null"); @@ -92,17 +104,17 @@ public class JavaFile extends Resource { this.unitTest = unitTest; if (realKey.contains(".")) { - this.filename = StringUtils.substringAfterLast(realKey, "."); - this.packageKey = StringUtils.substringBeforeLast(realKey, "."); - this.longName = realKey; + this.className = StringUtils.substringAfterLast(realKey, "."); + this.packageFullyQualifiedName = StringUtils.substringBeforeLast(realKey, "."); + this.fullyQualifiedName = realKey; } else { - this.filename = realKey; - this.longName = realKey; - this.packageKey = JavaPackage.DEFAULT_PACKAGE_NAME; + this.className = realKey; + this.fullyQualifiedName = realKey; + this.packageFullyQualifiedName = JavaPackage.DEFAULT_PACKAGE_NAME; realKey = new StringBuilder().append(JavaPackage.DEFAULT_PACKAGE_NAME).append(".").append(realKey).toString(); } - setKey(realKey); + setDeprecatedKey(realKey); } /** @@ -111,14 +123,9 @@ public class JavaFile extends Resource { @Override public JavaPackage getParent() { if (parent == null) { - parent = new JavaPackage(packageKey); - String filePath = getPath(); - if (StringUtils.isNotBlank(filePath)) { - parent.setPath(StringUtils.substringBeforeLast(filePath, Directory.SEPARATOR)); - } + parent = new JavaPackage(packageFullyQualifiedName); } return parent; - } /** @@ -142,7 +149,7 @@ public class JavaFile extends Resource { */ @Override public String getName() { - return filename; + return className; } /** @@ -150,7 +157,7 @@ public class JavaFile extends Resource { */ @Override public String getLongName() { - return longName; + return fullyQualifiedName; } /** @@ -181,22 +188,14 @@ public class JavaFile extends Resource { */ @Override public boolean matchFilePattern(String antPattern) { - String fileKey = getKey(); - if (!fileKey.endsWith(JAVA_SUFFIX)) { - fileKey += JAVA_SUFFIX; - } - // Add wildcard extension if not provided - if ((antPattern.contains("/") && StringUtils.substringAfterLast(antPattern, "/").indexOf('.') < 0) || antPattern.indexOf('.') < 0) { - antPattern += ".*"; - } - String noPackagePrefix = JavaPackage.DEFAULT_PACKAGE_NAME + "."; - if (fileKey.startsWith(noPackagePrefix)) { - fileKey = fileKey.substring(noPackagePrefix.length()); - } - WildcardPattern matcher = WildcardPattern.create(antPattern, "."); - return matcher.match(fileKey); + WildcardPattern matcher = WildcardPattern.create(antPattern, Directory.SEPARATOR); + return matcher.match(getKey()); } + /** + * @deprecated since 4.2 use {@link #create(String, String, boolean)} + */ + @Deprecated public static JavaFile fromIOFile(File file, Project module, boolean unitTest) { if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), JAVA_SUFFIX)) { return null; @@ -212,6 +211,43 @@ public class JavaFile extends Resource { 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); + String directoryKey = StringUtils.substringBeforeLast(normalizedPath, Directory.SEPARATOR); + javaFile.parent = JavaPackage.create(directoryKey); + return javaFile; + } + + 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.className = StringUtils.substringAfterLast(relativePathFromSourceDir, Directory.SEPARATOR); + javaFile.className = StringUtils.removeEndIgnoreCase(javaFile.className, JAVA_SUFFIX); + javaFile.fullyQualifiedName = javaFile.packageFullyQualifiedName + "." + javaFile.className; + javaFile.setDeprecatedKey(javaFile.fullyQualifiedName); + } 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.unitTest = unitTest; + javaFile.parent.setDeprecatedKey(javaFile.packageFullyQualifiedName); + 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; @@ -232,7 +268,9 @@ public class JavaFile extends Resource { * 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; @@ -246,7 +284,9 @@ public class JavaFile extends Resource { /** * 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; @@ -258,8 +298,9 @@ public class JavaFile extends Resource { public String toString() { return new ToStringBuilder(this) .append("key", getKey()) + .append("deprecatedKey", getDeprecatedKey()) .append("path", getPath()) - .append("filename", filename) + .append("filename", className) .toString(); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaPackage.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaPackage.java index 71bed21e468..499d2706a58 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaPackage.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaPackage.java @@ -22,9 +22,11 @@ package org.sonar.api.resources; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.ToStringBuilder; +import javax.annotation.Nullable; + /** * A class that represents a Java package in Sonar - * + * * @since 1.10 */ public class JavaPackage extends Resource { @@ -36,23 +38,27 @@ public class JavaPackage extends Resource { /** * Default constructor + * @deprecated since 4.2 use {@link #create(String, String)} */ + @Deprecated public JavaPackage() { this(null); } /** - * Creates a JavaPackage from its key. Will use DEFAULT_PACKAGE_NAME if key is null + * Creates a JavaPackage from its key. + * @deprecated since 4.2 use {@link #create(String, String)} */ - public JavaPackage(String key) { - setKey(StringUtils.defaultIfEmpty(StringUtils.trim(key), DEFAULT_PACKAGE_NAME)); + @Deprecated + public JavaPackage(String deprecatedKey) { + setDeprecatedKey(StringUtils.defaultIfEmpty(StringUtils.trim(deprecatedKey), DEFAULT_PACKAGE_NAME)); } /** * @return whether the JavaPackage key is the default key */ public boolean isDefault() { - return StringUtils.equals(getKey(), DEFAULT_PACKAGE_NAME); + return StringUtils.equals(getDeprecatedKey(), DEFAULT_PACKAGE_NAME); } /** @@ -92,7 +98,7 @@ public class JavaPackage extends Resource { */ @Override public String getName() { - return getKey(); + return getDeprecatedKey(); } /** @@ -119,11 +125,29 @@ public class JavaPackage extends Resource { return Java.INSTANCE; } + /** + * For internal use only. + */ + public static JavaPackage create(String path) { + JavaPackage pac = new JavaPackage(); + String normalizedPath = normalize(path); + pac.setKey(normalizedPath); + pac.setPath(normalizedPath); + return pac; + } + + public static JavaPackage create(String relativePathFromBasedir, @Nullable String packageQualifiedName) { + JavaPackage pac = JavaPackage.create(relativePathFromBasedir); + pac.setDeprecatedKey(StringUtils.defaultIfEmpty(StringUtils.trim(packageQualifiedName), DEFAULT_PACKAGE_NAME)); + return pac; + } + @Override public String toString() { return new ToStringBuilder(this) - .append("id", getId()) - .append("key", getKey()) - .toString(); + .append("id", getId()) + .append("key", getKey()) + .append("deprecatedKey", getDeprecatedKey()) + .toString(); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Resource.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Resource.java index 6588de10201..0bd6309a7e7 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Resource.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Resource.java @@ -19,8 +19,6 @@ */ package org.sonar.api.resources; -import org.apache.commons.lang.StringUtils; - import javax.annotation.Nullable; import java.io.Serializable; @@ -126,10 +124,14 @@ public abstract class Resource implements Serializable { private String key = null; + private String deprecatedKey = null; + private String path = null; private String effectiveKey = null; + private String deprecatedEffectiveKey = null; + private boolean isExcluded = false; /** @@ -139,11 +141,29 @@ public abstract class Resource implements Serializable { return key; } - protected void setKey(String s) { + /** + * Internal use only + */ + public void setKey(String s) { this.key = s; } /** + * @return the resource deprecated key. Should not be used except to deal with backward compatibility. + * @since 4.2 + */ + public final String getDeprecatedKey() { + return deprecatedKey; + } + + /** + * For internal use only + */ + public void setDeprecatedKey(String s) { + this.deprecatedKey = s; + } + + /** * @return the resource name */ public abstract String getName(); @@ -215,7 +235,7 @@ public abstract class Resource implements Serializable { return this; } - private String normalize(@Nullable String path) { + protected static String normalize(@Nullable String path) { if (path == null) { return null; } @@ -242,6 +262,21 @@ public abstract class Resource implements Serializable { } /** + * Internal use only + */ + public String getDeprecatedEffectiveKey() { + return deprecatedEffectiveKey; + } + + /** + * Internal use only + */ + public final Resource setDeprecatedEffectiveKey(String deprecatedEffectiveKey) { + this.deprecatedEffectiveKey = deprecatedEffectiveKey; + return this; + } + + /** * @deprecated since 2.6 should use SensorContext#isExcluded(resource). It will make inheritance of Resource easier. */ @Deprecated @@ -269,15 +304,15 @@ public abstract class Resource implements Serializable { } Resource resource = (Resource) o; - if (StringUtils.isBlank(path)) { + if (key != null) { return key.equals(resource.key); + } else { + return resource.key == null && deprecatedKey.equals(resource.deprecatedKey); } - return path.equals(resource.path); - } @Override public int hashCode() { - return key.hashCode(); + return key != null ? key.hashCode() : deprecatedKey.hashCode(); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/DefaultInputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/DefaultInputFile.java index cade6314655..43024fdf860 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/DefaultInputFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/DefaultInputFile.java @@ -42,6 +42,8 @@ public class DefaultInputFile implements InputFile { */ public static final String ATTRIBUTE_COMPONENT_KEY = "CMP_KEY"; + public static final String ATTRIBUTE_COMPONENT_DEPRECATED_KEY = "CMP_DEPRECATED_KEY"; + private final String absolutePath; private final String path; private final Map<String, String> attributes; diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/DirectoryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/DirectoryTest.java index b3d10a3a72f..a5f673b1649 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/DirectoryTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/DirectoryTest.java @@ -19,31 +19,40 @@ */ package org.sonar.api.resources; +import org.junit.Test; + import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; -import org.junit.Test; public class DirectoryTest { @Test - public void shouldNotStartOrEndBySlash() { + public void shouldStartBySlashAndNotEndBySlash() { + Resource dir = Directory.create("src/foo/bar/", " /foo/bar/ "); + assertThat(dir.getKey(), is("/src/foo/bar")); + assertThat(dir.getDeprecatedKey(), is("foo/bar")); + assertThat(dir.getName(), is("foo/bar")); + } + + @Test + public void shouldNotStartOrEndBySlashDeprecatedConstructor() { Resource dir = new Directory(" /foo/bar/ "); - assertThat(dir.getKey(), is("foo/bar")); + assertThat(dir.getDeprecatedKey(), is("foo/bar")); assertThat(dir.getName(), is("foo/bar")); } @Test - public void rootDirectory() { - assertThat(new Directory(null).getKey(), is(Directory.ROOT)); - assertThat(new Directory("").getKey(), is(Directory.ROOT)); - assertThat(new Directory(" ").getKey(), is(Directory.ROOT)); + public void rootDirectoryDeprecatedConstructor() { + assertThat(new Directory(null).getDeprecatedKey(), is(Directory.ROOT)); + assertThat(new Directory("").getDeprecatedKey(), is(Directory.ROOT)); + assertThat(new Directory(" ").getDeprecatedKey(), is(Directory.ROOT)); } @Test public void backSlashesShouldBeReplacedBySlashes() { Resource dir = new Directory(" foo\\bar\\ "); - assertThat(dir.getKey(), is("foo/bar")); + assertThat(dir.getDeprecatedKey(), is("foo/bar")); assertThat(dir.getName(), is("foo/bar")); } @@ -66,17 +75,18 @@ public class DirectoryTest { @Test public void matchExclusionPatterns() { - assertThat(new Directory("one/two/third").matchFilePattern("one/two/*.java"), is(false)); - assertThat(new Directory("one/two/third").matchFilePattern("false"), is(false)); - assertThat(new Directory("one/two/third").matchFilePattern("two/one/**"), is(false)); - assertThat(new Directory("one/two/third").matchFilePattern("other*/**"), is(false)); + Directory directory = Directory.create("src/one/two/third", "one/two/third"); + assertThat(directory.matchFilePattern("one/two/*.java"), is(false)); + assertThat(directory.matchFilePattern("false"), is(false)); + assertThat(directory.matchFilePattern("two/one/**"), is(false)); + assertThat(directory.matchFilePattern("other*/**"), is(false)); - assertThat(new Directory("one/two/third").matchFilePattern("one*/**"), is(true)); - assertThat(new Directory("one/two/third").matchFilePattern("one/t?o/**"), is(true)); - assertThat(new Directory("one/two/third").matchFilePattern("**/*"), is(true)); - assertThat(new Directory("one/two/third").matchFilePattern("**"), is(true)); - assertThat(new Directory("one/two/third").matchFilePattern("one/two/*"), is(true)); - assertThat(new Directory("one/two/third").matchFilePattern("/one/two/*"), is(true)); - assertThat(new Directory("one/two/third").matchFilePattern("one/**"), is(true)); + assertThat(directory.matchFilePattern("src/one*/**"), is(true)); + assertThat(directory.matchFilePattern("src/one/t?o/**"), is(true)); + assertThat(directory.matchFilePattern("**/*"), is(true)); + assertThat(directory.matchFilePattern("**"), is(true)); + assertThat(directory.matchFilePattern("src/one/two/*"), is(true)); + assertThat(directory.matchFilePattern("/src/one/two/*"), is(true)); + assertThat(directory.matchFilePattern("src/one/**"), is(true)); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/FileTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/FileTest.java index 0caefa9796d..4a2a757ebed 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/FileTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/FileTest.java @@ -19,73 +19,69 @@ */ 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.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; -import org.junit.Test; public class FileTest { @Test public void trimKeyAndName() { File file = new File(" foo/bar/ ", " toto.sql "); - assertThat(file.getKey(), is("foo/bar/toto.sql")); - assertThat(file.getLongName(), is("foo/bar/toto.sql")); + assertThat(file.getDeprecatedKey(), is("foo/bar/toto.sql")); assertThat(file.getName(), is("toto.sql")); - assertThat(file.getParent().getKey(), is("foo/bar")); - assertThat(file.getScope(), is(Resource.SCOPE_ENTITY)); - assertThat(file.getQualifier(), is(Resource.QUALIFIER_FILE)); } @Test public void parentIsDirectory() { - File file = new File(" foo/bar/", "toto.sql "); - assertThat(file.getKey(), is("foo/bar/toto.sql")); - assertThat(file.getLongName(), is("foo/bar/toto.sql")); + File file = File.create("src/foo/bar/toto.sql", "foo/bar/toto.sql", null, false); + assertThat(file.getKey(), is("/src/foo/bar/toto.sql")); + assertThat(file.getDeprecatedKey(), is("foo/bar/toto.sql")); + assertThat(file.getLongName(), is("/src/foo/bar/toto.sql")); assertThat(file.getName(), is("toto.sql")); - assertThat(file.getParent().getKey(), is("foo/bar")); + assertThat(file.getParent().getKey(), is("/src/foo/bar")); assertThat(ResourceUtils.isSpace(file.getParent()), is(true)); } @Test - public void rootFilesHaveParent() { - File file = new File((String) null, "toto.sql"); - assertThat(file.getKey(), is("toto.sql")); - assertThat(file.getName(), is("toto.sql")); - assertThat(file.getParent().getKey(), is(Directory.ROOT)); - - file = new File("", "toto.sql"); - assertThat(file.getKey(), is("toto.sql")); - assertThat(file.getName(), is("toto.sql")); - assertThat(file.getParent().getKey(), is(Directory.ROOT)); + public void parentIsDirectoryWithDeprecatedKey() { + File file = new File(" foo/bar/", "toto.sql "); + assertThat(file.getDeprecatedKey(), is("foo/bar/toto.sql")); + assertThat(file.getParent().getDeprecatedKey(), is("foo/bar")); + assertThat(ResourceUtils.isSpace(file.getParent()), is(true)); + } - file = new File("toto.sql"); - assertThat(file.getKey(), is("toto.sql")); + @Test + public void rootFilesHaveParent() { + File file = File.create("toto.sql", "toto.sql", null, false); + assertThat(file.getKey(), is("/toto.sql")); + assertThat(file.getDeprecatedKey(), is("toto.sql")); assertThat(file.getName(), is("toto.sql")); - assertThat(file.getParent().getKey(), is(Directory.ROOT)); + assertThat(file.getParent().getKey(), is("/")); + assertThat(file.getParent().getDeprecatedKey(), is(Directory.ROOT)); } @Test - public void newFileByKey() { + public void newFileByDeprecatedKey() { File file = new File("toto.sql"); - assertThat(file.getKey(), is("toto.sql")); + assertThat(file.getDeprecatedKey(), is("toto.sql")); assertThat(file.getName(), is("toto.sql")); - assertThat(file.getLongName(), is("toto.sql")); - assertThat(file.getParent().getKey(), is(Directory.ROOT)); + assertThat(file.getParent().getDeprecatedKey(), is(Directory.ROOT)); assertThat(file.getScope(), is(Resource.SCOPE_ENTITY)); assertThat(file.getQualifier(), is(Resource.QUALIFIER_FILE)); file = new File("foo/bar/toto.sql"); - assertThat(file.getKey(), is("foo/bar/toto.sql")); - assertThat(file.getLongName(), is("foo/bar/toto.sql")); + assertThat(file.getDeprecatedKey(), is("foo/bar/toto.sql")); assertThat(file.getName(), is("toto.sql")); - assertThat(file.getParent().getKey(), is("foo/bar")); + assertThat(file.getParent().getDeprecatedKey(), is("foo/bar")); file = new File("/foo/bar/toto.sql "); - assertThat(file.getKey(), is("foo/bar/toto.sql")); - assertThat(file.getLongName(), is("foo/bar/toto.sql")); + assertThat(file.getDeprecatedKey(), is("foo/bar/toto.sql")); assertThat(file.getName(), is("toto.sql")); - assertThat(file.getParent().getKey(), is("foo/bar")); + assertThat(file.getParent().getDeprecatedKey(), is("foo/bar")); } @Test @@ -100,16 +96,17 @@ public class FileTest { @Test public void matchAntPatterns() { - assertThat(new File("one/two/foo.sql").matchFilePattern("one/two/*.java"), is(false)); - assertThat(new File("one/two/foo.sql").matchFilePattern("false"), is(false)); - assertThat(new File("one/two/foo.sql").matchFilePattern("two/one/**"), is(false)); - assertThat(new File("one/two/foo.sql").matchFilePattern("other*/**"), is(false)); + File file = File.create("src/one/two/foo.sql", "one/two/foo.sql", null, false); + assertThat(file.matchFilePattern("/src/one/two/*.java")).isFalse(); + assertThat(file.matchFilePattern("false")).isFalse(); + assertThat(file.matchFilePattern("two/one/**")).isFalse(); + assertThat(file.matchFilePattern("other*/**")).isFalse(); - assertThat(new File("one/two/foo.sql").matchFilePattern("one*/**/*.sql"), is(true)); - assertThat(new File("one/two/foo.sql").matchFilePattern("one/t?o/**/*"), is(true)); - assertThat(new File("one/two/foo.sql").matchFilePattern("**/*"), is(true)); - assertThat(new File("one/two/foo.sql").matchFilePattern("one/two/*"), is(true)); - assertThat(new File("one/two/foo.sql").matchFilePattern("/one/two/*"), is(true)); - assertThat(new File("one/two/foo.sql").matchFilePattern("one/**"), is(true)); + assertThat(file.matchFilePattern("/src/one*/**/*.sql")).isTrue(); + assertThat(file.matchFilePattern("/src/one/t?o/**/*")).isTrue(); + assertThat(file.matchFilePattern("**/*")).isTrue(); + assertThat(file.matchFilePattern("src/one/two/*")).isTrue(); + assertThat(file.matchFilePattern("/src/one/two/*")).isTrue(); + assertThat(file.matchFilePattern("src/**")).isTrue(); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaFileTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaFileTest.java index 53b9061bc4e..a826c207c0a 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaFileTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaFileTest.java @@ -27,6 +27,7 @@ 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.assertFalse; @@ -41,42 +42,65 @@ public class JavaFileTest { @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")); + 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.getKey(), is("org.foo.bar.Hello")); + assertThat(javaClass.getDeprecatedKey(), is("org.foo.bar.Hello")); assertThat(javaClass.getName(), is("Hello")); assertThat(javaClass.getLongName(), is("org.foo.bar.Hello")); - assertThat(javaClass.getParent().getKey(), is("org.foo.bar")); + assertThat(javaClass.getParent().getDeprecatedKey(), is("org.foo.bar")); } @Test public void testNewClassWithExplicitPackage() { JavaFile javaClass = new JavaFile("org.foo.bar", "Hello", false); - assertThat(javaClass.getKey(), is("org.foo.bar.Hello")); + assertThat(javaClass.getDeprecatedKey(), is("org.foo.bar.Hello")); assertThat(javaClass.getName(), is("Hello")); assertThat(javaClass.getLongName(), is("org.foo.bar.Hello")); - assertThat(javaClass.getParent().getKey(), is("org.foo.bar")); + 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.getKey(), is("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")); + assertThat(javaClass.getLongName(), is("Hello")); + assertThat(javaClass.getParent().getKey()).isEqualTo("/src/main/java"); + assertThat(javaClass.getParent().getDeprecatedKey()).isEqualTo(JavaPackage.DEFAULT_PACKAGE_NAME); + assertThat(javaClass.getParent().isDefault()).isTrue(); + } + + @Test + public void testNewClassWithEmptyPackageDeprecatedConstructor() { JavaFile javaClass = new JavaFile("", "Hello", false); - assertThat(javaClass.getKey(), is(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello")); + assertThat(javaClass.getDeprecatedKey(), is(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello")); assertThat(javaClass.getName(), is("Hello")); assertThat(javaClass.getLongName(), is("Hello")); - assertThat((javaClass.getParent()).isDefault(), is(true)); + assertThat(javaClass.getParent().isDefault(), is(true)); } @Test - public void testNewClassWithNullPackage() { + public void testNewClassWithNullPackageDeprecatedConstructor() { JavaFile javaClass = new JavaFile(null, "Hello", false); - assertThat(javaClass.getKey(), is(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello")); + assertThat(javaClass.getDeprecatedKey(), is(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello")); assertThat(javaClass.getName(), is("Hello")); assertThat(javaClass.getLongName(), is("Hello")); assertThat((javaClass.getParent()).isDefault(), is(true)); @@ -85,7 +109,7 @@ public class JavaFileTest { @Test public void shouldBeDefaultPackageIfNoPackage() { JavaFile javaClass = new JavaFile("Hello", false); - assertEquals(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello", javaClass.getKey()); + assertEquals(JavaPackage.DEFAULT_PACKAGE_NAME + ".Hello", javaClass.getDeprecatedKey()); assertThat(javaClass.getName(), is("Hello")); assertThat(javaClass.getLongName(), is("Hello")); assertThat(javaClass.getParent().isDefault(), is(true)); @@ -94,21 +118,21 @@ public class JavaFileTest { @Test public void aClassShouldBeNamedJava() { JavaFile javaClass = new JavaFile("org.foo.bar.Java", false); - assertThat(javaClass.getKey(), is("org.foo.bar.Java")); + assertThat(javaClass.getDeprecatedKey(), is("org.foo.bar.Java")); assertThat(javaClass.getLongName(), is("org.foo.bar.Java")); assertThat(javaClass.getName(), is("Java")); JavaPackage parent = javaClass.getParent(); - assertEquals("org.foo.bar", parent.getKey()); + assertEquals("org.foo.bar", parent.getDeprecatedKey()); } @Test public void shouldTrimClasses() { JavaFile clazz = new JavaFile(" org.foo.bar.Hello ", false); - assertThat(clazz.getKey(), is("org.foo.bar.Hello")); + assertThat(clazz.getDeprecatedKey(), is("org.foo.bar.Hello")); assertThat(clazz.getLongName(), is("org.foo.bar.Hello")); assertThat(clazz.getName(), is("Hello")); JavaPackage parent = clazz.getParent(); - assertThat(parent.getKey(), is("org.foo.bar")); + assertThat(parent.getDeprecatedKey(), is("org.foo.bar")); } @Test @@ -126,21 +150,21 @@ public class JavaFileTest { @Test public void oneLevelPackage() { JavaFile clazz = new JavaFile("onelevel.MyFile"); - assertEquals("onelevel.MyFile", clazz.getKey()); - assertEquals("onelevel", clazz.getParent().getKey()); + assertEquals("onelevel.MyFile", clazz.getDeprecatedKey()); + assertEquals("onelevel", clazz.getParent().getDeprecatedKey()); clazz = new JavaFile("onelevel", "MyFile"); - assertEquals("onelevel.MyFile", clazz.getKey()); - assertEquals("onelevel", clazz.getParent().getKey()); + 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.getKey()); + assertEquals("onelevel.MyFile", javaFile.getDeprecatedKey()); assertEquals("MyFile", javaFile.getName()); - assertEquals("onelevel", javaFile.getParent().getKey()); + assertEquals("onelevel", javaFile.getParent().getDeprecatedKey()); assertEquals("onelevel", javaFile.getParent().getName()); - assertThat((javaFile.getParent()).isDefault(), is(false)); + assertThat(javaFile.getParent().isDefault(), is(false)); } @Test @@ -149,10 +173,10 @@ public class JavaFileTest { 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.getKey())); + assertThat("foo.bar.MyFile", is(javaFile.getDeprecatedKey())); assertThat(javaFile.getLongName(), is("foo.bar.MyFile")); assertThat(javaFile.getName(), is("MyFile")); - assertThat(javaFile.getParent().getKey(), is("foo.bar")); + assertThat(javaFile.getParent().getDeprecatedKey(), is("foo.bar")); } @Test @@ -162,7 +186,7 @@ public class JavaFileTest { List<File> sources = Arrays.asList(source1, source2); JavaFile javaClass = JavaFile.fromAbsolutePath(absPath(source1, "MyClass.java"), sources, false); - assertEquals(JavaPackage.DEFAULT_PACKAGE_NAME + ".MyClass", javaClass.getKey()); + assertEquals(JavaPackage.DEFAULT_PACKAGE_NAME + ".MyClass", javaClass.getDeprecatedKey()); assertEquals("MyClass", javaClass.getName()); assertThat((javaClass.getParent()).isDefault(), is(true)); @@ -184,24 +208,24 @@ public class JavaFileTest { @Test public void shouldMatchFilePatterns() { - JavaFile clazz = new JavaFile("org.sonar.commons.Foo"); + JavaFile clazz = JavaFile.create("src/main/java/org/sonar/commons/Foo.java", "org/sonar/commons/Foo.java", false); assertTrue(clazz.matchFilePattern("**/commons/**/*.java")); assertTrue(clazz.matchFilePattern("/**/commons/**/*.java")); assertTrue(clazz.matchFilePattern("/**/commons/**/*.*")); assertFalse(clazz.matchFilePattern("/**/sonar/*.java")); - assertTrue(clazz.matchFilePattern("/org/*/commons/**/*.java")); - assertTrue(clazz.matchFilePattern("org/sonar/commons/*")); - assertTrue(clazz.matchFilePattern("org/sonar/**/*.java")); - assertFalse(clazz.matchFilePattern("org/sonar/*")); - assertFalse(clazz.matchFilePattern("org/sonar*/*")); - assertTrue(clazz.matchFilePattern("org/**")); - assertTrue(clazz.matchFilePattern("*org/sona?/co??ons/**.*")); - assertFalse(clazz.matchFilePattern("org/sonar/core/**")); - assertTrue(clazz.matchFilePattern("org/sonar/commons/Foo")); - assertTrue(clazz.matchFilePattern("**/*Foo")); + assertTrue(clazz.matchFilePattern("/src/main/java/org/*/commons/**/*.java")); + assertTrue(clazz.matchFilePattern("src/main/java/org/sonar/commons/*")); + assertTrue(clazz.matchFilePattern("src/main/java/org/sonar/**/*.java")); + assertFalse(clazz.matchFilePattern("src/main/java/org/sonar/*")); + assertFalse(clazz.matchFilePattern("src/main/java/org/sonar*/*")); + assertTrue(clazz.matchFilePattern("src/main/java/org/**")); + assertTrue(clazz.matchFilePattern("*src/main/java/org/sona?/co??ons/**.*")); + assertFalse(clazz.matchFilePattern("src/main/java/org/sonar/core/**")); + assertTrue(clazz.matchFilePattern("src/main/java/org/sonar/commons/Foo.java")); + assertTrue(clazz.matchFilePattern("**/*Foo.java")); assertTrue(clazz.matchFilePattern("**/*Foo.*")); - assertTrue(clazz.matchFilePattern("org/*/*/Foo")); - assertTrue(clazz.matchFilePattern("org/**/**/Foo")); + assertTrue(clazz.matchFilePattern("src/main/java/org/*/*/Foo.java")); + assertTrue(clazz.matchFilePattern("src/main/java/org/**/**/Foo.java")); assertTrue(clazz.matchFilePattern("**/commons/**/*")); assertTrue(clazz.matchFilePattern("**/*")); } @@ -209,11 +233,11 @@ public class JavaFileTest { // SONAR-4397 @Test public void shouldMatchFilePatternsWhenNoPackage() { - JavaFile clazz = new JavaFile("[default].Foo.java"); - assertTrue(clazz.matchFilePattern("**/*Foo")); + JavaFile clazz = JavaFile.create("src/main/java/Foo.java", "Foo.java", false); + assertTrue(clazz.matchFilePattern("**/*Foo.java")); assertTrue(clazz.matchFilePattern("**/*Foo.*")); assertTrue(clazz.matchFilePattern("**/*")); - assertTrue(clazz.matchFilePattern("Foo*.*")); + assertTrue(clazz.matchFilePattern("src/main/java/Foo*.*")); } /** @@ -221,14 +245,14 @@ public class JavaFileTest { */ @Test public void doNotMatchAPattern() { - JavaFile file = new JavaFile("org.sonar.commons.Foo"); + JavaFile file = JavaFile.create("src/main/java/org/sonar/commons/Foo.java", "org/sonar/commons/Foo.java", false); assertFalse(file.matchFilePattern("**/*.aj")); assertTrue(file.matchFilePattern("**/*.java")); } @Test public void should_exclude_test_files() { - JavaFile unitTest = new JavaFile("org.sonar.commons.FooTest", true); + JavaFile unitTest = JavaFile.create("src/main/java/org/sonar/commons/Foo.java", "org/sonar/commons/Foo.java", true); assertTrue(unitTest.matchFilePattern("**/*")); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaPackageTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaPackageTest.java index 1a3a694df31..f2934b14478 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaPackageTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/JavaPackageTest.java @@ -19,25 +19,37 @@ */ package org.sonar.api.resources; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.*; 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 defaultPackage() { assertEquals(new JavaPackage(), new JavaPackage()); - assertEquals(JavaPackage.DEFAULT_PACKAGE_NAME, new JavaPackage(null).getKey()); + assertEquals(JavaPackage.DEFAULT_PACKAGE_NAME, new JavaPackage(null).getDeprecatedKey()); assertEquals(JavaPackage.DEFAULT_PACKAGE_NAME, new JavaPackage(null).getName()); - assertEquals(JavaPackage.DEFAULT_PACKAGE_NAME, new JavaPackage("").getKey()); + assertEquals(JavaPackage.DEFAULT_PACKAGE_NAME, new JavaPackage("").getDeprecatedKey()); assertThat(new JavaPackage(null).isDefault(), is(true)); } @Test public void testNewPackage() { + assertEquals(JavaPackage.create("src/foo/bar", " foo.bar "), JavaPackage.create("src/foo/bar", "foo.bar")); + JavaPackage pac = JavaPackage.create("src/foo/bar", "foo.bar"); + assertEquals("/src/foo/bar", pac.getKey()); + assertEquals("foo.bar", pac.getDeprecatedKey()); + assertEquals("foo.bar", pac.getName()); + } + + @Test + public void testNewPackageDeprecatedConstructor() { assertEquals(new JavaPackage(" foo.bar "), new JavaPackage("foo.bar")); JavaPackage pac = new JavaPackage("foo.bar"); - assertEquals("foo.bar", pac.getKey()); + assertEquals("foo.bar", pac.getDeprecatedKey()); assertEquals("foo.bar", pac.getName()); } @@ -45,7 +57,7 @@ public class JavaPackageTest { public void singleLevelPackage() { assertEquals(new JavaPackage("foo"), new JavaPackage("foo")); JavaPackage pac = new JavaPackage("foo"); - assertEquals("foo", pac.getKey()); + assertEquals("foo", pac.getDeprecatedKey()); assertEquals("foo", pac.getName()); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/test/IsResource.java b/sonar-plugin-api/src/test/java/org/sonar/api/test/IsResource.java index a51e9ff45bc..97c4c048ad8 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/test/IsResource.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/test/IsResource.java @@ -43,7 +43,7 @@ public class IsResource extends ArgumentMatcher<Resource> { @Override public boolean matches(Object o) { Resource r = (Resource) o; - boolean keyMatch = (key != null) ? StringUtils.equals(r.getKey(), key) : true; + boolean keyMatch = (key != null) ? StringUtils.equals(r.getKey() != null ? r.getKey() : r.getDeprecatedKey(), key) : true; return ObjectUtils.equals(r.getScope(), scope) && ObjectUtils.equals(r.getQualifier(), qualifier) && keyMatch; } } |