View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.shade.resource;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.IOException;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.maven.plugins.shade.relocation.Relocator;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  import static org.junit.Assert.fail;
31  
32  /**
33   * Tests {@link ApacheLicenseResourceTransformer} parameters.
34   */
35  public class ApacheNoticeResourceTransformerParameterTests {
36  
37      private static final String NOTICE_RESOURCE = "META-INF/NOTICE";
38      private ApacheNoticeResourceTransformer subject;
39  
40      @Before
41      public void setUp() {
42          subject = new ApacheNoticeResourceTransformer();
43      }
44  
45      @Test
46      public void testNoParametersShouldNotThrowNullPointerWhenNoInput() throws IOException {
47          processAndFailOnNullPointer("");
48      }
49  
50      @Test
51      public void testNoParametersShouldNotThrowNullPointerWhenNoLinesOfInput() throws IOException {
52          processAndFailOnNullPointer("Some notice text");
53      }
54  
55      @Test
56      public void testNoParametersShouldNotThrowNullPointerWhenOneLineOfInput() throws IOException {
57          processAndFailOnNullPointer("Some notice text\n");
58      }
59  
60      @Test
61      public void testNoParametersShouldNotThrowNullPointerWhenTwoLinesOfInput() throws IOException {
62          processAndFailOnNullPointer("Some notice text\nSome notice text\n");
63      }
64  
65      @Test
66      public void testNoParametersShouldNotThrowNullPointerWhenLineStartsWithSlashSlash() throws IOException {
67          processAndFailOnNullPointer("Some notice text\n//Some notice text\n");
68      }
69  
70      @Test
71      public void testNoParametersShouldNotThrowNullPointerWhenLineIsSlashSlash() throws IOException {
72          processAndFailOnNullPointer("//\n");
73      }
74  
75      @Test
76      public void testNoParametersShouldNotThrowNullPointerWhenLineIsEmpty() throws IOException {
77          processAndFailOnNullPointer("\n");
78      }
79  
80      private void processAndFailOnNullPointer(final String noticeText) throws IOException {
81          try {
82              final ByteArrayInputStream noticeInputStream = new ByteArrayInputStream(noticeText.getBytes());
83              final List<Relocator> emptyList = Collections.emptyList();
84              subject.processResource(NOTICE_RESOURCE, noticeInputStream, emptyList, 0);
85              noticeInputStream.close();
86          } catch (NullPointerException e) {
87              fail("Null pointer should not be thrown when no parameters are set.");
88          }
89      }
90  }