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