blob: 2cfc3b914efb9ceabcfe9cc8b31c431a0defde26 [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
19import org.junit.Test;
20import org.junit.Rule;
21import org.junit.rules.ExpectedException;
22import org.onosproject.yangutils.utils.UtilConstants;
23
24import java.io.File;
25import java.io.IOException;
26
27import org.apache.maven.project.MavenProject;
28import org.sonatype.plexus.build.incremental.BuildContext;
29import org.sonatype.plexus.build.incremental.DefaultBuildContext;
30
31import org.slf4j.Logger;
32import static org.slf4j.LoggerFactory.getLogger;
33import static org.junit.Assert.assertThat;
34import static org.junit.Assert.assertNotNull;
35import static org.hamcrest.core.Is.is;
36import java.lang.reflect.Constructor;
37import java.lang.reflect.InvocationTargetException;
38
39/**
40 * Unit tests for adding package-info, creating directories, cleaning the folder and to add sources.
41 */
42public final class YangIoUtilsTest {
43
44 public static String baseDir = "target/UnitTestCase";
45
46 public static String createPath = baseDir + File.separator + "dir1/dir2/dir3/dir4/";
47
48 private final Logger log = getLogger(getClass());
49
50 @Rule
51 public ExpectedException thrown = ExpectedException.none();
52
53 /**
54 * This test case checks whether the package-info file is created.
55 */
56 @Test
57 public void addPackageInfoTest() throws IOException {
58
59 File dirPath = new File(createPath);
60 dirPath.mkdirs();
61 CopyrightHeader.parseCopyrightHeader();
62 YangIoUtils.addPackageInfo(dirPath, "check1", createPath);
63 File filePath = new File(dirPath + File.separator + "package-info.java");
64 assertThat(filePath.isFile(), is(true));
65 }
66
67 /**
68 * This test case checks whether the package-info file is created when invalid path is given.
69 */
70 @Test
71 public void addPackageInfoWithEmptyPathTest() throws IOException {
72
b.janani1fef2732016-03-04 12:29:05 +053073 File dirPath = new File("invalid/check");
b.janani68c55e12016-02-24 12:23:03 +053074 thrown.expect(IOException.class);
75 thrown.expectMessage("Exception occured while creating package info file.");
76 YangIoUtils.addPackageInfo(dirPath, "check1", createPath);
77 File filePath1 = new File(dirPath + File.separator + "package-info.java");
78 assertThat(filePath1.isFile(), is(false));
79 }
80
81 /**
82 * A private constructor is tested.
83 *
84 * @throws SecurityException if any security violation is observed.
85 * @throws NoSuchMethodException if when the method is not found.
86 * @throws IllegalArgumentException if there is illegal argument found.
87 * @throws InstantiationException if instantiation is provoked for the private constructor.
88 * @throws IllegalAccessException if instance is provoked or a method is provoked.
89 * @throws InvocationTargetException when an exception occurs by the method or constructor.
90 */
91 @Test
92 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
93 InstantiationException, IllegalAccessException, InvocationTargetException {
94
95 Class<?>[] classesToConstruct = {YangIoUtils.class };
96 for (Class<?> clazz : classesToConstruct) {
97 Constructor<?> constructor = clazz.getDeclaredConstructor();
98 constructor.setAccessible(true);
99 assertNotNull(constructor.newInstance());
100 }
101 }
102
103 /**
104 * This test case checks if the directory is cleaned.
105 */
106 @Test
107 public void cleanGeneratedDirTest() throws IOException {
108
109 File baseDirPath = new File(baseDir);
110 File createNewDir = new File(baseDir + File.separator + UtilConstants.YANG_GEN_DIR);
111 createNewDir.mkdirs();
112 File createFile = new File(createNewDir + File.separator + "check1.java");
113 createFile.createNewFile();
114 YangIoUtils.clean(baseDirPath.getAbsolutePath());
115 }
116
117 /**
118 * This test case checks the cleaning method when an invalid path is provided.
119 */
120 @Test
121 public void cleanWithInvalidDirTest() throws IOException {
122
123 File baseDirPath = new File(baseDir + "invalid");
124 YangIoUtils.clean(baseDirPath.getAbsolutePath());
125 }
126
127 /**
128 * This test case tests whether the directories are getting created.
129 */
130 @Test
131 public void createDirectoryTest() {
132
133 File dirPath = YangIoUtils.createDirectories(createPath);
134 assertThat(dirPath.isDirectory(), is(true));
135 }
136
137 /**
138 * This testcase checks whether the source is getting added.
139 */
140 @Test
141 public void testForAddSource() {
142
143 MavenProject project = new MavenProject();
144 BuildContext context = new DefaultBuildContext();
145 File sourceDir = new File(baseDir + File.separator + "yang");
146 sourceDir.mkdirs();
147 YangIoUtils.addToSource(sourceDir.toString(), project, context);
148 }
149}