blob: 1d17976bab107b46466bb9e17870e476b41f3341 [file] [log] [blame]
janani b5c12efc2016-02-08 18:56:13 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
janani b5c12efc2016-02-08 18:56:13 +05303 *
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
17package org.onosproject.yangutils.utils.io.impl;
18
b.janani68c55e12016-02-24 12:23:03 +053019import java.io.File;
Bharat saraswal2f11f652016-03-25 18:19:46 +053020import java.io.IOException;
b.janani68c55e12016-02-24 12:23:03 +053021import java.lang.reflect.Constructor;
22import java.lang.reflect.InvocationTargetException;
23import java.util.LinkedList;
24import java.util.List;
janani b5c12efc2016-02-08 18:56:13 +053025
Bharat saraswal2f11f652016-03-25 18:19:46 +053026import org.junit.Rule;
27import org.junit.Test;
28import org.junit.rules.ExpectedException;
Bharat saraswal2f11f652016-03-25 18:19:46 +053029
Bharat saraswal6ef0b762016-04-05 12:45:45 +053030import static org.hamcrest.core.Is.is;
31import static org.hamcrest.core.IsNot.not;
32import static org.junit.Assert.assertThat;
33import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getJavaFiles;
34import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getYangFiles;
janani b5c12efc2016-02-08 18:56:13 +053035
Bharat saraswal6ef0b762016-04-05 12:45:45 +053036import static java.io.File.separator;
37
janani b5c12efc2016-02-08 18:56:13 +053038/**
b.janani68c55e12016-02-24 12:23:03 +053039 * Test the file scanner service.
janani b5c12efc2016-02-08 18:56:13 +053040 */
b.janani68c55e12016-02-24 12:23:03 +053041public final class YangFileScannerTest {
janani b5c12efc2016-02-08 18:56:13 +053042
b.janani68c55e12016-02-24 12:23:03 +053043 @Rule
44 public ExpectedException thrown = ExpectedException.none();
45
46 String baseDir = "target/UnitTestCase";
janani b5c12efc2016-02-08 18:56:13 +053047
48 /**
b.janani68c55e12016-02-24 12:23:03 +053049 * A private constructor is tested.
50 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053051 * @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
Vinod Kumar S38046502016-03-23 15:30:27 +053056 * @throws InvocationTargetException when an exception occurs by the method or constructor
janani b5c12efc2016-02-08 18:56:13 +053057 */
58 @Test
b.janani68c55e12016-02-24 12:23:03 +053059 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Bharat saraswal2f11f652016-03-25 18:19:46 +053060 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani68c55e12016-02-24 12:23:03 +053061
62 Class<?>[] classesToConstruct = {YangFileScanner.class };
63 for (Class<?> clazz : classesToConstruct) {
64 Constructor<?> constructor = clazz.getDeclaredConstructor();
65 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053066 assertThat(null, not(constructor.newInstance()));
janani b5c12efc2016-02-08 18:56:13 +053067 }
68 }
69
70 /**
b.janani68c55e12016-02-24 12:23:03 +053071 * This test case checks for a .java file inside the specified dir.
Bharat saraswal6ef0b762016-04-05 12:45:45 +053072 *
73 * @throws IOException when fails to do IO operations
janani b5c12efc2016-02-08 18:56:13 +053074 */
75 @Test
b.janani68c55e12016-02-24 12:23:03 +053076 public void checkJavaFileInsideDirTest() throws IOException {
janani b5c12efc2016-02-08 18:56:13 +053077
Bharat saraswal6ef0b762016-04-05 12:45:45 +053078 String dir = baseDir + separator + "scanner2";
b.janani68c55e12016-02-24 12:23:03 +053079 File path = createDirectory(dir);
80 createFile(path, "testScanner.java");
Bharat saraswal6ef0b762016-04-05 12:45:45 +053081 List<String> dirContents = getJavaFiles(path.toString());
b.janani68c55e12016-02-24 12:23:03 +053082 List<String> expectedContents = new LinkedList<>();
Bharat saraswal6ef0b762016-04-05 12:45:45 +053083 expectedContents.add(path.getCanonicalPath() + separator + "testScanner.java");
84 assertThat(true, is(dirContents.equals(expectedContents)));
janani b5c12efc2016-02-08 18:56:13 +053085 }
86
87 /**
88 * Method used for creating multiple directories inside the target file.
89 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053090 * @param path where directories should be created
91 * @return the directory path that is created
janani b5c12efc2016-02-08 18:56:13 +053092 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +053093 private File createDirectory(String path) {
b.janani68c55e12016-02-24 12:23:03 +053094
95 File myDir = new File(path);
janani b5c12efc2016-02-08 18:56:13 +053096 myDir.mkdirs();
97 return myDir;
98 }
99
100 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530101 * Method used for creating file inside the specified directory.
102 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530103 * @param myDir the path where file has to be created inside
Vinod Kumar S38046502016-03-23 15:30:27 +0530104 * @param fileName the name of the file to be created
Bharat saraswal870c56f2016-02-20 21:57:16 +0530105 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530106 private void createFile(File myDir, String fileName) throws IOException {
b.janani68c55e12016-02-24 12:23:03 +0530107
janani b5c12efc2016-02-08 18:56:13 +0530108 File file = null;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530109 file = new File(myDir + separator + fileName);
b.janani68c55e12016-02-24 12:23:03 +0530110 file.createNewFile();
janani b5c12efc2016-02-08 18:56:13 +0530111 }
b.janani68c55e12016-02-24 12:23:03 +0530112
113 /**
114 * This testcase checks for a java file inside an empty directory.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530115 *
116 * @throws IOException when fails to do IO operations
b.janani68c55e12016-02-24 12:23:03 +0530117 */
118 @Test
119 public void emptyDirJavaScannerTest() throws IOException {
120
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530121 String emptyDir = baseDir + separator + "scanner1";
b.janani68c55e12016-02-24 12:23:03 +0530122 File path = createDirectory(emptyDir);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530123 List<String> emptyDirContents = getJavaFiles(path.toString());
b.janani68c55e12016-02-24 12:23:03 +0530124 List<String> expectedContents = new LinkedList<>();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530125 assertThat(true, is(emptyDirContents.equals(expectedContents)));
b.janani68c55e12016-02-24 12:23:03 +0530126 }
127
128 /**
129 * This testcase checks for a yang file inside an empty directory.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530130 *
131 * @throws IOException when fails to do IO operations
b.janani68c55e12016-02-24 12:23:03 +0530132 */
133 @Test
134 public void emptyDirYangScannerTest() throws IOException {
135
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530136 String emptyYangDir = baseDir + separator + "scanner1";
b.janani68c55e12016-02-24 12:23:03 +0530137 File path = createDirectory(emptyYangDir);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530138 List<String> emptyDirContents = getYangFiles(path.toString());
b.janani68c55e12016-02-24 12:23:03 +0530139 List<String> expectedContents = new LinkedList<>();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530140 assertThat(true, is(emptyDirContents.equals(expectedContents)));
b.janani68c55e12016-02-24 12:23:03 +0530141 }
142
143 /**
144 * This test case checks with the sub directories in the given path for java files.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530145 *
146 * @throws IOException when fails to do IO operations
b.janani68c55e12016-02-24 12:23:03 +0530147 */
148 @Test
149 public void emptySubDirScannerTest() throws IOException {
150
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530151 String dir = baseDir + separator + "scanner3";
b.janani68c55e12016-02-24 12:23:03 +0530152 File path = createDirectory(dir);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530153 String subDir = path.toString() + separator + "subDir1";
b.janani68c55e12016-02-24 12:23:03 +0530154 createDirectory(subDir);
155 createFile(path, "invalidFile.txt");
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530156 List<String> emptySubDirContents = getJavaFiles(path.toString());
b.janani68c55e12016-02-24 12:23:03 +0530157 List<String> expectedContents = new LinkedList<>();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530158 assertThat(true, is(emptySubDirContents.equals(expectedContents)));
b.janani68c55e12016-02-24 12:23:03 +0530159 }
160
Bharat saraswal2f11f652016-03-25 18:19:46 +0530161}