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