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; |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 29 | |
Bharat saraswal | 780eca3 | 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.io.impl.YangFileScanner.getJavaFiles; |
| 34 | import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getYangFiles; |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 35 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 36 | import static java.io.File.separator; |
| 37 | |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 38 | /** |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 39 | * Test the file scanner service. |
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 | public final class YangFileScannerTest { |
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 | @Rule |
| 44 | public ExpectedException thrown = ExpectedException.none(); |
| 45 | |
| 46 | String baseDir = "target/UnitTestCase"; |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 47 | |
| 48 | /** |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 49 | * A private constructor is tested. |
| 50 | * |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +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 |
| 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 |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 57 | */ |
| 58 | @Test |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 59 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 60 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 61 | |
| 62 | Class<?>[] classesToConstruct = {YangFileScanner.class }; |
| 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())); |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 71 | * This test case checks for a .java file inside the specified dir. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 72 | * |
| 73 | * @throws IOException when fails to do IO operations |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 74 | */ |
| 75 | @Test |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 76 | public void checkJavaFileInsideDirTest() throws IOException { |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 77 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 78 | String dir = baseDir + separator + "scanner2"; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 79 | File path = createDirectory(dir); |
| 80 | createFile(path, "testScanner.java"); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 81 | List<String> dirContents = getJavaFiles(path.toString()); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 82 | List<String> expectedContents = new LinkedList<>(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 83 | expectedContents.add(path.getCanonicalPath() + separator + "testScanner.java"); |
| 84 | assertThat(true, is(dirContents.equals(expectedContents))); |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Method used for creating multiple directories inside the target file. |
| 89 | * |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 90 | * @param path where directories should be created |
| 91 | * @return the directory path that is created |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 92 | */ |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 93 | private File createDirectory(String path) { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 94 | |
| 95 | File myDir = new File(path); |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 96 | myDir.mkdirs(); |
| 97 | return myDir; |
| 98 | } |
| 99 | |
| 100 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 101 | * Method used for creating file inside the specified directory. |
| 102 | * |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 103 | * @param myDir the path where file has to be created inside |
| 104 | * @param fileName the name of the file to be created |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 105 | */ |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 106 | private void createFile(File myDir, String fileName) throws IOException { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 107 | |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 108 | File file = null; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 109 | file = new File(myDir + separator + fileName); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 110 | file.createNewFile(); |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 111 | } |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * This testcase checks for a java file inside an empty directory. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 115 | * |
| 116 | * @throws IOException when fails to do IO operations |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 117 | */ |
| 118 | @Test |
| 119 | public void emptyDirJavaScannerTest() throws IOException { |
| 120 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 121 | String emptyDir = baseDir + separator + "scanner1"; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 122 | File path = createDirectory(emptyDir); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 123 | List<String> emptyDirContents = getJavaFiles(path.toString()); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 124 | List<String> expectedContents = new LinkedList<>(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 125 | assertThat(true, is(emptyDirContents.equals(expectedContents))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * This testcase checks for a yang file inside an empty directory. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 130 | * |
| 131 | * @throws IOException when fails to do IO operations |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 132 | */ |
| 133 | @Test |
| 134 | public void emptyDirYangScannerTest() throws IOException { |
| 135 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 136 | String emptyYangDir = baseDir + separator + "scanner1"; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 137 | File path = createDirectory(emptyYangDir); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 138 | List<String> emptyDirContents = getYangFiles(path.toString()); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 139 | List<String> expectedContents = new LinkedList<>(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 140 | assertThat(true, is(emptyDirContents.equals(expectedContents))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
| 144 | * 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] | 145 | * |
| 146 | * @throws IOException when fails to do IO operations |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 147 | */ |
| 148 | @Test |
| 149 | public void emptySubDirScannerTest() throws IOException { |
| 150 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 151 | String dir = baseDir + separator + "scanner3"; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 152 | File path = createDirectory(dir); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 153 | String subDir = path.toString() + separator + "subDir1"; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 154 | createDirectory(subDir); |
| 155 | createFile(path, "invalidFile.txt"); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 156 | List<String> emptySubDirContents = getJavaFiles(path.toString()); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 157 | List<String> expectedContents = new LinkedList<>(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 158 | assertThat(true, is(emptySubDirContents.equals(expectedContents))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 159 | } |
| 160 | |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 161 | } |