b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
b.janani | 68c55e1 | 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 | 68c55e1 | 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 | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 24 | import org.junit.Test; |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangNode; |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
| 27 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 28 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 29 | |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 30 | import static org.hamcrest.core.Is.is; |
| 31 | import static org.hamcrest.core.IsNot.not; |
| 32 | import static org.junit.Assert.assertThat; |
| 33 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 34 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
| 35 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents; |
| 36 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage; |
| 37 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.doesPackageExist; |
| 38 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle; |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Tests the file handle utilities. |
| 42 | */ |
| 43 | public final class FileSystemUtilTest { |
| 44 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 45 | private static final String BASE_DIR_PKG = "target.UnitTestCase."; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 46 | private static final String BASE_PKG = "target/UnitTestCase"; |
| 47 | private static final String TEST_DATA_1 = "This is to append a text to the file first1\n"; |
| 48 | private static final String TEST_DATA_2 = "This is next second line\n"; |
| 49 | private static final String TEST_DATA_3 = "This is next third line in the file"; |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 50 | private static final String TEST_FILE = "testFile"; |
| 51 | private static final String SOURCE_TEST_FILE = "sourceTestFile"; |
| 52 | private static final String DIR_PATH = "exist1.exist2.exist3"; |
| 53 | private static final String PKG_INFO = "package-info.java"; |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * A private constructor is tested. |
| 57 | * |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 58 | * @throws SecurityException if any security violation is observed |
| 59 | * @throws NoSuchMethodException if when the method is not found |
| 60 | * @throws IllegalArgumentException if there is illegal argument found |
| 61 | * @throws InstantiationException if instantiation is provoked for the private constructor |
| 62 | * @throws IllegalAccessException if instance is provoked or a method is provoked |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 63 | * @throws InvocationTargetException when an exception occurs by the method or constructor |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 64 | */ |
| 65 | @Test |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 66 | public void callPrivateConstructors() |
| 67 | throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 68 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 69 | |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 70 | Class<?>[] classesToConstruct = {FileSystemUtil.class }; |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 71 | for (Class<?> clazz : classesToConstruct) { |
| 72 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 73 | constructor.setAccessible(true); |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 74 | assertThat(null, not(constructor.newInstance())); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 79 | * This test case checks the contents to be written in the file. |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 80 | * |
| 81 | * @throws IOException when fails to create a test file |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 82 | */ |
| 83 | @Test |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 84 | public void updateFileHandleTest() throws IOException { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 85 | |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 86 | File dir = new File(BASE_PKG + SLASH + TEST_FILE); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 87 | dir.mkdirs(); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 88 | File createFile = new File(dir + TEST_FILE); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 89 | createFile.createNewFile(); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 90 | File createSourceFile = new File(dir + SOURCE_TEST_FILE); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 91 | createSourceFile.createNewFile(); |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 92 | updateFileHandle(createFile, TEST_DATA_1, false); |
| 93 | updateFileHandle(createFile, TEST_DATA_2, false); |
| 94 | updateFileHandle(createFile, TEST_DATA_3, false); |
| 95 | appendFileContents(createFile, createSourceFile); |
| 96 | updateFileHandle(createFile, null, true); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 100 | * This test case checks whether the package is existing. |
| 101 | * |
| 102 | * @throws IOException when failed to create a test file |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 103 | */ |
| 104 | @Test |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 105 | public void packageExistTest() throws IOException { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 106 | |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 107 | String strPath = BASE_DIR_PKG + DIR_PATH; |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 108 | File createDir = new File(strPath.replace(PERIOD, SLASH)); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 109 | createDir.mkdirs(); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 110 | File createFile = new File(createDir + SLASH + PKG_INFO); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 111 | createFile.createNewFile(); |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 112 | assertThat(true, is(doesPackageExist(strPath))); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 113 | createPackage(getStubNode()); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 114 | createDir.delete(); |
| 115 | } |
| 116 | |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 117 | /** |
| 118 | * Returns stub YANG node. |
| 119 | * |
| 120 | * @return stub node |
| 121 | */ |
| 122 | private YangNode getStubNode() { |
| 123 | YangJavaModule module = new YangJavaModule(); |
| 124 | module.setName(TEST_DATA_1); |
| 125 | JavaFileInfo javafileInfo = new JavaFileInfo(); |
| 126 | javafileInfo.setJavaName(TEST_DATA_1); |
| 127 | javafileInfo.setBaseCodeGenPath(""); |
| 128 | javafileInfo.setPackageFilePath(BASE_PKG); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 129 | javafileInfo.setPluginConfig(getStubPluginConfig()); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 130 | module.setJavaFileInfo(javafileInfo); |
| 131 | return module; |
| 132 | } |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 133 | |
| 134 | /** |
| 135 | * Returns stub pluginConfig. |
| 136 | * |
| 137 | * @return stub pluginConfig |
| 138 | */ |
| 139 | private YangPluginConfig getStubPluginConfig() { |
| 140 | YangPluginConfig pluginConfig = new YangPluginConfig(); |
| 141 | pluginConfig.setConflictResolver(null); |
| 142 | return pluginConfig; |
| 143 | } |
| 144 | |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 145 | } |