]> source.dussan.org Git - poi.git/commitdiff
rewrite CopyCompare to remove its ThreadLocal which causes a sonar code smell
authorPJ Fanning <fanningpj@apache.org>
Thu, 30 Dec 2021 20:02:03 +0000 (20:02 +0000)
committerPJ Fanning <fanningpj@apache.org>
Thu, 30 Dec 2021 20:02:03 +0000 (20:02 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896536 13f79535-47bb-0310-9956-ffa450edef68

poi-examples/src/main/java/org/apache/poi/examples/hpsf/CopyCompare.java
poi-integration/src/test/java/org/apache/poi/stress/HPSFFileHandler.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/ThemesTable.java

index d933b23e22a5c9bdd163eb224e366e71687fbb8b..8a9e0e2f0ef375747607d15f555c50003a5418cd 100644 (file)
@@ -62,9 +62,8 @@ import org.apache.poi.util.TempFile;
  */
 @SuppressWarnings({"java:S106","java:S4823"})
 public final class CopyCompare {
-    private CopyCompare() {}
 
-    private static final ThreadLocal<PrintStream> out = ThreadLocal.withInitial(() -> System.out);
+    private PrintStream out = System.out;
 
     /**
      * Runs the example program. The application expects one or two arguments:
@@ -83,6 +82,10 @@ public final class CopyCompare {
      *                                      supported.
      */
     public static void main(final String[] args) throws IOException {
+        new CopyCompare().run(args);
+    }
+
+    public void run(String[] args) throws IOException {
         String originalFileName = null;
         String copyFileName = null;
 
@@ -120,12 +123,12 @@ public final class CopyCompare {
              POIFSFileSystem cpfs = new POIFSFileSystem(new File(copyFileName))) {
             final DirectoryEntry oRoot = opfs.getRoot();
             final DirectoryEntry cRoot = cpfs.getRoot();
-            out.get().println(EntryUtils.areDirectoriesIdentical(oRoot, cRoot) ? "Equal" : "Not equal");
+            out.println(EntryUtils.areDirectoriesIdentical(oRoot, cRoot) ? "Equal" : "Not equal");
         }
     }
 
-    public static void setOut(PrintStream ps) {
-        out.set(ps);
+    public void setOut(PrintStream ps) {
+        out = ps;
     }
 
     private interface InputStreamSupplier {
index 4b8d0016e2471d55ad8787ddb0807d62b80530e8..9809a7a11f8799fa8721f042fdf3198e86f088cd 100644 (file)
@@ -33,7 +33,6 @@ import java.util.Set;
 
 import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
 import org.apache.poi.examples.hpsf.CopyCompare;
-import org.apache.poi.extractor.POITextExtractor;
 import org.apache.poi.hpsf.DocumentSummaryInformation;
 import org.apache.poi.hpsf.HPSFPropertiesOnlyDocument;
 import org.apache.poi.hpsf.PropertySet;
@@ -129,11 +128,13 @@ public class HPSFFileHandler extends POIFSFileHandler {
     public void handleAdditional(File file) throws Exception {
         assumeFalse(EXCLUDES_HANDLE_ADD.contains(file.getParentFile().getName()+"/"+file.getName()));
 
-        UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream();
-        PrintStream psNew = new PrintStream(bos, true, "ISO-8859-1");
-        CopyCompare.setOut(psNew);
-        CopyCompare.main(new String[]{file.getAbsolutePath(), copyOutput.get().getAbsolutePath()});
-        assertEquals("Equal" + NL, bos.toString(StandardCharsets.UTF_8));
+        try (UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
+            PrintStream psNew = new PrintStream(bos, true, "ISO-8859-1");
+            CopyCompare copyCompare = new CopyCompare();
+            copyCompare.setOut(psNew);
+            CopyCompare.main(new String[]{file.getAbsolutePath(), copyOutput.get().getAbsolutePath()});
+            assertEquals("Equal" + NL, bos.toString(StandardCharsets.UTF_8));
+        }
     }
 
 
index 72fe4e0107e3d6458471fa5a553580b84ac2b33c..7bd391a06b779d945588cdbf80775ab3127ebca5 100644 (file)
@@ -56,7 +56,8 @@ public class ThemesTable extends POIXMLDocumentPart implements Themes {
            return values()[idx];
        }
        ThemeElement(int idx, String name) {
-           this.idx = idx; this.name = name;
+           this.idx = idx;
+           this.name = name;
        }
        public final int idx;
        public final String name;