blob: 6ead5e368c2586b9a6867fec2e0f27988f5c6950 [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
19import java.io.IOException;
20
21import org.junit.Test;
22import java.io.File;
23import java.util.List;
24import org.slf4j.Logger;
25import static org.slf4j.LoggerFactory.getLogger;
26
27/**
28 * Unit tests for searching yang files.
29 */
30public class YangFileScannerTest {
31
32 private final Logger log = getLogger(getClass());
33
Bharat saraswal870c56f2016-02-20 21:57:16 +053034 private static final String BASEDIR = "target/UnitTestCase";
janani b5c12efc2016-02-08 18:56:13 +053035
36 /**
37 * Checks an empty directory.
38 */
39 @Test
40 public void testWithSingleEmptyDirectoryInRoot() {
41 try {
Bharat saraswal870c56f2016-02-20 21:57:16 +053042 File dir = new File(BASEDIR);
janani b5c12efc2016-02-08 18:56:13 +053043 dir.mkdirs();
Bharat saraswal870c56f2016-02-20 21:57:16 +053044 List<String> list = YangFileScanner.getYangFiles(BASEDIR.toString());
janani b5c12efc2016-02-08 18:56:13 +053045 } catch (IOException e) {
46 log.info("IO Exception throwed");
47 }
48 }
49
50 /**
51 * Checks multiple empty directories in root directory.
52 */
53 @Test
54 public void testWithMultiEmptyDirectoriesInRoot() {
55 try {
56 String dir = "emptyDir";
57 String dir1 = "emptyDir1";
58 String dir2 = "emptyDir2";
59 String dir3 = "emptyDir3";
60 String dir4 = "emptyDir4";
61 File firstpath = createDirectory(dir);
62 File firstpath1 = createDirectory(dir1);
63 File firstpath2 = createDirectory(dir2);
64 File firstpath3 = createDirectory(dir3);
65 File firstpath4 = createDirectory(dir4);
Bharat saraswal870c56f2016-02-20 21:57:16 +053066 List<String> list = YangFileScanner.getYangFiles(BASEDIR.toString());
janani b5c12efc2016-02-08 18:56:13 +053067 } catch (IOException e) {
68 log.info("IO Exception throwed");
69 }
70 }
71
72 /**
73 * Checks one directory with one .yang file.
74 */
75 @Test
76 public void testWithSingleDirectorySingleFileInRoot() {
77 try {
78 String dir1 = "level1";
79 String firstFileName1 = "secondFile.yang";
80 File firstpath1 = createDirectory(dir1);
81 createFile(firstpath1, firstFileName1);
Bharat saraswal870c56f2016-02-20 21:57:16 +053082 List<String> list = YangFileScanner.getYangFiles(BASEDIR.toString());
janani b5c12efc2016-02-08 18:56:13 +053083 } catch (IOException e) {
84 log.info("IO Exception throwed");
85 }
86 }
87
88 /**
89 * Checks one directory with many .yang file.
90 */
91 @Test
92 public void testWithSingleDirectoryMultiFilesInRoot() {
93 try {
94 String dir2 = "level2";
95 String firstFileName2 = "thirdFile.yang";
96 String firstFileName3 = "fourthFile.yang";
97 String firstFileName4 = "fifthFile.yang";
98 String firstFileName5 = "sixthFile.yang";
99 File firstpath2 = createDirectory(dir2);
100 createFile(firstpath2, firstFileName2);
101 createFile(firstpath2, firstFileName3);
102 createFile(firstpath2, firstFileName4);
103 createFile(firstpath2, firstFileName5);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530104 List<String> list = YangFileScanner.getYangFiles(BASEDIR.toString());
janani b5c12efc2016-02-08 18:56:13 +0530105 } catch (IOException e) {
106 log.info("IO Exception throwed");
107 }
108 }
109
110 /**
111 * Checks multi directories with many .yang file.
112 */
113 @Test
114 public void testWithMultiDirectoriesMultiFiles() {
115 try {
116 String dir2 = "newDir1/newDir2/newDir3/newDir4";
117 File dir3 = new File("target/UnitTestCase/newDir1");
118 File dir4 = new File("target/UnitTestCase/newDir1/newDir2");
119 File dir5 = new File("target/UnitTestCase/newDir1/newDir2/newDir3");
120 File dir6 = new File("target/UnitTestCase/newDir1/newDir2/newDir3/newDir4");
121 String firstFileName2 = "thirdFile.yang";
122 String firstFileName3 = "fourthFile.yang";
123 String firstFileName4 = "fifthFile.yang";
124 String firstFileName5 = "sixthFile.yang";
125 File firstpath2 = createDirectory(dir2);
126 createFile(firstpath2, firstFileName2);
127 createFile(firstpath2, firstFileName3);
128 createFile(firstpath2, firstFileName4);
129 createFile(dir3, firstFileName5);
130 createFile(dir3, firstFileName2);
131 createFile(dir3, firstFileName3);
132 createFile(dir3, firstFileName4);
133 createFile(dir3, firstFileName5);
134 createFile(dir4, firstFileName2);
135 createFile(dir4, firstFileName3);
136 createFile(dir4, firstFileName4);
137 createFile(dir4, firstFileName5);
138 createFile(dir5, firstFileName2);
139 createFile(dir5, firstFileName3);
140 createFile(dir5, firstFileName4);
141 createFile(dir5, firstFileName5);
142 createFile(dir6, firstFileName2);
143 createFile(dir6, firstFileName3);
144 createFile(dir6, firstFileName4);
145 createFile(dir6, firstFileName5);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530146 List<String> list = YangFileScanner.getYangFiles(BASEDIR.toString());
147 } catch (IOException e) {
148 log.info("IO Exception throwed");
149 }
150 }
151
152 /**
153 * Checks multi directories with many .java file.
154 */
155 @Test
156 public void testWithMultiDirectoriesMultiJavaFiles() {
157 try {
158 String dir2 = "newDir1/newDir2/newDir3/newDir4";
159 File dir3 = new File("target/UnitTestCase/newDir1");
160 File dir4 = new File("target/UnitTestCase/newDir1/newDir2");
161 File dir5 = new File("target/UnitTestCase/newDir1/newDir2/newDir3");
162 File dir6 = new File("target/UnitTestCase/newDir1/newDir2/newDir3/newDir4");
163 String firstFileName2 = "thirdFile.java";
164 String firstFileName3 = "fourthFile.java";
165 String firstFileName4 = "fifthFile.java";
166 String firstFileName5 = "sixthFile.java";
167 File firstpath2 = createDirectory(dir2);
168 createFile(firstpath2, firstFileName2);
169 createFile(firstpath2, firstFileName3);
170 createFile(firstpath2, firstFileName4);
171 createFile(dir3, firstFileName5);
172 createFile(dir3, firstFileName2);
173 createFile(dir3, firstFileName3);
174 createFile(dir3, firstFileName4);
175 createFile(dir3, firstFileName5);
176 createFile(dir4, firstFileName2);
177 createFile(dir4, firstFileName3);
178 createFile(dir4, firstFileName4);
179 createFile(dir4, firstFileName5);
180 createFile(dir5, firstFileName2);
181 createFile(dir5, firstFileName3);
182 createFile(dir5, firstFileName4);
183 createFile(dir5, firstFileName5);
184 createFile(dir6, firstFileName2);
185 createFile(dir6, firstFileName3);
186 createFile(dir6, firstFileName4);
187 createFile(dir6, firstFileName5);
188 List<String> list = YangFileScanner.getJavaFiles(BASEDIR.toString());
janani b5c12efc2016-02-08 18:56:13 +0530189 } catch (IOException e) {
190 log.info("IO Exception throwed");
191 }
192 }
193
194 /**
195 * Method used for creating multiple directories inside the target file.
196 *
197 * @param path directory path
198 * @return directory path
199 */
200 public File createDirectory(String path) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530201 File myDir = new File(BASEDIR + File.separator + path);
janani b5c12efc2016-02-08 18:56:13 +0530202 myDir.mkdirs();
203 return myDir;
204 }
205
206 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530207 * Method used for creating file inside the specified directory.
208 *
209 * @param myDir my current dirctory
210 * @param fileName file name
211 * @throws IOException io exception when fails to create a file.
212 */
janani b5c12efc2016-02-08 18:56:13 +0530213 public void createFile(File myDir, String fileName) throws IOException {
214 File file = null;
215 try {
216 file = new File(myDir + File.separator + fileName);
217 file.createNewFile();
218 } catch (final IOException e) {
219 throw new IOException("IOException occured");
220 }
221 }
222}