Unit Test Cases For YANG Io
Change-Id: Ie8876c25e4a293c52ae4c135921b7fe168f5f7c1
diff --git a/utils/yangutils/src/test/java/org/onosproject/yangutils/utils/io/impl/YangFileScannerTest.java b/utils/yangutils/src/test/java/org/onosproject/yangutils/utils/io/impl/YangFileScannerTest.java
index 6ead5e3..6441b19 100644
--- a/utils/yangutils/src/test/java/org/onosproject/yangutils/utils/io/impl/YangFileScannerTest.java
+++ b/utils/yangutils/src/test/java/org/onosproject/yangutils/utils/io/impl/YangFileScannerTest.java
@@ -16,189 +16,80 @@
package org.onosproject.yangutils.utils.io.impl;
+import org.junit.Test;
+import org.junit.Rule;
+import org.junit.rules.ExpectedException;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.LinkedList;
+import java.util.List;
import java.io.IOException;
-import org.junit.Test;
-import java.io.File;
-import java.util.List;
import org.slf4j.Logger;
import static org.slf4j.LoggerFactory.getLogger;
/**
- * Unit tests for searching yang files.
+ * Test the file scanner service.
*/
-public class YangFileScannerTest {
+public final class YangFileScannerTest {
private final Logger log = getLogger(getClass());
- private static final String BASEDIR = "target/UnitTestCase";
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ String baseDir = "target/UnitTestCase";
/**
- * Checks an empty directory.
+ * A private constructor is tested.
+ *
+ * @throws SecurityException if any security violation is observed.
+ * @throws NoSuchMethodException if when the method is not found.
+ * @throws IllegalArgumentException if there is illegal argument found.
+ * @throws InstantiationException if instantiation is provoked for the private constructor.
+ * @throws IllegalAccessException if instance is provoked or a method is provoked.
+ * @throws InvocationTargetException when an exception occurs by the method or constructor.
*/
@Test
- public void testWithSingleEmptyDirectoryInRoot() {
- try {
- File dir = new File(BASEDIR);
- dir.mkdirs();
- List<String> list = YangFileScanner.getYangFiles(BASEDIR.toString());
- } catch (IOException e) {
- log.info("IO Exception throwed");
+ public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
+ InstantiationException, IllegalAccessException, InvocationTargetException {
+
+ Class<?>[] classesToConstruct = {YangFileScanner.class };
+ for (Class<?> clazz : classesToConstruct) {
+ Constructor<?> constructor = clazz.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ assertNotNull(constructor.newInstance());
}
}
/**
- * Checks multiple empty directories in root directory.
+ * This test case checks for a .java file inside the specified dir.
*/
@Test
- public void testWithMultiEmptyDirectoriesInRoot() {
- try {
- String dir = "emptyDir";
- String dir1 = "emptyDir1";
- String dir2 = "emptyDir2";
- String dir3 = "emptyDir3";
- String dir4 = "emptyDir4";
- File firstpath = createDirectory(dir);
- File firstpath1 = createDirectory(dir1);
- File firstpath2 = createDirectory(dir2);
- File firstpath3 = createDirectory(dir3);
- File firstpath4 = createDirectory(dir4);
- List<String> list = YangFileScanner.getYangFiles(BASEDIR.toString());
- } catch (IOException e) {
- log.info("IO Exception throwed");
- }
- }
+ public void checkJavaFileInsideDirTest() throws IOException {
- /**
- * Checks one directory with one .yang file.
- */
- @Test
- public void testWithSingleDirectorySingleFileInRoot() {
- try {
- String dir1 = "level1";
- String firstFileName1 = "secondFile.yang";
- File firstpath1 = createDirectory(dir1);
- createFile(firstpath1, firstFileName1);
- List<String> list = YangFileScanner.getYangFiles(BASEDIR.toString());
- } catch (IOException e) {
- log.info("IO Exception throwed");
- }
- }
-
- /**
- * Checks one directory with many .yang file.
- */
- @Test
- public void testWithSingleDirectoryMultiFilesInRoot() {
- try {
- String dir2 = "level2";
- String firstFileName2 = "thirdFile.yang";
- String firstFileName3 = "fourthFile.yang";
- String firstFileName4 = "fifthFile.yang";
- String firstFileName5 = "sixthFile.yang";
- File firstpath2 = createDirectory(dir2);
- createFile(firstpath2, firstFileName2);
- createFile(firstpath2, firstFileName3);
- createFile(firstpath2, firstFileName4);
- createFile(firstpath2, firstFileName5);
- List<String> list = YangFileScanner.getYangFiles(BASEDIR.toString());
- } catch (IOException e) {
- log.info("IO Exception throwed");
- }
- }
-
- /**
- * Checks multi directories with many .yang file.
- */
- @Test
- public void testWithMultiDirectoriesMultiFiles() {
- try {
- String dir2 = "newDir1/newDir2/newDir3/newDir4";
- File dir3 = new File("target/UnitTestCase/newDir1");
- File dir4 = new File("target/UnitTestCase/newDir1/newDir2");
- File dir5 = new File("target/UnitTestCase/newDir1/newDir2/newDir3");
- File dir6 = new File("target/UnitTestCase/newDir1/newDir2/newDir3/newDir4");
- String firstFileName2 = "thirdFile.yang";
- String firstFileName3 = "fourthFile.yang";
- String firstFileName4 = "fifthFile.yang";
- String firstFileName5 = "sixthFile.yang";
- File firstpath2 = createDirectory(dir2);
- createFile(firstpath2, firstFileName2);
- createFile(firstpath2, firstFileName3);
- createFile(firstpath2, firstFileName4);
- createFile(dir3, firstFileName5);
- createFile(dir3, firstFileName2);
- createFile(dir3, firstFileName3);
- createFile(dir3, firstFileName4);
- createFile(dir3, firstFileName5);
- createFile(dir4, firstFileName2);
- createFile(dir4, firstFileName3);
- createFile(dir4, firstFileName4);
- createFile(dir4, firstFileName5);
- createFile(dir5, firstFileName2);
- createFile(dir5, firstFileName3);
- createFile(dir5, firstFileName4);
- createFile(dir5, firstFileName5);
- createFile(dir6, firstFileName2);
- createFile(dir6, firstFileName3);
- createFile(dir6, firstFileName4);
- createFile(dir6, firstFileName5);
- List<String> list = YangFileScanner.getYangFiles(BASEDIR.toString());
- } catch (IOException e) {
- log.info("IO Exception throwed");
- }
- }
-
- /**
- * Checks multi directories with many .java file.
- */
- @Test
- public void testWithMultiDirectoriesMultiJavaFiles() {
- try {
- String dir2 = "newDir1/newDir2/newDir3/newDir4";
- File dir3 = new File("target/UnitTestCase/newDir1");
- File dir4 = new File("target/UnitTestCase/newDir1/newDir2");
- File dir5 = new File("target/UnitTestCase/newDir1/newDir2/newDir3");
- File dir6 = new File("target/UnitTestCase/newDir1/newDir2/newDir3/newDir4");
- String firstFileName2 = "thirdFile.java";
- String firstFileName3 = "fourthFile.java";
- String firstFileName4 = "fifthFile.java";
- String firstFileName5 = "sixthFile.java";
- File firstpath2 = createDirectory(dir2);
- createFile(firstpath2, firstFileName2);
- createFile(firstpath2, firstFileName3);
- createFile(firstpath2, firstFileName4);
- createFile(dir3, firstFileName5);
- createFile(dir3, firstFileName2);
- createFile(dir3, firstFileName3);
- createFile(dir3, firstFileName4);
- createFile(dir3, firstFileName5);
- createFile(dir4, firstFileName2);
- createFile(dir4, firstFileName3);
- createFile(dir4, firstFileName4);
- createFile(dir4, firstFileName5);
- createFile(dir5, firstFileName2);
- createFile(dir5, firstFileName3);
- createFile(dir5, firstFileName4);
- createFile(dir5, firstFileName5);
- createFile(dir6, firstFileName2);
- createFile(dir6, firstFileName3);
- createFile(dir6, firstFileName4);
- createFile(dir6, firstFileName5);
- List<String> list = YangFileScanner.getJavaFiles(BASEDIR.toString());
- } catch (IOException e) {
- log.info("IO Exception throwed");
- }
+ String dir = baseDir + File.separator + "scanner2";
+ File path = createDirectory(dir);
+ createFile(path, "testScanner.java");
+ List<String> dirContents = YangFileScanner.getJavaFiles(path.toString());
+ List<String> expectedContents = new LinkedList<>();
+ expectedContents.add(path.getCanonicalPath() + File.separator + "testScanner.java");
+ assertEquals(dirContents, expectedContents);
}
/**
* Method used for creating multiple directories inside the target file.
*
- * @param path directory path
- * @return directory path
+ * @param path where directories should be created.
+ * @return
*/
public File createDirectory(String path) {
- File myDir = new File(BASEDIR + File.separator + path);
+
+ File myDir = new File(path);
myDir.mkdirs();
return myDir;
}
@@ -206,17 +97,71 @@
/**
* Method used for creating file inside the specified directory.
*
- * @param myDir my current dirctory
- * @param fileName file name
- * @throws IOException io exception when fails to create a file.
+ * @param myDir the path where file has to be created inside.
+ * @param fileName the name of the file to be created.
*/
public void createFile(File myDir, String fileName) throws IOException {
+
File file = null;
- try {
- file = new File(myDir + File.separator + fileName);
- file.createNewFile();
- } catch (final IOException e) {
- throw new IOException("IOException occured");
- }
+ file = new File(myDir + File.separator + fileName);
+ file.createNewFile();
}
-}
+
+ /**
+ * This testcase checks for a java file inside an empty directory.
+ */
+ @Test
+ public void emptyDirJavaScannerTest() throws IOException {
+
+ String emptyDir = baseDir + File.separator + "scanner1";
+ File path = createDirectory(emptyDir);
+ List<String> emptyDirContents = YangFileScanner.getJavaFiles(path.toString());
+ List<String> expectedContents = new LinkedList<>();
+ assertEquals(emptyDirContents, expectedContents);
+ }
+
+ /**
+ * This testcase checks for a yang file inside an empty directory.
+ */
+ @Test
+ public void emptyDirYangScannerTest() throws IOException {
+
+ String emptyYangDir = baseDir + File.separator + "scanner1";
+ File path = createDirectory(emptyYangDir);
+ List<String> emptyDirContents = YangFileScanner.getYangFiles(path.toString());
+ List<String> expectedContents = new LinkedList<>();
+ assertEquals(emptyDirContents, expectedContents);
+ }
+
+ /**
+ * This test case checks with the sub directories in the given path for java files.
+ */
+ @Test
+ public void emptySubDirScannerTest() throws IOException {
+
+ String dir = baseDir + File.separator + "scanner3";
+ File path = createDirectory(dir);
+ String subDir = path.toString() + File.separator + "subDir1";
+ createDirectory(subDir);
+ createFile(path, "invalidFile.txt");
+ List<String> emptySubDirContents = YangFileScanner.getJavaFiles(path.toString());
+ List<String> expectedContents = new LinkedList<>();
+ assertEquals(emptySubDirContents, expectedContents);
+ }
+
+ /**
+ * This test case checks with the sub directories in the given path for java files.
+ */
+ @Test
+ public void exceptionHandleTest() throws IOException {
+
+ String dir = baseDir + File.separator + "scanner4";
+ thrown.expect(IOException.class);
+ thrown.expectMessage("NullPointerException occured");
+ List<String> invalidContents = YangFileScanner.getJavaFiles(dir);
+ File path = createDirectory(dir);
+ createFile(path, "except.java");
+ List<String> dirWithFileName = YangFileScanner
+ .getJavaFiles(path + File.separator + "except.java" + File.separator + "scanner5");
+ }
+}
\ No newline at end of file