blob: 0d97cf00fd175937e7a70b86c07a93065d7bd345 [file] [log] [blame]
b.janani66b749c2016-02-24 12:23:03 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
b.janani66b749c2016-02-24 12:23:03 +05303 *
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
b.janani66b749c2016-02-24 12:23:03 +053019import java.io.File;
20import java.io.IOException;
21import java.lang.reflect.Constructor;
22import java.lang.reflect.InvocationTargetException;
23
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053024import org.junit.Test;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053025import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
26import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
Bharat saraswal022dae92016-03-04 20:08:09 +053027
Bharat saraswal780eca32016-04-05 12:45:45 +053028import static org.hamcrest.core.Is.is;
29import static org.hamcrest.core.IsNot.not;
30import static org.junit.Assert.assertThat;
31import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
32import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
33import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
34import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
35import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.doesPackageExist;
36import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
b.janani66b749c2016-02-24 12:23:03 +053037
38/**
39 * Tests the file handle utilities.
40 */
41public final class FileSystemUtilTest {
42
Bharat saraswal84366c52016-03-23 19:40:35 +053043 private static final String BASE_DIR_PKG = "target.UnitTestCase.";
44 private static final String PKG_INFO_CONTENT = "testGeneration6";
45 private static final String BASE_PKG = "target/UnitTestCase";
46 private static final String TEST_DATA_1 = "This is to append a text to the file first1\n";
47 private static final String TEST_DATA_2 = "This is next second line\n";
48 private static final String TEST_DATA_3 = "This is next third line in the file";
b.janani66b749c2016-02-24 12:23:03 +053049
50 /**
51 * A private constructor is tested.
52 *
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053053 * @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
Bharat saraswal84366c52016-03-23 19:40:35 +053058 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani66b749c2016-02-24 12:23:03 +053059 */
60 @Test
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053061 public void callPrivateConstructors()
62 throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053063 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani66b749c2016-02-24 12:23:03 +053064
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053065 Class<?>[] classesToConstruct = {FileSystemUtil.class};
b.janani66b749c2016-02-24 12:23:03 +053066 for (Class<?> clazz : classesToConstruct) {
67 Constructor<?> constructor = clazz.getDeclaredConstructor();
68 constructor.setAccessible(true);
Bharat saraswal780eca32016-04-05 12:45:45 +053069 assertThat(null, not(constructor.newInstance()));
b.janani66b749c2016-02-24 12:23:03 +053070 }
71 }
72
73 /**
b.janani66b749c2016-02-24 12:23:03 +053074 * This test case checks the contents to be written in the file.
Bharat saraswal84366c52016-03-23 19:40:35 +053075 *
76 * @throws IOException when fails to create a test file
b.janani66b749c2016-02-24 12:23:03 +053077 */
78 @Test
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053079 public void updateFileHandleTest()
80 throws IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053081
Bharat saraswal780eca32016-04-05 12:45:45 +053082 File dir = new File(BASE_PKG + SLASH + "File1");
b.janani66b749c2016-02-24 12:23:03 +053083 dir.mkdirs();
84 File createFile = new File(dir + "testFile");
85 createFile.createNewFile();
86 File createSourceFile = new File(dir + "sourceTestFile");
87 createSourceFile.createNewFile();
Bharat saraswal780eca32016-04-05 12:45:45 +053088 updateFileHandle(createFile, TEST_DATA_1, false);
89 updateFileHandle(createFile, TEST_DATA_2, false);
90 updateFileHandle(createFile, TEST_DATA_3, false);
91 appendFileContents(createFile, createSourceFile);
92 updateFileHandle(createFile, null, true);
b.janani66b749c2016-02-24 12:23:03 +053093 }
94
95 /**
Bharat saraswal84366c52016-03-23 19:40:35 +053096 * This test case checks whether the package is existing.
97 *
98 * @throws IOException when failed to create a test file
b.janani66b749c2016-02-24 12:23:03 +053099 */
100 @Test
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530101 public void packageExistTest()
102 throws IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530103
b.janani66b749c2016-02-24 12:23:03 +0530104 String dirPath = "exist1.exist2.exist3";
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530105 String strPath = BASE_DIR_PKG + dirPath;
Bharat saraswal780eca32016-04-05 12:45:45 +0530106 File createDir = new File(strPath.replace(PERIOD, SLASH));
b.janani66b749c2016-02-24 12:23:03 +0530107 createDir.mkdirs();
Bharat saraswal780eca32016-04-05 12:45:45 +0530108 File createFile = new File(createDir + SLASH + "package-info.java");
b.janani66b749c2016-02-24 12:23:03 +0530109 createFile.createNewFile();
Bharat saraswal780eca32016-04-05 12:45:45 +0530110 assertThat(true, is(doesPackageExist(strPath)));
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530111 JavaFileInfo javaFileInfo = new JavaFileInfo();
112 javaFileInfo.setBaseCodeGenPath(BASE_DIR_PKG);
113 javaFileInfo.setPackageFilePath(dirPath);
114 YangJavaModule moduleNode = new YangJavaModule();
115 moduleNode.setJavaFileInfo(javaFileInfo);
116 createPackage(moduleNode);
b.janani66b749c2016-02-24 12:23:03 +0530117 createDir.delete();
118 }
119
b.janani66b749c2016-02-24 12:23:03 +0530120}