b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.yangutils.utils.io.impl; |
| 18 | |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 19 | import java.io.File; |
| 20 | import java.io.IOException; |
| 21 | import java.lang.reflect.Constructor; |
| 22 | import java.lang.reflect.InvocationTargetException; |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 23 | |
| 24 | import org.apache.commons.io.FileUtils; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 25 | import org.junit.Test; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 26 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 27 | import static org.apache.commons.io.FileUtils.deleteDirectory; |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 28 | import static org.hamcrest.core.IsNot.not; |
| 29 | import static org.junit.Assert.assertThat; |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 30 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
| 31 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents; |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle; |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Tests the file handle utilities. |
| 36 | */ |
| 37 | public final class FileSystemUtilTest { |
| 38 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 39 | private static final String BASE_PKG = "target/UnitTestCase"; |
| 40 | private static final String TEST_DATA_1 = "This is to append a text to the file first1\n"; |
| 41 | private static final String TEST_DATA_2 = "This is next second line\n"; |
| 42 | private static final String TEST_DATA_3 = "This is next third line in the file"; |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 43 | private static final String TEST_FILE = "testFile"; |
| 44 | private static final String SOURCE_TEST_FILE = "sourceTestFile"; |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * A private constructor is tested. |
| 48 | * |
Gaurav Agrawal | c5f6327 | 2016-06-16 13:09:53 +0530 | [diff] [blame] | 49 | * @throws SecurityException if any security violation is observed |
| 50 | * @throws NoSuchMethodException if when the method is not found |
| 51 | * @throws IllegalArgumentException if there is illegal argument found |
| 52 | * @throws InstantiationException if instantiation is provoked for the private constructor |
| 53 | * @throws IllegalAccessException if instance is provoked or a method is provoked |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 54 | * @throws InvocationTargetException when an exception occurs by the method or constructor |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 55 | */ |
| 56 | @Test |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 57 | public void callPrivateConstructors() |
| 58 | throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 59 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 60 | |
Gaurav Agrawal | c5f6327 | 2016-06-16 13:09:53 +0530 | [diff] [blame] | 61 | Class<?>[] classesToConstruct = {FileSystemUtil.class}; |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 62 | for (Class<?> clazz : classesToConstruct) { |
| 63 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 64 | constructor.setAccessible(true); |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 65 | assertThat(null, not(constructor.newInstance())); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 70 | * This test case checks the contents to be written in the file. |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 71 | * |
| 72 | * @throws IOException when fails to create a test file |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 73 | */ |
| 74 | @Test |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 75 | public void updateFileHandleTest() throws IOException { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 76 | |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 77 | File dir = new File(BASE_PKG + SLASH + TEST_FILE); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 78 | dir.mkdirs(); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 79 | File createFile = new File(dir + TEST_FILE); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 80 | createFile.createNewFile(); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 81 | File createSourceFile = new File(dir + SOURCE_TEST_FILE); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 82 | createSourceFile.createNewFile(); |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 83 | updateFileHandle(createFile, TEST_DATA_1, false); |
| 84 | updateFileHandle(createFile, TEST_DATA_2, false); |
| 85 | updateFileHandle(createFile, TEST_DATA_3, false); |
| 86 | appendFileContents(createFile, createSourceFile); |
| 87 | updateFileHandle(createFile, null, true); |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 88 | deleteDirectory(dir); |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 89 | FileUtils.deleteDirectory(new File(BASE_PKG)); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 90 | } |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 91 | } |