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