Unit Test Cases For YANG Io
Change-Id: Ie8876c25e4a293c52ae4c135921b7fe168f5f7c1
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtil.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtil.java
index f7e3556..58dfb69 100644
--- a/src/main/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtil.java
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtil.java
@@ -45,13 +45,16 @@
* @param pkg Package to check if it is created.
* @return existence status of package.
*/
- public static boolean doesPackageExist(File pkg) {
- if (pkg.exists()) {
+ public static boolean doesPackageExist(String pkg) {
+ File pkgDir = new File(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
+ File pkgWithFile = new File(pkgDir + File.separator + "package-info.java");
+ if (pkgDir.exists() && pkgWithFile.isFile()) {
return true;
}
return false;
}
+
/**
* Create a package structure with package info java file if not present.
*
@@ -60,7 +63,7 @@
* @throws IOException any IO exception
*/
public static void createPackage(String pkg, String pkgInfo) throws IOException {
- if (!doesPackageExist(new File(pkg))) {
+ if (!doesPackageExist(pkg)) {
try {
File pack = YangIoUtils
.createDirectories(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
@@ -82,9 +85,6 @@
*/
public static CachedFileHandle createSourceFiles(String pkg, String yangName, GeneratedFileType types)
throws IOException {
- //if (!doesPackageExist(new File(pkg))) {
- // throw new IOException("package does not exist.");
- //}
yangName = JavaIdentifierSyntax.getCamelCase(yangName);
CachedFileHandle handler = new CachedJavaFileHandle(pkg, yangName, types);
@@ -104,7 +104,6 @@
public static void appendFileContents(File toAppend, File srcFile) throws IOException {
insertStringInFile(srcFile, UtilConstants.NEW_LINE + readAppendFile(toAppend.toString()));
- //TODO: read the contents from src file and append its contents to append file.
return;
}
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
index 826eb7b..19a130e 100644
--- a/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
@@ -114,7 +114,7 @@
javaDoc = generateForDefaultConstructors();
} else if (type.equals(JavaDocType.BUILD)) {
javaDoc = generateForBuild(name);
- } else if (type.equals(JavaDocType.CONSTRUCTOR)) {
+ } else {
javaDoc = generateForConstructors(name);
}
return javaDoc;
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStore.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStore.java
index d9d3732..9ceb50f 100644
--- a/src/main/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStore.java
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStore.java
@@ -19,6 +19,7 @@
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -107,6 +108,11 @@
private static final String SERIALIZE_FILE_EXTENSION = ".ser";
/**
+ * Directory for generating Serialized files.
+ */
+ private static final String GEN_DIR = "target/";
+
+ /**
* Buffer size.
*/
private static final int BUFFER_SIZE = 8 * 1024;
@@ -137,14 +143,12 @@
fileName = BUILDER_METHOD_FILE_NAME;
} else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) {
fileName = IMPL_METHOD_FILE_NAME;
- } else if (type.equals(SerializedDataStoreType.IMPORT)) {
- fileName = IMPORT_FILE_NAME;
} else {
- throw new IOException("Unresolved file type.");
+ fileName = IMPORT_FILE_NAME;
}
try {
- OutputStream file = new FileOutputStream(fileName + SERIALIZE_FILE_EXTENSION);
+ OutputStream file = new FileOutputStream(GEN_DIR + fileName + SERIALIZE_FILE_EXTENSION);
OutputStream buffer = new BufferedOutputStream(file, BUFFER_SIZE);
ObjectOutput output = new ObjectOutputStream(buffer);
@@ -164,10 +168,11 @@
* @param type type of serialized data store
* @return list of attribute info.
* @throws IOException when fails to read from the file.
- * @throws ClassNotFoundException when file is missing.
+ * @throws ClassNotFoundException when class is missing.
+ * @throws FileNotFoundException when file is missing.
*/
public static List<String> getSerializeData(SerializedDataStoreType type)
- throws IOException, ClassNotFoundException {
+ throws IOException, FileNotFoundException, ClassNotFoundException {
String fileName = "";
if (type.equals(SerializedDataStoreType.ATTRIBUTE)) {
@@ -180,14 +185,11 @@
fileName = BUILDER_METHOD_FILE_NAME;
} else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) {
fileName = IMPL_METHOD_FILE_NAME;
- } else if (type.equals(SerializedDataStoreType.IMPORT)) {
- fileName = IMPORT_FILE_NAME;
} else {
- throw new IOException("Unresolved file type.");
+ fileName = IMPORT_FILE_NAME;
}
-
try {
- InputStream file = new FileInputStream(fileName + SERIALIZE_FILE_EXTENSION);
+ InputStream file = new FileInputStream(GEN_DIR + fileName + SERIALIZE_FILE_EXTENSION);
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream(buffer);
try {
@@ -199,6 +201,8 @@
input.close();
file.close();
}
+ } catch (FileNotFoundException ex) {
+ throw new FileNotFoundException("No such file or directory.");
} catch (ClassNotFoundException ex) {
throw new ClassNotFoundException("failed to fetch the serialized data file.");
}
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/YangFileScanner.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/YangFileScanner.java
index a9006d2..1f8a159 100644
--- a/src/main/java/org/onosproject/yangutils/utils/io/impl/YangFileScanner.java
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/YangFileScanner.java
@@ -33,31 +33,34 @@
private YangFileScanner() {
}
- /**
- * Returns the list of YANG files.
- *
- * @param root specified directory
- * @return list of YANG files.
- * @throws IOException when files get deleted while performing the
- * operations.
- */
- public static List<String> getYangFiles(String root) throws IOException {
- return getFiles(root, ".yang");
- }
/**
* Returns the list of java files.
*
* @param root specified directory
* @return list of java files.
+ * @throws NullPointerException when no files are there.
* @throws IOException when files get deleted while performing the
* operations.
*/
- public static List<String> getJavaFiles(String root) throws IOException {
+ public static List<String> getJavaFiles(String root) throws NullPointerException, IOException {
return getFiles(root, ".java");
}
/**
+ * Returns the list of YANG files.
+ *
+ * @param root specified directory
+ * @return list of YANG files.
+ * @throws NullPointerException when no files are there.
+ * @throws IOException when files get deleted while performing the
+ * operations.
+ */
+ public static List<String> getYangFiles(String root) throws NullPointerException, IOException {
+ return getFiles(root, ".yang");
+ }
+
+ /**
* Returns the list of required files.
*
* @param root specified directory
@@ -66,7 +69,7 @@
* @throws IOException when files get deleted while performing the
* operations.
*/
- public static List<String> getFiles(String root, String extension) throws IOException {
+ public static List<String> getFiles(String root, String extension) throws NullPointerException, IOException {
List<String> store = new LinkedList<>();
Stack<String> stack = new Stack<>();
stack.push(root);
@@ -77,7 +80,7 @@
root = stack.pop();
file = new File(root);
filelist = file.listFiles();
- if (filelist == null) {
+ if (filelist.length == 0) {
continue;
}
for (File current : filelist) {
@@ -92,8 +95,8 @@
}
}
return store;
- } catch (IOException e) {
- throw new IOException("IOException occured");
+ } catch (NullPointerException e) {
+ throw new IOException("NullPointerException occured");
}
}
}
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
index 7f394db..8e97a7a 100644
--- a/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
@@ -53,7 +53,7 @@
*/
public static File createDirectories(String path) {
- File generatedDir = new File(UtilConstants.YANG_GEN_DIR + File.separator + path);
+ File generatedDir = new File(path);
generatedDir.mkdirs();
return generatedDir;
}
@@ -68,21 +68,23 @@
*/
public static void addPackageInfo(File path, String classInfo, String pack) throws IOException {
+ if (pack.contains(UtilConstants.YANG_GEN_DIR)) {
+ String[] strArray = pack.split(UtilConstants.YANG_GEN_DIR + UtilConstants.SLASH);
+ pack = strArray[1];
+ }
+
try {
File packageInfo = new File(path + File.separator + "package-info.java");
packageInfo.createNewFile();
- if (packageInfo.exists()) {
-
- FileWriter fileWriter = null;
- BufferedWriter bufferedWriter = null;
- fileWriter = new FileWriter(packageInfo);
- bufferedWriter = new BufferedWriter(fileWriter);
- bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
- bufferedWriter.write(JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.PACKAGE_INFO, classInfo));
- bufferedWriter.write(UtilConstants.PACKAGE + UtilConstants.SPACE + pack + UtilConstants.SEMI_COLAN);
- bufferedWriter.close();
- }
+ FileWriter fileWriter = null;
+ BufferedWriter bufferedWriter = null;
+ fileWriter = new FileWriter(packageInfo);
+ bufferedWriter = new BufferedWriter(fileWriter);
+ bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
+ bufferedWriter.write(JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.PACKAGE_INFO, classInfo));
+ bufferedWriter.write(UtilConstants.PACKAGE + UtilConstants.SPACE + pack + UtilConstants.SEMI_COLAN);
+ bufferedWriter.close();
} catch (IOException e) {
throw new IOException("Exception occured while creating package info file.");
}