changeset 11:f010a4361c96

2012-02-21 Pavel Tisnovsky <ptisnovs@redhat.com> * src/FileUtils.java: * src/PrintTestCoverage.java: * src/ReportGenerator.java: Fixed (usally minor) issues found by FindBugs.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Tue, 21 Feb 2012 17:16:21 +0100
parents 4a18f35c8dff
children 5030a59cd546
files ChangeLog src/FileUtils.java src/PrintTestCoverage.java src/ReportGenerator.java
diffstat 4 files changed, 66 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Feb 03 17:39:45 2012 +0100
+++ b/ChangeLog	Tue Feb 21 17:16:21 2012 +0100
@@ -1,3 +1,10 @@
+2012-02-21  Pavel Tisnovsky  <ptisnovs@redhat.com>
+
+	* src/FileUtils.java:
+	* src/PrintTestCoverage.java:
+	* src/ReportGenerator.java:
+	Fixed (usally minor) issues found by FindBugs.
+
 2012-02-03  Pavel Tisnovsky  <ptisnovs@redhat.com>
 
 	* src/ReportGenerator.java:
--- a/src/FileUtils.java	Fri Feb 03 17:39:45 2012 +0100
+++ b/src/FileUtils.java	Tue Feb 21 17:16:21 2012 +0100
@@ -42,9 +42,10 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.FileWriter;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -56,6 +57,11 @@
 class FileUtils
 {
     /**
+     * Default encoding used for all input/output operations.
+     */
+    private static final String DEFAULT_ENCODING = "UTF-8";
+
+    /**
      * EOF constant used as special return value in some methods.
      */
     private static final int EOF = -1;
@@ -75,7 +81,7 @@
         try
         {
             // try to open file for reading
-            reader = new BufferedReader(new FileReader(fileName));
+            reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), DEFAULT_ENCODING));
             readAllLinesFromTextFile(reader, out);
         }
         catch (FileNotFoundException e)
@@ -112,7 +118,7 @@
         BufferedWriter fout = null;
         try
         {
-            fout = new BufferedWriter(new FileWriter(fileName));
+            fout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), DEFAULT_ENCODING));
             // write all lines to a text file
             writeAllLinesToTextFile(fout, lines);
         }
--- a/src/PrintTestCoverage.java	Fri Feb 03 17:39:45 2012 +0100
+++ b/src/PrintTestCoverage.java	Tue Feb 21 17:16:21 2012 +0100
@@ -40,8 +40,8 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
@@ -623,7 +623,7 @@
     @Override
     public String toString(ConstantPoolRecord[] poolEntries)
     {
-        return String.format("%2d   %-26s %Ld", getTag().getValue(), "Long", this.longConstant);
+        return String.format("%2d   %-26s %d", getTag().getValue(), "Long", this.longConstant);
     }
 }
 
@@ -732,15 +732,25 @@
 public class PrintTestCoverage
 {
     /**
+     * Default encoding used for all input/output operations.
+     */
+    private static final String DEFAULT_ENCODING = "UTF-8";
+
+    /**
      * Default name of file containing directory to tests.
      */
     private static final String TEST_DIRECTORY = "test_directory.txt";
 
+    /**
+     * Whether to print constant pool to standard output.
+     */
+    private static final boolean PRINT_CONSTANT_POOL = false;
+
     @SuppressWarnings("boxing")
     private static void processMagicNumber(FileInputStream fin) throws Exception
     {
         int magic = FileUtils.readFourBytes(fin);
-        System.err.format("Magic constant:     0x%x\n", magic);
+        System.err.format("Magic constant:     0x%x%n", magic);
         if (magic != 0xcafebabe)
         {
             throw new Exception("Improper magic constant");
@@ -752,8 +762,8 @@
     {
         int minorVersion = FileUtils.readTwoBytes(fin);
         int majorVersion = FileUtils.readTwoBytes(fin);
-        System.err.format("Major version:      %d\n", majorVersion);
-        System.err.format("Minor version:      %d\n", minorVersion);
+        System.err.format("Major version:      %d%n", majorVersion);
+        System.err.format("Minor version:      %d%n", minorVersion);
     }
 
     private static ConstantPoolRecord readConstantPoolEntry(FileInputStream fin) throws IOException
@@ -836,11 +846,11 @@
     @SuppressWarnings("boxing")
     private static ConstantPoolRecord[] processContantPool(FileInputStream fin) throws IOException
     {
-        ConstantPoolRecord[] poolEntries = null;
+        ConstantPoolRecord[] poolEntries;
 
         int constantPoolCount = FileUtils.readTwoBytes(fin) - 1;
 
-        System.err.format("\nConstant pool size: %d\n", constantPoolCount);
+        System.err.format("%nConstant pool size: %d%n", constantPoolCount);
         poolEntries = new ConstantPoolRecord[constantPoolCount];
 
         int i = 0;
@@ -914,7 +924,6 @@
         return interfaceNames;
     }
 
-    @SuppressWarnings("unused")
     private static void printConstantPool(ConstantPoolRecord[] poolEntries)
     {
         for (ConstantPoolRecord record : poolEntries)
@@ -1014,7 +1023,10 @@
         {
             determineAllCalledAPIMethods(testedClassName, poolEntries, methodSet);
         }
-        //printConstantPool(poolEntries);
+        if (PRINT_CONSTANT_POOL)
+        {
+            printConstantPool(poolEntries);
+        }
         System.err.println();
     }
 
@@ -1125,9 +1137,25 @@
     private static String readPathToTest() throws IOException
     {
         // read path where test classes are stored
-        BufferedReader fin = new BufferedReader(new FileReader(TEST_DIRECTORY));
-        String directory = fin.readLine();
-        return directory;
+        BufferedReader fin = null;
+        try
+        {
+            fin = new BufferedReader(new InputStreamReader(new FileInputStream(TEST_DIRECTORY), DEFAULT_ENCODING));
+            String directory = fin.readLine();
+            return directory;
+        }
+        catch (FileNotFoundException e)
+        {
+            return null;
+        }
+        finally
+        {
+            // close input stream if it's possible
+            if (fin != null)
+            {
+                fin.close();
+            }
+        }
     }
 
     /**
--- a/src/ReportGenerator.java	Fri Feb 03 17:39:45 2012 +0100
+++ b/src/ReportGenerator.java	Tue Feb 21 17:16:21 2012 +0100
@@ -38,9 +38,10 @@
 
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
@@ -57,6 +58,11 @@
 public class ReportGenerator
 {
     /**
+     * Default charset used during all input/output operations.
+     */
+    private static final String DEFAULT_CHARSET = "UTF-8";
+
+    /**
      * Location of internal or external URI to standard API JavaDoc
      */
     private static final String DOC_BASE = "http://docs.oracle.com/javase/6/docs/api";
@@ -310,7 +316,7 @@
             out.add("<h2>Package " + packageName + "</h2>");
             out.add("<table class='classes_list'>");
             out.add("<tr><th>class</th><th>methods</th><th>covered</th><th>ratio</th><th>doc</th></tr>");
-            http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html
+            // http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html
             for (String className : testedClasses)
             {
                 if (className.startsWith(packageName))
@@ -515,7 +521,7 @@
         Set<String> allMethods = new TreeSet<String>();
         try
         {
-            reader = new BufferedReader(new FileReader(fileName));
+            reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), DEFAULT_CHARSET));
             String line;
             while ((line = reader.readLine()) != null)
             {