[Title] Fix potential bug as sonar
authorBon-Yong Lee <bonyong.lee@samsung.com>
Tue, 18 Dec 2012 08:32:59 +0000 (17:32 +0900)
committerBon-Yong Lee <bonyong.lee@samsung.com>
Tue, 18 Dec 2012 13:37:28 +0000 (22:37 +0900)
[Desc.]
[Issue]

16 files changed:
org.tizen.common.sign/src/org/tizen/common/sign/command/ReadSigningProfileFileCommand.java [changed mode: 0644->0755]
org.tizen.common.sign/src/org/tizen/common/sign/preferences/SigningPreferencePage.java
org.tizen.common.verrari.realm/src/org/tizen/common/verrari/template/StandardTemplate.java
org.tizen.common.verrari/src/org/tizen/common/verrari/engine/freemarker/FreemarkerTemplateEngine.java
org.tizen.common.verrari/src/org/tizen/common/verrari/engine/jsoup/JsoupTemplateEngine.java
org.tizen.common.verrari/test/src/org/tizen/common/verrari/template/FileTemplateTest.java
org.tizen.common/src/org/tizen/common/config/Preference.java
org.tizen.common/src/org/tizen/common/core/command/sdb/PullSdbCommand.java [changed mode: 0644->0755]
org.tizen.common/src/org/tizen/common/core/command/zip/ZipCommand.java
org.tizen.common/src/org/tizen/common/rds/RdsDeltaDetector.java [changed mode: 0644->0755]
org.tizen.common/src/org/tizen/common/util/HostUtil.java
org.tizen.common/src/org/tizen/common/util/ParsingUtil.java [changed mode: 0644->0755]
org.tizen.common/src/org/tizen/common/util/PropertyUtil.java
org.tizen.common/src/org/tizen/common/util/log/Logger.java [changed mode: 0644->0755]
org.tizen.common/test/src/org/tizen/common/util/ArrayUtilTest.java
org.tizen.common/test/src/org/tizen/common/util/CollectionUtilTest.java [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index d14bed6..5a4c7f3
@@ -169,7 +169,7 @@ extends AbstractCommand<Object>
             
             // save profiles
             boolean saveAuthor = getBoolean( authorButton.getValue() );
-            boolean saveDistributor = hasDistributor ? saveDistributor = getBoolean( distributor2Button.getValue() ) : false;
+            boolean saveDistributor = hasDistributor ? getBoolean( distributor2Button.getValue() ) : false;
             
             if ( saveAuthor ) {
                 authorItem.setPassword( (char[]) authorText.getValue() );
index 6be3550..bac7369 100755 (executable)
@@ -812,17 +812,24 @@ public class SigningPreferencePage extends PreferencePage implements IWorkbenchP
         setValid(true);
         setErrorMessage(null);
         for (SigningProfile profile : profileList) {
-            if (profile == null) continue;
+            if (profile == null)
+            {
+                continue;
+            }
 
             List<SigningProfileItem> profileItemList = profile.getProfileItemList();
             for (SigningProfileItem item : profileItemList) {
-                if (item == null) continue;
+                if (item == null)
+                {
+                    continue;
+                }
 
                 checkFile(item.getKeyLocation());
             }
             
             SigningProfileItem authorItem = profile.getAuthorItem();
-            if (authorItem != null) {
+            if (authorItem != null)
+            {
                 checkFile(authorItem.getKeyLocation());
             }
         } 
@@ -835,7 +842,9 @@ public class SigningPreferencePage extends PreferencePage implements IWorkbenchP
     public FontData[] boldFont(Font font) {
         FontData[] datas = font.getFontData();
         for (FontData data : datas)
+        {
             data.setStyle(SWT.BOLD);
+        }
         return datas;
     }
 
index e49a8fc..d816cb0 100755 (executable)
@@ -483,8 +483,9 @@ implements Template
                     mappingMap.put( args[0], args[1] );\r
                 }\r
             }\r
-            catch (IOException e)\r
+            catch ( final IOException e )\r
             {\r
+                logger.error( "Unreachable block" );\r
             }\r
         }\r
         mapper.setMappings( mappingMap );\r
index a4349d2..65b5374 100755 (executable)
@@ -31,6 +31,7 @@ package org.tizen.common.verrari.engine.freemarker;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 
+import org.tizen.common.util.Assert;
 import org.tizen.common.util.FreeMarkerUtil;
 import org.tizen.common.verrari.IModelProvider;
 import org.tizen.common.verrari.ITemplateProvider;
@@ -109,10 +110,8 @@ public class FreemarkerTemplateEngine extends AbstractTemplateEngine {
     throws Exception
     {
         final Template freemarkerTemplate = cfg.getTemplate( templateKey );
-        if ( null == freemarkerTemplate )
-        {
-            logger.error( "Template is null for {}", templateKey );
-        }
+        Assert.notNull( freemarkerTemplate, "Template is null for " + templateKey );
+        
         final IModelProvider adapter = new FreemarkerModelManager( modelProvider );
         freemarkerTemplate.process( adapter, new OutputStreamWriter( output ) );
     }
index cd4d89a..059c9d1 100755 (executable)
@@ -36,6 +36,7 @@ import org.jsoup.nodes.Attributes;
 import org.jsoup.nodes.Document;
 import org.jsoup.nodes.Element;
 import org.jsoup.select.Elements;
+import org.tizen.common.util.Assert;
 import org.tizen.common.verrari.IModelProvider;
 import org.tizen.common.verrari.ITemplate;
 import org.tizen.common.verrari.engine.AbstractTemplateEngine;
@@ -84,10 +85,7 @@ extends AbstractTemplateEngine
         final ITemplate template = getTemplate( templateKey );
         logger.debug( "Template :{}", template );
         
-        if ( null == template )
-        {
-            logger.error( "Template is null for {}", templateKey );
-        }
+        Assert.notNull( template, "Template is null for " + templateKey );
 
         String encoding = template.getEncoding();
         if (encoding == null) {
index 9be0cb7..0261147 100755 (executable)
 package org.tizen.common.verrari.template;
 
 import static org.junit.Assert.assertEquals;
+import static org.tizen.common.util.IOUtil.tryClose;
 
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.tizen.common.config.Preference;
-import org.tizen.common.config.provider.EnvironmentProvider;
 import org.tizen.common.config.provider.SystemPropertiesProvider;
 
 /**
@@ -92,11 +93,13 @@ public class FileTemplateTest {
         String templateContents;
         String fileConents;
 
+        InputStream in = null;
         BufferedReader bufferedReader = null;
         InputStreamReader streamReader = null;
         // get FileTemplate contents.
         try {
-            streamReader = new InputStreamReader(fileTemplate.open(), fileTemplate.getEncoding());
+            in = fileTemplate.open(); 
+            streamReader = new InputStreamReader( in, fileTemplate.getEncoding());
             bufferedReader = new BufferedReader(streamReader, 8192);
             StringBuilder text = new StringBuilder();
             final char[] buffer = new char[8192];
@@ -105,16 +108,14 @@ public class FileTemplateTest {
                 text.append( buffer, 0, nRead );
             }
             templateContents = text.toString();
-        } catch (Exception e) {
-            throw e;
         } finally {
-            if (streamReader != null) { streamReader.close(); }
-            if (bufferedReader != null) { bufferedReader.close(); }
+            tryClose( bufferedReader, streamReader, in );
         }
 
         // get File contents.
         try {
-            streamReader = new InputStreamReader(new FileInputStream(file), System.getProperty("file.encoding"));
+            in = new FileInputStream( file );
+            streamReader = new InputStreamReader( in, System.getProperty("file.encoding"));
             bufferedReader = new BufferedReader(streamReader, 8192);
             StringBuilder text = new StringBuilder();
             final char[] buffer = new char[8192];
@@ -123,11 +124,8 @@ public class FileTemplateTest {
                 text.append( buffer, 0, nRead );
             }
             fileConents = text.toString();
-        } catch (Exception e) {
-            throw e;
         } finally {
-            if (streamReader != null) { streamReader.close(); }
-            if (bufferedReader != null) { bufferedReader.close(); }
+            tryClose( bufferedReader, streamReader, in );
         }
 
         assertEquals(templateContents, fileConents);
index 8139b47..dfd6873 100755 (executable)
@@ -78,8 +78,14 @@ Preference
     {\r
         final char[] chars = name.toCharArray();\r
         for ( int i=0, n=chars.length ; i<n ; ++i ) {\r
-            if ( Character.isJavaIdentifierPart( chars[i] ) ) continue;\r
-            if ( '.' == chars[i] ) continue;\r
+            if ( Character.isJavaIdentifierPart( chars[i] ) )\r
+            {\r
+                continue;\r
+            }\r
+            if ( '.' == chars[i] )\r
+            {\r
+                continue;\r
+            }\r
             return false;\r
         }\r
         return true;\r
old mode 100644 (file)
new mode 100755 (executable)
index c9ee84e..9f9c65d
@@ -65,17 +65,12 @@ extends SdbDevicesHandlingCommand
         IDevice device = getDevice();
         Assert.notNull( device );
 
-        try
-        {
-            SyncService service = getSyncService();
-            SyncResult syncSuccess = 
-                    service.pullFile( remotePath, localPath, getProgressMonitor() );
-            if ( syncSuccess.getCode() != SyncService.RESULT_OK ) {
-                // TODO Message fix need
-                context.getPrompter().notify( "Error: Failed to Transfer." );
-            }
-        } catch (Exception e) {
-            throw new IllegalStateException( e );
+        SyncService service = getSyncService();
+        SyncResult syncSuccess = 
+        service.pullFile( remotePath, localPath, getProgressMonitor() );
+        if ( syncSuccess.getCode() != SyncService.RESULT_OK ) {
+            // TODO Message fix need
+            context.getPrompter().notify( "Error: Failed to Transfer." );
         }
     }
 
index 3f9b452..754abce 100755 (executable)
@@ -123,6 +123,7 @@ extends FileHandlingCommand<Object>
                        
                        if ( FilePolicy.OVERWRITE.equals( filePolicy ) )
                        {
+                           logger.debug( "No operation" );
                                // No op
                        }
                        else
old mode 100644 (file)
new mode 100755 (executable)
index 57a49e2..7546d14
@@ -232,7 +232,7 @@ public class RdsDeltaDetector {
             output = new DataOutputStream(new SafeFileOutputStream(strLocalFile, strLocalFile + ".temp"));
             
             treeWriter.writeTree(tree, subTreePath, ElementTreeWriter.D_INFINITE, output);
-        } catch (Exception e) {
+        } catch ( final IOException e ) {
             return "";
         } finally {
             IOUtil.tryClose(output);
index 79a8c6c..44658ad 100755 (executable)
@@ -33,9 +33,11 @@ import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.FileWriter;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.nio.channels.FileChannel;
 
@@ -61,7 +63,8 @@ public abstract class HostUtil{
             input = new FileReader(filePath);
             redirect( input, contents );
             return trim( contents.toString() );
-        }catch(Exception e) {
+        } catch( final IOException e) {
+            logger.warn( "Exception occurred: ", e );
             return null;
         }finally{
             tryClose( input, contents );
@@ -93,9 +96,13 @@ public abstract class HostUtil{
         try {
             proc= runtime.exec(fullCommand);
             i = proc.waitFor();
-        } catch (Exception e) {
-            e.printStackTrace();
+        } catch (IOException e) {
+            logger.warn( "Exception occurred: ", e );
             return false;
+        }
+        catch (InterruptedException e)
+        {
+            logger.warn( "Exception occurred: ", e );
         } finally{
             logger.info( "HostUtil execute - exit value : {}", i );
             if(proc!=null)    
@@ -125,8 +132,12 @@ public abstract class HostUtil{
             StreamGobbler gb2 = new StreamGobbler(p.getErrorStream());
             gb1.start();
             gb2.start();
-        }catch(Exception e){
-            e.printStackTrace();
+        }catch(IOException e){
+            logger.warn( "Exception occurred: ", e );
+            return false;
+        } catch (InterruptedException e)
+        {
+            logger.warn( "Exception occurred: ", e );
             return false;
         } 
         return i == 0;
@@ -155,8 +166,8 @@ public abstract class HostUtil{
             gb2 = new StreamGobbler(p.getErrorStream());
             gb1.start();
             gb2.start();
-        }catch(Exception e){
-            e.printStackTrace();
+        }catch(IOException e){
+            logger.warn( "Exception occurred: ", e );
             return false;
         }finally{
             logger.debug( "Output Stream :{}", gb1.getResult() );
@@ -195,8 +206,8 @@ public abstract class HostUtil{
                 contents.append(line);
                 contents.append(LINE_SEPARATOR);
             }
-        } catch (Exception e) {
-            e.printStackTrace();
+        } catch (IOException e) {
+            logger.warn( "Exception occurred: ", e );
             return null;
         } finally{
             tryClose( input );
@@ -214,8 +225,12 @@ public abstract class HostUtil{
      * Show output messages while executes the command on console view
      * @param command - Command to execute.
      * @param viewName - View name showing messages while executes the command.
+     * 
+     * @throws IOException 
+     * @throws InterruptedException 
      */
-    public static void executeWithConsole(String command, String viewName) throws Exception {
+    public static void executeWithConsole(String command, String viewName) throws IOException, InterruptedException
+    {
         BufferedReader input = null;
 
         String line = null;
@@ -240,7 +255,7 @@ public abstract class HostUtil{
             // abnormal termination
             if (proc.exitValue() != 0)
             {
-                throw new Exception("Failed to execute command: " + command);
+                throw new IllegalStateException("Failed to execute command: " + command);
             }
         } finally {
             tryClose( input );
@@ -297,8 +312,8 @@ public abstract class HostUtil{
             output = new BufferedWriter(new FileWriter(file));
             output.write(contents);
             return true;
-        } catch (Exception e) {
-            e.printStackTrace();
+        } catch (IOException e) {
+            logger.warn( "Exception occurred: ", e );
             return false;
         }finally{
             tryClose( output );
@@ -307,10 +322,12 @@ public abstract class HostUtil{
     
     
     /**
-    * @deprecated Use {@link FileUtil#copyTo(String, String)} instead.
+     * @deprecated Use {@link FileUtil#copyTo(String, String)} instead.
+     * 
+     * @throws IOException 
     */
     @Deprecated
-    public static boolean copyFile(String src, String des) throws Exception  {
+    public static boolean copyFile(String src, String des) throws IOException  {
         boolean canExecute = false;
         File srcFile=new File(src);
         File desFile=null;
old mode 100644 (file)
new mode 100755 (executable)
index 6d91b94..ac67961
@@ -107,7 +107,7 @@ public class ParsingUtil
                try
                {
                        return Integer.decode( trimmed );
-               } catch ( final Throwable e )
+               } catch ( final NumberFormatException e )
                {
                        return defaultValue;
                }
@@ -139,7 +139,7 @@ public class ParsingUtil
                try
                {
                        return Long.decode( trimmed );
-               } catch ( final Throwable e )
+               } catch ( final NumberFormatException e )
                {
                        return defaultValue;
                }
@@ -173,7 +173,7 @@ public class ParsingUtil
                {
                        return Double.valueOf( trimmed );
                }
-               catch( final Throwable e )
+               catch( final NumberFormatException e )
                {
                        return defaultValue;
                }
index 4c38d33..e10bf19 100755 (executable)
@@ -31,11 +31,16 @@ import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.FileReader;
 import java.io.FileWriter;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.Properties;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 public abstract class PropertyUtil {
+    protected static final Logger logger = LoggerFactory.getLogger( PropertyUtil.class );
        public static Properties loadProperties(String fileName) {
                Properties props = new Properties();
                BufferedReader reader = null;
@@ -53,8 +58,10 @@ public abstract class PropertyUtil {
                                props.setProperty(key, value);
                        }
                        //props.load(reader);
-               } catch (Exception e) {
-                       e.printStackTrace();
+               }
+               catch ( final IOException e )
+               {
+                   logger.info( "Exception Occurred:", e );
                } finally {
                        tryClose( reader );
                }
@@ -65,7 +72,7 @@ public abstract class PropertyUtil {
                Properties props = new Properties();
                try {
                        props.load(inputStream);
-               } catch (Exception e) {
+               } catch (IOException e) {
                } finally {
                        tryClose( inputStream );
                }
@@ -75,7 +82,7 @@ public abstract class PropertyUtil {
        public static boolean storeProperties(OutputStream outputStream, Properties props) {
                try {
                        props.store(outputStream, null);
-               } catch (Exception e) {
+               } catch (IOException e) {
                        return false;
                } finally {
                        tryClose( outputStream );
@@ -86,10 +93,13 @@ public abstract class PropertyUtil {
        public static boolean storeProperties(String fileName, Properties props) {
                BufferedWriter writer = null;
                
-               try {
+               try
+               {
                        writer = new BufferedWriter(new FileWriter(fileName));
                        props.store(writer, null);
-               } catch (Exception e) {
+               }
+               catch ( IOException e )
+               {
                        return false;
                } finally {
                        tryClose( writer );
old mode 100644 (file)
new mode 100755 (executable)
index 59d1721..83f368e
@@ -53,7 +53,7 @@ public class Logger {
 
     private static String getCallerName() {
         // Get the stack trace.
-        StackTraceElement stack[] = (new Throwable()).getStackTrace();
+        StackTraceElement stack[] = (new IllegalArgumentException()).getStackTrace();
         int ix = 0;
         // Now search for the first frame before the "Logger" class.
         while (ix < stack.length) {
index 863a1b7..f19e942 100755 (executable)
@@ -40,6 +40,7 @@ import java.util.ConcurrentModificationException;
 import java.util.Iterator;
 
 import org.junit.Test;
+import org.slf4j.LoggerFactory;
 
 
 /**
@@ -169,6 +170,7 @@ public class ArrayUtilTest
         } catch ( ConcurrentModificationException e )
         {
             // Test Success
+            LoggerFactory.getLogger( getClass() ).info( "Test case successfult" );
         }
     }
 
@@ -549,10 +551,10 @@ public class ArrayUtilTest
                 true
         };
 
-        Object[] actualData = null;
 
         for ( Object[] input : inputArray ) {
             for ( Object arg : inputAddArg ) {
+                Object[] actualData = null;
                 try {
                     actualData = ArrayUtil.add(input, arg);
                 } catch (ArrayStoreException e) {
@@ -594,7 +596,6 @@ public class ArrayUtilTest
         assertArrayEquals( new String[] { "c" }, ArrayUtil.remove( new String[] { "a", "b", "c" }, -1, 2 ) );
 
         final Object[][] inputArray = new Object[][]{
-                null,
                 new String[]{"hello", "test", "java", "c", "c++", "junit", "time"},
                 new Integer[]{10, 20, 30, 40, 50, 60, 70, 80, 90},
                 {10, 20, 30, 40, 50, 60, 70, 80, 90, 100},
@@ -606,14 +607,23 @@ public class ArrayUtilTest
                 new Object[]{null, null, null},
                 new Object[]{ }
         };
-        Object[] actualData = null;
-
+        
+        try
+        {
+            ArrayUtil.remove( null, 0, 1 );
+            fail();
+        }
+        catch( final IllegalArgumentException e )
+        {
+            LoggerFactory.getLogger( getClass() ).info( "Test case successfult" );
+        }
+        
         for ( Object[] input : inputArray ) {
             for ( int i = -1 ; i < 10 ; i++ ) {
-                for( int j = -1 ; j < 10 ; j++ ) {
+                for( int j = i ; j < 10 ; j++ ) {
                     int deleteCount = 0;
                     try {
-                        actualData = ArrayUtil.remove( input, i, j);
+                        Object[] actualData = ArrayUtil.remove( input, i, j);
                         i = Math.max( 0, i );
                         if( j <= i) {
                             assertArrayEquals(input , actualData);
@@ -651,14 +661,6 @@ public class ArrayUtilTest
                             fail("Failed remove(Object[], int, int) test");
                         }
                     }
-                    catch ( NullPointerException e ){
-                        if ( input == null) {
-                            continue;
-                        }
-                        else {
-                            fail("Failed remove(Object[], int, int) test");
-                        }
-                    }
                     catch (NegativeArraySizeException e) {
                         if ( input.length < j - i ) {
                             continue;
old mode 100644 (file)
new mode 100755 (executable)
index b98b36b..fc38008
@@ -53,8 +53,8 @@ import java.util.TreeSet;
 import java.util.Vector;
 import java.util.concurrent.atomic.AtomicInteger;
 
-import org.junit.Before;
 import org.junit.Test;
+import org.slf4j.LoggerFactory;
 
 
 /**
@@ -402,7 +402,8 @@ CollectionUtilTest
         try {
             CollectionUtil.mergeArrayIntoCollection( TEST_CASES[0], null );
             fail( "mergeArrayIntoCollection must throw exception" );
-        } catch (NullPointerException e) {
+        } catch ( IllegalArgumentException e) {
+            LoggerFactory.getLogger( getClass() ).info( "Test case successful" );
         }
 
         for ( final Object[] TEST_CASE : TEST_CASES )
@@ -808,6 +809,8 @@ CollectionUtilTest
                     assertEquals( "Tizen"+StringUtil.EMPTY_STRING+"Test",
                             CollectionUtil.concatenate( iter, StringUtil.EMPTY_STRING ) );
                     break;
+                default:
+                    break;
             }
 
         }
@@ -936,18 +939,21 @@ CollectionUtilTest
             CollectionUtil.swap( objectArray, 0, 5 );
             fail( "swap must throw exception" );
         } catch (Exception e) {
+            LoggerFactory.getLogger( getClass() ).info( "Test case successful" );
         }
 
         try {
             CollectionUtil.swap( objectArray, -1, 0 );
             fail( "swap must throw exception" );
         } catch (Exception e) {
+            LoggerFactory.getLogger( getClass() ).info( "Test case successful" );
         }
 
         try {
             CollectionUtil.swap( null, 0, 1 );
             fail( "swap must throw exception" );
         } catch (Exception e) {
+            LoggerFactory.getLogger( getClass() ).info( "Test case successful" );
         }
     }