blob: 7d199b9a73ddb6f8a8ddfcb92f6b7ef48714fb7f [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;
Vidyashree Rama1db15562016-05-17 16:16:15 +053029import org.onosproject.yangutils.plugin.manager.YangFileInfo;
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 +053036
Bharat saraswal6ef0b762016-04-05 12:45:45 +053037import static java.io.File.separator;
38
janani b5c12efc2016-02-08 18:56:13 +053039/**
b.janani68c55e12016-02-24 12:23:03 +053040 * Test the file scanner service.
janani b5c12efc2016-02-08 18:56:13 +053041 */
b.janani68c55e12016-02-24 12:23:03 +053042public final class YangFileScannerTest {
janani b5c12efc2016-02-08 18:56:13 +053043
b.janani68c55e12016-02-24 12:23:03 +053044 @Rule
45 public ExpectedException thrown = ExpectedException.none();
46
47 String baseDir = "target/UnitTestCase";
janani b5c12efc2016-02-08 18:56:13 +053048
49 /**
b.janani68c55e12016-02-24 12:23:03 +053050 * A private constructor is tested.
51 *
Vinod Kumar S38046502016-03-23 15:30:27 +053052 * @throws SecurityException if any security violation is observed
53 * @throws NoSuchMethodException if when the method is not found
54 * @throws IllegalArgumentException if there is illegal argument found
55 * @throws InstantiationException if instantiation is provoked for the private constructor
56 * @throws IllegalAccessException if instance is provoked or a method is provoked
57 * @throws InvocationTargetException when an exception occurs by the method or constructor
janani b5c12efc2016-02-08 18:56:13 +053058 */
59 @Test
b.janani68c55e12016-02-24 12:23:03 +053060 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Bharat saraswal2f11f652016-03-25 18:19:46 +053061 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani68c55e12016-02-24 12:23:03 +053062
63 Class<?>[] classesToConstruct = {YangFileScanner.class };
64 for (Class<?> clazz : classesToConstruct) {
65 Constructor<?> constructor = clazz.getDeclaredConstructor();
66 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053067 assertThat(null, not(constructor.newInstance()));
janani b5c12efc2016-02-08 18:56:13 +053068 }
69 }
70
71 /**
b.janani68c55e12016-02-24 12:23:03 +053072 * This test case checks for a .java file inside the specified dir.
Bharat saraswal6ef0b762016-04-05 12:45:45 +053073 *
74 * @throws IOException when fails to do IO operations
janani b5c12efc2016-02-08 18:56:13 +053075 */
76 @Test
b.janani68c55e12016-02-24 12:23:03 +053077 public void checkJavaFileInsideDirTest() throws IOException {
janani b5c12efc2016-02-08 18:56:13 +053078
Bharat saraswal6ef0b762016-04-05 12:45:45 +053079 String dir = baseDir + separator + "scanner2";
b.janani68c55e12016-02-24 12:23:03 +053080 File path = createDirectory(dir);
81 createFile(path, "testScanner.java");
Bharat saraswal6ef0b762016-04-05 12:45:45 +053082 List<String> dirContents = getJavaFiles(path.toString());
b.janani68c55e12016-02-24 12:23:03 +053083 List<String> expectedContents = new LinkedList<>();
Bharat saraswal6ef0b762016-04-05 12:45:45 +053084 expectedContents.add(path.getCanonicalPath() + separator + "testScanner.java");
85 assertThat(true, is(dirContents.equals(expectedContents)));
janani b5c12efc2016-02-08 18:56:13 +053086 }
87
88 /**
89 * Method used for creating multiple directories inside the target file.
90 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053091 * @param path where directories should be created
92 * @return the directory path that is created
janani b5c12efc2016-02-08 18:56:13 +053093 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +053094 private File createDirectory(String path) {
b.janani68c55e12016-02-24 12:23:03 +053095
96 File myDir = new File(path);
janani b5c12efc2016-02-08 18:56:13 +053097 myDir.mkdirs();
98 return myDir;
99 }
100
101 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530102 * Method used for creating file inside the specified directory.
103 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530104 * @param myDir the path where file has to be created inside
105 * @param fileName the name of the file to be created
Bharat saraswal870c56f2016-02-20 21:57:16 +0530106 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530107 private void createFile(File myDir, String fileName) throws IOException {
b.janani68c55e12016-02-24 12:23:03 +0530108
janani b5c12efc2016-02-08 18:56:13 +0530109 File file = null;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530110 file = new File(myDir + separator + fileName);
b.janani68c55e12016-02-24 12:23:03 +0530111 file.createNewFile();
janani b5c12efc2016-02-08 18:56:13 +0530112 }
b.janani68c55e12016-02-24 12:23:03 +0530113
114 /**
115 * This testcase checks for a java file inside an empty directory.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530116 *
117 * @throws IOException when fails to do IO operations
b.janani68c55e12016-02-24 12:23:03 +0530118 */
119 @Test
120 public void emptyDirJavaScannerTest() throws IOException {
121
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530122 String emptyDir = baseDir + separator + "scanner1";
b.janani68c55e12016-02-24 12:23:03 +0530123 File path = createDirectory(emptyDir);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530124 List<String> emptyDirContents = getJavaFiles(path.toString());
b.janani68c55e12016-02-24 12:23:03 +0530125 List<String> expectedContents = new LinkedList<>();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530126 assertThat(true, is(emptyDirContents.equals(expectedContents)));
b.janani68c55e12016-02-24 12:23:03 +0530127 }
128
129 /**
130 * This testcase checks for a yang file inside an empty directory.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530131 *
132 * @throws IOException when fails to do IO operations
b.janani68c55e12016-02-24 12:23:03 +0530133 */
134 @Test
135 public void emptyDirYangScannerTest() throws IOException {
136
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530137 String emptyYangDir = baseDir + separator + "scanner1";
b.janani68c55e12016-02-24 12:23:03 +0530138 File path = createDirectory(emptyYangDir);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530139 List<YangFileInfo> emptyDirContents = getYangFiles(path.toString());
b.janani68c55e12016-02-24 12:23:03 +0530140 List<String> expectedContents = new LinkedList<>();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530141 assertThat(true, is(emptyDirContents.equals(expectedContents)));
b.janani68c55e12016-02-24 12:23:03 +0530142 }
143
144 /**
145 * This test case checks with the sub directories in the given path for java files.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530146 *
147 * @throws IOException when fails to do IO operations
b.janani68c55e12016-02-24 12:23:03 +0530148 */
149 @Test
150 public void emptySubDirScannerTest() throws IOException {
151
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530152 String dir = baseDir + separator + "scanner3";
b.janani68c55e12016-02-24 12:23:03 +0530153 File path = createDirectory(dir);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530154 String subDir = path.toString() + separator + "subDir1";
b.janani68c55e12016-02-24 12:23:03 +0530155 createDirectory(subDir);
156 createFile(path, "invalidFile.txt");
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530157 List<String> emptySubDirContents = getJavaFiles(path.toString());
b.janani68c55e12016-02-24 12:23:03 +0530158 List<String> expectedContents = new LinkedList<>();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530159 assertThat(true, is(emptySubDirContents.equals(expectedContents)));
b.janani68c55e12016-02-24 12:23:03 +0530160 }
161
Bharat saraswal2f11f652016-03-25 18:19:46 +0530162}