janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
janani b | 0863755 | 2016-02-08 18:56:13 +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; |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 20 | import java.io.IOException; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 21 | import java.lang.reflect.Constructor; |
| 22 | import java.lang.reflect.InvocationTargetException; |
| 23 | import java.util.LinkedList; |
| 24 | import java.util.List; |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 25 | |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 26 | import org.junit.Rule; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.rules.ExpectedException; |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 29 | import org.slf4j.Logger; |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 30 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 31 | import static org.hamcrest.core.Is.is; |
| 32 | import static org.hamcrest.core.IsNot.not; |
| 33 | import static org.junit.Assert.assertThat; |
| 34 | import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getJavaFiles; |
| 35 | import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getYangFiles; |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 36 | import static org.slf4j.LoggerFactory.getLogger; |
| 37 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 38 | import static java.io.File.separator; |
| 39 | |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 40 | /** |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 41 | * Test the file scanner service. |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 42 | */ |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 43 | public final class YangFileScannerTest { |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 44 | |
| 45 | private final Logger log = getLogger(getClass()); |
| 46 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 47 | @Rule |
| 48 | public ExpectedException thrown = ExpectedException.none(); |
| 49 | |
| 50 | String baseDir = "target/UnitTestCase"; |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 51 | |
| 52 | /** |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 53 | * A private constructor is tested. |
| 54 | * |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 55 | * @throws SecurityException if any security violation is observed |
| 56 | * @throws NoSuchMethodException if when the method is not found |
| 57 | * @throws IllegalArgumentException if there is illegal argument found |
| 58 | * @throws InstantiationException if instantiation is provoked for the private constructor |
| 59 | * @throws IllegalAccessException if instance is provoked or a method is provoked |
| 60 | * @throws InvocationTargetException when an exception occurs by the method or constructor |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 61 | */ |
| 62 | @Test |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 63 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 64 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 65 | |
| 66 | Class<?>[] classesToConstruct = {YangFileScanner.class }; |
| 67 | for (Class<?> clazz : classesToConstruct) { |
| 68 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 69 | constructor.setAccessible(true); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 70 | assertThat(null, not(constructor.newInstance())); |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 75 | * This test case checks for a .java file inside the specified dir. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 76 | * |
| 77 | * @throws IOException when fails to do IO operations |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 78 | */ |
| 79 | @Test |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 80 | public void checkJavaFileInsideDirTest() throws IOException { |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 81 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 82 | String dir = baseDir + separator + "scanner2"; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 83 | File path = createDirectory(dir); |
| 84 | createFile(path, "testScanner.java"); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 85 | List<String> dirContents = getJavaFiles(path.toString()); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 86 | List<String> expectedContents = new LinkedList<>(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 87 | expectedContents.add(path.getCanonicalPath() + separator + "testScanner.java"); |
| 88 | assertThat(true, is(dirContents.equals(expectedContents))); |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Method used for creating multiple directories inside the target file. |
| 93 | * |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 94 | * @param path where directories should be created |
| 95 | * @return the directory path that is created |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 96 | */ |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 97 | private File createDirectory(String path) { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 98 | |
| 99 | File myDir = new File(path); |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 100 | myDir.mkdirs(); |
| 101 | return myDir; |
| 102 | } |
| 103 | |
| 104 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 105 | * Method used for creating file inside the specified directory. |
| 106 | * |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 107 | * @param myDir the path where file has to be created inside |
| 108 | * @param fileName the name of the file to be created |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 109 | */ |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 110 | private void createFile(File myDir, String fileName) throws IOException { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 111 | |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 112 | File file = null; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 113 | file = new File(myDir + separator + fileName); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 114 | file.createNewFile(); |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 115 | } |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * This testcase checks for a java file inside an empty directory. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 119 | * |
| 120 | * @throws IOException when fails to do IO operations |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 121 | */ |
| 122 | @Test |
| 123 | public void emptyDirJavaScannerTest() throws IOException { |
| 124 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 125 | String emptyDir = baseDir + separator + "scanner1"; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 126 | File path = createDirectory(emptyDir); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 127 | List<String> emptyDirContents = getJavaFiles(path.toString()); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 128 | List<String> expectedContents = new LinkedList<>(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 129 | assertThat(true, is(emptyDirContents.equals(expectedContents))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
| 133 | * This testcase checks for a yang file inside an empty directory. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 134 | * |
| 135 | * @throws IOException when fails to do IO operations |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 136 | */ |
| 137 | @Test |
| 138 | public void emptyDirYangScannerTest() throws IOException { |
| 139 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 140 | String emptyYangDir = baseDir + separator + "scanner1"; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 141 | File path = createDirectory(emptyYangDir); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 142 | List<String> emptyDirContents = getYangFiles(path.toString()); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 143 | List<String> expectedContents = new LinkedList<>(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 144 | assertThat(true, is(emptyDirContents.equals(expectedContents))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * This test case checks with the sub directories in the given path for java files. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 149 | * |
| 150 | * @throws IOException when fails to do IO operations |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 151 | */ |
| 152 | @Test |
| 153 | public void emptySubDirScannerTest() throws IOException { |
| 154 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 155 | String dir = baseDir + separator + "scanner3"; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 156 | File path = createDirectory(dir); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 157 | String subDir = path.toString() + separator + "subDir1"; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 158 | createDirectory(subDir); |
| 159 | createFile(path, "invalidFile.txt"); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 160 | List<String> emptySubDirContents = getJavaFiles(path.toString()); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 161 | List<String> expectedContents = new LinkedList<>(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 162 | assertThat(true, is(emptySubDirContents.equals(expectedContents))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 163 | } |
| 164 | |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 165 | } |