b.janani
Committed by Gerrit Code Review

Unit Test Cases For YANG Io

Change-Id: Ie8876c25e4a293c52ae4c135921b7fe168f5f7c1
...@@ -45,13 +45,16 @@ public final class FileSystemUtil { ...@@ -45,13 +45,16 @@ public final class FileSystemUtil {
45 * @param pkg Package to check if it is created. 45 * @param pkg Package to check if it is created.
46 * @return existence status of package. 46 * @return existence status of package.
47 */ 47 */
48 - public static boolean doesPackageExist(File pkg) { 48 + public static boolean doesPackageExist(String pkg) {
49 - if (pkg.exists()) { 49 + File pkgDir = new File(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
50 + File pkgWithFile = new File(pkgDir + File.separator + "package-info.java");
51 + if (pkgDir.exists() && pkgWithFile.isFile()) {
50 return true; 52 return true;
51 } 53 }
52 return false; 54 return false;
53 } 55 }
54 56
57 +
55 /** 58 /**
56 * Create a package structure with package info java file if not present. 59 * Create a package structure with package info java file if not present.
57 * 60 *
...@@ -60,7 +63,7 @@ public final class FileSystemUtil { ...@@ -60,7 +63,7 @@ public final class FileSystemUtil {
60 * @throws IOException any IO exception 63 * @throws IOException any IO exception
61 */ 64 */
62 public static void createPackage(String pkg, String pkgInfo) throws IOException { 65 public static void createPackage(String pkg, String pkgInfo) throws IOException {
63 - if (!doesPackageExist(new File(pkg))) { 66 + if (!doesPackageExist(pkg)) {
64 try { 67 try {
65 File pack = YangIoUtils 68 File pack = YangIoUtils
66 .createDirectories(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH)); 69 .createDirectories(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
...@@ -82,9 +85,6 @@ public final class FileSystemUtil { ...@@ -82,9 +85,6 @@ public final class FileSystemUtil {
82 */ 85 */
83 public static CachedFileHandle createSourceFiles(String pkg, String yangName, GeneratedFileType types) 86 public static CachedFileHandle createSourceFiles(String pkg, String yangName, GeneratedFileType types)
84 throws IOException { 87 throws IOException {
85 - //if (!doesPackageExist(new File(pkg))) {
86 - // throw new IOException("package does not exist.");
87 - //}
88 yangName = JavaIdentifierSyntax.getCamelCase(yangName); 88 yangName = JavaIdentifierSyntax.getCamelCase(yangName);
89 CachedFileHandle handler = new CachedJavaFileHandle(pkg, yangName, types); 89 CachedFileHandle handler = new CachedJavaFileHandle(pkg, yangName, types);
90 90
...@@ -104,7 +104,6 @@ public final class FileSystemUtil { ...@@ -104,7 +104,6 @@ public final class FileSystemUtil {
104 public static void appendFileContents(File toAppend, File srcFile) throws IOException { 104 public static void appendFileContents(File toAppend, File srcFile) throws IOException {
105 105
106 insertStringInFile(srcFile, UtilConstants.NEW_LINE + readAppendFile(toAppend.toString())); 106 insertStringInFile(srcFile, UtilConstants.NEW_LINE + readAppendFile(toAppend.toString()));
107 - //TODO: read the contents from src file and append its contents to append file.
108 return; 107 return;
109 } 108 }
110 109
......
...@@ -114,7 +114,7 @@ public final class JavaDocGen { ...@@ -114,7 +114,7 @@ public final class JavaDocGen {
114 javaDoc = generateForDefaultConstructors(); 114 javaDoc = generateForDefaultConstructors();
115 } else if (type.equals(JavaDocType.BUILD)) { 115 } else if (type.equals(JavaDocType.BUILD)) {
116 javaDoc = generateForBuild(name); 116 javaDoc = generateForBuild(name);
117 - } else if (type.equals(JavaDocType.CONSTRUCTOR)) { 117 + } else {
118 javaDoc = generateForConstructors(name); 118 javaDoc = generateForConstructors(name);
119 } 119 }
120 return javaDoc; 120 return javaDoc;
......
...@@ -19,6 +19,7 @@ package org.onosproject.yangutils.utils.io.impl; ...@@ -19,6 +19,7 @@ package org.onosproject.yangutils.utils.io.impl;
19 import java.io.BufferedInputStream; 19 import java.io.BufferedInputStream;
20 import java.io.BufferedOutputStream; 20 import java.io.BufferedOutputStream;
21 import java.io.FileInputStream; 21 import java.io.FileInputStream;
22 +import java.io.FileNotFoundException;
22 import java.io.FileOutputStream; 23 import java.io.FileOutputStream;
23 import java.io.IOException; 24 import java.io.IOException;
24 import java.io.InputStream; 25 import java.io.InputStream;
...@@ -107,6 +108,11 @@ public final class SerializedDataStore { ...@@ -107,6 +108,11 @@ public final class SerializedDataStore {
107 private static final String SERIALIZE_FILE_EXTENSION = ".ser"; 108 private static final String SERIALIZE_FILE_EXTENSION = ".ser";
108 109
109 /** 110 /**
111 + * Directory for generating Serialized files.
112 + */
113 + private static final String GEN_DIR = "target/";
114 +
115 + /**
110 * Buffer size. 116 * Buffer size.
111 */ 117 */
112 private static final int BUFFER_SIZE = 8 * 1024; 118 private static final int BUFFER_SIZE = 8 * 1024;
...@@ -137,14 +143,12 @@ public final class SerializedDataStore { ...@@ -137,14 +143,12 @@ public final class SerializedDataStore {
137 fileName = BUILDER_METHOD_FILE_NAME; 143 fileName = BUILDER_METHOD_FILE_NAME;
138 } else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) { 144 } else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) {
139 fileName = IMPL_METHOD_FILE_NAME; 145 fileName = IMPL_METHOD_FILE_NAME;
140 - } else if (type.equals(SerializedDataStoreType.IMPORT)) {
141 - fileName = IMPORT_FILE_NAME;
142 } else { 146 } else {
143 - throw new IOException("Unresolved file type."); 147 + fileName = IMPORT_FILE_NAME;
144 } 148 }
145 149
146 try { 150 try {
147 - OutputStream file = new FileOutputStream(fileName + SERIALIZE_FILE_EXTENSION); 151 + OutputStream file = new FileOutputStream(GEN_DIR + fileName + SERIALIZE_FILE_EXTENSION);
148 OutputStream buffer = new BufferedOutputStream(file, BUFFER_SIZE); 152 OutputStream buffer = new BufferedOutputStream(file, BUFFER_SIZE);
149 153
150 ObjectOutput output = new ObjectOutputStream(buffer); 154 ObjectOutput output = new ObjectOutputStream(buffer);
...@@ -164,10 +168,11 @@ public final class SerializedDataStore { ...@@ -164,10 +168,11 @@ public final class SerializedDataStore {
164 * @param type type of serialized data store 168 * @param type type of serialized data store
165 * @return list of attribute info. 169 * @return list of attribute info.
166 * @throws IOException when fails to read from the file. 170 * @throws IOException when fails to read from the file.
167 - * @throws ClassNotFoundException when file is missing. 171 + * @throws ClassNotFoundException when class is missing.
172 + * @throws FileNotFoundException when file is missing.
168 */ 173 */
169 public static List<String> getSerializeData(SerializedDataStoreType type) 174 public static List<String> getSerializeData(SerializedDataStoreType type)
170 - throws IOException, ClassNotFoundException { 175 + throws IOException, FileNotFoundException, ClassNotFoundException {
171 176
172 String fileName = ""; 177 String fileName = "";
173 if (type.equals(SerializedDataStoreType.ATTRIBUTE)) { 178 if (type.equals(SerializedDataStoreType.ATTRIBUTE)) {
...@@ -180,14 +185,11 @@ public final class SerializedDataStore { ...@@ -180,14 +185,11 @@ public final class SerializedDataStore {
180 fileName = BUILDER_METHOD_FILE_NAME; 185 fileName = BUILDER_METHOD_FILE_NAME;
181 } else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) { 186 } else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) {
182 fileName = IMPL_METHOD_FILE_NAME; 187 fileName = IMPL_METHOD_FILE_NAME;
183 - } else if (type.equals(SerializedDataStoreType.IMPORT)) {
184 - fileName = IMPORT_FILE_NAME;
185 } else { 188 } else {
186 - throw new IOException("Unresolved file type."); 189 + fileName = IMPORT_FILE_NAME;
187 } 190 }
188 -
189 try { 191 try {
190 - InputStream file = new FileInputStream(fileName + SERIALIZE_FILE_EXTENSION); 192 + InputStream file = new FileInputStream(GEN_DIR + fileName + SERIALIZE_FILE_EXTENSION);
191 InputStream buffer = new BufferedInputStream(file); 193 InputStream buffer = new BufferedInputStream(file);
192 ObjectInput input = new ObjectInputStream(buffer); 194 ObjectInput input = new ObjectInputStream(buffer);
193 try { 195 try {
...@@ -199,6 +201,8 @@ public final class SerializedDataStore { ...@@ -199,6 +201,8 @@ public final class SerializedDataStore {
199 input.close(); 201 input.close();
200 file.close(); 202 file.close();
201 } 203 }
204 + } catch (FileNotFoundException ex) {
205 + throw new FileNotFoundException("No such file or directory.");
202 } catch (ClassNotFoundException ex) { 206 } catch (ClassNotFoundException ex) {
203 throw new ClassNotFoundException("failed to fetch the serialized data file."); 207 throw new ClassNotFoundException("failed to fetch the serialized data file.");
204 } 208 }
......
...@@ -33,28 +33,31 @@ public final class YangFileScanner { ...@@ -33,28 +33,31 @@ public final class YangFileScanner {
33 private YangFileScanner() { 33 private YangFileScanner() {
34 } 34 }
35 35
36 +
36 /** 37 /**
37 - * Returns the list of YANG files. 38 + * Returns the list of java files.
38 * 39 *
39 * @param root specified directory 40 * @param root specified directory
40 - * @return list of YANG files. 41 + * @return list of java files.
42 + * @throws NullPointerException when no files are there.
41 * @throws IOException when files get deleted while performing the 43 * @throws IOException when files get deleted while performing the
42 * operations. 44 * operations.
43 */ 45 */
44 - public static List<String> getYangFiles(String root) throws IOException { 46 + public static List<String> getJavaFiles(String root) throws NullPointerException, IOException {
45 - return getFiles(root, ".yang"); 47 + return getFiles(root, ".java");
46 } 48 }
47 49
48 /** 50 /**
49 - * Returns the list of java files. 51 + * Returns the list of YANG files.
50 * 52 *
51 * @param root specified directory 53 * @param root specified directory
52 - * @return list of java files. 54 + * @return list of YANG files.
55 + * @throws NullPointerException when no files are there.
53 * @throws IOException when files get deleted while performing the 56 * @throws IOException when files get deleted while performing the
54 * operations. 57 * operations.
55 */ 58 */
56 - public static List<String> getJavaFiles(String root) throws IOException { 59 + public static List<String> getYangFiles(String root) throws NullPointerException, IOException {
57 - return getFiles(root, ".java"); 60 + return getFiles(root, ".yang");
58 } 61 }
59 62
60 /** 63 /**
...@@ -66,7 +69,7 @@ public final class YangFileScanner { ...@@ -66,7 +69,7 @@ public final class YangFileScanner {
66 * @throws IOException when files get deleted while performing the 69 * @throws IOException when files get deleted while performing the
67 * operations. 70 * operations.
68 */ 71 */
69 - public static List<String> getFiles(String root, String extension) throws IOException { 72 + public static List<String> getFiles(String root, String extension) throws NullPointerException, IOException {
70 List<String> store = new LinkedList<>(); 73 List<String> store = new LinkedList<>();
71 Stack<String> stack = new Stack<>(); 74 Stack<String> stack = new Stack<>();
72 stack.push(root); 75 stack.push(root);
...@@ -77,7 +80,7 @@ public final class YangFileScanner { ...@@ -77,7 +80,7 @@ public final class YangFileScanner {
77 root = stack.pop(); 80 root = stack.pop();
78 file = new File(root); 81 file = new File(root);
79 filelist = file.listFiles(); 82 filelist = file.listFiles();
80 - if (filelist == null) { 83 + if (filelist.length == 0) {
81 continue; 84 continue;
82 } 85 }
83 for (File current : filelist) { 86 for (File current : filelist) {
...@@ -92,8 +95,8 @@ public final class YangFileScanner { ...@@ -92,8 +95,8 @@ public final class YangFileScanner {
92 } 95 }
93 } 96 }
94 return store; 97 return store;
95 - } catch (IOException e) { 98 + } catch (NullPointerException e) {
96 - throw new IOException("IOException occured"); 99 + throw new IOException("NullPointerException occured");
97 } 100 }
98 } 101 }
99 } 102 }
......
...@@ -53,7 +53,7 @@ public final class YangIoUtils { ...@@ -53,7 +53,7 @@ public final class YangIoUtils {
53 */ 53 */
54 public static File createDirectories(String path) { 54 public static File createDirectories(String path) {
55 55
56 - File generatedDir = new File(UtilConstants.YANG_GEN_DIR + File.separator + path); 56 + File generatedDir = new File(path);
57 generatedDir.mkdirs(); 57 generatedDir.mkdirs();
58 return generatedDir; 58 return generatedDir;
59 } 59 }
...@@ -68,12 +68,15 @@ public final class YangIoUtils { ...@@ -68,12 +68,15 @@ public final class YangIoUtils {
68 */ 68 */
69 public static void addPackageInfo(File path, String classInfo, String pack) throws IOException { 69 public static void addPackageInfo(File path, String classInfo, String pack) throws IOException {
70 70
71 + if (pack.contains(UtilConstants.YANG_GEN_DIR)) {
72 + String[] strArray = pack.split(UtilConstants.YANG_GEN_DIR + UtilConstants.SLASH);
73 + pack = strArray[1];
74 + }
75 +
71 try { 76 try {
72 77
73 File packageInfo = new File(path + File.separator + "package-info.java"); 78 File packageInfo = new File(path + File.separator + "package-info.java");
74 packageInfo.createNewFile(); 79 packageInfo.createNewFile();
75 - if (packageInfo.exists()) {
76 -
77 FileWriter fileWriter = null; 80 FileWriter fileWriter = null;
78 BufferedWriter bufferedWriter = null; 81 BufferedWriter bufferedWriter = null;
79 fileWriter = new FileWriter(packageInfo); 82 fileWriter = new FileWriter(packageInfo);
...@@ -82,7 +85,6 @@ public final class YangIoUtils { ...@@ -82,7 +85,6 @@ public final class YangIoUtils {
82 bufferedWriter.write(JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.PACKAGE_INFO, classInfo)); 85 bufferedWriter.write(JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.PACKAGE_INFO, classInfo));
83 bufferedWriter.write(UtilConstants.PACKAGE + UtilConstants.SPACE + pack + UtilConstants.SEMI_COLAN); 86 bufferedWriter.write(UtilConstants.PACKAGE + UtilConstants.SPACE + pack + UtilConstants.SEMI_COLAN);
84 bufferedWriter.close(); 87 bufferedWriter.close();
85 - }
86 } catch (IOException e) { 88 } catch (IOException e) {
87 throw new IOException("Exception occured while creating package info file."); 89 throw new IOException("Exception occured while creating package info file.");
88 } 90 }
......
1 +/*
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 +
17 +package org.onosproject.yangutils.utils;
18 +
19 +import org.junit.Test;
20 +import org.junit.Rule;
21 +import org.junit.rules.ExpectedException;
22 +import java.lang.reflect.Constructor;
23 +import java.lang.reflect.InvocationTargetException;
24 +import static org.junit.Assert.assertNotNull;
25 +
26 +/**
27 + * Test case for testing the util constants.
28 + */
29 +public final class UtilConstantsTest {
30 +
31 + @Rule
32 + public ExpectedException thrown = ExpectedException.none();
33 +
34 + /**
35 + * A private constructor is tested.
36 + *
37 + * @throws SecurityException if any security violation is observed.
38 + * @throws NoSuchMethodException if when the method is not found.
39 + * @throws IllegalArgumentException if there is illegal argument found.
40 + * @throws InstantiationException if instantiation is provoked for the private constructor.
41 + * @throws IllegalAccessException if instance is provoked or a method is provoked.
42 + * @throws InvocationTargetException when an exception occurs by the method or constructor.
43 + */
44 + @Test
45 + public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
46 + InstantiationException, IllegalAccessException, InvocationTargetException {
47 +
48 + Class<?>[] classesToConstruct = {UtilConstants.class};
49 + for (Class<?> clazz : classesToConstruct) {
50 + Constructor<?> constructor = clazz.getDeclaredConstructor();
51 + constructor.setAccessible(true);
52 + assertNotNull(constructor.newInstance());
53 + }
54 + }
55 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
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 +
17 +package org.onosproject.yangutils.utils.io.impl;
18 +
19 +import org.junit.Test;
20 +import org.junit.Rule;
21 +import org.junit.rules.ExpectedException;
22 +
23 +import static org.hamcrest.core.Is.is;
24 +import static org.junit.Assert.assertThat;
25 +import static org.junit.Assert.assertNotNull;
26 +import org.slf4j.Logger;
27 +import static org.slf4j.LoggerFactory.getLogger;
28 +
29 +import java.io.BufferedReader;
30 +import java.io.File;
31 +import java.io.FileOutputStream;
32 +import java.io.FileReader;
33 +import java.io.FileWriter;
34 +import java.io.IOException;
35 +import java.io.InputStream;
36 +import java.io.OutputStream;
37 +import java.lang.reflect.Constructor;
38 +import java.lang.reflect.InvocationTargetException;
39 +
40 +/**
41 + * Unit Tests for the CopyrightHeader contents.
42 + */
43 +public final class CopyrightHeaderTest {
44 +
45 + private final Logger log = getLogger(getClass());
46 +
47 + @Rule
48 + public ExpectedException thrown = ExpectedException.none();
49 +
50 + /**
51 + * Unit test for testing private constructor.
52 + *
53 + * @throws SecurityException if any security violation is observed.
54 + * @throws NoSuchMethodException if when the method is not found.
55 + * @throws IllegalArgumentException if there is illegal argument found.
56 + * @throws InstantiationException if instantiation is provoked for the private constructor.
57 + * @throws IllegalAccessException if instance is provoked or a method is provoked.
58 + * @throws InvocationTargetException when an exception occurs by the method or constructor.
59 + */
60 + @Test
61 + public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
62 + InstantiationException, IllegalAccessException, InvocationTargetException {
63 +
64 + Class<?>[] classesToConstruct = {CopyrightHeader.class };
65 + for (Class<?> clazz : classesToConstruct) {
66 + Constructor<?> constructor = clazz.getDeclaredConstructor();
67 + constructor.setAccessible(true);
68 + assertNotNull(constructor.newInstance());
69 + }
70 + }
71 +
72 + /**
73 + * This test case checks the received copyright header contents.
74 + */
75 + @Test
76 + public void testGetCopyrightHeader() throws IOException {
77 +
78 + CopyrightHeader.parseCopyrightHeader();
79 + String licenseHeader = CopyrightHeader.getCopyrightHeader();
80 + ClassLoader classLoader = CopyrightHeaderTest.class.getClassLoader();
81 + File test = new File("target/TestCopyrightHeader.txt");
82 +
83 + FileWriter out = new FileWriter(test);
84 + out.write(licenseHeader);
85 + out.close();
86 +
87 + File temp = new File("target/temp.txt");
88 + InputStream stream = classLoader.getResourceAsStream("CopyrightHeader.txt");
89 + OutputStream outStream = new FileOutputStream(temp);
90 + int i;
91 + while ((i = stream.read()) != -1) {
92 + outStream.write(i);
93 + }
94 + outStream.close();
95 + stream.close();
96 +
97 + BufferedReader br1 = new BufferedReader(new FileReader(test));
98 + BufferedReader br2 = new BufferedReader(new FileReader(temp));
99 + while (br1.readLine() != null && br2.readLine() != null) {
100 + assertThat(true, is((br1.readLine()).equals(br2.readLine())));
101 + }
102 + br1.close();
103 + br2.close();
104 + }
105 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
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 +
17 +package org.onosproject.yangutils.utils.io.impl;
18 +
19 +import org.junit.Test;
20 +import org.junit.Rule;
21 +import org.junit.rules.ExpectedException;
22 +import org.onosproject.yangutils.translator.GeneratedFileType;
23 +import org.onosproject.yangutils.utils.UtilConstants;
24 +
25 +import java.io.File;
26 +import java.io.IOException;
27 +import java.lang.reflect.Constructor;
28 +import java.lang.reflect.InvocationTargetException;
29 +
30 +import org.slf4j.Logger;
31 +import static org.slf4j.LoggerFactory.getLogger;
32 +import static org.junit.Assert.assertNotNull;
33 +import static org.junit.Assert.assertTrue;
34 +import static org.junit.Assert.assertFalse;
35 +
36 +/**
37 + * Tests the file handle utilities.
38 + */
39 +public final class FileSystemUtilTest {
40 +
41 + public static String baseDirPkg = "target.UnitTestCase.";
42 + public static String packageInfoContent = "testGeneration6";
43 + public static String baseDir = "target/UnitTestCase";
44 +
45 + private final Logger log = getLogger(getClass());
46 +
47 + @Rule
48 + public ExpectedException thrown = ExpectedException.none();
49 +
50 + /**
51 + * A private constructor is tested.
52 + *
53 + * @throws SecurityException if any security violation is observed.
54 + * @throws NoSuchMethodException if when the method is not found.
55 + * @throws IllegalArgumentException if there is illegal argument found.
56 + * @throws InstantiationException if instantiation is provoked for the private constructor.
57 + * @throws IllegalAccessException if instance is provoked or a method is provoked.
58 + * @throws InvocationTargetException when an exception occurs by the method or constructor.
59 + */
60 + @Test
61 + public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
62 + InstantiationException, IllegalAccessException, InvocationTargetException {
63 +
64 + Class<?>[] classesToConstruct = {FileSystemUtil.class};
65 + for (Class<?> clazz : classesToConstruct) {
66 + Constructor<?> constructor = clazz.getDeclaredConstructor();
67 + constructor.setAccessible(true);
68 + assertNotNull(constructor.newInstance());
69 + }
70 + }
71 +
72 + /**
73 + * This test case checks the creation of source files.
74 + */
75 + @Test
76 + public void createSourceFilesTest() throws IOException {
77 +
78 + FileSystemUtil.createSourceFiles(baseDirPkg + "srcFile1", packageInfoContent, GeneratedFileType.INTERFACE);
79 + }
80 +
81 + /**
82 + * This test case checks the contents to be written in the file.
83 + */
84 + @Test
85 + public void insertStringInFileTest() throws IOException {
86 + File dir = new File(baseDir + File.separator + "File1");
87 + dir.mkdirs();
88 + File createFile = new File(dir + "testFile");
89 + createFile.createNewFile();
90 + File createSourceFile = new File(dir + "sourceTestFile");
91 + createSourceFile.createNewFile();
92 + FileSystemUtil.insertStringInFile(createFile, "This is to append a text to the file first1\n");
93 + FileSystemUtil.insertStringInFile(createFile, "This is next second line\n");
94 + FileSystemUtil.insertStringInFile(createFile, "This is next third line in the file");
95 + FileSystemUtil.appendFileContents(createFile, createSourceFile);
96 + }
97 +
98 + /**
99 + * This test case checks whether the package is existing.
100 + */
101 + @Test
102 + public void packageExistTest() throws IOException {
103 + String dirPath = "exist1.exist2.exist3";
104 + String strPath = baseDirPkg + dirPath;
105 + File createDir = new File(strPath.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
106 + createDir.mkdirs();
107 + File createFile = new File(createDir + File.separator + "package-info.java");
108 + createFile.createNewFile();
109 + assertTrue(FileSystemUtil.doesPackageExist(strPath));
110 + FileSystemUtil.createPackage(strPath, packageInfoContent);
111 + createDir.delete();
112 + }
113 +
114 + /**
115 + * This test case checks the package does not exist.
116 + */
117 + @Test
118 + public void packageNotExistTest() throws IOException {
119 + String dirPath = "notexist1.notexist2";
120 + String strPath = baseDirPkg + dirPath;
121 + File createDir = new File(strPath.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
122 + assertFalse(FileSystemUtil.doesPackageExist(strPath));
123 + createDir.mkdirs();
124 + assertFalse(FileSystemUtil.doesPackageExist(strPath));
125 + CopyrightHeader.parseCopyrightHeader();
126 + FileSystemUtil.createPackage(strPath, packageInfoContent);
127 + assertTrue(FileSystemUtil.doesPackageExist(strPath));
128 + createDir.delete();
129 + }
130 +}
1 +/*
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 +
17 +package org.onosproject.yangutils.utils.io.impl;
18 +
19 +import org.junit.Test;
20 +import org.junit.Rule;
21 +import org.junit.rules.ExpectedException;
22 +import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType;
23 +
24 +import java.lang.reflect.Constructor;
25 +import java.lang.reflect.InvocationTargetException;
26 +
27 +import static org.junit.Assert.assertTrue;
28 +import static org.junit.Assert.assertNotNull;
29 +
30 +/**
31 + * Tests the java doc that is generated.
32 + */
33 +public final class JavaDocGenTest {
34 +
35 + @Rule
36 + public ExpectedException thrown = ExpectedException.none();
37 +
38 + /**
39 + * This test case checks the content recieved for the builder class java doc.
40 + */
41 + @Test
42 + public void builderClassGenerationTest() {
43 +
44 + String builderClassJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.BUILDER_CLASS, "testGeneration1");
45 + assertTrue(builderClassJavaDoc.contains("Provides the builder implementation of")
46 + && builderClassJavaDoc.contains(" */\n"));
47 + }
48 +
49 + /**
50 + * This test case checks the content recieved for the builder interface ge java doc.
51 + */
52 + @Test
53 + public void builderInterfaceGenerationTest() {
54 +
55 + String builderInterfaceJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.BUILDER_INTERFACE, "testGeneration1");
56 + assertTrue(builderInterfaceJavaDoc.contains("Builder for") && builderInterfaceJavaDoc.contains(" */\n"));
57 + }
58 +
59 + /**
60 + * This test case checks the content recieved for the build java doc.
61 + */
62 + @Test
63 + public void buildGenerationTest() {
64 +
65 + String buildDoc = JavaDocGen.getJavaDoc(JavaDocType.BUILD, "testGeneration1");
66 + assertTrue(buildDoc.contains("Builds object of") && buildDoc.contains(" */\n"));
67 + }
68 +
69 + /**
70 + * A private constructor is tested.
71 + *
72 + * @throws SecurityException if any security violation is observed.
73 + * @throws NoSuchMethodException if when the method is not found.
74 + * @throws IllegalArgumentException if there is illegal argument found.
75 + * @throws InstantiationException if instantiation is provoked for the private constructor.
76 + * @throws IllegalAccessException if instance is provoked or a method is provoked.
77 + * @throws InvocationTargetException when an exception occurs by the method or constructor.
78 + */
79 + @Test
80 + public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
81 + InstantiationException, IllegalAccessException, InvocationTargetException {
82 +
83 + Class<?>[] classesToConstruct = {JavaDocGen.class };
84 + for (Class<?> clazz : classesToConstruct) {
85 + Constructor<?> constructor = clazz.getDeclaredConstructor();
86 + constructor.setAccessible(true);
87 + assertNotNull(constructor.newInstance());
88 + }
89 + }
90 +
91 + /**
92 + * This test case checks the content recieved for the constructor java doc.
93 + */
94 + @Test
95 + public void constructorGenerationTest() {
96 +
97 + String constructorDoc = JavaDocGen.getJavaDoc(JavaDocType.CONSTRUCTOR, "testGeneration1");
98 + assertTrue(
99 + constructorDoc.contains("Construct the object of") && constructorDoc.contains("builder object of")
100 + && constructorDoc.contains("@param") && constructorDoc.contains("*/\n"));
101 + JavaDocType.valueOf(JavaDocType.CONSTRUCTOR.toString());
102 + }
103 +
104 + /**
105 + * This test case checks the content recieved for the default constructor java doc.
106 + */
107 + @Test
108 + public void defaultConstructorGenerationTest() {
109 +
110 + String defaultConstructorDoc = JavaDocGen.getJavaDoc(JavaDocType.DEFAULT_CONSTRUCTOR, "testGeneration1");
111 + assertTrue(defaultConstructorDoc.contains("Default Constructor") && defaultConstructorDoc.contains(" */\n"));
112 + }
113 +
114 + /**
115 + * This test case checks the content recieved for the getter java doc.
116 + */
117 + @Test
118 + public void getterGenerationTest() {
119 +
120 + String getterJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.GETTER, "testGeneration1");
121 + assertTrue(getterJavaDoc.contains("Returns the attribute") && getterJavaDoc.contains(" */\n"));
122 + }
123 +
124 + /**
125 + * This test case checks the content recieved for the impl class java doc.
126 + */
127 + @Test
128 + public void implClassGenerationTest() {
129 + String implClassJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.IMPL_CLASS, "testGeneration1");
130 + assertTrue(implClassJavaDoc.contains("Provides the implementation of") && implClassJavaDoc.contains(" */\n"));
131 + }
132 +
133 + /**
134 + * This test case checks the content recieved for the interface java doc.
135 + */
136 + @Test
137 + public void interfaceGenerationTest() {
138 +
139 + String interfaceJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.INTERFACE, "testGeneration1");
140 + assertTrue(interfaceJavaDoc.contains("Abstraction of an entity which provides functionalities of")
141 + && interfaceJavaDoc.contains(" */\n"));
142 + }
143 +
144 + /**
145 + * This test case checks the content recieved for the package info java doc.
146 + */
147 + @Test
148 + public void packageInfoGenerationTest() {
149 +
150 + String packageInfo = JavaDocGen.getJavaDoc(JavaDocType.PACKAGE_INFO, "testGeneration1");
151 + assertTrue(packageInfo.contains("Generated java code for the YANG file") && packageInfo.contains(" */\n"));
152 + }
153 +
154 + /**
155 + * This test case checks the content recieved for the setter java doc.
156 + */
157 + @Test
158 + public void setterGenerationTest() {
159 +
160 + String setterJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.SETTER, "testGeneration1");
161 + assertTrue(setterJavaDoc.contains("Returns the builder object of") && setterJavaDoc.contains(" */\n"));
162 + }
163 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
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 +
17 +package org.onosproject.yangutils.utils.io.impl;
18 +
19 +import org.junit.Test;
20 +import org.junit.Rule;
21 +import org.junit.rules.ExpectedException;
22 +import org.onosproject.yangutils.utils.io.impl.SerializedDataStore.SerializedDataStoreType;
23 +
24 +import java.io.FileNotFoundException;
25 +import java.io.IOException;
26 +import java.lang.reflect.Constructor;
27 +import java.lang.reflect.InvocationTargetException;
28 +import java.util.LinkedList;
29 +import java.util.List;
30 +
31 +import org.slf4j.Logger;
32 +import static org.slf4j.LoggerFactory.getLogger;
33 +
34 +import static org.hamcrest.core.Is.is;
35 +import static org.junit.Assert.assertThat;
36 +import static org.junit.Assert.assertNotNull;
37 +
38 +/**
39 + * Unit tests for the serialized data store for its contents.
40 + */
41 +public final class SerializedDataStoreTest {
42 +
43 + private final Logger log = getLogger(getClass());
44 +
45 + @Rule
46 + public ExpectedException thrown = ExpectedException.none();
47 +
48 + /**
49 + * A private constructor is tested.
50 + *
51 + * @throws SecurityException if any security violation is observed.
52 + * @throws NoSuchMethodException if when the method is not found.
53 + * @throws IllegalArgumentException if there is illegal argument found.
54 + * @throws InstantiationException if instantiation is provoked for the private constructor.
55 + * @throws IllegalAccessException if instance is provoked or a method is provoked.
56 + * @throws InvocationTargetException when an exception occurs by the method or constructor.
57 + */
58 + @Test
59 + public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
60 + InstantiationException, IllegalAccessException, InvocationTargetException {
61 +
62 + Class<?>[] classesToConstruct = {SerializedDataStore.class };
63 + for (Class<?> clazz : classesToConstruct) {
64 + Constructor<?> constructor = clazz.getDeclaredConstructor();
65 + constructor.setAccessible(true);
66 + assertNotNull(constructor.newInstance());
67 + }
68 + }
69 +
70 + /**
71 + * This test case checks the attribute info that is read and put into the list.
72 + */
73 + @Test
74 + public void insertAttributeDataTest() throws IOException, ClassNotFoundException, FileNotFoundException {
75 +
76 + String attributeData = "attribute content lists this";
77 + SerializedDataStore.setSerializeData(attributeData, SerializedDataStoreType.ATTRIBUTE);
78 + List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.ATTRIBUTE);
79 + List<String> expectedinfo = new LinkedList<>();
80 + expectedinfo.add(attributeData);
81 + assertThat(true, is(attributeInfo.equals(expectedinfo)));
82 + SerializedDataStoreType.valueOf(SerializedDataStoreType.ATTRIBUTE.toString());
83 + }
84 +
85 + /**
86 + * This test case checks the builder interface that is read and put into the list.
87 + */
88 + @Test
89 + public void insertBuilderInterfaceMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
90 +
91 + String builderInterfaceMethodsData = "builder interface methods content lists this";
92 + SerializedDataStore.setSerializeData(builderInterfaceMethodsData,
93 + SerializedDataStoreType.BUILDER_INTERFACE_METHODS);
94 + List<String> attributeInfo = SerializedDataStore
95 + .getSerializeData(SerializedDataStoreType.BUILDER_INTERFACE_METHODS);
96 + List<String> expectedinfo = new LinkedList<>();
97 + expectedinfo.add(builderInterfaceMethodsData);
98 + assertThat(true, is(attributeInfo.equals(expectedinfo)));
99 + }
100 +
101 + /**
102 + * This test case checks the builder methods that is read and put into the list.
103 + */
104 + @Test
105 + public void insertBuilderMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
106 +
107 + String builderMethodsData = "builder methods content lists this";
108 + SerializedDataStore.setSerializeData(builderMethodsData, SerializedDataStoreType.BUILDER_METHODS);
109 + List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.BUILDER_METHODS);
110 + List<String> expectedinfo = new LinkedList<>();
111 + expectedinfo.add(builderMethodsData);
112 + assertThat(true, is(attributeInfo.equals(expectedinfo)));
113 + }
114 +
115 + /**
116 + * This test case checks the impl methods that is read and put into the list.
117 + */
118 + @Test
119 + public void insertImplMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
120 +
121 + String implMethodsData = "impl methods content lists this";
122 + SerializedDataStore.setSerializeData(implMethodsData, SerializedDataStoreType.IMPL_METHODS);
123 + List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.IMPL_METHODS);
124 + List<String> expectedinfo = new LinkedList<>();
125 + expectedinfo.add(implMethodsData);
126 + assertThat(true, is(attributeInfo.equals(expectedinfo)));
127 + }
128 +
129 + /**
130 + * This test case checks the import methods that is read and put into the list.
131 + */
132 + @Test
133 + public void insertImportTest() throws IOException, ClassNotFoundException, FileNotFoundException {
134 +
135 + String importData = "interface methods content lists this";
136 + SerializedDataStore.setSerializeData(importData, SerializedDataStoreType.IMPORT);
137 + List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.IMPORT);
138 + List<String> expectedinfo = new LinkedList<>();
139 + expectedinfo.add(importData);
140 + assertThat(true, is(attributeInfo.equals(expectedinfo)));
141 + }
142 +
143 + /**
144 + * This test case checks the interface methods that is read and put into the list.
145 + */
146 + @Test
147 + public void insertInterfaceMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
148 +
149 + String interfaceMethodsData = "interface methods content lists this";
150 + SerializedDataStore.setSerializeData(interfaceMethodsData, SerializedDataStoreType.INTERFACE_METHODS);
151 + List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.INTERFACE_METHODS);
152 + List<String> expectedinfo = new LinkedList<>();
153 + expectedinfo.add(interfaceMethodsData);
154 + assertThat(true, is(attributeInfo.equals(expectedinfo)));
155 + }
156 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -16,207 +16,152 @@ ...@@ -16,207 +16,152 @@
16 16
17 package org.onosproject.yangutils.utils.io.impl; 17 package org.onosproject.yangutils.utils.io.impl;
18 18
19 -import java.io.IOException;
20 -
21 import org.junit.Test; 19 import org.junit.Test;
20 +import org.junit.Rule;
21 +import org.junit.rules.ExpectedException;
22 +import static org.junit.Assert.assertNotNull;
23 +import static org.junit.Assert.assertEquals;
24 +
22 import java.io.File; 25 import java.io.File;
26 +import java.lang.reflect.Constructor;
27 +import java.lang.reflect.InvocationTargetException;
28 +import java.util.LinkedList;
23 import java.util.List; 29 import java.util.List;
30 +import java.io.IOException;
31 +
24 import org.slf4j.Logger; 32 import org.slf4j.Logger;
25 import static org.slf4j.LoggerFactory.getLogger; 33 import static org.slf4j.LoggerFactory.getLogger;
26 34
27 /** 35 /**
28 - * Unit tests for searching yang files. 36 + * Test the file scanner service.
29 */ 37 */
30 -public class YangFileScannerTest { 38 +public final class YangFileScannerTest {
31 39
32 private final Logger log = getLogger(getClass()); 40 private final Logger log = getLogger(getClass());
33 41
34 - private static final String BASEDIR = "target/UnitTestCase"; 42 + @Rule
43 + public ExpectedException thrown = ExpectedException.none();
44 +
45 + String baseDir = "target/UnitTestCase";
35 46
36 /** 47 /**
37 - * Checks an empty directory. 48 + * 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.
38 */ 56 */
39 @Test 57 @Test
40 - public void testWithSingleEmptyDirectoryInRoot() { 58 + public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
41 - try { 59 + InstantiationException, IllegalAccessException, InvocationTargetException {
42 - File dir = new File(BASEDIR); 60 +
43 - dir.mkdirs(); 61 + Class<?>[] classesToConstruct = {YangFileScanner.class };
44 - List<String> list = YangFileScanner.getYangFiles(BASEDIR.toString()); 62 + for (Class<?> clazz : classesToConstruct) {
45 - } catch (IOException e) { 63 + Constructor<?> constructor = clazz.getDeclaredConstructor();
46 - log.info("IO Exception throwed"); 64 + constructor.setAccessible(true);
65 + assertNotNull(constructor.newInstance());
47 } 66 }
48 } 67 }
49 68
50 /** 69 /**
51 - * Checks multiple empty directories in root directory. 70 + * This test case checks for a .java file inside the specified dir.
52 */ 71 */
53 @Test 72 @Test
54 - public void testWithMultiEmptyDirectoriesInRoot() { 73 + public void checkJavaFileInsideDirTest() throws IOException {
55 - try { 74 +
56 - String dir = "emptyDir"; 75 + String dir = baseDir + File.separator + "scanner2";
57 - String dir1 = "emptyDir1"; 76 + File path = createDirectory(dir);
58 - String dir2 = "emptyDir2"; 77 + createFile(path, "testScanner.java");
59 - String dir3 = "emptyDir3"; 78 + List<String> dirContents = YangFileScanner.getJavaFiles(path.toString());
60 - String dir4 = "emptyDir4"; 79 + List<String> expectedContents = new LinkedList<>();
61 - File firstpath = createDirectory(dir); 80 + expectedContents.add(path.getCanonicalPath() + File.separator + "testScanner.java");
62 - File firstpath1 = createDirectory(dir1); 81 + assertEquals(dirContents, expectedContents);
63 - File firstpath2 = createDirectory(dir2);
64 - File firstpath3 = createDirectory(dir3);
65 - File firstpath4 = createDirectory(dir4);
66 - List<String> list = YangFileScanner.getYangFiles(BASEDIR.toString());
67 - } catch (IOException e) {
68 - log.info("IO Exception throwed");
69 - }
70 } 82 }
71 83
72 /** 84 /**
73 - * Checks one directory with one .yang file. 85 + * Method used for creating multiple directories inside the target file.
86 + *
87 + * @param path where directories should be created.
88 + * @return
74 */ 89 */
75 - @Test 90 + public File createDirectory(String path) {
76 - public void testWithSingleDirectorySingleFileInRoot() { 91 +
77 - try { 92 + File myDir = new File(path);
78 - String dir1 = "level1"; 93 + myDir.mkdirs();
79 - String firstFileName1 = "secondFile.yang"; 94 + return myDir;
80 - File firstpath1 = createDirectory(dir1);
81 - createFile(firstpath1, firstFileName1);
82 - List<String> list = YangFileScanner.getYangFiles(BASEDIR.toString());
83 - } catch (IOException e) {
84 - log.info("IO Exception throwed");
85 - }
86 } 95 }
87 96
88 /** 97 /**
89 - * Checks one directory with many .yang file. 98 + * Method used for creating file inside the specified directory.
99 + *
100 + * @param myDir the path where file has to be created inside.
101 + * @param fileName the name of the file to be created.
90 */ 102 */
91 - @Test 103 + public void createFile(File myDir, String fileName) throws IOException {
92 - public void testWithSingleDirectoryMultiFilesInRoot() { 104 +
93 - try { 105 + File file = null;
94 - String dir2 = "level2"; 106 + file = new File(myDir + File.separator + fileName);
95 - String firstFileName2 = "thirdFile.yang"; 107 + file.createNewFile();
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);
104 - List<String> list = YangFileScanner.getYangFiles(BASEDIR.toString());
105 - } catch (IOException e) {
106 - log.info("IO Exception throwed");
107 - }
108 } 108 }
109 109
110 /** 110 /**
111 - * Checks multi directories with many .yang file. 111 + * This testcase checks for a java file inside an empty directory.
112 */ 112 */
113 @Test 113 @Test
114 - public void testWithMultiDirectoriesMultiFiles() { 114 + public void emptyDirJavaScannerTest() throws IOException {
115 - try { 115 +
116 - String dir2 = "newDir1/newDir2/newDir3/newDir4"; 116 + String emptyDir = baseDir + File.separator + "scanner1";
117 - File dir3 = new File("target/UnitTestCase/newDir1"); 117 + File path = createDirectory(emptyDir);
118 - File dir4 = new File("target/UnitTestCase/newDir1/newDir2"); 118 + List<String> emptyDirContents = YangFileScanner.getJavaFiles(path.toString());
119 - File dir5 = new File("target/UnitTestCase/newDir1/newDir2/newDir3"); 119 + List<String> expectedContents = new LinkedList<>();
120 - File dir6 = new File("target/UnitTestCase/newDir1/newDir2/newDir3/newDir4"); 120 + assertEquals(emptyDirContents, expectedContents);
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);
146 - List<String> list = YangFileScanner.getYangFiles(BASEDIR.toString());
147 - } catch (IOException e) {
148 - log.info("IO Exception throwed");
149 - }
150 } 121 }
151 122
152 /** 123 /**
153 - * Checks multi directories with many .java file. 124 + * This testcase checks for a yang file inside an empty directory.
154 */ 125 */
155 @Test 126 @Test
156 - public void testWithMultiDirectoriesMultiJavaFiles() { 127 + public void emptyDirYangScannerTest() throws IOException {
157 - try { 128 +
158 - String dir2 = "newDir1/newDir2/newDir3/newDir4"; 129 + String emptyYangDir = baseDir + File.separator + "scanner1";
159 - File dir3 = new File("target/UnitTestCase/newDir1"); 130 + File path = createDirectory(emptyYangDir);
160 - File dir4 = new File("target/UnitTestCase/newDir1/newDir2"); 131 + List<String> emptyDirContents = YangFileScanner.getYangFiles(path.toString());
161 - File dir5 = new File("target/UnitTestCase/newDir1/newDir2/newDir3"); 132 + List<String> expectedContents = new LinkedList<>();
162 - File dir6 = new File("target/UnitTestCase/newDir1/newDir2/newDir3/newDir4"); 133 + assertEquals(emptyDirContents, expectedContents);
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());
189 - } catch (IOException e) {
190 - log.info("IO Exception throwed");
191 - }
192 } 134 }
193 135
194 /** 136 /**
195 - * Method used for creating multiple directories inside the target file. 137 + * This test case checks with the sub directories in the given path for java files.
196 - *
197 - * @param path directory path
198 - * @return directory path
199 */ 138 */
200 - public File createDirectory(String path) { 139 + @Test
201 - File myDir = new File(BASEDIR + File.separator + path); 140 + public void emptySubDirScannerTest() throws IOException {
202 - myDir.mkdirs(); 141 +
203 - return myDir; 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);
204 } 150 }
205 151
206 /** 152 /**
207 - * Method used for creating file inside the specified directory. 153 + * This test case checks with the sub directories in the given path for java files.
208 - *
209 - * @param myDir my current dirctory
210 - * @param fileName file name
211 - * @throws IOException io exception when fails to create a file.
212 */ 154 */
213 - public void createFile(File myDir, String fileName) throws IOException { 155 + @Test
214 - File file = null; 156 + public void exceptionHandleTest() throws IOException {
215 - try { 157 +
216 - file = new File(myDir + File.separator + fileName); 158 + String dir = baseDir + File.separator + "scanner4";
217 - file.createNewFile(); 159 + thrown.expect(IOException.class);
218 - } catch (final IOException e) { 160 + thrown.expectMessage("NullPointerException occured");
219 - throw new IOException("IOException occured"); 161 + List<String> invalidContents = YangFileScanner.getJavaFiles(dir);
220 - } 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");
221 } 166 }
222 } 167 }
...\ No newline at end of file ...\ No newline at end of file
......
1 +/*
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 +
17 +package org.onosproject.yangutils.utils.io.impl;
18 +
19 +import org.junit.Test;
20 +import org.junit.Rule;
21 +import org.junit.rules.ExpectedException;
22 +import org.onosproject.yangutils.utils.UtilConstants;
23 +
24 +import java.io.File;
25 +import java.io.IOException;
26 +
27 +import org.apache.maven.project.MavenProject;
28 +import org.sonatype.plexus.build.incremental.BuildContext;
29 +import org.sonatype.plexus.build.incremental.DefaultBuildContext;
30 +
31 +import org.slf4j.Logger;
32 +import static org.slf4j.LoggerFactory.getLogger;
33 +import static org.junit.Assert.assertThat;
34 +import static org.junit.Assert.assertNotNull;
35 +import static org.hamcrest.core.Is.is;
36 +import java.lang.reflect.Constructor;
37 +import java.lang.reflect.InvocationTargetException;
38 +
39 +/**
40 + * Unit tests for adding package-info, creating directories, cleaning the folder and to add sources.
41 + */
42 +public final class YangIoUtilsTest {
43 +
44 + public static String baseDir = "target/UnitTestCase";
45 +
46 + public static String createPath = baseDir + File.separator + "dir1/dir2/dir3/dir4/";
47 +
48 + private final Logger log = getLogger(getClass());
49 +
50 + @Rule
51 + public ExpectedException thrown = ExpectedException.none();
52 +
53 + /**
54 + * This test case checks whether the package-info file is created.
55 + */
56 + @Test
57 + public void addPackageInfoTest() throws IOException {
58 +
59 + File dirPath = new File(createPath);
60 + dirPath.mkdirs();
61 + CopyrightHeader.parseCopyrightHeader();
62 + YangIoUtils.addPackageInfo(dirPath, "check1", createPath);
63 + File filePath = new File(dirPath + File.separator + "package-info.java");
64 + assertThat(filePath.isFile(), is(true));
65 + }
66 +
67 + /**
68 + * This test case checks whether the package-info file is created when invalid path is given.
69 + */
70 + @Test
71 + public void addPackageInfoWithEmptyPathTest() throws IOException {
72 +
73 + File dirPath = new File("");
74 + thrown.expect(IOException.class);
75 + thrown.expectMessage("Exception occured while creating package info file.");
76 + YangIoUtils.addPackageInfo(dirPath, "check1", createPath);
77 + File filePath1 = new File(dirPath + File.separator + "package-info.java");
78 + assertThat(filePath1.isFile(), is(false));
79 + }
80 +
81 + /**
82 + * A private constructor is tested.
83 + *
84 + * @throws SecurityException if any security violation is observed.
85 + * @throws NoSuchMethodException if when the method is not found.
86 + * @throws IllegalArgumentException if there is illegal argument found.
87 + * @throws InstantiationException if instantiation is provoked for the private constructor.
88 + * @throws IllegalAccessException if instance is provoked or a method is provoked.
89 + * @throws InvocationTargetException when an exception occurs by the method or constructor.
90 + */
91 + @Test
92 + public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
93 + InstantiationException, IllegalAccessException, InvocationTargetException {
94 +
95 + Class<?>[] classesToConstruct = {YangIoUtils.class };
96 + for (Class<?> clazz : classesToConstruct) {
97 + Constructor<?> constructor = clazz.getDeclaredConstructor();
98 + constructor.setAccessible(true);
99 + assertNotNull(constructor.newInstance());
100 + }
101 + }
102 +
103 + /**
104 + * This test case checks if the directory is cleaned.
105 + */
106 + @Test
107 + public void cleanGeneratedDirTest() throws IOException {
108 +
109 + File baseDirPath = new File(baseDir);
110 + File createNewDir = new File(baseDir + File.separator + UtilConstants.YANG_GEN_DIR);
111 + createNewDir.mkdirs();
112 + File createFile = new File(createNewDir + File.separator + "check1.java");
113 + createFile.createNewFile();
114 + YangIoUtils.clean(baseDirPath.getAbsolutePath());
115 + }
116 +
117 + /**
118 + * This test case checks the cleaning method when an invalid path is provided.
119 + */
120 + @Test
121 + public void cleanWithInvalidDirTest() throws IOException {
122 +
123 + File baseDirPath = new File(baseDir + "invalid");
124 + YangIoUtils.clean(baseDirPath.getAbsolutePath());
125 + }
126 +
127 + /**
128 + * This test case tests whether the directories are getting created.
129 + */
130 + @Test
131 + public void createDirectoryTest() {
132 +
133 + File dirPath = YangIoUtils.createDirectories(createPath);
134 + assertThat(dirPath.isDirectory(), is(true));
135 + }
136 +
137 + /**
138 + * This testcase checks whether the source is getting added.
139 + */
140 + @Test
141 + public void testForAddSource() {
142 +
143 + MavenProject project = new MavenProject();
144 + BuildContext context = new DefaultBuildContext();
145 + File sourceDir = new File(baseDir + File.separator + "yang");
146 + sourceDir.mkdirs();
147 + YangIoUtils.addToSource(sourceDir.toString(), project, context);
148 + }
149 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
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 +