blob: 6d1bedec57b3f12b16a428686be1ab3a5c046ee2 [file] [log] [blame]
janani b5c12efc2016-02-08 18:56:13 +05301/*
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
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;
janani b5c12efc2016-02-08 18:56:13 +053029import org.slf4j.Logger;
Bharat saraswal2f11f652016-03-25 18:19:46 +053030
Bharat saraswal6ef0b762016-04-05 12:45:45 +053031import static org.hamcrest.core.Is.is;
32import static org.hamcrest.core.IsNot.not;
33import static org.junit.Assert.assertThat;
34import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getJavaFiles;
35import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getYangFiles;
janani b5c12efc2016-02-08 18:56:13 +053036import static org.slf4j.LoggerFactory.getLogger;
37
Bharat saraswal6ef0b762016-04-05 12:45:45 +053038import static java.io.File.separator;
39
janani b5c12efc2016-02-08 18:56:13 +053040/**
b.janani68c55e12016-02-24 12:23:03 +053041 * Test the file scanner service.
janani b5c12efc2016-02-08 18:56:13 +053042 */
b.janani68c55e12016-02-24 12:23:03 +053043public final class YangFileScannerTest {
janani b5c12efc2016-02-08 18:56:13 +053044
45 private final Logger log = getLogger(getClass());
46
b.janani68c55e12016-02-24 12:23:03 +053047 @Rule
48 public ExpectedException thrown = ExpectedException.none();
49
50 String baseDir = "target/UnitTestCase";
janani b5c12efc2016-02-08 18:56:13 +053051
52 /**
b.janani68c55e12016-02-24 12:23:03 +053053 * A private constructor is tested.
54 *
Vinod Kumar S38046502016-03-23 15:30:27 +053055 * @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 b5c12efc2016-02-08 18:56:13 +053061 */
62 @Test
b.janani68c55e12016-02-24 12:23:03 +053063 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Bharat saraswal2f11f652016-03-25 18:19:46 +053064 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani68c55e12016-02-24 12:23:03 +053065
66 Class<?>[] classesToConstruct = {YangFileScanner.class };
67 for (Class<?> clazz : classesToConstruct) {
68 Constructor<?> constructor = clazz.getDeclaredConstructor();
69 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053070 assertThat(null, not(constructor.newInstance()));
janani b5c12efc2016-02-08 18:56:13 +053071 }
72 }
73
74 /**
b.janani68c55e12016-02-24 12:23:03 +053075 * This test case checks for a .java file inside the specified dir.
Bharat saraswal6ef0b762016-04-05 12:45:45 +053076 *
77 * @throws IOException when fails to do IO operations
janani b5c12efc2016-02-08 18:56:13 +053078 */
79 @Test
b.janani68c55e12016-02-24 12:23:03 +053080 public void checkJavaFileInsideDirTest() throws IOException {
janani b5c12efc2016-02-08 18:56:13 +053081
Bharat saraswal6ef0b762016-04-05 12:45:45 +053082 String dir = baseDir + separator + "scanner2";
b.janani68c55e12016-02-24 12:23:03 +053083 File path = createDirectory(dir);
84 createFile(path, "testScanner.java");
Bharat saraswal6ef0b762016-04-05 12:45:45 +053085 List<String> dirContents = getJavaFiles(path.toString());
b.janani68c55e12016-02-24 12:23:03 +053086 List<String> expectedContents = new LinkedList<>();
Bharat saraswal6ef0b762016-04-05 12:45:45 +053087 expectedContents.add(path.getCanonicalPath() + separator + "testScanner.java");
88 assertThat(true, is(dirContents.equals(expectedContents)));
janani b5c12efc2016-02-08 18:56:13 +053089 }
90
91 /**
92 * Method used for creating multiple directories inside the target file.
93 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053094 * @param path where directories should be created
95 * @return the directory path that is created
janani b5c12efc2016-02-08 18:56:13 +053096 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +053097 private File createDirectory(String path) {
b.janani68c55e12016-02-24 12:23:03 +053098
99 File myDir = new File(path);
janani b5c12efc2016-02-08 18:56:13 +0530100 myDir.mkdirs();
101 return myDir;
102 }
103
104 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530105 * Method used for creating file inside the specified directory.
106 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530107 * @param myDir the path where file has to be created inside
108 * @param fileName the name of the file to be created
Bharat saraswal870c56f2016-02-20 21:57:16 +0530109 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530110 private void createFile(File myDir, String fileName) throws IOException {
b.janani68c55e12016-02-24 12:23:03 +0530111
janani b5c12efc2016-02-08 18:56:13 +0530112 File file = null;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530113 file = new File(myDir + separator + fileName);
b.janani68c55e12016-02-24 12:23:03 +0530114 file.createNewFile();
janani b5c12efc2016-02-08 18:56:13 +0530115 }
b.janani68c55e12016-02-24 12:23:03 +0530116
117 /**
118 * This testcase checks for a java file inside an empty directory.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530119 *
120 * @throws IOException when fails to do IO operations
b.janani68c55e12016-02-24 12:23:03 +0530121 */
122 @Test
123 public void emptyDirJavaScannerTest() throws IOException {
124
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530125 String emptyDir = baseDir + separator + "scanner1";
b.janani68c55e12016-02-24 12:23:03 +0530126 File path = createDirectory(emptyDir);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530127 List<String> emptyDirContents = getJavaFiles(path.toString());
b.janani68c55e12016-02-24 12:23:03 +0530128 List<String> expectedContents = new LinkedList<>();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530129 assertThat(true, is(emptyDirContents.equals(expectedContents)));
b.janani68c55e12016-02-24 12:23:03 +0530130 }
131
132 /**
133 * This testcase checks for a yang file inside an empty directory.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530134 *
135 * @throws IOException when fails to do IO operations
b.janani68c55e12016-02-24 12:23:03 +0530136 */
137 @Test
138 public void emptyDirYangScannerTest() throws IOException {
139
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530140 String emptyYangDir = baseDir + separator + "scanner1";
b.janani68c55e12016-02-24 12:23:03 +0530141 File path = createDirectory(emptyYangDir);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530142 List<String> emptyDirContents = getYangFiles(path.toString());
b.janani68c55e12016-02-24 12:23:03 +0530143 List<String> expectedContents = new LinkedList<>();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530144 assertThat(true, is(emptyDirContents.equals(expectedContents)));
b.janani68c55e12016-02-24 12:23:03 +0530145 }
146
147 /**
148 * This test case checks with the sub directories in the given path for java files.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530149 *
150 * @throws IOException when fails to do IO operations
b.janani68c55e12016-02-24 12:23:03 +0530151 */
152 @Test
153 public void emptySubDirScannerTest() throws IOException {
154
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530155 String dir = baseDir + separator + "scanner3";
b.janani68c55e12016-02-24 12:23:03 +0530156 File path = createDirectory(dir);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530157 String subDir = path.toString() + separator + "subDir1";
b.janani68c55e12016-02-24 12:23:03 +0530158 createDirectory(subDir);
159 createFile(path, "invalidFile.txt");
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530160 List<String> emptySubDirContents = getJavaFiles(path.toString());
b.janani68c55e12016-02-24 12:23:03 +0530161 List<String> expectedContents = new LinkedList<>();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530162 assertThat(true, is(emptySubDirContents.equals(expectedContents)));
b.janani68c55e12016-02-24 12:23:03 +0530163 }
164
Bharat saraswal2f11f652016-03-25 18:19:46 +0530165}