Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 1 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2016 Open Networking Laboratory |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | package org.onosproject.yangutils.utils.io.impl; |
| 19 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 20 | import java.io.File; |
| 21 | import java.io.IOException; |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 22 | import java.lang.reflect.Constructor; |
| 23 | import java.lang.reflect.InvocationTargetException; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 24 | |
| 25 | import org.apache.maven.project.MavenProject; |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 26 | import org.junit.Rule; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.rules.ExpectedException; |
| 29 | import org.onosproject.yangutils.utils.UtilConstants; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 30 | import org.sonatype.plexus.build.incremental.BuildContext; |
| 31 | import org.sonatype.plexus.build.incremental.DefaultBuildContext; |
| 32 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 33 | import static org.hamcrest.core.Is.is; |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 34 | import static org.junit.Assert.assertNotNull; |
| 35 | import static org.junit.Assert.assertThat; |
| 36 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo; |
| 37 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource; |
| 38 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean; |
| 39 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories; |
| 40 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 41 | |
| 42 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 43 | * Unit tests for YANG io utils. |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 44 | */ |
| 45 | public final class YangIoUtilsTest { |
| 46 | |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 47 | private static final String BASE_DIR = "target/UnitTestCase"; |
| 48 | private static final String CREATE_PATH = BASE_DIR + File.separator + "dir1/dir2/dir3/dir4/"; |
| 49 | private static final String CHECK_STRING = "one, two, three, four, five, six"; |
| 50 | private static final String TRIM_STRING = "one, two, three, four, five, "; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 51 | |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 52 | /** |
| 53 | * Expected exceptions. |
| 54 | */ |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 55 | @Rule |
| 56 | public ExpectedException thrown = ExpectedException.none(); |
| 57 | |
| 58 | /** |
| 59 | * This test case checks whether the package-info file is created. |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 60 | * |
| 61 | * @throws IOException when fails to do IO operations for test case |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 62 | */ |
| 63 | @Test |
| 64 | public void addPackageInfoTest() throws IOException { |
| 65 | |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 66 | File dirPath = new File(CREATE_PATH); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 67 | dirPath.mkdirs(); |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 68 | addPackageInfo(dirPath, "check1", CREATE_PATH); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 69 | File filePath = new File(dirPath + File.separator + "package-info.java"); |
| 70 | assertThat(filePath.isFile(), is(true)); |
| 71 | } |
| 72 | |
| 73 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 74 | * This test case checks with an additional info in the path. |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 75 | * |
| 76 | * @throws IOException when fails to do IO operations for test case |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 77 | */ |
| 78 | @Test |
| 79 | public void addPackageInfoWithPathTest() throws IOException { |
| 80 | |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 81 | File dirPath = new File(CREATE_PATH); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 82 | dirPath.mkdirs(); |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 83 | addPackageInfo(dirPath, "check1", "src/main/yangmodel/" + CREATE_PATH); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 84 | File filePath = new File(dirPath + File.separator + "package-info.java"); |
| 85 | assertThat(filePath.isFile(), is(true)); |
| 86 | } |
| 87 | |
| 88 | /** |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 89 | * This test case checks whether the package-info file is created when invalid path is given. |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 90 | * |
| 91 | * @throws IOException when fails to do IO operations for test case |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 92 | */ |
| 93 | @Test |
| 94 | public void addPackageInfoWithEmptyPathTest() throws IOException { |
| 95 | |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 96 | File dirPath = new File("invalid/check"); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 97 | thrown.expect(IOException.class); |
| 98 | thrown.expectMessage("Exception occured while creating package info file."); |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 99 | addPackageInfo(dirPath, "check1", CREATE_PATH); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 100 | File filePath1 = new File(dirPath + File.separator + "package-info.java"); |
| 101 | assertThat(filePath1.isFile(), is(false)); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * A private constructor is tested. |
| 106 | * |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 107 | * @throws SecurityException if any security violation is observed |
| 108 | * @throws NoSuchMethodException if when the method is not found |
| 109 | * @throws IllegalArgumentException if there is illegal argument found |
| 110 | * @throws InstantiationException if instantiation is provoked for the private constructor |
| 111 | * @throws IllegalAccessException if instance is provoked or a method is provoked |
| 112 | * @throws InvocationTargetException when an exception occurs by the method or constructor |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 113 | */ |
| 114 | @Test |
| 115 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 116 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 117 | |
| 118 | Class<?>[] classesToConstruct = {YangIoUtils.class }; |
| 119 | for (Class<?> clazz : classesToConstruct) { |
| 120 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 121 | constructor.setAccessible(true); |
| 122 | assertNotNull(constructor.newInstance()); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * This test case checks if the directory is cleaned. |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 128 | * |
| 129 | * @throws IOException when fails to do IO operations for test case |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 130 | */ |
| 131 | @Test |
| 132 | public void cleanGeneratedDirTest() throws IOException { |
| 133 | |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 134 | File baseDirPath = new File(BASE_DIR); |
| 135 | File createNewDir = new File(BASE_DIR + File.separator + UtilConstants.YANG_GEN_DIR); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 136 | createNewDir.mkdirs(); |
| 137 | File createFile = new File(createNewDir + File.separator + "check1.java"); |
| 138 | createFile.createNewFile(); |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 139 | clean(baseDirPath.getAbsolutePath()); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
| 143 | * This test case checks the cleaning method when an invalid path is provided. |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 144 | * |
| 145 | * @throws IOException when fails to do IO operations for test case |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 146 | */ |
| 147 | @Test |
| 148 | public void cleanWithInvalidDirTest() throws IOException { |
| 149 | |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 150 | File baseDirPath = new File(BASE_DIR + "invalid"); |
| 151 | clean(baseDirPath.getAbsolutePath()); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * This test case tests whether the directories are getting created. |
| 156 | */ |
| 157 | @Test |
| 158 | public void createDirectoryTest() { |
| 159 | |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 160 | File dirPath = createDirectories(CREATE_PATH); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 161 | assertThat(dirPath.isDirectory(), is(true)); |
| 162 | } |
| 163 | |
| 164 | /** |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 165 | * This test case checks whether the source is getting added. |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 166 | */ |
| 167 | @Test |
| 168 | public void testForAddSource() { |
| 169 | |
| 170 | MavenProject project = new MavenProject(); |
| 171 | BuildContext context = new DefaultBuildContext(); |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 172 | File sourceDir = new File(BASE_DIR + File.separator + "yang"); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 173 | sourceDir.mkdirs(); |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 174 | addToSource(sourceDir.toString(), project, context); |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Unit test case for trim at last method. |
| 179 | */ |
| 180 | @Test |
| 181 | public void testForTrimAtLast() { |
| 182 | |
| 183 | String test = trimAtLast(CHECK_STRING, "six"); |
| 184 | assertThat(test.contains(TRIM_STRING), is(true)); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 185 | } |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 186 | } |