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  import java.util.Locale;
26  
27  import org.apache.maven.plugins.shade.relocation.Relocator;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  import static org.junit.Assert.assertFalse;
32  import static org.junit.Assert.assertTrue;
33  import static org.junit.Assert.fail;
34  
35  /**
36   * Test for {@link ApacheNoticeResourceTransformer}.
37   *
38   * @author Benjamin Bentmann
39   */
40  public class ApacheNoticeResourceTransformerTest {
41  
42      private static final String NOTICE_RESOURCE = "META-INF/NOTICE";
43      private ApacheNoticeResourceTransformer transformer;
44  
45      static {
46          /*
47           * NOTE: The Turkish locale has an usual case transformation for the letters "I" and "i", making it a prime
48           * choice to test for improper case-less string comparisions.
49           */
50          Locale.setDefault(new Locale("tr"));
51      }
52  
53      @Before
54      public void setUp() {
55          transformer = new ApacheNoticeResourceTransformer();
56      }
57  
58      @Test
59      public void testCanTransformResource() {
60          assertTrue(transformer.canTransformResource("META-INF/NOTICE"));
61          assertTrue(transformer.canTransformResource("META-INF/NOTICE.TXT"));
62          assertTrue(transformer.canTransformResource("META-INF/NOTICE.md"));
63          assertTrue(transformer.canTransformResource("META-INF/Notice.txt"));
64          assertTrue(transformer.canTransformResource("META-INF/Notice.md"));
65          assertFalse(transformer.canTransformResource("META-INF/MANIFEST.MF"));
66      }
67  
68      @Test
69      public void testNoParametersShouldNotThrowNullPointerWhenNoInput() throws IOException {
70          processAndFailOnNullPointer("");
71      }
72  
73      @Test
74      public void testNoParametersShouldNotThrowNullPointerWhenNoLinesOfInput() throws IOException {
75          processAndFailOnNullPointer("Some notice text");
76      }
77  
78      @Test
79      public void testNoParametersShouldNotThrowNullPointerWhenOneLineOfInput() throws IOException {
80          processAndFailOnNullPointer("Some notice text\n");
81      }
82  
83      @Test
84      public void testNoParametersShouldNotThrowNullPointerWhenTwoLinesOfInput() throws IOException {
85          processAndFailOnNullPointer("Some notice text\nSome notice text\n");
86      }
87  
88      @Test
89      public void testNoParametersShouldNotThrowNullPointerWhenLineStartsWithSlashSlash() throws IOException {
90          processAndFailOnNullPointer("Some notice text\n//Some notice text\n");
91      }
92  
93      @Test
94      public void testNoParametersShouldNotThrowNullPointerWhenLineIsSlashSlash() throws IOException {
95          processAndFailOnNullPointer("//\n");
96      }
97  
98      @Test
99      public void testNoParametersShouldNotThrowNullPointerWhenLineIsEmpty() throws IOException {
100         processAndFailOnNullPointer("\n");
101     }
102 
103     private void processAndFailOnNullPointer(final String noticeText) throws IOException {
104         try {
105             final ByteArrayInputStream noticeInputStream = new ByteArrayInputStream(noticeText.getBytes());
106             final List<Relocator> emptyList = Collections.emptyList();
107             transformer.processResource(NOTICE_RESOURCE, noticeInputStream, emptyList, 0);
108             noticeInputStream.close();
109         } catch (NullPointerException e) {
110             fail("Null pointer should not be thrown when no parameters are set.");
111         }
112     }
113 }