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