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.Rule; |
| 25 | import org.junit.Test; |
| 26 | import org.junit.rules.ExpectedException; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.utils.UtilConstants; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 28 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 29 | import static org.junit.Assert.assertNotNull; |
| 30 | import static org.junit.Assert.assertTrue; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Tests the file handle utilities. |
| 34 | */ |
| 35 | public final class FileSystemUtilTest { |
| 36 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 37 | public static final String BASE_DIR_PKG = "target.UnitTestCase."; |
| 38 | public static final String PKG_INFO_CONTENT = "testGeneration6"; |
| 39 | public static final String BASE_PKG = "target/UnitTestCase"; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 40 | |
| 41 | @Rule |
| 42 | public ExpectedException thrown = ExpectedException.none(); |
| 43 | |
| 44 | /** |
| 45 | * A private constructor is tested. |
| 46 | * |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 47 | * @throws SecurityException if any security violation is observed |
| 48 | * @throws NoSuchMethodException if when the method is not found |
| 49 | * @throws IllegalArgumentException if there is illegal argument found |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 50 | * @throws InstantiationException if instantiation is provoked for the |
| 51 | * private constructor |
| 52 | * @throws IllegalAccessException if instance is provoked or a method is |
| 53 | * provoked |
| 54 | * @throws InvocationTargetException when an exception occurs by the method |
| 55 | * or constructor |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 56 | */ |
| 57 | @Test |
| 58 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 59 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 60 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 61 | Class<?>[] classesToConstruct = { |
| 62 | FileSystemUtil.class |
| 63 | }; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 64 | for (Class<?> clazz : classesToConstruct) { |
| 65 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 66 | constructor.setAccessible(true); |
| 67 | assertNotNull(constructor.newInstance()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 72 | * This test case checks the contents to be written in the file. |
| 73 | */ |
| 74 | @Test |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 75 | public void updateFileHandleTest() throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 76 | |
| 77 | File dir = new File(BASE_PKG + File.separator + "File1"); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 78 | dir.mkdirs(); |
| 79 | File createFile = new File(dir + "testFile"); |
| 80 | createFile.createNewFile(); |
| 81 | File createSourceFile = new File(dir + "sourceTestFile"); |
| 82 | createSourceFile.createNewFile(); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 83 | FileSystemUtil.updateFileHandle(createFile, "This is to append a text to the file first1\n", false); |
| 84 | FileSystemUtil.updateFileHandle(createFile, "This is next second line\n", false); |
| 85 | FileSystemUtil.updateFileHandle(createFile, "This is next third line in the file", false); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 86 | FileSystemUtil.appendFileContents(createFile, createSourceFile); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 87 | FileSystemUtil.updateFileHandle(createFile, null, true); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 91 | * This test case checks whether the package is existing. |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 92 | */ |
| 93 | @Test |
| 94 | public void packageExistTest() throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 95 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 96 | String dirPath = "exist1.exist2.exist3"; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 97 | String strPath = BASE_DIR_PKG + dirPath; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 98 | File createDir = new File(strPath.replace(UtilConstants.PERIOD, UtilConstants.SLASH)); |
| 99 | createDir.mkdirs(); |
| 100 | File createFile = new File(createDir + File.separator + "package-info.java"); |
| 101 | createFile.createNewFile(); |
| 102 | assertTrue(FileSystemUtil.doesPackageExist(strPath)); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 103 | FileSystemUtil.createPackage(strPath, PKG_INFO_CONTENT); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 104 | createDir.delete(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * This test case checks the package does not exist. |
| 109 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 110 | // @Test |
| 111 | // public void packageNotExistTest() throws IOException { |
| 112 | |
| 113 | // String dirPath = "notexist1.notexist2"; |
| 114 | // String strPath = BASE_DIR_PKG + dirPath; |
| 115 | // File createDir = new File(strPath.replace(UtilConstants.PERIOD, UtilConstants.SLASH)); |
| 116 | // assertFalse(FileSystemUtil.doesPackageExist(strPath)); |
| 117 | // createDir.mkdirs(); |
| 118 | // assertFalse(FileSystemUtil.doesPackageExist(strPath)); |
| 119 | // CopyrightHeader.parseCopyrightHeader(); |
| 120 | // FileSystemUtil.createPackage(strPath, PKG_INFO_CONTENT); |
| 121 | // assertTrue(FileSystemUtil.doesPackageExist(strPath)); |
| 122 | // createDir.delete(); |
| 123 | // } |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 124 | } |