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