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; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 20 | import java.io.FileInputStream; |
| 21 | import java.io.FileOutputStream; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 22 | import java.io.FileWriter; |
| 23 | import java.io.IOException; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 24 | import java.lang.reflect.Constructor; |
| 25 | import java.lang.reflect.InvocationTargetException; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 26 | import java.util.Calendar; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 27 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 28 | import org.junit.Rule; |
| 29 | import org.junit.Test; |
| 30 | import org.junit.rules.ExpectedException; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 31 | |
| 32 | import static org.apache.commons.io.FileUtils.contentEquals; |
| 33 | import static org.hamcrest.core.Is.is; |
| 34 | import static org.hamcrest.core.IsNot.not; |
| 35 | import static org.junit.Assert.assertThat; |
| 36 | import static org.onosproject.yangutils.utils.io.impl.CopyrightHeader.getCopyrightHeader; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 37 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 38 | /** |
| 39 | * Unit Tests for the CopyrightHeader contents. |
| 40 | */ |
| 41 | public final class CopyrightHeaderTest { |
| 42 | |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 43 | private static final String COPYRIGHTS_FIRST_LINE = "/*\n * Copyright " + Calendar.getInstance().get(Calendar.YEAR) |
| 44 | + "-present Open Networking Laboratory\n"; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 45 | @Rule |
| 46 | public ExpectedException thrown = ExpectedException.none(); |
| 47 | |
| 48 | /** |
| 49 | * Unit test for testing private constructor. |
| 50 | * |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 51 | * @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.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 57 | */ |
| 58 | @Test |
| 59 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 60 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 61 | |
| 62 | Class<?>[] classesToConstruct = {CopyrightHeader.class }; |
| 63 | for (Class<?> clazz : classesToConstruct) { |
| 64 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 65 | constructor.setAccessible(true); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 66 | assertThat(null, not(constructor.newInstance())); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * This test case checks the received copyright header contents. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 72 | * |
| 73 | * @throws IOException when fails to do IO operations |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 74 | */ |
| 75 | @Test |
| 76 | public void testGetCopyrightHeader() throws IOException { |
| 77 | |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 78 | 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 saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 91 | |
| 92 | String licenseHeader = getCopyrightHeader(); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 93 | File test = new File("target/TestCopyrightHeader.txt"); |
| 94 | |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 95 | FileWriter writer = new FileWriter(test); |
| 96 | writer.write(licenseHeader); |
| 97 | writer.close(); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 98 | out.close(); |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 99 | out.flush(); |
| 100 | in.close(); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 101 | |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 102 | assertThat(true, is(contentEquals(test, testFile))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 103 | } |
| 104 | } |