Fixing Sonar issues in BuildHelper 89/49489/6
authorGautam Shah <gautams@amdocs.com>
Wed, 30 May 2018 13:02:14 +0000 (18:32 +0530)
committerOren Kleks <orenkle@amdocs.com>
Thu, 31 May 2018 11:12:20 +0000 (11:12 +0000)
Sonar issue fixes.

Change-Id: Icc08fcc50bc683c51831ebf3ae6a9ca282e9cc22
Issue-ID: SDC-1189
Signed-off-by: GAUTAMS <gautams@amdocs.com>
openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/BuildHelper.java
openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/BuildState.java
openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/Constants.java
openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/InitializationHelperMojo.java
openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/PostCompileHelperMojo.java
openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/PreCompileHelperMojo.java

index 42f3166..681e5f7 100644 (file)
@@ -19,15 +19,14 @@ package org.openecomp.sdc.onboarding;
 import static org.openecomp.sdc.onboarding.Constants.JAVA_EXT;
 import static org.openecomp.sdc.onboarding.Constants.UNICORN;
 
-import java.io.BufferedInputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.ObjectInputStream;
 import java.io.UncheckedIOException;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -35,6 +34,7 @@ import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.Arrays;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -63,7 +63,7 @@ class BuildHelper {
         md.update(data.getBytes());
         byte[] hashBytes = md.digest();
 
-        StringBuffer buffer = new StringBuffer();
+        StringBuilder buffer = new StringBuilder();
         for (byte hashByte : hashBytes) {
             buffer.append(Integer.toString((hashByte & 0xff) + 0x100, 16).substring(1));
         }
@@ -71,8 +71,8 @@ class BuildHelper {
     }
 
 
-    private static Map<String, String> readSources(File file, String fileType) throws IOException {
-        Map<String, String> source = new HashMap<>();
+    private static Map<String, List<String>> readSources(File file, String fileType) throws IOException {
+        Map<String, List<String>> source = new HashMap<>();
         if (file.exists()) {
             List<File> list = Files.walk(Paths.get(file.getAbsolutePath()))
                                    .filter(JAVA_EXT.equals(fileType) ? BuildHelper::isRegularJavaFile :
@@ -89,42 +89,35 @@ class BuildHelper {
         return file.isFile() && file.getName().endsWith(JAVA_EXT);
     }
 
-    private static String getData(File file, byte[] buffer) {
-        try (FileInputStream fis = new FileInputStream(file);
-             BufferedInputStream bis = new BufferedInputStream(fis, 64 * 1024)) {
-            bis.read(buffer, 0, ((int) file.length()));
-            if (file.getAbsolutePath().contains(File.separator + "generated-sources" + File.separator)) {
-                StringBuffer sb = new StringBuffer();
-                List<String> coll = Files.readAllLines(file.toPath());
-                for (String s : coll) {
-                    if (s != null && !s.trim().startsWith("/") && !s.trim().startsWith("*")) {
-                        sb.append(s);
-                    }
-                }
-                return sb.toString();
-            }
-        } catch (IOException ioe) {
-            throw new UncheckedIOException(ioe);
-        }
-        return new String(buffer, 0, ((int) file.length()));
-    }
-
-
-    private static class FileReadTask extends RecursiveTask<Map<String, String>> {
+    private static class FileReadTask extends RecursiveTask<Map<String, List<String>>> {
 
-        Map<String, String> store = new HashMap<>();
-        private byte[] buffer = new byte[1024 * 1024];
+        private Map<String, List<String>> store = new HashMap<>();
         File[] files;
         String pathPrefix;
-        private final int MAX_FILES = 10;
+        private static final int MAX_FILES = 10;
 
         FileReadTask(File[] files, String pathPrefix) {
             this.files = files;
             this.pathPrefix = pathPrefix;
         }
 
+        private static List<String> getData(File file) throws IOException {
+            List<String> coll = Files.readAllLines(file.toPath(), StandardCharsets.ISO_8859_1);
+            if (file.getAbsolutePath().contains(File.separator + "generated-sources" + File.separator)) {
+                Iterator<String> itr = coll.iterator();
+                while (itr.hasNext()) {
+                    String s = itr.next();
+                    if (s == null || s.trim().startsWith("/") || s.trim().startsWith("*")) {
+                        itr.remove();
+                    }
+                }
+            }
+            return coll;
+        }
+
+
         @Override
-        protected Map<String, String> compute() {
+        protected Map<String, List<String>> compute() {
             if (files.length > MAX_FILES) {
                 FileReadTask task1 = new FileReadTask(Arrays.copyOfRange(files, 0, files.length / 2), pathPrefix);
                 FileReadTask task2 =
@@ -135,7 +128,12 @@ class BuildHelper {
                 store.putAll(task2.join());
             } else {
                 for (File toRead : files) {
-                    store.put(toRead.getAbsolutePath().substring(pathPrefix.length()), getData(toRead, buffer));
+                    try {
+                        store.put(toRead.getAbsolutePath().substring(pathPrefix.length())
+                                        .replace(File.separatorChar, '.'), getData(toRead));
+                    } catch (IOException e) {
+                        throw new UncheckedIOException(e);
+                    }
                 }
             }
 
@@ -151,7 +149,7 @@ class BuildHelper {
             uri = new URI(repoPath + (project.getGroupId().replace('.', '/')) + '/' + project.getArtifactId() + '/'
                                   + project.getVersion());
         } catch (URISyntaxException e) {
-            throw new MojoFailureException(e.getMessage());
+            throw new MojoFailureException(e.getMessage(), e);
         }
         File f = new File(uri);
         File[] list = f.listFiles(t -> t.getName().equals(project.getArtifactId() + "-" + project.getVersion() + "."
index 17ff7c9..ead6e7b 100644 (file)
@@ -28,10 +28,8 @@ import static org.openecomp.sdc.onboarding.Constants.RESOURCE_BUILD_DATA;
 import static org.openecomp.sdc.onboarding.Constants.SKIP_MAIN_SOURCE_COMPILE;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.util.Collection;
 import java.util.HashMap;
@@ -39,7 +37,6 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
-import java.util.function.BiFunction;
 import java.util.function.Function;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.project.MavenProject;
index c639c6d..fb69cce 100644 (file)
@@ -1,26 +1,43 @@
+/*
+ * Copyright © 2018 European Support Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on a "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package org.openecomp.sdc.onboarding;
 
 public class Constants {
 
-    private Constants() {
-    }
 
+    public static final String UNICORN = "unicorn";
+    public static final String EMPTY_STRING = "";
+    public static final String PREFIX = System.getProperties().contains(UNICORN) ? EMPTY_STRING : UNICORN;
+    ;
     public static final String JACOCO_SKIP = "jacoco.skip";
     public static final String FORK_COUNT = "fork.count";
     public static final String FORK_MODE = "fork.mode";
     public static final String SKIP_PMD = "skipPMD";
     public static final String JAVA_EXT = ".java";
     public static final String ANY_EXT = "*";
-    public static final String SKIP_TEST_RUN = "skipTestRun";
+    public static final String SKIP_TEST_RUN = PREFIX + "skipTestRun";
     public static final String SKIP_TESTS = "skipTests";
     public static final String MAIN = "main";
     public static final String TEST = "test";
     public static final String RESOURCES_CHANGED = "resourcesChanged";
-    public static final String UNICORN = "unicorn";
     public static final String ANSI_YELLOW = "\u001B[43m";
     public static final String ANSI_COLOR_RESET = "\u001B[0m";
-    public static final String SKIP_MAIN_SOURCE_COMPILE = "skipMainSourceCompile";
-    public static final String SKIP_TEST_SOURCE_COMPILE = "skipTestSourceCompile";
+    public static final String SKIP_MAIN_SOURCE_COMPILE = PREFIX + "skipMainSourceCompile";
+    public static final String SKIP_TEST_SOURCE_COMPILE = PREFIX + "skipTestSourceCompile";
     public static final String MAIN_CHECKSUM = "mainChecksum";
     public static final String TEST_CHECKSUM = "testChecksum";
     public static final String RESOURCE_CHECKSUM = "resourceChecksum";
@@ -42,4 +59,10 @@ public class Constants {
     public static final String RESOURCE_WITH_TEST_ONLY = "resourceWithTestOnly";
     public static final String INSTRUMENT_ONLY = "instrumentOnly";
     public static final String TEST_ONLY = "testOnly";
+    public static final String SKIP_RESOURCE_COLLECTION = PREFIX + "skipResourceCollection";
+    public static final String SKIP_INSTALL = PREFIX + "skipInstall";
+
+
+    private Constants() {
+    }
 }
index 8b0ff0e..9aa48b2 100644 (file)
@@ -19,6 +19,7 @@ package org.openecomp.sdc.onboarding;
 import static org.openecomp.sdc.onboarding.Constants.FORK_COUNT;
 import static org.openecomp.sdc.onboarding.Constants.FORK_MODE;
 import static org.openecomp.sdc.onboarding.Constants.JACOCO_SKIP;
+import static org.openecomp.sdc.onboarding.Constants.PREFIX;
 import static org.openecomp.sdc.onboarding.Constants.SKIP_PMD;
 import static org.openecomp.sdc.onboarding.Constants.UNICORN;
 
@@ -42,12 +43,15 @@ public class InitializationHelperMojo extends AbstractMojo {
     @Parameter
     private String excludePackaging;
 
+    @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
-
+        if (PREFIX == UNICORN) {
+            System.getProperties().setProperty(UNICORN, Boolean.TRUE.toString());
+        }
         if (project.getPackaging().equals(excludePackaging)) {
             return;
         }
-        project.getProperties().setProperty("skipGet", "false");
+        project.getProperties().setProperty("skipGet", Boolean.FALSE.toString());
         if (System.getProperties().containsKey(JACOCO_SKIP) && Boolean.FALSE.equals(Boolean.valueOf(
                 System.getProperties().getProperty(JACOCO_SKIP)))) {
             project.getProperties().setProperty(FORK_COUNT, "1");
@@ -62,8 +66,8 @@ public class InitializationHelperMojo extends AbstractMojo {
         if (System.getProperties().containsKey(UNICORN)) {
             buildState.init();
         } else {
-            project.getProperties().setProperty("skipMainSourceCompile", "false");
-            project.getProperties().setProperty("skipTestSourceCompile", "false");
+            project.getProperties().setProperty("skipMainSourceCompile", Boolean.FALSE.toString());
+            project.getProperties().setProperty("skipTestSourceCompile", Boolean.FALSE.toString());
         }
 
     }
index 04e0ca8..621e793 100644 (file)
@@ -65,15 +65,8 @@ public class PostCompileHelperMojo extends AbstractMojo {
             project.getProperties().setProperty(SKIP_MAIN_SOURCE_COMPILE, Boolean.TRUE.toString());
             project.getProperties().remove(TEST_ONLY);
         }
-        if (project.getProperties().containsKey(INSTRUMENT_ONLY)) {
-            project.getProperties().setProperty(SKIP_MAIN_SOURCE_COMPILE, Boolean.TRUE.toString());
-            project.getProperties().setProperty(SKIP_TEST_SOURCE_COMPILE, Boolean.TRUE.toString());
-            project.getProperties().remove(INSTRUMENT_ONLY);
-        }
-        if (project.getProperties().containsKey(INSTRUMENT_WITH_TEST_ONLY)) {
-            project.getProperties().setProperty(SKIP_MAIN_SOURCE_COMPILE, Boolean.TRUE.toString());
-            project.getProperties().remove(INSTRUMENT_WITH_TEST_ONLY);
-        }
+        postProcessInstrumentedModules();
+
         if (project.getProperties().containsKey(RESOURCE_WITH_TEST_ONLY)) {
             project.getProperties().setProperty(SKIP_MAIN_SOURCE_COMPILE, Boolean.TRUE.toString());
             project.getProperties().remove(RESOURCE_WITH_TEST_ONLY);
@@ -97,4 +90,16 @@ public class PostCompileHelperMojo extends AbstractMojo {
         }
         buildState.saveModuleBuildData(moduleCoordinates);
     }
+
+    private void postProcessInstrumentedModules() {
+        if (project.getProperties().containsKey(INSTRUMENT_ONLY)) {
+            project.getProperties().setProperty(SKIP_MAIN_SOURCE_COMPILE, Boolean.TRUE.toString());
+            project.getProperties().setProperty(SKIP_TEST_SOURCE_COMPILE, Boolean.TRUE.toString());
+            project.getProperties().remove(INSTRUMENT_ONLY);
+        }
+        if (project.getProperties().containsKey(INSTRUMENT_WITH_TEST_ONLY)) {
+            project.getProperties().setProperty(SKIP_MAIN_SOURCE_COMPILE, Boolean.TRUE.toString());
+            project.getProperties().remove(INSTRUMENT_WITH_TEST_ONLY);
+        }
+    }
 }
index 854ef7f..15a45a7 100644 (file)
@@ -37,8 +37,10 @@ import static org.openecomp.sdc.onboarding.Constants.RESOURCE_CHECKSUM;
 import static org.openecomp.sdc.onboarding.Constants.RESOURCE_ONLY;
 import static org.openecomp.sdc.onboarding.Constants.RESOURCE_WITH_TEST_ONLY;
 import static org.openecomp.sdc.onboarding.Constants.SHA1;
+import static org.openecomp.sdc.onboarding.Constants.SKIP_INSTALL;
 import static org.openecomp.sdc.onboarding.Constants.SKIP_MAIN_SOURCE_COMPILE;
 import static org.openecomp.sdc.onboarding.Constants.SKIP_PMD;
+import static org.openecomp.sdc.onboarding.Constants.SKIP_RESOURCE_COLLECTION;
 import static org.openecomp.sdc.onboarding.Constants.SKIP_TEST_RUN;
 import static org.openecomp.sdc.onboarding.Constants.SKIP_TEST_SOURCE_COMPILE;
 import static org.openecomp.sdc.onboarding.Constants.TEST;
@@ -114,6 +116,10 @@ public class PreCompileHelperMojo extends AbstractMojo {
     private Map<String, Object> resourceBuildData;
 
     private static Map<String, String> checksumMap;
+    private long mainChecksum = 0;
+    private long testChecksum = 0;
+    private long resourceChecksum = 0;
+    Optional<String> artifactPath;
 
     static {
         checksumMap = readCurrentPMDState("pmd.dat");
@@ -121,29 +127,18 @@ public class PreCompileHelperMojo extends AbstractMojo {
 
     public void execute() throws MojoExecutionException, MojoFailureException {
 
-        long mainChecksum = 0, testChecksum = 0, resourceChecksum = 0;
-        Optional<String> artifactPath;
 
         if (project.getPackaging().equals(excludePackaging)) {
             return;
         }
         init();
-        project.getProperties()
-               .setProperty(MAIN_CHECKSUM, String.valueOf(mainChecksum = getChecksum(mainSourceLocation, JAVA_EXT)));
-        project.getProperties()
-               .setProperty(TEST_CHECKSUM, String.valueOf(testChecksum = getChecksum(testSourceLocation, JAVA_EXT)));
-        String checksum = mainChecksum + COLON + testChecksum;
-        if (!checksum.equals(checksumMap.get(moduleCoordinates)) || isPMDMandatory(project.getArtifacts())) {
-            project.getProperties().setProperty(SKIP_PMD, Boolean.FALSE.toString());
-            buildState.recordPMDRun(moduleCoordinates);
-        }
+        processPMDCheck();
         project.getProperties().setProperty(EMPTY_JAR, "");
         if (!System.getProperties().containsKey(UNICORN)) {
             return;
         }
-
-        project.getProperties().setProperty(RESOURCE_CHECKSUM,
-                String.valueOf(resourceChecksum = getChecksum(mainResourceLocation, ANY_EXT)));
+        resourceChecksum = getChecksum(mainResourceLocation, ANY_EXT);
+        project.getProperties().setProperty(RESOURCE_CHECKSUM, String.valueOf(resourceChecksum));
         byte[] sourceChecksum = calculateChecksum(mainChecksum, resourceChecksum).getBytes();
         boolean instrumented = isCurrentModuleInstrumented();
         artifactPath = getArtifactPathInLocalRepo(session.getLocalRepository().getUrl(), project, sourceChecksum);
@@ -180,14 +175,13 @@ public class PreCompileHelperMojo extends AbstractMojo {
         setInstallFlags(mainToBeCompiled, instrumented, project.getPackaging(),
                 !resourceMainBuildDataSameWithPreviousBuild);
 
-        if (!mainToBeCompiled && !instrumented && JAR.equals(project.getPackaging())
-                    && resourceMainBuildDataSameWithPreviousBuild) {
-            project.getProperties().setProperty("artifactPathToCopy", artifactPath.orElse(null));
-        }
+        setArtifactPath(mainToBeCompiled, instrumented, JAR.equals(project.getPackaging()),
+                resourceMainBuildDataSameWithPreviousBuild);
     }
 
     private void generateSignature(byte[] sourceChecksum) {
         try {
+            Paths.get(project.getBuild().getDirectory()).toFile().mkdirs();
             Files.write(Paths.get(project.getBuild().getDirectory(), project.getBuild().getFinalName() + DOT + UNICORN),
                     sourceChecksum, StandardOpenOption.CREATE);
         } catch (IOException e) {
@@ -234,10 +228,17 @@ public class PreCompileHelperMojo extends AbstractMojo {
         }
     }
 
+    private void setArtifactPath(boolean mainToBeCompiled, boolean instrumented, boolean isJar,
+            boolean resourceDataSame) {
+        if (!mainToBeCompiled && !instrumented && isJar && resourceDataSame) {
+            project.getProperties().setProperty("artifactPathToCopy", artifactPath.orElse(null));
+        }
+    }
+
     private void setResourceBuild(boolean resourceMainBuildDataSameWithPreviousBuild, boolean mainToBeCompiled,
             boolean testToBeCompiled) {
         if (resourceMainBuildDataSameWithPreviousBuild) {
-            project.getProperties().setProperty("skipResourceCollection", Boolean.TRUE.toString());
+            project.getProperties().setProperty(SKIP_RESOURCE_COLLECTION, Boolean.TRUE.toString());
         } else {
             project.getProperties().setProperty(RESOURCES_CHANGED, Boolean.TRUE.toString());
         }
@@ -292,12 +293,12 @@ public class PreCompileHelperMojo extends AbstractMojo {
 
     private void setInstallFlags(boolean compile, boolean instrumented, String packaging, boolean resourceChanged) {
         if (!compile && !instrumented && !resourceChanged && JAR.equals(packaging)) {
-            project.getProperties().setProperty("skipInstall", Boolean.TRUE.toString());
+            project.getProperties().setProperty(SKIP_INSTALL, Boolean.TRUE.toString());
         }
     }
 
     private boolean isCompileNeeded(Collection<String> dependencyCoordinates, boolean isFirstBuild,
-            boolean buildDataSame) throws MojoFailureException {
+            boolean buildDataSame) {
         return isFirstBuild || !buildDataSame || buildState.isCompileMust(moduleCoordinates, dependencyCoordinates);
     }
 
@@ -306,6 +307,7 @@ public class PreCompileHelperMojo extends AbstractMojo {
             return scanModuleFor(LifecyclePhase.PROCESS_CLASSES.id(), LifecyclePhase.PROCESS_TEST_CLASSES.id(),
                     LifecyclePhase.COMPILE.id(), LifecyclePhase.TEST_COMPILE.id());
         } catch (Exception e) {
+            getLog().debug(e);
             return true;
         }
     }
@@ -315,6 +317,7 @@ public class PreCompileHelperMojo extends AbstractMojo {
             return scanModuleFor(LifecyclePhase.GENERATE_RESOURCES.id(), LifecyclePhase.GENERATE_SOURCES.id(),
                     LifecyclePhase.GENERATE_TEST_RESOURCES.id(), LifecyclePhase.GENERATE_TEST_SOURCES.id());
         } catch (Exception e) {
+            getLog().debug(e);
             return true;
         }
     }
@@ -358,7 +361,7 @@ public class PreCompileHelperMojo extends AbstractMojo {
 
     private boolean isPMDMandatory(Set<Artifact> dependencies) {
         for (Artifact artifact : dependencies) {
-            if (buildState.isPMDRun(artifact.getGroupId() + COLON + artifact.getArtifactId())) {
+            if (BuildState.isPMDRun(artifact.getGroupId() + COLON + artifact.getArtifactId())) {
                 return true;
             }
         }
@@ -369,9 +372,10 @@ public class PreCompileHelperMojo extends AbstractMojo {
             throws InvalidPluginDescriptorException, PluginResolutionException, MojoNotFoundException,
                            PluginDescriptorParsingException {
         for (Plugin plugin : project.getBuildPlugins()) {
-            if (!(plugin.getGroupId().equals("org.apache.maven.plugins") && plugin.getArtifactId().startsWith("maven"))
+            if (!("org.apache.maven.plugins".equals(plugin.getGroupId()) && plugin.getArtifactId().startsWith("maven"))
                         && !plugin.getGroupId().startsWith("org.openecomp.sdc")) {
-                if (scanPlugin(plugin, types)) {
+                boolean success = scanPlugin(plugin, types);
+                if (success) {
                     return true;
                 }
             }
@@ -384,7 +388,8 @@ public class PreCompileHelperMojo extends AbstractMojo {
                            PluginResolutionException {
         for (PluginExecution pluginExecution : plugin.getExecutions()) {
             if (pluginExecution.getPhase() != null) {
-                if (Arrays.asList(types).contains(pluginExecution.getPhase())) {
+                boolean phaseAvailable = Arrays.asList(types).contains(pluginExecution.getPhase());
+                if (phaseAvailable) {
                     return true;
                 }
             }
@@ -429,4 +434,16 @@ public class PreCompileHelperMojo extends AbstractMojo {
             testResourceLocation = Paths.get(project.getBuild().getTestResources().get(0).getDirectory()).toFile();
         }
     }
+
+    private void processPMDCheck() {
+        mainChecksum = getChecksum(mainSourceLocation, JAVA_EXT);
+        testChecksum = getChecksum(testSourceLocation, JAVA_EXT);
+        project.getProperties().setProperty(MAIN_CHECKSUM, String.valueOf(mainChecksum));
+        project.getProperties().setProperty(TEST_CHECKSUM, String.valueOf(testChecksum));
+        String checksum = mainChecksum + COLON + testChecksum;
+        if (!checksum.equals(checksumMap.get(moduleCoordinates)) || isPMDMandatory(project.getArtifacts())) {
+            project.getProperties().setProperty(SKIP_PMD, Boolean.FALSE.toString());
+            BuildState.recordPMDRun(moduleCoordinates);
+        }
+    }
 }