b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.utils.io.impl; |
| 18 | |
| 19 | import org.junit.Test; |
| 20 | import org.junit.Rule; |
| 21 | import org.junit.rules.ExpectedException; |
| 22 | |
| 23 | import static org.hamcrest.core.Is.is; |
| 24 | import static org.junit.Assert.assertThat; |
| 25 | import static org.junit.Assert.assertNotNull; |
| 26 | import org.slf4j.Logger; |
| 27 | import static org.slf4j.LoggerFactory.getLogger; |
| 28 | |
| 29 | import java.io.BufferedReader; |
| 30 | import java.io.File; |
| 31 | import java.io.FileOutputStream; |
| 32 | import java.io.FileReader; |
| 33 | import java.io.FileWriter; |
| 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | import java.io.OutputStream; |
| 37 | import java.lang.reflect.Constructor; |
| 38 | import java.lang.reflect.InvocationTargetException; |
| 39 | |
| 40 | /** |
| 41 | * Unit Tests for the CopyrightHeader contents. |
| 42 | */ |
| 43 | public final class CopyrightHeaderTest { |
| 44 | |
| 45 | private final Logger log = getLogger(getClass()); |
| 46 | |
| 47 | @Rule |
| 48 | public ExpectedException thrown = ExpectedException.none(); |
| 49 | |
| 50 | /** |
| 51 | * Unit test for testing private constructor. |
| 52 | * |
| 53 | * @throws SecurityException if any security violation is observed. |
| 54 | * @throws NoSuchMethodException if when the method is not found. |
| 55 | * @throws IllegalArgumentException if there is illegal argument found. |
| 56 | * @throws InstantiationException if instantiation is provoked for the private constructor. |
| 57 | * @throws IllegalAccessException if instance is provoked or a method is provoked. |
| 58 | * @throws InvocationTargetException when an exception occurs by the method or constructor. |
| 59 | */ |
| 60 | @Test |
| 61 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
| 62 | InstantiationException, IllegalAccessException, InvocationTargetException { |
| 63 | |
| 64 | Class<?>[] classesToConstruct = {CopyrightHeader.class }; |
| 65 | for (Class<?> clazz : classesToConstruct) { |
| 66 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 67 | constructor.setAccessible(true); |
| 68 | assertNotNull(constructor.newInstance()); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * This test case checks the received copyright header contents. |
| 74 | */ |
| 75 | @Test |
| 76 | public void testGetCopyrightHeader() throws IOException { |
| 77 | |
| 78 | CopyrightHeader.parseCopyrightHeader(); |
| 79 | String licenseHeader = CopyrightHeader.getCopyrightHeader(); |
| 80 | ClassLoader classLoader = CopyrightHeaderTest.class.getClassLoader(); |
| 81 | File test = new File("target/TestCopyrightHeader.txt"); |
| 82 | |
| 83 | FileWriter out = new FileWriter(test); |
| 84 | out.write(licenseHeader); |
| 85 | out.close(); |
| 86 | |
| 87 | File temp = new File("target/temp.txt"); |
| 88 | InputStream stream = classLoader.getResourceAsStream("CopyrightHeader.txt"); |
| 89 | OutputStream outStream = new FileOutputStream(temp); |
| 90 | int i; |
| 91 | while ((i = stream.read()) != -1) { |
| 92 | outStream.write(i); |
| 93 | } |
| 94 | outStream.close(); |
| 95 | stream.close(); |
| 96 | |
| 97 | BufferedReader br1 = new BufferedReader(new FileReader(test)); |
| 98 | BufferedReader br2 = new BufferedReader(new FileReader(temp)); |
| 99 | while (br1.readLine() != null && br2.readLine() != null) { |
| 100 | assertThat(true, is((br1.readLine()).equals(br2.readLine()))); |
| 101 | } |
| 102 | br1.close(); |
| 103 | br2.close(); |
| 104 | } |
| 105 | } |