Add UT for FileUtils.java 95/36995/1
authorluxin <luxin7@huawei.com>
Tue, 20 Mar 2018 08:40:20 +0000 (16:40 +0800)
committerluxin <luxin7@huawei.com>
Tue, 20 Mar 2018 08:40:20 +0000 (16:40 +0800)
Change-Id: I66e463f628c9c05a01d477fc411c94952370da93
Issue-ID: VFC-833
Signed-off-by: luxin <luxin7@huawei.com>
juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/test/java/FileTest [new file with mode: 0644]
juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/FileUtilsTest.java

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 (file)
index 0000000..1117cee
--- /dev/null
@@ -0,0 +1 @@
+(2
\ No newline at end of file
index 422888b..9fc383e 100644 (file)
  * 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<File> files = fileUtils.listFiles(f);
+        List<File> files = FileUtils.listFiles(f);
         assertNotNull(files);
 
     }
+
+    @Test(expected = FileNotFoundException.class)
+    public void testListFilesFail() throws Exception {
+        String file = "abc";
+        File f = new File(file);
+        List<File> 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<File> files = fileUtils.getFiles(resourcesDirectory.getAbsolutePath());
+        List<File> 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/"));
+
+    }
 }