2 * Copyright 2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
24 import java.io.IOException;
25 import java.lang.reflect.Constructor;
26 import java.lang.reflect.Modifier;
27 import java.util.List;
29 import org.junit.Before;
30 import org.junit.Test;
32 public class FileUtilsTest {
38 fileUtils.getAppAbsoluteUrl();
42 public void testGetClassPath() throws Exception {
43 assertNotNull(FileUtils.getClassPath());
47 public void testReadFileNull() throws Exception {
48 assertNotNull(FileUtils.readFile(null, null));
52 public void testReadFile() throws Exception {
53 assertNotNull(FileUtils.readFile(
54 new File("src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/FileUtil.java"), "utf-8"));
57 @Test(expected = IOException.class)
58 public void testReadFileFail() throws Exception {
59 FileUtils.readFile(new File("src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/aaa.java"),
64 public void testWriteFile() throws Exception {
66 byte[] bytes = new byte[] {40, 50};
67 int b = FileUtils.writeFile(bytes, filePath);
72 public void testWriteFileSuccess() throws Exception {
73 String filePath = "src/test/java/FileTest";
74 byte[] bytes = new byte[] {40, 50};
75 int b = FileUtils.writeFile(bytes, filePath);
80 public void testListFiles() throws Exception {
82 File f = new File(file);
83 List<File> files = FileUtils.listFiles(f);
88 // @Test(expected = FileNotFoundException.class)
89 // public void testListFilesFail() throws Exception {
90 // String file = "abc";
91 // File f = new File(file);
92 // List<File> files = FileUtils.listFiles(f);
93 // assertNotNull(files);
98 public void testMkDirs() throws Exception {
99 File resourcesDirectory = new File("src/test/resources");
100 String path = resourcesDirectory.getAbsolutePath() + "/TestDir";
101 resourcesDirectory.getAbsolutePath();
102 FileUtils.newFloder(path);
103 FileUtils.mkDirs(path);
104 FileUtils.newFloder(path);
108 public void testDelFiles() throws Exception {
109 File resourcesDirectory = new File("src/test/resources/TestDir/Test.txt");
110 assertTrue(FileUtils.delFiles(resourcesDirectory.getAbsolutePath()));
114 public void testgetFiles() throws Exception {
115 File resourcesDirectory = new File("src/test/resources");
116 List<File> files = FileUtils.getFiles(resourcesDirectory.getAbsolutePath());
117 assertNotNull(files);
121 public void testCopy() throws Exception {
122 File oldfile = new File("");
123 File newfile = new File("");
124 FileUtils.copy(oldfile.getAbsolutePath(), newfile.getAbsolutePath(), true);
128 public void testPrivateConstructor() throws Exception {
129 Constructor constructor = FileUtils.class.getDeclaredConstructor();
130 assertTrue("Constructor private", Modifier.isPrivate(constructor.getModifiers()));
131 constructor.setAccessible(true);
132 constructor.newInstance();
135 // @Test(expected = FileNotFoundException.class)
136 // public void testIsUsed() throws Exception {
137 // assertNotNull(FileUtils.isUsed("abc.txt"));
142 public void testGetBaseFileName() throws Exception {
143 assertNotNull(FileUtils.getBaseFileName(new File("aaa.txt")));
144 assertNotNull(FileUtils.getBaseFileName(new File("aaa")));
149 public void testFixPath() throws Exception {
150 assertNull(FileUtils.fixPath(null));
151 assertNotNull(FileUtils.fixPath("/abc"));
152 assertNotNull(FileUtils.fixPath("/abc/"));
153 assertNotNull(FileUtils.fixPath("abc"));
154 assertNotNull(FileUtils.fixPath("abc/"));
159 public void testGetFiendlyPath() throws Exception {
160 assertNull(FileUtils.getFriendlyPath(null));
161 assertNotNull(FileUtils.getFriendlyPath("/abc"));
162 assertNotNull(FileUtils.getFriendlyPath("/abc/"));
163 assertNotNull(FileUtils.getFriendlyPath("abc"));
164 assertNotNull(FileUtils.getFriendlyPath("abc/"));