blob: c353b6d2133f5ef5bddb3f23aa1ace3f4098d0d8 [file] [log] [blame]
b.janani68c55e12016-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
b.janani68c55e12016-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 S38046502016-03-23 15:30:27 +053024import org.junit.Test;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053025
Bharat saraswal6ef0b762016-04-05 12:45:45 +053026import static org.hamcrest.core.Is.is;
27import static org.hamcrest.core.IsNot.not;
28import static org.junit.Assert.assertThat;
29import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
30import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
31import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
32import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
33import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.doesPackageExist;
34import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
b.janani68c55e12016-02-24 12:23:03 +053035
36/**
37 * Tests the file handle utilities.
38 */
39public final class FileSystemUtilTest {
40
Bharat saraswale2d51d62016-03-23 19:40:35 +053041 private static final String BASE_DIR_PKG = "target.UnitTestCase.";
42 private static final String PKG_INFO_CONTENT = "testGeneration6";
43 private static final String BASE_PKG = "target/UnitTestCase";
44 private static final String TEST_DATA_1 = "This is to append a text to the file first1\n";
45 private static final String TEST_DATA_2 = "This is next second line\n";
46 private static final String TEST_DATA_3 = "This is next third line in the file";
b.janani68c55e12016-02-24 12:23:03 +053047
48 /**
49 * A private constructor is tested.
50 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053051 * @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
Bharat saraswale2d51d62016-03-23 19:40:35 +053054 * @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
b.janani68c55e12016-02-24 12:23:03 +053057 */
58 @Test
59 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S38046502016-03-23 15:30:27 +053060 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani68c55e12016-02-24 12:23:03 +053061
Bharat saraswale2d51d62016-03-23 19:40:35 +053062 Class<?>[] classesToConstruct = {FileSystemUtil.class };
b.janani68c55e12016-02-24 12:23:03 +053063 for (Class<?> clazz : classesToConstruct) {
64 Constructor<?> constructor = clazz.getDeclaredConstructor();
65 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053066 assertThat(null, not(constructor.newInstance()));
b.janani68c55e12016-02-24 12:23:03 +053067 }
68 }
69
70 /**
b.janani68c55e12016-02-24 12:23:03 +053071 * This test case checks the contents to be written in the file.
Bharat saraswale2d51d62016-03-23 19:40:35 +053072 *
73 * @throws IOException when fails to create a test file
b.janani68c55e12016-02-24 12:23:03 +053074 */
75 @Test
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053076 public void updateFileHandleTest() throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053077
Bharat saraswal6ef0b762016-04-05 12:45:45 +053078 File dir = new File(BASE_PKG + SLASH + "File1");
b.janani68c55e12016-02-24 12:23:03 +053079 dir.mkdirs();
80 File createFile = new File(dir + "testFile");
81 createFile.createNewFile();
82 File createSourceFile = new File(dir + "sourceTestFile");
83 createSourceFile.createNewFile();
Bharat saraswal6ef0b762016-04-05 12:45:45 +053084 updateFileHandle(createFile, TEST_DATA_1, false);
85 updateFileHandle(createFile, TEST_DATA_2, false);
86 updateFileHandle(createFile, TEST_DATA_3, false);
87 appendFileContents(createFile, createSourceFile);
88 updateFileHandle(createFile, null, true);
b.janani68c55e12016-02-24 12:23:03 +053089 }
90
91 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +053092 * This test case checks whether the package is existing.
93 *
94 * @throws IOException when failed to create a test file
b.janani68c55e12016-02-24 12:23:03 +053095 */
96 @Test
97 public void packageExistTest() throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053098
b.janani68c55e12016-02-24 12:23:03 +053099 String dirPath = "exist1.exist2.exist3";
Vinod Kumar S38046502016-03-23 15:30:27 +0530100 String strPath = BASE_DIR_PKG + dirPath;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530101 File createDir = new File(strPath.replace(PERIOD, SLASH));
b.janani68c55e12016-02-24 12:23:03 +0530102 createDir.mkdirs();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530103 File createFile = new File(createDir + SLASH + "package-info.java");
b.janani68c55e12016-02-24 12:23:03 +0530104 createFile.createNewFile();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530105 assertThat(true, is(doesPackageExist(strPath)));
106 createPackage(strPath, PKG_INFO_CONTENT);
b.janani68c55e12016-02-24 12:23:03 +0530107 createDir.delete();
108 }
109
b.janani68c55e12016-02-24 12:23:03 +0530110}