blob: 97302d81c8a72f32237ae2ff15c8d4fc621d6b71 [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;
31import org.slf4j.Logger;
32
33import static org.apache.commons.io.FileUtils.contentEquals;
34import static org.hamcrest.core.Is.is;
35import static org.hamcrest.core.IsNot.not;
36import static org.junit.Assert.assertThat;
37import static org.onosproject.yangutils.utils.io.impl.CopyrightHeader.getCopyrightHeader;
38import static org.slf4j.LoggerFactory.getLogger;
39
b.janani68c55e12016-02-24 12:23:03 +053040/**
41 * Unit Tests for the CopyrightHeader contents.
42 */
43public final class CopyrightHeaderTest {
44
45 private final Logger log = getLogger(getClass());
Bharat saraswald72411a2016-04-19 01:00:16 +053046 private static final String COPYRIGHTS_FIRST_LINE = "/*\n * Copyright " + Calendar.getInstance().get(Calendar.YEAR)
47 + "-present Open Networking Laboratory\n";
b.janani68c55e12016-02-24 12:23:03 +053048 @Rule
49 public ExpectedException thrown = ExpectedException.none();
50
51 /**
52 * Unit test for testing private constructor.
53 *
Vinod Kumar S38046502016-03-23 15:30:27 +053054 * @throws SecurityException if any security violation is observed
55 * @throws NoSuchMethodException if when the method is not found
56 * @throws IllegalArgumentException if there is illegal argument found
57 * @throws InstantiationException if instantiation is provoked for the private constructor
58 * @throws IllegalAccessException if instance is provoked or a method is provoked
59 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani68c55e12016-02-24 12:23:03 +053060 */
61 @Test
62 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Bharat saraswal6ef0b762016-04-05 12:45:45 +053063 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani68c55e12016-02-24 12:23:03 +053064
65 Class<?>[] classesToConstruct = {CopyrightHeader.class };
66 for (Class<?> clazz : classesToConstruct) {
67 Constructor<?> constructor = clazz.getDeclaredConstructor();
68 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053069 assertThat(null, not(constructor.newInstance()));
b.janani68c55e12016-02-24 12:23:03 +053070 }
71 }
72
73 /**
74 * This test case checks the received copyright header contents.
Bharat saraswal6ef0b762016-04-05 12:45:45 +053075 *
76 * @throws IOException when fails to do IO operations
b.janani68c55e12016-02-24 12:23:03 +053077 */
78 @Test
79 public void testGetCopyrightHeader() throws IOException {
80
Bharat saraswald72411a2016-04-19 01:00:16 +053081 String path = "src/test/resources/CopyrightHeader.txt";
82
83 File testRsc = new File(path);
84 FileInputStream in = new FileInputStream(testRsc);
85
86 File testFile = new File("target/TestHeader.txt");
87 FileOutputStream out = new FileOutputStream(testFile);
88
89 out.write(COPYRIGHTS_FIRST_LINE.getBytes());
90 int c = 0;
91 while ((c = in.read()) != -1) {
92 out.write(c);
93 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +053094
95 String licenseHeader = getCopyrightHeader();
b.janani68c55e12016-02-24 12:23:03 +053096 File test = new File("target/TestCopyrightHeader.txt");
97
Bharat saraswald72411a2016-04-19 01:00:16 +053098 FileWriter writer = new FileWriter(test);
99 writer.write(licenseHeader);
100 writer.close();
b.janani68c55e12016-02-24 12:23:03 +0530101 out.close();
Bharat saraswald72411a2016-04-19 01:00:16 +0530102 out.flush();
103 in.close();
b.janani68c55e12016-02-24 12:23:03 +0530104
Bharat saraswald72411a2016-04-19 01:00:16 +0530105 assertThat(true, is(contentEquals(test, testFile)));
b.janani68c55e12016-02-24 12:23:03 +0530106 }
107}