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.reporting;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.lang.reflect.Method;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  import static org.junit.jupiter.api.Assertions.fail;
30  
31  /**
32   * Test case for some public method in AbstractMavenReportRenderer.
33   */
34  public class AbstractMavenReportRendererTest {
35      private static List<String> applyPattern(String pattern) throws Throwable {
36          try {
37              Method method = AbstractMavenReportRenderer.class.getDeclaredMethod("applyPattern", String.class);
38              method.setAccessible(true);
39              return (List<String>) method.invoke(null, pattern);
40          } catch (InvocationTargetException ite) {
41              throw ite.getTargetException();
42          }
43      }
44  
45      private static void checkPattern(String pattern, String[] expectedResult) throws Throwable {
46          List<String> result = applyPattern(pattern);
47          assertEquals(expectedResult.length, result.size(), "result size");
48          int i = 0;
49          for (Iterator<String> it = result.iterator(); it.hasNext(); ) {
50              String name = it.next();
51              String href = it.next();
52              assertEquals(expectedResult[i], name);
53              assertEquals(expectedResult[i + 1], href);
54              i += 2;
55          }
56      }
57  
58      private static void checkPatternIllegalArgument(String cause, String pattern) throws Throwable {
59          try {
60              applyPattern(pattern);
61              fail(cause + " should throw an IllegalArgumentException");
62          } catch (IllegalArgumentException iae) {
63              // ok
64          }
65      }
66  
67      /**
68       * @throws Throwable if any
69       */
70      @Test
71      public void testApplyPattern() throws Throwable {
72          // the most simple test
73          checkPattern("test {text,url}", new String[] {"test ", null, "text", "url"});
74  
75          // check that link content is trimmed, and no problem if 2 text values are the same
76          checkPattern("test{ text , url }test", new String[] {"test", null, "text", "url", "test", null});
77  
78          // check brace stacking
79          checkPattern("test{ {text} , url }", new String[] {"test", null, "{text}", "url"});
80  
81          // check quoting
82          checkPatternIllegalArgument("unmatched brace", "{");
83          checkPattern("'{'", new String[] {"'{'", null});
84          checkPattern(" ' { '.", new String[] {" ' { '.", null});
85  
86          // unmatched quote: the actual behavior is to ignore that they are unmatched
87          checkPattern(" '", new String[] {" '", null});
88          // but shouldn't it be different and throw an IllegalArgumentException?
89          //    checkPatternIllegalArgument( "unmatched quote", " ' " );
90          //    checkPatternIllegalArgument( "unmatched quote", " '" );
91          // impact is too important to make the change for the moment
92  
93          // check double quoting
94          checkPattern(" ''", new String[] {" '", null});
95          checkPattern(" '' ", new String[] {" '", null});
96          checkPattern(" ''   ", new String[] {" '", null});
97  
98          // real world cases with quote
99          checkPattern("project''s info", new String[] {"project'", null, "s info", null});
100         checkPattern(
101                 "it''s a question of {chance, http://en.wikipedia.org/wiki/Chance}",
102                 new String[] {"it'", null, "s a question of ", null, "chance", "http://en.wikipedia.org/wiki/Chance"});
103         checkPattern("{s'inscrire,mail@mail.com}", new String[] {"s'inscrire", "mail@mail.com"});
104 
105         // throwing an IllegalArgumentException in case of unmatched quote would avoid the following:
106         checkPattern(
107                 "it's a question of {chance, http://en.wikipedia.org/wiki/Chance}",
108                 new String[] {"it's a question of {chance, http://en.wikipedia.org/wiki/Chance}", null});
109 
110         checkPattern("{}test,", new String[] {"", null, "test,", null});
111         checkPattern("Hi ${name}. How is it going, sir?", new String[] {"Hi ${name}. How is it going, sir?", null});
112 
113         // MSHARED-392 multiple links
114         checkPattern(
115                 "{Indiana University Extreme! Lab Software License, vesion 1.1.1,"
116                         + "http://www.extreme.indiana.edu/viewcvs/~checkout~/XPP3/java/LICENSE.txt}"
117                         + "{Public Domain,http://creativecommons.org/licenses/publicdomain}"
118                         + "{Apache Software License, version 1.1,http://www.apache.org/licenses/LICENSE-1.1}",
119                 new String[] {
120                     "Indiana University Extreme! Lab Software License, vesion 1.1.1",
121                     "http://www.extreme.indiana.edu/viewcvs/~checkout~/XPP3/java/LICENSE.txt",
122                     "Public Domain",
123                     "http://creativecommons.org/licenses/publicdomain",
124                     "Apache Software License, version 1.1",
125                     "http://www.apache.org/licenses/LICENSE-1.1"
126                 });
127     }
128 }