b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.utils.io.impl; |
| 18 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 19 | import java.io.File; |
| 20 | import java.io.IOException; |
| 21 | import java.lang.reflect.Constructor; |
| 22 | import java.lang.reflect.InvocationTargetException; |
| 23 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 24 | import org.junit.Test; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.utils.UtilConstants; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 26 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 27 | import static org.junit.Assert.assertNotNull; |
| 28 | import static org.junit.Assert.assertTrue; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Tests the file handle utilities. |
| 32 | */ |
| 33 | public final class FileSystemUtilTest { |
| 34 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 35 | private static final String BASE_DIR_PKG = "target.UnitTestCase."; |
| 36 | private static final String PKG_INFO_CONTENT = "testGeneration6"; |
| 37 | private static final String BASE_PKG = "target/UnitTestCase"; |
| 38 | private static final String TEST_DATA_1 = "This is to append a text to the file first1\n"; |
| 39 | private static final String TEST_DATA_2 = "This is next second line\n"; |
| 40 | private static final String TEST_DATA_3 = "This is next third line in the file"; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * A private constructor is tested. |
| 44 | * |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 45 | * @throws SecurityException if any security violation is observed |
| 46 | * @throws NoSuchMethodException if when the method is not found |
| 47 | * @throws IllegalArgumentException if there is illegal argument found |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 48 | * @throws InstantiationException if instantiation is provoked for the private constructor |
| 49 | * @throws IllegalAccessException if instance is provoked or a method is provoked |
| 50 | * @throws InvocationTargetException when an exception occurs by the method or constructor |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 51 | */ |
| 52 | @Test |
| 53 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 54 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 55 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 56 | Class<?>[] classesToConstruct = {FileSystemUtil.class }; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 57 | for (Class<?> clazz : classesToConstruct) { |
| 58 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 59 | constructor.setAccessible(true); |
| 60 | assertNotNull(constructor.newInstance()); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 65 | * This test case checks the contents to be written in the file. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 66 | * |
| 67 | * @throws IOException when fails to create a test file |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 68 | */ |
| 69 | @Test |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 70 | public void updateFileHandleTest() throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 71 | |
| 72 | File dir = new File(BASE_PKG + File.separator + "File1"); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 73 | dir.mkdirs(); |
| 74 | File createFile = new File(dir + "testFile"); |
| 75 | createFile.createNewFile(); |
| 76 | File createSourceFile = new File(dir + "sourceTestFile"); |
| 77 | createSourceFile.createNewFile(); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 78 | FileSystemUtil.updateFileHandle(createFile, TEST_DATA_1, false); |
| 79 | FileSystemUtil.updateFileHandle(createFile, TEST_DATA_2, false); |
| 80 | FileSystemUtil.updateFileHandle(createFile, TEST_DATA_3, false); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 81 | FileSystemUtil.appendFileContents(createFile, createSourceFile); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 82 | FileSystemUtil.updateFileHandle(createFile, null, true); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 86 | * This test case checks whether the package is existing. |
| 87 | * |
| 88 | * @throws IOException when failed to create a test file |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 89 | */ |
| 90 | @Test |
| 91 | public void packageExistTest() throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 92 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 93 | String dirPath = "exist1.exist2.exist3"; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 94 | String strPath = BASE_DIR_PKG + dirPath; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 95 | File createDir = new File(strPath.replace(UtilConstants.PERIOD, UtilConstants.SLASH)); |
| 96 | createDir.mkdirs(); |
| 97 | File createFile = new File(createDir + File.separator + "package-info.java"); |
| 98 | createFile.createNewFile(); |
| 99 | assertTrue(FileSystemUtil.doesPackageExist(strPath)); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 100 | FileSystemUtil.createPackage(strPath, PKG_INFO_CONTENT); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 101 | createDir.delete(); |
| 102 | } |
| 103 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 104 | } |