blob: 22a8cacd35bf14a721eb54dc0fd5539933377eb2 [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
Bharat saraswal022dae92016-03-04 20:08:09 +053019import static org.slf4j.LoggerFactory.getLogger;
20
b.janani66b749c2016-02-24 12:23:03 +053021import org.junit.Test;
22import org.junit.Rule;
23import org.junit.rules.ExpectedException;
Bharat saraswal022dae92016-03-04 20:08:09 +053024
25import static org.junit.Assert.assertNotNull;
26import static org.junit.Assert.assertTrue;
27import static org.junit.Assert.assertFalse;
b.janani66b749c2016-02-24 12:23:03 +053028
29import java.io.File;
30import java.io.IOException;
31import java.lang.reflect.Constructor;
32import java.lang.reflect.InvocationTargetException;
33
Bharat saraswal022dae92016-03-04 20:08:09 +053034import org.onosproject.yangutils.translator.GeneratedFileType;
35import org.onosproject.yangutils.utils.UtilConstants;
b.janani66b749c2016-02-24 12:23:03 +053036import org.slf4j.Logger;
Bharat saraswal022dae92016-03-04 20:08:09 +053037
b.janani66b749c2016-02-24 12:23:03 +053038
39/**
40 * Tests the file handle utilities.
41 */
42public final class FileSystemUtilTest {
43
44 public static String baseDirPkg = "target.UnitTestCase.";
45 public static String packageInfoContent = "testGeneration6";
46 public static String baseDir = "target/UnitTestCase";
47
48 private final Logger log = getLogger(getClass());
49
50 @Rule
51 public ExpectedException thrown = ExpectedException.none();
52
53 /**
54 * A private constructor is tested.
55 *
Bharat saraswal022dae92016-03-04 20:08:09 +053056 * @throws SecurityException if any security violation is observed
57 * @throws NoSuchMethodException if when the method is not found
58 * @throws IllegalArgumentException if there is illegal argument found
59 * @throws InstantiationException if instantiation is provoked for the private constructor
60 * @throws IllegalAccessException if instance is provoked or a method is provoked
61 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani66b749c2016-02-24 12:23:03 +053062 */
63 @Test
64 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
65 InstantiationException, IllegalAccessException, InvocationTargetException {
66
67 Class<?>[] classesToConstruct = {FileSystemUtil.class};
68 for (Class<?> clazz : classesToConstruct) {
69 Constructor<?> constructor = clazz.getDeclaredConstructor();
70 constructor.setAccessible(true);
71 assertNotNull(constructor.newInstance());
72 }
73 }
74
75 /**
76 * This test case checks the creation of source files.
77 */
78 @Test
79 public void createSourceFilesTest() throws IOException {
80
Vinod Kumar S08710982016-03-03 19:55:30 +053081 FileSystemUtil.createSourceFiles(baseDirPkg + "srcFile1", packageInfoContent, GeneratedFileType.INTERFACE_MASK);
b.janani66b749c2016-02-24 12:23:03 +053082 }
83
84 /**
85 * This test case checks the contents to be written in the file.
86 */
87 @Test
Bharat saraswal022dae92016-03-04 20:08:09 +053088 public void updateFileHandleTest() throws IOException {
b.janani66b749c2016-02-24 12:23:03 +053089 File dir = new File(baseDir + File.separator + "File1");
90 dir.mkdirs();
91 File createFile = new File(dir + "testFile");
92 createFile.createNewFile();
93 File createSourceFile = new File(dir + "sourceTestFile");
94 createSourceFile.createNewFile();
Bharat saraswal022dae92016-03-04 20:08:09 +053095 FileSystemUtil.updateFileHandle(createFile, "This is to append a text to the file first1\n", false);
96 FileSystemUtil.updateFileHandle(createFile, "This is next second line\n", false);
97 FileSystemUtil.updateFileHandle(createFile, "This is next third line in the file", false);
b.janani66b749c2016-02-24 12:23:03 +053098 FileSystemUtil.appendFileContents(createFile, createSourceFile);
Bharat saraswal022dae92016-03-04 20:08:09 +053099 FileSystemUtil.updateFileHandle(createFile, null, true);
b.janani66b749c2016-02-24 12:23:03 +0530100 }
101
102 /**
103 * This test case checks whether the package is existing.
104 */
105 @Test
106 public void packageExistTest() throws IOException {
107 String dirPath = "exist1.exist2.exist3";
108 String strPath = baseDirPkg + dirPath;
109 File createDir = new File(strPath.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
110 createDir.mkdirs();
111 File createFile = new File(createDir + File.separator + "package-info.java");
112 createFile.createNewFile();
113 assertTrue(FileSystemUtil.doesPackageExist(strPath));
114 FileSystemUtil.createPackage(strPath, packageInfoContent);
115 createDir.delete();
116 }
117
118 /**
119 * This test case checks the package does not exist.
120 */
121 @Test
122 public void packageNotExistTest() throws IOException {
123 String dirPath = "notexist1.notexist2";
124 String strPath = baseDirPkg + dirPath;
125 File createDir = new File(strPath.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
126 assertFalse(FileSystemUtil.doesPackageExist(strPath));
127 createDir.mkdirs();
128 assertFalse(FileSystemUtil.doesPackageExist(strPath));
129 CopyrightHeader.parseCopyrightHeader();
130 FileSystemUtil.createPackage(strPath, packageInfoContent);
131 assertTrue(FileSystemUtil.doesPackageExist(strPath));
132 createDir.delete();
133 }
134}