Committed by
Gerrit Code Review
Unit Test Cases For YANG Io
Change-Id: Ie8876c25e4a293c52ae4c135921b7fe168f5f7c1
Showing
13 changed files
with
825 additions
and
43 deletions
| ... | @@ -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,21 +68,23 @@ public final class YangIoUtils { | ... | @@ -68,21 +68,23 @@ 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()) { | 80 | + FileWriter fileWriter = null; |
| 76 | - | 81 | + BufferedWriter bufferedWriter = null; |
| 77 | - FileWriter fileWriter = null; | 82 | + fileWriter = new FileWriter(packageInfo); |
| 78 | - BufferedWriter bufferedWriter = null; | 83 | + bufferedWriter = new BufferedWriter(fileWriter); |
| 79 | - fileWriter = new FileWriter(packageInfo); | 84 | + bufferedWriter.write(CopyrightHeader.getCopyrightHeader()); |
| 80 | - bufferedWriter = new BufferedWriter(fileWriter); | 85 | + bufferedWriter.write(JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.PACKAGE_INFO, classInfo)); |
| 81 | - bufferedWriter.write(CopyrightHeader.getCopyrightHeader()); | 86 | + bufferedWriter.write(UtilConstants.PACKAGE + UtilConstants.SPACE + pack + UtilConstants.SEMI_COLAN); |
| 82 | - bufferedWriter.write(JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.PACKAGE_INFO, classInfo)); | 87 | + bufferedWriter.close(); |
| 83 | - bufferedWriter.write(UtilConstants.PACKAGE + UtilConstants.SPACE + pack + UtilConstants.SEMI_COLAN); | ||
| 84 | - 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 |
utils/yangutils/src/test/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeaderTest.java
0 → 100644
| 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 |
utils/yangutils/src/test/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtilTest.java
0 → 100644
| 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 | +} |
utils/yangutils/src/test/java/org/onosproject/yangutils/utils/io/impl/JavaDocGenTest.java
0 → 100644
| 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 |
utils/yangutils/src/test/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStoreTest.java
0 → 100644
| 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 |
This diff is collapsed. Click to expand it.
utils/yangutils/src/test/java/org/onosproject/yangutils/utils/io/impl/YangIoUtilsTest.java
0 → 100644
| 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 | + |
-
Please register or login to post a comment