[Title] Refactoring multi package
authorho.namkoong <ho.namkoong@samsung.com>
Tue, 19 Mar 2013 06:10:26 +0000 (15:10 +0900)
committerho.namkoong <ho.namkoong@samsung.com>
Wed, 20 Mar 2013 07:50:20 +0000 (16:50 +0900)
[Type]
[Module]
[Priority]
[Jira#]
[Redmine#] 8635
[Problem]
[Cause]
[Solution]
[TestCase]

Change-Id: I35df91f9ae53711a761273d3d5b8e222bc90307e

org.tizen.common/src/org/tizen/common/util/FileUtil.java
org.tizen.common/src/org/tizen/common/util/StringUtil.java
org.tizen.common/test/src/org/tizen/common/util/FileUtilTest.java
org.tizen.common/test/src/org/tizen/common/util/StringUtilTest.java

index 9e49fd4..6289747 100755 (executable)
@@ -236,13 +236,7 @@ public class FileUtil {
      * @author Jihoon Song {@literal <jihoon80.song@samsung.com>} (S-Core)
      */
     public static String getFileExtension(String fullName) {
-        if (fullName == null) {
-            return null;
-        }
-
-        int k = fullName.lastIndexOf(".");
-
-        return (k != -1) ? fullName.substring(k + 1, fullName.length()) : null;
+        return StringUtil.getLastStringAfter(fullName, ".");
     }
 
     /** 
@@ -259,6 +253,26 @@ public class FileUtil {
 
         return (k != -1) ? fullName.substring(0, k) : fullName;
     }
+    
+    /**
+     * Returns file name from path
+     * 
+     * @param path
+     * @return
+     */
+    public static String getFileNameFromPath(String path) {
+        String result = StringUtil.getLastStringAfter(path, "/");
+        
+        if(result == null) {
+            result = StringUtil.getLastStringAfter(path, "\\");
+        }
+        
+        if(result == null) {
+            result = path;
+        }
+        
+        return result;
+    }
 
     /**
      * delete file and its children.
index bee6cd1..cc9f01a 100755 (executable)
@@ -1150,4 +1150,22 @@ public class StringUtil
         str = new String(str_array);
         return str;
     }
+    
+    /**
+     * Returns last string after  <code>lastIndexStr</code> from <code>input</code>.
+     * For example, if <code>input</code>  is "HOME/File", and <code>lastIndexStr</code> is "/" then, it returns File.
+     * 
+     * @param input input string
+     * @param lastIndexStr string indicates last string
+     * @return
+     */
+    public static String getLastStringAfter(String input, String lastIndexStr) {
+        if (input == null) {
+            return null;
+        }
+
+        int k = input.lastIndexOf(lastIndexStr);
+
+        return (k != -1) ? input.substring(k + 1, input.length()) : null;
+    }
 }
index 7d559a4..54b6312 100644 (file)
@@ -51,37 +51,56 @@ public class FileUtilTest {
     public static final String TEST_RESOURCE_DEST = "test/_test_files";
     public static final String TEST_RESOURCE_SRC = "test/test_files";
     
-       /**
-        * Test {@link FileUtil#getFileExtension(String)}
-        * 
-        * @throws Exception in case of failure in test
-        * 
-        * @see {@link FileUtil#getFileExtension(String)}
-        */
-       @Test
-       public void test_getFileExtension() throws Exception {
-               final Object[][] TEST_CASES = new Object[][] {
-                               new Object[] { "aaaa.exe", "exe" },
-                               new Object[] { "index.html", "html" },
-                               new Object[] { "index.test.html", "html" },
-                               new Object[] { "index..html", "html" },
-                               new Object[] { ".profile", "profile" },
-                               new Object[] { "ReadMe ", null },
-                               new Object[] { "ReadMe", null },
-                               new Object[] { "ReadMe. ", " " },
-                               new Object[] { "ReadMe. test", " test" }
-               };
-               
-               int i = 0;
-               for ( final Object[] TEST_CASE : TEST_CASES )
-               {
-                       final String fileName = (String) TEST_CASE[0];
-                       final String expected = (String) TEST_CASE[1];
-                       final String result = FileUtil.getFileExtension( fileName );
-                       
-                       assertEquals( ++i + "th Test case :<" + fileName + ", " + expected + ">", expected, result );
-               }
-       }
+    /**
+     * Test {@link FileUtil#getFileExtension(String)}
+     * 
+     * @throws Exception in case of failure in test
+     * 
+     * @see {@link FileUtil#getFileExtension(String)}
+     */
+    @Test
+    public void test_getFileExtension() throws Exception {
+        final Object[][] TEST_CASES = new Object[][] {
+                new Object[] { "aaaa.exe", "exe" },
+                new Object[] { "index.html", "html" },
+                new Object[] { "index.test.html", "html" },
+                new Object[] { "index..html", "html" },
+                new Object[] { ".profile", "profile" },
+                new Object[] { "ReadMe ", null },
+                new Object[] { "ReadMe", null },
+                new Object[] { "ReadMe. ", " " },
+                new Object[] { "ReadMe. test", " test" }
+        };
+        
+        int i = 0;
+        for ( final Object[] TEST_CASE : TEST_CASES )
+        {
+            final String fileName = (String) TEST_CASE[0];
+            final String expected = (String) TEST_CASE[1];
+            final String result = FileUtil.getFileExtension( fileName );
+            
+            assertEquals( ++i + "th Test case :<" + fileName + ", " + expected + ">", expected, result );
+        }
+    }
+    
+    /**
+     * Test case for {@link FileUtil#getFileNameFromPath(String)}
+     * 
+     * @throws Exception
+     */
+    public void test_getFileNameFromPath() throws Exception {
+        final String[][] testCases = {
+                {"ASDFwew/jkl/wer23/gfdgfdg", "gfdgfdg"},
+                {"wekljf\\hgrfg\\wefwv1.aaa", "wefwv1.aaa"},
+                {null, null},
+                {"ggg.sss", "ggg.sss"}
+        };
+        
+        for(String[] testCase: testCases) {
+            String result = FileUtil.getFileNameFromPath(testCase[0]);
+            assertEquals(testCase[1], result);
+        }
+    }
 
     /**
      * Test {@link FileUtil#readTextStream(InputStream input, String encoding)}
@@ -122,7 +141,7 @@ public class FileUtilTest {
     @Test
     public void test_findFiles() throws Exception{
         final Object [][] TEST_CASE = new Object[][] {
-          new Object[] {"",""},      
+          new Object[] {"",""},
         };
     }
     
index e95191c..b521635 100644 (file)
@@ -327,6 +327,27 @@ StringUtilTest
             );
         }
     }
+    
+    /**
+     * Test case for {@link StringUtil#getLastStringAfter(String, String)}
+     * 
+     * @throws Exception
+     */
+    public void test_getLastStringAfter() throws Exception {
+        String[][] testCases = {
+                {"ASDF|||JKL", "|||", "JKL"},
+                {"THISTHOSETHAT", "H", "AT"},
+                {"P|||AAA|||BBB", "|||", "BBB"},
+                {"WEF", null, null},
+                {"ALL", "", "ALL"},
+                {null, "S", null}
+        };
+        
+        for(String[] testCase: testCases) {
+            String result = StringUtil.getLastStringAfter(testCase[0], testCase[1]);
+            assertEquals(result, testCase[2]);
+        }
+    }
 
     /**
      * Test {@link StringUtil#getLastSegment(String, String)}