blob: 8359ef31ac049db3729c55fa1b03f172e38e81e2 [file] [log] [blame]
b.janani68c55e12016-02-24 12:23:03 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
b.janani68c55e12016-02-24 12:23:03 +05303 *
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
b.janani68c55e12016-02-24 12:23:03 +053019import java.io.File;
Bharat saraswald72411a2016-04-19 01:00:16 +053020import java.io.FileInputStream;
21import java.io.FileOutputStream;
b.janani68c55e12016-02-24 12:23:03 +053022import java.io.FileWriter;
23import java.io.IOException;
b.janani68c55e12016-02-24 12:23:03 +053024import java.lang.reflect.Constructor;
25import java.lang.reflect.InvocationTargetException;
Bharat saraswald72411a2016-04-19 01:00:16 +053026import java.util.Calendar;
b.janani68c55e12016-02-24 12:23:03 +053027
Bharat saraswal6ef0b762016-04-05 12:45:45 +053028import org.junit.Rule;
29import org.junit.Test;
30import org.junit.rules.ExpectedException;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053031
32import static org.apache.commons.io.FileUtils.contentEquals;
33import static org.hamcrest.core.Is.is;
34import static org.hamcrest.core.IsNot.not;
35import static org.junit.Assert.assertThat;
36import static org.onosproject.yangutils.utils.io.impl.CopyrightHeader.getCopyrightHeader;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053037
b.janani68c55e12016-02-24 12:23:03 +053038/**
39 * Unit Tests for the CopyrightHeader contents.
40 */
41public final class CopyrightHeaderTest {
42
Bharat saraswald72411a2016-04-19 01:00:16 +053043 private static final String COPYRIGHTS_FIRST_LINE = "/*\n * Copyright " + Calendar.getInstance().get(Calendar.YEAR)
44 + "-present Open Networking Laboratory\n";
b.janani68c55e12016-02-24 12:23:03 +053045 @Rule
46 public ExpectedException thrown = ExpectedException.none();
47
48 /**
49 * Unit test for testing private constructor.
50 *
Vinod Kumar S38046502016-03-23 15:30:27 +053051 * @throws SecurityException if any security violation is observed
52 * @throws NoSuchMethodException if when the method is not found
53 * @throws IllegalArgumentException if there is illegal argument found
54 * @throws InstantiationException if instantiation is provoked for the private constructor
55 * @throws IllegalAccessException if instance is provoked or a method is provoked
56 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani68c55e12016-02-24 12:23:03 +053057 */
58 @Test
59 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Bharat saraswal6ef0b762016-04-05 12:45:45 +053060 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani68c55e12016-02-24 12:23:03 +053061
62 Class<?>[] classesToConstruct = {CopyrightHeader.class };
63 for (Class<?> clazz : classesToConstruct) {
64 Constructor<?> constructor = clazz.getDeclaredConstructor();
65 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053066 assertThat(null, not(constructor.newInstance()));
b.janani68c55e12016-02-24 12:23:03 +053067 }
68 }
69
70 /**
71 * This test case checks the received copyright header contents.
Bharat saraswal6ef0b762016-04-05 12:45:45 +053072 *
73 * @throws IOException when fails to do IO operations
b.janani68c55e12016-02-24 12:23:03 +053074 */
75 @Test
76 public void testGetCopyrightHeader() throws IOException {
77
Bharat saraswald72411a2016-04-19 01:00:16 +053078 String path = "src/test/resources/CopyrightHeader.txt";
79
80 File testRsc = new File(path);
81 FileInputStream in = new FileInputStream(testRsc);
82
83 File testFile = new File("target/TestHeader.txt");
84 FileOutputStream out = new FileOutputStream(testFile);
85
86 out.write(COPYRIGHTS_FIRST_LINE.getBytes());
87 int c = 0;
88 while ((c = in.read()) != -1) {
89 out.write(c);
90 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +053091
92 String licenseHeader = getCopyrightHeader();
b.janani68c55e12016-02-24 12:23:03 +053093 File test = new File("target/TestCopyrightHeader.txt");
94
Bharat saraswald72411a2016-04-19 01:00:16 +053095 FileWriter writer = new FileWriter(test);
96 writer.write(licenseHeader);
97 writer.close();
b.janani68c55e12016-02-24 12:23:03 +053098 out.close();
Bharat saraswald72411a2016-04-19 01:00:16 +053099 out.flush();
100 in.close();
b.janani68c55e12016-02-24 12:23:03 +0530101
Bharat saraswald72411a2016-04-19 01:00:16 +0530102 assertThat(true, is(contentEquals(test, testFile)));
b.janani68c55e12016-02-24 12:23:03 +0530103 }
104}