blob: 032c48372b2344da595ef982ded25a164c4af844 [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;
22
23import static org.hamcrest.core.Is.is;
24import static org.junit.Assert.assertThat;
25import static org.junit.Assert.assertNotNull;
26import org.slf4j.Logger;
27import static org.slf4j.LoggerFactory.getLogger;
28
29import java.io.BufferedReader;
30import java.io.File;
31import java.io.FileOutputStream;
32import java.io.FileReader;
33import java.io.FileWriter;
34import java.io.IOException;
35import java.io.InputStream;
36import java.io.OutputStream;
37import java.lang.reflect.Constructor;
38import java.lang.reflect.InvocationTargetException;
39
40/**
41 * Unit Tests for the CopyrightHeader contents.
42 */
43public 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}