* @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, ".");
}
/**
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.
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)}
@Test
public void test_findFiles() throws Exception{
final Object [][] TEST_CASE = new Object[][] {
- new Object[] {"",""},
+ new Object[] {"",""},
};
}
);
}
}
+
+ /**
+ * 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)}