blob: 6441b196731da41167df8a8cedfaaa53432b3187 [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 org.junit.Test;
20import org.junit.Rule;
21import org.junit.rules.ExpectedException;
22import static org.junit.Assert.assertNotNull;
23import static org.junit.Assert.assertEquals;
24
25import java.io.File;
26import java.lang.reflect.Constructor;
27import java.lang.reflect.InvocationTargetException;
28import java.util.LinkedList;
29import java.util.List;
janani b5c12efc2016-02-08 18:56:13 +053030import java.io.IOException;
31
janani b5c12efc2016-02-08 18:56:13 +053032import org.slf4j.Logger;
33import static org.slf4j.LoggerFactory.getLogger;
34
35/**
b.janani68c55e12016-02-24 12:23:03 +053036 * Test the file scanner service.
janani b5c12efc2016-02-08 18:56:13 +053037 */
b.janani68c55e12016-02-24 12:23:03 +053038public final class YangFileScannerTest {
janani b5c12efc2016-02-08 18:56:13 +053039
40 private final Logger log = getLogger(getClass());
41
b.janani68c55e12016-02-24 12:23:03 +053042 @Rule
43 public ExpectedException thrown = ExpectedException.none();
44
45 String baseDir = "target/UnitTestCase";
janani b5c12efc2016-02-08 18:56:13 +053046
47 /**
b.janani68c55e12016-02-24 12:23:03 +053048 * A private constructor is tested.
49 *
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 b5c12efc2016-02-08 18:56:13 +053056 */
57 @Test
b.janani68c55e12016-02-24 12:23:03 +053058 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 b5c12efc2016-02-08 18:56:13 +053066 }
67 }
68
69 /**
b.janani68c55e12016-02-24 12:23:03 +053070 * This test case checks for a .java file inside the specified dir.
janani b5c12efc2016-02-08 18:56:13 +053071 */
72 @Test
b.janani68c55e12016-02-24 12:23:03 +053073 public void checkJavaFileInsideDirTest() throws IOException {
janani b5c12efc2016-02-08 18:56:13 +053074
b.janani68c55e12016-02-24 12:23:03 +053075 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 b5c12efc2016-02-08 18:56:13 +053082 }
83
84 /**
85 * Method used for creating multiple directories inside the target file.
86 *
b.janani68c55e12016-02-24 12:23:03 +053087 * @param path where directories should be created.
88 * @return
janani b5c12efc2016-02-08 18:56:13 +053089 */
90 public File createDirectory(String path) {
b.janani68c55e12016-02-24 12:23:03 +053091
92 File myDir = new File(path);
janani b5c12efc2016-02-08 18:56:13 +053093 myDir.mkdirs();
94 return myDir;
95 }
96
97 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +053098 * Method used for creating file inside the specified directory.
99 *
b.janani68c55e12016-02-24 12:23:03 +0530100 * @param myDir the path where file has to be created inside.
101 * @param fileName the name of the file to be created.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530102 */
janani b5c12efc2016-02-08 18:56:13 +0530103 public void createFile(File myDir, String fileName) throws IOException {
b.janani68c55e12016-02-24 12:23:03 +0530104
janani b5c12efc2016-02-08 18:56:13 +0530105 File file = null;
b.janani68c55e12016-02-24 12:23:03 +0530106 file = new File(myDir + File.separator + fileName);
107 file.createNewFile();
janani b5c12efc2016-02-08 18:56:13 +0530108 }
b.janani68c55e12016-02-24 12:23:03 +0530109
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}