blob: 05ffa81e96e1caf04673a23ca68c1fa4a5e26f9f [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 saraswal2f00b4b2016-03-04 20:08:09 +053028
Bharat saraswal96dfef02016-06-16 00:29:12 +053029import static org.apache.commons.io.FileUtils.deleteDirectory;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053030import static org.hamcrest.core.Is.is;
31import static org.hamcrest.core.IsNot.not;
32import static org.junit.Assert.assertThat;
33import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
34import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
35import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053036import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
37import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.doesPackageExist;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053038import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
b.janani68c55e12016-02-24 12:23:03 +053039
40/**
41 * Tests the file handle utilities.
42 */
43public final class FileSystemUtilTest {
44
Bharat saraswale2d51d62016-03-23 19:40:35 +053045 private static final String BASE_DIR_PKG = "target.UnitTestCase.";
Bharat saraswale2d51d62016-03-23 19:40:35 +053046 private static final String BASE_PKG = "target/UnitTestCase";
47 private static final String TEST_DATA_1 = "This is to append a text to the file first1\n";
48 private static final String TEST_DATA_2 = "This is next second line\n";
49 private static final String TEST_DATA_3 = "This is next third line in the file";
Bharat saraswalc0e04842016-05-12 13:16:57 +053050 private static final String TEST_FILE = "testFile";
51 private static final String SOURCE_TEST_FILE = "sourceTestFile";
52 private static final String DIR_PATH = "exist1.exist2.exist3";
53 private static final String PKG_INFO = "package-info.java";
b.janani68c55e12016-02-24 12:23:03 +053054
55 /**
56 * A private constructor is tested.
57 *
Bharat saraswalc0e04842016-05-12 13:16:57 +053058 * @throws SecurityException if any security violation is observed
59 * @throws NoSuchMethodException if when the method is not found
60 * @throws IllegalArgumentException if there is illegal argument found
61 * @throws InstantiationException if instantiation is provoked for the private constructor
62 * @throws IllegalAccessException if instance is provoked or a method is provoked
Bharat saraswale2d51d62016-03-23 19:40:35 +053063 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani68c55e12016-02-24 12:23:03 +053064 */
65 @Test
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053066 public void callPrivateConstructors()
67 throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S38046502016-03-23 15:30:27 +053068 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani68c55e12016-02-24 12:23:03 +053069
Bharat saraswalc0e04842016-05-12 13:16:57 +053070 Class<?>[] classesToConstruct = {FileSystemUtil.class };
b.janani68c55e12016-02-24 12:23:03 +053071 for (Class<?> clazz : classesToConstruct) {
72 Constructor<?> constructor = clazz.getDeclaredConstructor();
73 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053074 assertThat(null, not(constructor.newInstance()));
b.janani68c55e12016-02-24 12:23:03 +053075 }
76 }
77
78 /**
b.janani68c55e12016-02-24 12:23:03 +053079 * This test case checks the contents to be written in the file.
Bharat saraswale2d51d62016-03-23 19:40:35 +053080 *
81 * @throws IOException when fails to create a test file
b.janani68c55e12016-02-24 12:23:03 +053082 */
83 @Test
Bharat saraswalc0e04842016-05-12 13:16:57 +053084 public void updateFileHandleTest() throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053085
Bharat saraswalc0e04842016-05-12 13:16:57 +053086 File dir = new File(BASE_PKG + SLASH + TEST_FILE);
b.janani68c55e12016-02-24 12:23:03 +053087 dir.mkdirs();
Bharat saraswalc0e04842016-05-12 13:16:57 +053088 File createFile = new File(dir + TEST_FILE);
b.janani68c55e12016-02-24 12:23:03 +053089 createFile.createNewFile();
Bharat saraswalc0e04842016-05-12 13:16:57 +053090 File createSourceFile = new File(dir + SOURCE_TEST_FILE);
b.janani68c55e12016-02-24 12:23:03 +053091 createSourceFile.createNewFile();
Bharat saraswal6ef0b762016-04-05 12:45:45 +053092 updateFileHandle(createFile, TEST_DATA_1, false);
93 updateFileHandle(createFile, TEST_DATA_2, false);
94 updateFileHandle(createFile, TEST_DATA_3, false);
95 appendFileContents(createFile, createSourceFile);
96 updateFileHandle(createFile, null, true);
Bharat saraswal96dfef02016-06-16 00:29:12 +053097 deleteDirectory(dir);
b.janani68c55e12016-02-24 12:23:03 +053098 }
99
100 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530101 * This test case checks whether the package is existing.
102 *
103 * @throws IOException when failed to create a test file
b.janani68c55e12016-02-24 12:23:03 +0530104 */
105 @Test
Bharat saraswalc0e04842016-05-12 13:16:57 +0530106 public void packageExistTest() throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530107
Bharat saraswalc0e04842016-05-12 13:16:57 +0530108 String strPath = BASE_DIR_PKG + DIR_PATH;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530109 File createDir = new File(strPath.replace(PERIOD, SLASH));
b.janani68c55e12016-02-24 12:23:03 +0530110 createDir.mkdirs();
Bharat saraswalc0e04842016-05-12 13:16:57 +0530111 File createFile = new File(createDir + SLASH + PKG_INFO);
b.janani68c55e12016-02-24 12:23:03 +0530112 createFile.createNewFile();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530113 assertThat(true, is(doesPackageExist(strPath)));
Bharat saraswalc0e04842016-05-12 13:16:57 +0530114 createPackage(getStubNode());
b.janani68c55e12016-02-24 12:23:03 +0530115 createDir.delete();
Bharat saraswal96dfef02016-06-16 00:29:12 +0530116 deleteDirectory(createDir);
b.janani68c55e12016-02-24 12:23:03 +0530117 }
118
Bharat saraswalc0e04842016-05-12 13:16:57 +0530119 /**
120 * Returns stub YANG node.
121 *
122 * @return stub node
123 */
124 private YangNode getStubNode() {
125 YangJavaModule module = new YangJavaModule();
126 module.setName(TEST_DATA_1);
127 JavaFileInfo javafileInfo = new JavaFileInfo();
128 javafileInfo.setJavaName(TEST_DATA_1);
129 javafileInfo.setBaseCodeGenPath("");
130 javafileInfo.setPackageFilePath(BASE_PKG);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530131 javafileInfo.setPluginConfig(getStubPluginConfig());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530132 module.setJavaFileInfo(javafileInfo);
133 return module;
134 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530135
136 /**
137 * Returns stub pluginConfig.
138 *
139 * @return stub pluginConfig
140 */
141 private YangPluginConfig getStubPluginConfig() {
142 YangPluginConfig pluginConfig = new YangPluginConfig();
143 pluginConfig.setConflictResolver(null);
144 return pluginConfig;
145 }
146
b.janani68c55e12016-02-24 12:23:03 +0530147}