blob: cfa1cf1e69abbce047a8aa91be99ae5c8cc8927b [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 +053025import org.onosproject.yangutils.utils.UtilConstants;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053026
Vinod Kumar S38046502016-03-23 15:30:27 +053027import static org.junit.Assert.assertNotNull;
28import static org.junit.Assert.assertTrue;
b.janani68c55e12016-02-24 12:23:03 +053029
30/**
31 * Tests the file handle utilities.
32 */
33public final class FileSystemUtilTest {
34
Bharat saraswale2d51d62016-03-23 19:40:35 +053035 private static final String BASE_DIR_PKG = "target.UnitTestCase.";
36 private static final String PKG_INFO_CONTENT = "testGeneration6";
37 private static final String BASE_PKG = "target/UnitTestCase";
38 private static final String TEST_DATA_1 = "This is to append a text to the file first1\n";
39 private static final String TEST_DATA_2 = "This is next second line\n";
40 private static final String TEST_DATA_3 = "This is next third line in the file";
b.janani68c55e12016-02-24 12:23:03 +053041
42 /**
43 * A private constructor is tested.
44 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053045 * @throws SecurityException if any security violation is observed
46 * @throws NoSuchMethodException if when the method is not found
47 * @throws IllegalArgumentException if there is illegal argument found
Bharat saraswale2d51d62016-03-23 19:40:35 +053048 * @throws InstantiationException if instantiation is provoked for the private constructor
49 * @throws IllegalAccessException if instance is provoked or a method is provoked
50 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani68c55e12016-02-24 12:23:03 +053051 */
52 @Test
53 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S38046502016-03-23 15:30:27 +053054 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani68c55e12016-02-24 12:23:03 +053055
Bharat saraswale2d51d62016-03-23 19:40:35 +053056 Class<?>[] classesToConstruct = {FileSystemUtil.class };
b.janani68c55e12016-02-24 12:23:03 +053057 for (Class<?> clazz : classesToConstruct) {
58 Constructor<?> constructor = clazz.getDeclaredConstructor();
59 constructor.setAccessible(true);
60 assertNotNull(constructor.newInstance());
61 }
62 }
63
64 /**
b.janani68c55e12016-02-24 12:23:03 +053065 * This test case checks the contents to be written in the file.
Bharat saraswale2d51d62016-03-23 19:40:35 +053066 *
67 * @throws IOException when fails to create a test file
b.janani68c55e12016-02-24 12:23:03 +053068 */
69 @Test
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053070 public void updateFileHandleTest() throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053071
72 File dir = new File(BASE_PKG + File.separator + "File1");
b.janani68c55e12016-02-24 12:23:03 +053073 dir.mkdirs();
74 File createFile = new File(dir + "testFile");
75 createFile.createNewFile();
76 File createSourceFile = new File(dir + "sourceTestFile");
77 createSourceFile.createNewFile();
Bharat saraswale2d51d62016-03-23 19:40:35 +053078 FileSystemUtil.updateFileHandle(createFile, TEST_DATA_1, false);
79 FileSystemUtil.updateFileHandle(createFile, TEST_DATA_2, false);
80 FileSystemUtil.updateFileHandle(createFile, TEST_DATA_3, false);
b.janani68c55e12016-02-24 12:23:03 +053081 FileSystemUtil.appendFileContents(createFile, createSourceFile);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053082 FileSystemUtil.updateFileHandle(createFile, null, true);
b.janani68c55e12016-02-24 12:23:03 +053083 }
84
85 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +053086 * This test case checks whether the package is existing.
87 *
88 * @throws IOException when failed to create a test file
b.janani68c55e12016-02-24 12:23:03 +053089 */
90 @Test
91 public void packageExistTest() throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053092
b.janani68c55e12016-02-24 12:23:03 +053093 String dirPath = "exist1.exist2.exist3";
Vinod Kumar S38046502016-03-23 15:30:27 +053094 String strPath = BASE_DIR_PKG + dirPath;
b.janani68c55e12016-02-24 12:23:03 +053095 File createDir = new File(strPath.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
96 createDir.mkdirs();
97 File createFile = new File(createDir + File.separator + "package-info.java");
98 createFile.createNewFile();
99 assertTrue(FileSystemUtil.doesPackageExist(strPath));
Vinod Kumar S38046502016-03-23 15:30:27 +0530100 FileSystemUtil.createPackage(strPath, PKG_INFO_CONTENT);
b.janani68c55e12016-02-24 12:23:03 +0530101 createDir.delete();
102 }
103
b.janani68c55e12016-02-24 12:23:03 +0530104}