b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
b.janani | 66b749c | 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 | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 19 | import java.io.File; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 20 | import java.io.FileWriter; |
| 21 | import java.io.IOException; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 22 | import java.lang.reflect.Constructor; |
| 23 | import java.lang.reflect.InvocationTargetException; |
| 24 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 25 | import org.junit.Rule; |
| 26 | import org.junit.Test; |
| 27 | import org.junit.rules.ExpectedException; |
| 28 | import org.slf4j.Logger; |
| 29 | |
| 30 | import static org.apache.commons.io.FileUtils.contentEquals; |
| 31 | import static org.hamcrest.core.Is.is; |
| 32 | import static org.hamcrest.core.IsNot.not; |
| 33 | import static org.junit.Assert.assertThat; |
| 34 | import static org.onosproject.yangutils.utils.io.impl.CopyrightHeader.getCopyrightHeader; |
| 35 | import static org.slf4j.LoggerFactory.getLogger; |
| 36 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 37 | /** |
| 38 | * Unit Tests for the CopyrightHeader contents. |
| 39 | */ |
| 40 | public final class CopyrightHeaderTest { |
| 41 | |
| 42 | private final Logger log = getLogger(getClass()); |
| 43 | |
| 44 | @Rule |
| 45 | public ExpectedException thrown = ExpectedException.none(); |
| 46 | |
| 47 | /** |
| 48 | * Unit test for testing private constructor. |
| 49 | * |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 50 | * @throws SecurityException if any security violation is observed |
| 51 | * @throws NoSuchMethodException if when the method is not found |
| 52 | * @throws IllegalArgumentException if there is illegal argument found |
| 53 | * @throws InstantiationException if instantiation is provoked for the private constructor |
| 54 | * @throws IllegalAccessException if instance is provoked or a method is provoked |
| 55 | * @throws InvocationTargetException when an exception occurs by the method or constructor |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 56 | */ |
| 57 | @Test |
| 58 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 59 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 60 | |
| 61 | Class<?>[] classesToConstruct = {CopyrightHeader.class }; |
| 62 | for (Class<?> clazz : classesToConstruct) { |
| 63 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 64 | constructor.setAccessible(true); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 65 | assertThat(null, not(constructor.newInstance())); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * This test case checks the received copyright header contents. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 71 | * |
| 72 | * @throws IOException when fails to do IO operations |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 73 | */ |
| 74 | @Test |
| 75 | public void testGetCopyrightHeader() throws IOException { |
| 76 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 77 | String baseDir = System.getProperty("basedir"); |
| 78 | String path = "/src/test/resources/CopyrightHeader.txt"; |
| 79 | |
| 80 | String licenseHeader = getCopyrightHeader(); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 81 | File test = new File("target/TestCopyrightHeader.txt"); |
| 82 | |
| 83 | FileWriter out = new FileWriter(test); |
| 84 | out.write(licenseHeader); |
| 85 | out.close(); |
| 86 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 87 | assertThat(true, is(contentEquals(test, new File(baseDir + path)))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 88 | } |
| 89 | } |