blob: a41ed7d38b3d94befeb21134dc4da0e8c9bf0b15 [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
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.Rule;
25import org.junit.Test;
26import org.junit.rules.ExpectedException;
Bharat saraswal022dae92016-03-04 20:08:09 +053027import org.onosproject.yangutils.utils.UtilConstants;
Bharat saraswal022dae92016-03-04 20:08:09 +053028
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053029import static org.junit.Assert.assertNotNull;
30import static org.junit.Assert.assertTrue;
b.janani66b749c2016-02-24 12:23:03 +053031
32/**
33 * Tests the file handle utilities.
34 */
35public final class FileSystemUtilTest {
36
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053037 public static final String BASE_DIR_PKG = "target.UnitTestCase.";
38 public static final String PKG_INFO_CONTENT = "testGeneration6";
39 public static final String BASE_PKG = "target/UnitTestCase";
b.janani66b749c2016-02-24 12:23:03 +053040
41 @Rule
42 public ExpectedException thrown = ExpectedException.none();
43
44 /**
45 * A private constructor is tested.
46 *
Bharat saraswal022dae92016-03-04 20:08:09 +053047 * @throws SecurityException if any security violation is observed
48 * @throws NoSuchMethodException if when the method is not found
49 * @throws IllegalArgumentException if there is illegal argument found
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053050 * @throws InstantiationException if instantiation is provoked for the
51 * private constructor
52 * @throws IllegalAccessException if instance is provoked or a method is
53 * provoked
54 * @throws InvocationTargetException when an exception occurs by the method
55 * or constructor
b.janani66b749c2016-02-24 12:23:03 +053056 */
57 @Test
58 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053059 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani66b749c2016-02-24 12:23:03 +053060
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053061 Class<?>[] classesToConstruct = {
62 FileSystemUtil.class
63 };
b.janani66b749c2016-02-24 12:23:03 +053064 for (Class<?> clazz : classesToConstruct) {
65 Constructor<?> constructor = clazz.getDeclaredConstructor();
66 constructor.setAccessible(true);
67 assertNotNull(constructor.newInstance());
68 }
69 }
70
71 /**
b.janani66b749c2016-02-24 12:23:03 +053072 * This test case checks the contents to be written in the file.
73 */
74 @Test
Bharat saraswal022dae92016-03-04 20:08:09 +053075 public void updateFileHandleTest() throws IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053076
77 File dir = new File(BASE_PKG + File.separator + "File1");
b.janani66b749c2016-02-24 12:23:03 +053078 dir.mkdirs();
79 File createFile = new File(dir + "testFile");
80 createFile.createNewFile();
81 File createSourceFile = new File(dir + "sourceTestFile");
82 createSourceFile.createNewFile();
Bharat saraswal022dae92016-03-04 20:08:09 +053083 FileSystemUtil.updateFileHandle(createFile, "This is to append a text to the file first1\n", false);
84 FileSystemUtil.updateFileHandle(createFile, "This is next second line\n", false);
85 FileSystemUtil.updateFileHandle(createFile, "This is next third line in the file", false);
b.janani66b749c2016-02-24 12:23:03 +053086 FileSystemUtil.appendFileContents(createFile, createSourceFile);
Bharat saraswal022dae92016-03-04 20:08:09 +053087 FileSystemUtil.updateFileHandle(createFile, null, true);
b.janani66b749c2016-02-24 12:23:03 +053088 }
89
90 /**
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053091 * This test case checks whether the package is existing.
b.janani66b749c2016-02-24 12:23:03 +053092 */
93 @Test
94 public void packageExistTest() throws IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053095
b.janani66b749c2016-02-24 12:23:03 +053096 String dirPath = "exist1.exist2.exist3";
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053097 String strPath = BASE_DIR_PKG + dirPath;
b.janani66b749c2016-02-24 12:23:03 +053098 File createDir = new File(strPath.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
99 createDir.mkdirs();
100 File createFile = new File(createDir + File.separator + "package-info.java");
101 createFile.createNewFile();
102 assertTrue(FileSystemUtil.doesPackageExist(strPath));
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530103 FileSystemUtil.createPackage(strPath, PKG_INFO_CONTENT);
b.janani66b749c2016-02-24 12:23:03 +0530104 createDir.delete();
105 }
106
107 /**
108 * This test case checks the package does not exist.
109 */
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530110 // @Test
111 // public void packageNotExistTest() throws IOException {
112
113 // String dirPath = "notexist1.notexist2";
114 // String strPath = BASE_DIR_PKG + dirPath;
115 // File createDir = new File(strPath.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
116 // assertFalse(FileSystemUtil.doesPackageExist(strPath));
117 // createDir.mkdirs();
118 // assertFalse(FileSystemUtil.doesPackageExist(strPath));
119 // CopyrightHeader.parseCopyrightHeader();
120 // FileSystemUtil.createPackage(strPath, PKG_INFO_CONTENT);
121 // assertTrue(FileSystemUtil.doesPackageExist(strPath));
122 // createDir.delete();
123 // }
b.janani66b749c2016-02-24 12:23:03 +0530124}