From ec9caffae2316fe403b7e1eeac01632261c7a4e6 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Thu, 19 Feb 2015 08:30:16 +0000
Subject: [PATCH] (CR-6119) Code cleanup in copyright maven plugin

---
 opendj-copyright-maven-plugin/src/test/java/org/forgerock/maven/UpdateCopyrightTestCase.java |    2 
 opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/CopyrightAbstractMojo.java   |   36 +++++++-----------
 opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/UpdateCopyrightMojo.java     |   25 ++++++------
 3 files changed, 27 insertions(+), 36 deletions(-)

diff --git a/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/CopyrightAbstractMojo.java b/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/CopyrightAbstractMojo.java
index 3d77e58..d6e97ca 100644
--- a/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/CopyrightAbstractMojo.java
+++ b/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/CopyrightAbstractMojo.java
@@ -191,12 +191,10 @@
                 if (!CHECKED_EXTENSIONS.contains(extension.toLowerCase())) {
                     continue;
                 }
-            } else {
-                // We'll still want to check it if it's in a resource/bin directory.
-                if (fileNameEquals("bin", changedFile.getParentFile())
-                        && fileNameEquals("resource", changedFile.getParentFile().getParentFile())) {
-                    continue;
-                }
+            } else if (fileNameEquals("bin", changedFile.getParentFile())
+                    && fileNameEquals("resource", changedFile.getParentFile().getParentFile())) {
+                // ignore resource/bin directory.
+                continue;
             }
 
             if (!checkCopyrightForFile(changedFile)) {
@@ -218,28 +216,22 @@
         BufferedReader reader = null;
         try {
             reader = new BufferedReader(new FileReader(changedFile));
-            String line = reader.readLine();
-
-            while (line != null) {
+            String line;
+            while ((line = reader.readLine()) != null) {
                 String lowerLine = line.toLowerCase().trim();
-                if (isCommentLine(lowerLine)) {
-                    int copyrightPos = lowerLine.indexOf("copyright");
-                    if (copyrightPos > 0) {
-                        if (line.contains(currentYear.toString()) && line.contains(copyrightOwnerToken)) {
-                            reader.close();
-                            return true;
-                        }
-                    }
+                if (isCommentLine(lowerLine)
+                        && lowerLine.contains("copyright")
+                        && line.contains(currentYear.toString())
+                        && line.contains(copyrightOwnerToken)) {
+                    reader.close();
+                    return true;
                 }
-                line = reader.readLine();
             }
 
             return false;
         } catch (IOException ioe) {
-            StringBuilder errorMsg = new StringBuilder().append(" Could not read file ")
-                    .append(changedFile.getPath())
-                    .append(" to check copyright date. No further copyright date checking will be performed.");
-            throw new MojoExecutionException(errorMsg.toString());
+            throw new MojoExecutionException("Could not read file " + changedFile.getPath()
+                    + " to check copyright date. No further copyright date checking will be performed.");
         } finally {
             closeSilently(reader);
         }
diff --git a/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/UpdateCopyrightMojo.java b/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/UpdateCopyrightMojo.java
index b7e0b1d..4540498 100644
--- a/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/UpdateCopyrightMojo.java
+++ b/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/UpdateCopyrightMojo.java
@@ -173,27 +173,25 @@
                 previousLine = bufferedLines.get(indexAdd);
             }
             indexAdd++;
-            StringBuffer newCopyrightLine = new StringBuffer(getNewCommentedLine());
-            newCopyrightLine.append(indent())
-                            .append(portionsCopyrightNeeded ? portionsCopyrightStartToken : copyrightStartToken)
-                            .append(" ").append(currentYear).append(" ").append(copyrightEndToken);
             if (!portionsCopyrightNeeded) {
                 for (int i = 0; i < nbLinesToSkip; i++) {
                     bufferedLines.add(indexAdd++, getNewCommentedLine());
                 }
             }
-            bufferedLines.add(indexAdd, newCopyrightLine.toString());
+            final String newCopyrightLine = getNewCommentedLine()
+                    + indent() + (portionsCopyrightNeeded ? portionsCopyrightStartToken : copyrightStartToken)
+                    + " " + currentYear + " " + copyrightEndToken;
+            bufferedLines.add(indexAdd, newCopyrightLine);
             copyrightUpdated = true;
         }
 
         private void updateExistingCopyrightLine() throws Exception {
-            if (portionsCopyrightNeeded && copyrightSectionPresent) {
-                // Check if the line is a new copyright line
-                if (curLine.contains(copyrightStartToken) && !curLine.contains(portionsCopyrightStartToken)) {
-                    getLog().warn("File " + filePath + " contains old copyright line and coyright line. "
-                            + "The copyright line will be replaced by a portion copyright line.");
-                    curLine.replace(copyrightStartToken, portionsCopyrightStartToken);
-                }
+            if (portionsCopyrightNeeded && copyrightSectionPresent
+                    // Is it a new copyright line?
+                    && curLine.contains(copyrightStartToken) && !curLine.contains(portionsCopyrightStartToken)) {
+                getLog().warn("File " + filePath + " contains old copyright line and coyright line. "
+                        + "The copyright line will be replaced by a portions copyright line.");
+                curLine.replace(copyrightStartToken, portionsCopyrightStartToken);
             }
             readYearSection();
             final String newCopyrightLine;
@@ -360,6 +358,7 @@
      * @throws MojoExecutionException
      *             if any
      */
+    @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
         checkCopyrights();
         for (String filePath : getIncorrectCopyrightFilePaths()) {
@@ -380,7 +379,7 @@
     }
 
     private String intervalToString(Integer startYear, Integer endYear) {
-        return startYear.toString() + "-" + endYear;
+        return startYear + "-" + endYear;
     }
 
     private String indent() {
diff --git a/opendj-copyright-maven-plugin/src/test/java/org/forgerock/maven/UpdateCopyrightTestCase.java b/opendj-copyright-maven-plugin/src/test/java/org/forgerock/maven/UpdateCopyrightTestCase.java
index a085178..05a297b 100644
--- a/opendj-copyright-maven-plugin/src/test/java/org/forgerock/maven/UpdateCopyrightTestCase.java
+++ b/opendj-copyright-maven-plugin/src/test/java/org/forgerock/maven/UpdateCopyrightTestCase.java
@@ -48,7 +48,7 @@
     private static final String[] TEST_FOLDERS = new String[] {"openam-copyrights",
         "opendj-copyrights", "openidm-copyrights"};
 
-    /** Customs tags in tests files */
+    /** Customs tags in tests files. */
     private static final String MUST_BE_REMOVE_TAG = "MUST BE REMOVED:";
     private static final String EXPECTED_OUTPUT_TAG = "EXPECTED OUTPUT:";
     private static final String YEAR_TAG = "YEAR";

--
Gitblit v1.10.0