From 064c184a26cd8385c91de65afc4bf43b9907ab9d Mon Sep 17 00:00:00 2001 From: luxin Date: Tue, 20 Mar 2018 16:40:20 +0800 Subject: Add UT for FileUtils.java Change-Id: I66e463f628c9c05a01d477fc411c94952370da93 Issue-ID: VFC-833 Signed-off-by: luxin --- .../service/src/test/java/FileTest | 1 + .../jujuvnfmadapter/common/FileUtilsTest.java | 115 +++++++++++++++++---- 2 files changed, 95 insertions(+), 21 deletions(-) create mode 100644 juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/test/java/FileTest diff --git a/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/test/java/FileTest b/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/test/java/FileTest new file mode 100644 index 0000000..1117cee --- /dev/null +++ b/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/test/java/FileTest @@ -0,0 +1 @@ +(2 \ No newline at end of file diff --git a/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/FileUtilsTest.java b/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/FileUtilsTest.java index 422888b..9fc383e 100644 --- a/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/FileUtilsTest.java +++ b/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/FileUtilsTest.java @@ -13,78 +13,118 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; import java.util.List; + import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; -import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.FileUtils; public class FileUtilsTest { FileUtils fileUtils; + @Before - public void setUp(){ + public void setUp() { fileUtils.getAppAbsoluteUrl(); } + @Test + public void testGetClassPath() throws Exception { + assertNotNull(FileUtils.getClassPath()); + } + + @Test + public void testReadFileNull() throws Exception { + assertNotNull(FileUtils.readFile(null, null)); + } + + @Test + public void testReadFile() throws Exception { + assertNotNull(FileUtils.readFile( + new File("src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/FileUtil.java"), "utf-8")); + } + + @Test(expected = IOException.class) + public void testReadFileFail() throws Exception { + FileUtils.readFile(new File("src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/aaa.java"), + "utf-8"); + } + @Test public void testWriteFile() throws Exception { - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("").getFile()); String filePath = ""; - byte[] bytes = new byte[] {40,50}; - int b = fileUtils.writeFile(bytes, filePath); + byte[] bytes = new byte[] {40, 50}; + int b = FileUtils.writeFile(bytes, filePath); assertNotNull(b); } + @Test - @Ignore - public void testReadFile() throws Exception { - ClassLoader classLoader = getClass().getClassLoader(); - File f = new File(classLoader.getResource("").getFile()); - System.out.println(f.isAbsolute()); - String charsetName = "UTF-8"; - byte[] b = fileUtils.readFile(f, charsetName); + public void testWriteFileSuccess() throws Exception { + String filePath = "src/test/java/FileTest"; + byte[] bytes = new byte[] {40, 50}; + int b = FileUtils.writeFile(bytes, filePath); assertNotNull(b); } + @Test public void testListFiles() throws Exception { String file = "."; File f = new File(file); - List files = fileUtils.listFiles(f); + List files = FileUtils.listFiles(f); assertNotNull(files); } + + @Test(expected = FileNotFoundException.class) + public void testListFilesFail() throws Exception { + String file = "abc"; + File f = new File(file); + List files = FileUtils.listFiles(f); + assertNotNull(files); + + } + @Test public void testMkDirs() throws Exception { File resourcesDirectory = new File("src/test/resources"); - String path = resourcesDirectory.getAbsolutePath()+"/TestDir"; + String path = resourcesDirectory.getAbsolutePath() + "/TestDir"; resourcesDirectory.getAbsolutePath(); - fileUtils.mkDirs(path); + FileUtils.newFloder(path); + FileUtils.mkDirs(path); + FileUtils.newFloder(path); } + @Test public void testDelFiles() throws Exception { File resourcesDirectory = new File("src/test/resources/TestDir/Test.txt"); - assertTrue(fileUtils.delFiles(resourcesDirectory.getAbsolutePath())); + assertTrue(FileUtils.delFiles(resourcesDirectory.getAbsolutePath())); } + @Test public void testgetFiles() throws Exception { File resourcesDirectory = new File("src/test/resources"); - List files = fileUtils.getFiles(resourcesDirectory.getAbsolutePath()); + List files = FileUtils.getFiles(resourcesDirectory.getAbsolutePath()); assertNotNull(files); } + @Test public void testCopy() throws Exception { File oldfile = new File(""); File newfile = new File(""); - fileUtils.copy(oldfile.getAbsolutePath(), newfile.getAbsolutePath(), true); + FileUtils.copy(oldfile.getAbsolutePath(), newfile.getAbsolutePath(), true); } + @Test public void testPrivateConstructor() throws Exception { Constructor constructor = FileUtils.class.getDeclaredConstructor(); @@ -92,4 +132,37 @@ public class FileUtilsTest { constructor.setAccessible(true); constructor.newInstance(); } + + @Test(expected = FileNotFoundException.class) + public void testIsUsed() throws Exception { + assertNotNull(FileUtils.isUsed("abc.txt")); + + } + + @Test + public void testGetBaseFileName() throws Exception { + assertNotNull(FileUtils.getBaseFileName(new File("aaa.txt"))); + assertNotNull(FileUtils.getBaseFileName(new File("aaa"))); + + } + + @Test + public void testFixPath() throws Exception { + assertNull(FileUtils.fixPath(null)); + assertNotNull(FileUtils.fixPath("/abc")); + assertNotNull(FileUtils.fixPath("/abc/")); + assertNotNull(FileUtils.fixPath("abc")); + assertNotNull(FileUtils.fixPath("abc/")); + + } + + @Test + public void testGetFiendlyPath() throws Exception { + assertNull(FileUtils.getFriendlyPath(null)); + assertNotNull(FileUtils.getFriendlyPath("/abc")); + assertNotNull(FileUtils.getFriendlyPath("/abc/")); + assertNotNull(FileUtils.getFriendlyPath("abc")); + assertNotNull(FileUtils.getFriendlyPath("abc/")); + + } } -- cgit 1.2.3-korg