From: Simon Brandhof Date: Mon, 2 Apr 2012 20:32:30 +0000 (+0200) Subject: Fix some quality flaws X-Git-Tag: 3.0~57 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=787529a7ec4a567a31b375f76954ea31714a7c86;p=sonarqube.git Fix some quality flaws --- diff --git a/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaSensor.java b/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaSensor.java index 2c6ed02016e..6c629f15afd 100644 --- a/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaSensor.java +++ b/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaSensor.java @@ -46,7 +46,7 @@ public class CoberturaSensor implements Sensor, CoverageExtension { } File report = project.getFileSystem().resolvePath(path); if (!report.exists() || !report.isFile()) { - Logs.INFO.warn("Cobertura report not found at {}", report); + LoggerFactory.getLogger(getClass()).warn("Cobertura report not found at {}", report); return; } parseReport(report, context); @@ -54,12 +54,7 @@ public class CoberturaSensor implements Sensor, CoverageExtension { protected void parseReport(File xmlFile, final SensorContext context) { LoggerFactory.getLogger(CoberturaSensor.class).info("parsing {}", xmlFile); - new AbstractCoberturaParser() { - @Override - protected Resource getResource(String fileName) { - return new JavaFile(fileName); - } - }.parseReport(xmlFile, context); + new JavaCoberturaParser().parseReport(xmlFile, context); } @Override @@ -67,4 +62,10 @@ public class CoberturaSensor implements Sensor, CoverageExtension { return getClass().getSimpleName(); } + private static final class JavaCoberturaParser extends AbstractCoberturaParser { + @Override + protected Resource getResource(String fileName) { + return new JavaFile(fileName); + } + } } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewNotifications.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewNotifications.java index 3c79baff3f9..3fb852f651f 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewNotifications.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewNotifications.java @@ -65,7 +65,7 @@ public class ReviewNotifications implements BatchExtension { .setFieldValue("assignee", getAssignee(review)); } - private @Nullable String getCreator(ReviewDto review) { + private String getCreator(ReviewDto review) { if (review.getUserId() == null) { // no creator and in fact this should never happen in real-life, however happens during unit tests return null; } @@ -73,7 +73,7 @@ public class ReviewNotifications implements BatchExtension { return user != null ? user.getLogin() : null; } - private @Nullable String getAssignee(ReviewDto review) { + private String getAssignee(ReviewDto review) { if (review.getAssigneeId() == null) { // not assigned return null; } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewWorkflowDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewWorkflowDecorator.java index 9fc97e67ba8..3e27ee2f61b 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewWorkflowDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewWorkflowDecorator.java @@ -107,12 +107,10 @@ public class ReviewWorkflowDecorator implements Decorator { for (ReviewDto review : openReviews) { Violation violation = violationsByPermanentId.get(review.getViolationPermanentId()); - if (violation != null) { - if (!hasUpToDateInformation(review, violation)) { - review.setLine(violation.getLineId()); - review.setTitle(violation.getMessage()); - updated.add(review); - } + if (violation != null && !hasUpToDateInformation(review, violation)) { + review.setLine(violation.getLineId()); + review.setTitle(violation.getMessage()); + updated.add(review); } } } @@ -132,11 +130,7 @@ public class ReviewWorkflowDecorator implements Decorator { } private void closeResolvedStandardViolations(Collection openReviews, List violations, Project project, Resource resource, Set updated) { - Set violationIds = Sets.newHashSet(Collections2.transform(violations, new Function() { - public Integer apply(Violation violation) { - return violation.getPermanentId(); - } - })); + Set violationIds = Sets.newHashSet(Collections2.transform(violations, new ViolationToPermanentIdFunction())); for (ReviewDto openReview : openReviews) { if (!openReview.isManualViolation() && !violationIds.contains(openReview.getViolationPermanentId())) { @@ -168,4 +162,10 @@ public class ReviewWorkflowDecorator implements Decorator { review.setResolution(null); review.setUpdatedAt(new Date()); } + + private static final class ViolationToPermanentIdFunction implements Function { + public Integer apply(Violation violation) { + return violation.getPermanentId(); + } + } } diff --git a/sonar-core/src/main/java/org/sonar/core/plugins/PluginInstaller.java b/sonar-core/src/main/java/org/sonar/core/plugins/PluginInstaller.java index 94c2c6f4bad..5566a0e0ae0 100644 --- a/sonar-core/src/main/java/org/sonar/core/plugins/PluginInstaller.java +++ b/sonar-core/src/main/java/org/sonar/core/plugins/PluginInstaller.java @@ -48,37 +48,9 @@ public class PluginInstaller { public DefaultPluginMetadata install(DefaultPluginMetadata metadata, File toDir) { try { File pluginFile = metadata.getFile(); - File pluginBasedir; - if (toDir != null) { - pluginBasedir = toDir; - FileUtils.forceMkdir(pluginBasedir); - File targetFile = new File(pluginBasedir, pluginFile.getName()); - FileUtils.copyFile(pluginFile, targetFile); - metadata.addDeployedFile(targetFile); - } else { - pluginBasedir = pluginFile.getParentFile(); - metadata.addDeployedFile(pluginFile); - } - - if (metadata.getPathsToInternalDeps().length > 0) { - // needs to unzip the jar - ZipUtils.unzip(pluginFile, pluginBasedir, new LibFilter()); - for (String depPath : metadata.getPathsToInternalDeps()) { - File dependency = new File(pluginBasedir, depPath); - if (!dependency.isFile() || !dependency.exists()) { - throw new IllegalArgumentException("Dependency " + depPath + " can not be found in " + pluginFile.getName()); - } - metadata.addDeployedFile(dependency); - } - } - - for (File extension : metadata.getDeprecatedExtensions()) { - File toFile = new File(pluginBasedir, extension.getName()); - if (!toFile.equals(extension)) { - FileUtils.copyFile(extension, toFile); - } - metadata.addDeployedFile(toFile); - } + File pluginBasedir = copyPlugin(metadata, toDir, pluginFile); + copyDependencies(metadata, pluginFile, pluginBasedir); + copyDeprecatedExtensions(metadata, pluginBasedir); return metadata; @@ -87,7 +59,46 @@ public class PluginInstaller { } } - private static class LibFilter implements ZipUtils.ZipEntryFilter { + private File copyPlugin(DefaultPluginMetadata metadata, File toDir, File pluginFile) throws IOException { + File pluginBasedir; + if (toDir != null) { + pluginBasedir = toDir; + FileUtils.forceMkdir(pluginBasedir); + File targetFile = new File(pluginBasedir, pluginFile.getName()); + FileUtils.copyFile(pluginFile, targetFile); + metadata.addDeployedFile(targetFile); + } else { + pluginBasedir = pluginFile.getParentFile(); + metadata.addDeployedFile(pluginFile); + } + return pluginBasedir; + } + + private void copyDependencies(DefaultPluginMetadata metadata, File pluginFile, File pluginBasedir) throws IOException { + if (metadata.getPathsToInternalDeps().length > 0) { + // needs to unzip the jar + ZipUtils.unzip(pluginFile, pluginBasedir, new LibFilter()); + for (String depPath : metadata.getPathsToInternalDeps()) { + File dependency = new File(pluginBasedir, depPath); + if (!dependency.isFile() || !dependency.exists()) { + throw new IllegalArgumentException("Dependency " + depPath + " can not be found in " + pluginFile.getName()); + } + metadata.addDeployedFile(dependency); + } + } + } + + private void copyDeprecatedExtensions(DefaultPluginMetadata metadata, File pluginBasedir) throws IOException { + for (File extension : metadata.getDeprecatedExtensions()) { + File toFile = new File(pluginBasedir, extension.getName()); + if (!toFile.equals(extension)) { + FileUtils.copyFile(extension, toFile); + } + metadata.addDeployedFile(toFile); + } + } + + private static final class LibFilter implements ZipUtils.ZipEntryFilter { public boolean accept(ZipEntry entry) { return entry.getName().startsWith("META-INF/lib"); } @@ -120,7 +131,7 @@ public class PluginInstaller { } } - private void completeDeprecatedMetadata(DefaultPluginMetadata metadata) throws IOException { + private void completeDeprecatedMetadata(DefaultPluginMetadata metadata) { String mainClass = metadata.getMainClass(); File pluginFile = metadata.getFile(); try { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java index 658d328422c..e1007096245 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java @@ -38,12 +38,12 @@ public final class PropertyDefinition { private String errorKey = null; - private static Result newError(@Nullable String key) { + private static Result newError(String key) { return new Result(key); } @Nullable - private Result(String errorKey) { + private Result(@Nullable String errorKey) { this.errorKey = errorKey; } diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java b/sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java index 58e2d5ad0ad..9b971a24dc9 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java +++ b/sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java @@ -98,29 +98,28 @@ public final class ClassLoaderUtils { rootPath = StringUtils.removeStart(rootPath, "/"); URL root = classLoader.getResource(rootPath); - if (root == null) { - return paths; - } - if (!"jar".equals(root.getProtocol())) { - throw new IllegalStateException("Unsupported protocol: " + root.getProtocol()); - } + if (root != null) { + if (!"jar".equals(root.getProtocol())) { + throw new IllegalStateException("Unsupported protocol: " + root.getProtocol()); + } - // Path of the root directory - // Examples : - // org/sonar/sqale/index.txt -> rootDirectory is org/sonar/sqale - // org/sonar/sqale/ -> rootDirectory is org/sonar/sqale - // org/sonar/sqale -> rootDirectory is org/sonar/sqale - String rootDirectory = rootPath; - if (StringUtils.substringAfterLast(rootPath, "/").indexOf('.') >= 0) { - rootDirectory = StringUtils.substringBeforeLast(rootPath, "/"); - } - String jarPath = root.getPath().substring(5, root.getPath().indexOf("!")); //strip out only the JAR file - JarFile jar = new JarFile(URLDecoder.decode(jarPath, CharEncoding.UTF_8)); - Enumeration entries = jar.entries(); - while (entries.hasMoreElements()) { - String name = entries.nextElement().getName(); - if (name.startsWith(rootDirectory) && predicate.apply(name)) { - paths.add(name); + // Path of the root directory + // Examples : + // org/sonar/sqale/index.txt -> rootDirectory is org/sonar/sqale + // org/sonar/sqale/ -> rootDirectory is org/sonar/sqale + // org/sonar/sqale -> rootDirectory is org/sonar/sqale + String rootDirectory = rootPath; + if (StringUtils.substringAfterLast(rootPath, "/").indexOf('.') >= 0) { + rootDirectory = StringUtils.substringBeforeLast(rootPath, "/"); + } + String jarPath = root.getPath().substring(5, root.getPath().indexOf("!")); //strip out only the JAR file + JarFile jar = new JarFile(URLDecoder.decode(jarPath, CharEncoding.UTF_8)); + Enumeration entries = jar.entries(); + while (entries.hasMoreElements()) { + String name = entries.nextElement().getName(); + if (name.startsWith(rootDirectory) && predicate.apply(name)) { + paths.add(name); + } } } return paths;