blob: 531e73900ea651a4c1ab2d2c63b92a254fd00489 [file] [log] [blame]
b.janani66b749c2016-02-24 12:23:03 +05301/*
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
17package org.onosproject.yangutils.utils.io.impl;
18
19import org.junit.Test;
20import org.junit.Rule;
21import org.junit.rules.ExpectedException;
22import org.onosproject.yangutils.translator.GeneratedFileType;
23import org.onosproject.yangutils.utils.UtilConstants;
24
25import java.io.File;
26import java.io.IOException;
27import java.lang.reflect.Constructor;
28import java.lang.reflect.InvocationTargetException;
29
30import org.slf4j.Logger;
31import static org.slf4j.LoggerFactory.getLogger;
32import static org.junit.Assert.assertNotNull;
33import static org.junit.Assert.assertTrue;
34import static org.junit.Assert.assertFalse;
35
36/**
37 * Tests the file handle utilities.
38 */
39public 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}