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.FileNotFoundException;
25 import java.io.IOException;
26 import java.lang.reflect.Constructor;
27 import java.lang.reflect.Modifier;
28 import java.util.List;
30 import org.junit.Before;
31 import org.junit.Test;
33 public class FileUtilsTest {
39 fileUtils.getAppAbsoluteUrl();
43 public void testGetClassPath() throws Exception {
44 assertNotNull(FileUtils.getClassPath());
48 public void testReadFileNull() throws Exception {
49 assertNotNull(FileUtils.readFile(null, null));
53 public void testReadFile() throws Exception {
54 assertNotNull(FileUtils.readFile(
55 new File("src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/FileUtil.java"), "utf-8"));
58 @Test(expected = IOException.class)
59 public void testReadFileFail() throws Exception {
60 FileUtils.readFile(new File("src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/aaa.java"),
65 public void testWriteFile() throws Exception {
67 byte[] bytes = new byte[] {40, 50};
68 int b = FileUtils.writeFile(bytes, filePath);
73 public void testWriteFileSuccess() throws Exception {
74 String filePath = "src/test/java/FileTest";
75 byte[] bytes = new byte[] {40, 50};
76 int b = FileUtils.writeFile(bytes, filePath);
81 public void testListFiles() throws Exception {
83 File f = new File(file);
84 List<File> files = FileUtils.listFiles(f);
89 @Test(expected = FileNotFoundException.class)
90 public void testListFilesFail() throws Exception {
92 File f = new File(file);
93 List<File> files = FileUtils.listFiles(f);
99 public void testMkDirs() throws Exception {
100 File resourcesDirectory = new File("src/test/resources");
101 String path = resourcesDirectory.getAbsolutePath() + "/TestDir";
102 resourcesDirectory.getAbsolutePath();
103 FileUtils.newFloder(path);
104 FileUtils.mkDirs(path);
105 FileUtils.newFloder(path);
109 public void testDelFiles() throws Exception {
110 File resourcesDirectory = new File("src/test/resources/TestDir/Test.txt");
111 assertTrue(FileUtils.delFiles(resourcesDirectory.getAbsolutePath()));
115 public void testgetFiles() throws Exception {
116 File resourcesDirectory = new File("src/test/resources");
117 List<File> files = FileUtils.getFiles(resourcesDirectory.getAbsolutePath());
118 assertNotNull(files);
122 public void testCopy() throws Exception {
123 File oldfile = new File("");
124 File newfile = new File("");
125 FileUtils.copy(oldfile.getAbsolutePath(), newfile.getAbsolutePath(), true);
129 public void testPrivateConstructor() throws Exception {
130 Constructor constructor = FileUtils.class.getDeclaredConstructor();
131 assertTrue("Constructor private", Modifier.isPrivate(constructor.getModifiers()));
132 constructor.setAccessible(true);
133 constructor.newInstance();
136 @Test(expected = FileNotFoundException.class)
137 public void testIsUsed() throws Exception {
138 assertNotNull(FileUtils.isUsed("abc.txt"));
143 public void testGetBaseFileName() throws Exception {
144 assertNotNull(FileUtils.getBaseFileName(new File("aaa.txt")));
145 assertNotNull(FileUtils.getBaseFileName(new File("aaa")));
150 public void testFixPath() throws Exception {
151 assertNull(FileUtils.fixPath(null));
152 assertNotNull(FileUtils.fixPath("/abc"));
153 assertNotNull(FileUtils.fixPath("/abc/"));
154 assertNotNull(FileUtils.fixPath("abc"));
155 assertNotNull(FileUtils.fixPath("abc/"));
160 public void testGetFiendlyPath() throws Exception {
161 assertNull(FileUtils.getFriendlyPath(null));
162 assertNotNull(FileUtils.getFriendlyPath("/abc"));
163 assertNotNull(FileUtils.getFriendlyPath("/abc/"));
164 assertNotNull(FileUtils.getFriendlyPath("abc"));
165 assertNotNull(FileUtils.getFriendlyPath("abc/"));