blob: 5fd728b26c3461b4d89ed6d173cf46bca40f20b0 [file] [log] [blame]
b.janani68c55e12016-02-24 12:23:03 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
b.janani68c55e12016-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.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 saraswalc0e04842016-05-12 13:16:57 +053025import org.onosproject.yangutils.datamodel.YangNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053026import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
27import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
Bharat saraswal33dfa012016-05-17 19:59:16 +053028import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053029
Bharat saraswal96dfef02016-06-16 00:29:12 +053030import static org.apache.commons.io.FileUtils.deleteDirectory;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053031import static org.hamcrest.core.Is.is;
32import static org.hamcrest.core.IsNot.not;
33import static org.junit.Assert.assertThat;
34import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
35import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
36import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
37import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
38import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.doesPackageExist;
39import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
b.janani68c55e12016-02-24 12:23:03 +053040
41/**
42 * Tests the file handle utilities.
43 */
44public final class FileSystemUtilTest {
45
Bharat saraswale2d51d62016-03-23 19:40:35 +053046 private static final String BASE_DIR_PKG = "target.UnitTestCase.";
Bharat saraswale2d51d62016-03-23 19:40:35 +053047 private static final String BASE_PKG = "target/UnitTestCase";
48 private static final String TEST_DATA_1 = "This is to append a text to the file first1\n";
49 private static final String TEST_DATA_2 = "This is next second line\n";
50 private static final String TEST_DATA_3 = "This is next third line in the file";
Bharat saraswalc0e04842016-05-12 13:16:57 +053051 private static final String TEST_FILE = "testFile";
52 private static final String SOURCE_TEST_FILE = "sourceTestFile";
53 private static final String DIR_PATH = "exist1.exist2.exist3";
54 private static final String PKG_INFO = "package-info.java";
b.janani68c55e12016-02-24 12:23:03 +053055
56 /**
57 * A private constructor is tested.
58 *
Bharat saraswalc0e04842016-05-12 13:16:57 +053059 * @throws SecurityException if any security violation is observed
60 * @throws NoSuchMethodException if when the method is not found
61 * @throws IllegalArgumentException if there is illegal argument found
62 * @throws InstantiationException if instantiation is provoked for the private constructor
63 * @throws IllegalAccessException if instance is provoked or a method is provoked
Bharat saraswale2d51d62016-03-23 19:40:35 +053064 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani68c55e12016-02-24 12:23:03 +053065 */
66 @Test
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053067 public void callPrivateConstructors()
68 throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S38046502016-03-23 15:30:27 +053069 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani68c55e12016-02-24 12:23:03 +053070
Bharat saraswalc0e04842016-05-12 13:16:57 +053071 Class<?>[] classesToConstruct = {FileSystemUtil.class };
b.janani68c55e12016-02-24 12:23:03 +053072 for (Class<?> clazz : classesToConstruct) {
73 Constructor<?> constructor = clazz.getDeclaredConstructor();
74 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053075 assertThat(null, not(constructor.newInstance()));
b.janani68c55e12016-02-24 12:23:03 +053076 }
77 }
78
79 /**
b.janani68c55e12016-02-24 12:23:03 +053080 * This test case checks the contents to be written in the file.
Bharat saraswale2d51d62016-03-23 19:40:35 +053081 *
82 * @throws IOException when fails to create a test file
b.janani68c55e12016-02-24 12:23:03 +053083 */
84 @Test
Bharat saraswalc0e04842016-05-12 13:16:57 +053085 public void updateFileHandleTest() throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053086
Bharat saraswalc0e04842016-05-12 13:16:57 +053087 File dir = new File(BASE_PKG + SLASH + TEST_FILE);
b.janani68c55e12016-02-24 12:23:03 +053088 dir.mkdirs();
Bharat saraswalc0e04842016-05-12 13:16:57 +053089 File createFile = new File(dir + TEST_FILE);
b.janani68c55e12016-02-24 12:23:03 +053090 createFile.createNewFile();
Bharat saraswalc0e04842016-05-12 13:16:57 +053091 File createSourceFile = new File(dir + SOURCE_TEST_FILE);
b.janani68c55e12016-02-24 12:23:03 +053092 createSourceFile.createNewFile();
Bharat saraswal6ef0b762016-04-05 12:45:45 +053093 updateFileHandle(createFile, TEST_DATA_1, false);
94 updateFileHandle(createFile, TEST_DATA_2, false);
95 updateFileHandle(createFile, TEST_DATA_3, false);
96 appendFileContents(createFile, createSourceFile);
97 updateFileHandle(createFile, null, true);
Bharat saraswal96dfef02016-06-16 00:29:12 +053098 deleteDirectory(dir);
b.janani68c55e12016-02-24 12:23:03 +053099 }
100
101 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530102 * This test case checks whether the package is existing.
103 *
104 * @throws IOException when failed to create a test file
b.janani68c55e12016-02-24 12:23:03 +0530105 */
106 @Test
Bharat saraswalc0e04842016-05-12 13:16:57 +0530107 public void packageExistTest() throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530108
Bharat saraswalc0e04842016-05-12 13:16:57 +0530109 String strPath = BASE_DIR_PKG + DIR_PATH;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530110 File createDir = new File(strPath.replace(PERIOD, SLASH));
b.janani68c55e12016-02-24 12:23:03 +0530111 createDir.mkdirs();
Bharat saraswalc0e04842016-05-12 13:16:57 +0530112 File createFile = new File(createDir + SLASH + PKG_INFO);
b.janani68c55e12016-02-24 12:23:03 +0530113 createFile.createNewFile();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530114 assertThat(true, is(doesPackageExist(strPath)));
Bharat saraswalc0e04842016-05-12 13:16:57 +0530115 createPackage(getStubNode());
b.janani68c55e12016-02-24 12:23:03 +0530116 createDir.delete();
Bharat saraswal96dfef02016-06-16 00:29:12 +0530117 deleteDirectory(createDir);
b.janani68c55e12016-02-24 12:23:03 +0530118 }
119
Bharat saraswalc0e04842016-05-12 13:16:57 +0530120 /**
121 * Returns stub YANG node.
122 *
123 * @return stub node
124 */
125 private YangNode getStubNode() {
126 YangJavaModule module = new YangJavaModule();
127 module.setName(TEST_DATA_1);
128 JavaFileInfo javafileInfo = new JavaFileInfo();
129 javafileInfo.setJavaName(TEST_DATA_1);
130 javafileInfo.setBaseCodeGenPath("");
131 javafileInfo.setPackageFilePath(BASE_PKG);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530132 javafileInfo.setPluginConfig(getStubPluginConfig());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530133 module.setJavaFileInfo(javafileInfo);
134 return module;
135 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530136
137 /**
138 * Returns stub pluginConfig.
139 *
140 * @return stub pluginConfig
141 */
142 private YangPluginConfig getStubPluginConfig() {
143 YangPluginConfig pluginConfig = new YangPluginConfig();
144 pluginConfig.setConflictResolver(null);
145 return pluginConfig;
146 }
147
b.janani68c55e12016-02-24 12:23:03 +0530148}