BAEL-1734 add different methods to find file extension (#4122)

* BAEL-1734 add different methods to find file extension

* fix camel case formatting for variables

* fix variable name

Remove camel casing for variable name as it is a single word.
This commit is contained in:
Rajat Garg 2018-05-23 13:44:17 +05:30 committed by pauljervis
parent 6674850c03
commit 4c333aeeb3
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,23 @@
package com.baeldung.extension;
import com.google.common.io.Files;
import org.apache.commons.io.FilenameUtils;
public class Extension {
//Instead of file name we can also specify full path of a file eg. /baeldung/com/demo/abc.java
public String getExtensionByApacheCommonLib(String filename) {
return FilenameUtils.getExtension(filename);
}
public String getExtensionByStringHandling(String filename) {
String fileExtension = "";
if (filename.contains(".") && filename.lastIndexOf(".") != 0) {
fileExtension = filename.substring(filename.lastIndexOf(".") + 1);
}
return fileExtension;
}
public String getExtensionByGuava(String filename) {
return Files.getFileExtension(filename);
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.extension;
import org.junit.Assert;
import org.junit.Test;
public class ExtensionTest {
private Extension extension = new Extension();
@Test
public void getExtension_whenApacheCommonIO_thenExtensionIsTrue() {
String expectedExtension = "txt";
String actualExtension = extension.getExtensionByApacheCommonLib("jarvis.txt");
Assert.assertEquals(expectedExtension, actualExtension);
}
@Test
public void getExtension_whenStringHandle_thenExtensionIsTrue() {
String expectedExtension = "java";
String actualExtension = extension.getExtensionByStringHandling("Demo.java");
Assert.assertEquals(expectedExtension, actualExtension);
}
@Test
public void getExtension_whenGuava_thenExtensionIsTrue() {
String expectedExtension = "class";
String actualExtension = extension.getExtensionByGuava("baeldung/Demo.class");
Assert.assertEquals(expectedExtension, actualExtension);
}
}