View Javadoc

1   package org.apache.maven.reporting;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import junit.framework.Assert;
26  import junit.framework.TestCase;
27  import junitx.util.PrivateAccessor;
28  
29  /**
30   * Test case for some public method in AbstractMavenReportRenderer.
31   */
32  public class AbstractMavenReportRendererTest
33      extends TestCase
34  {
35      private static List<String> applyPattern( String pattern )
36          throws Throwable
37      {
38          return (List<String>) PrivateAccessor.invoke( AbstractMavenReportRenderer.class, "applyPattern",
39                                                new Class[] { String.class }, new Object[] { pattern } );
40      }
41  
42      private static void checkPattern( String pattern, String[] expectedResult ) throws Throwable
43      {
44          List<String> result = applyPattern( pattern );
45          Assert.assertEquals( "result size", expectedResult.length, result.size() );
46          int i = 0;
47          for ( Iterator<String> it = result.iterator(); it.hasNext(); )
48          {
49              String name = it.next();
50              String href = it.next();
51              Assert.assertEquals( expectedResult[i], name );
52              Assert.assertEquals( expectedResult[i + 1], href );
53              i += 2;
54          }
55      }
56  
57      private static void checkPatternIllegalArgument( String cause, String pattern ) throws Throwable
58      {
59          try
60          {
61              applyPattern( pattern );
62              Assert.fail( cause + " should throw an IllegalArgumentException" );
63          }
64          catch ( IllegalArgumentException iae )
65          {
66              // ok
67          }
68      }
69  
70      /**
71       * @throws Throwable if any
72       */
73      public void testApplyPattern() throws Throwable
74      {
75          // the most simple test
76          checkPattern( "test {text,url}", new String[] { "test ", null, "text", "url" } );
77  
78          // check that link content is trimmed, and no problem if 2 text values are the same
79          checkPattern( "test{ text , url }test", new String[] { "test", null, "text", "url", "test", null } );
80  
81          // check brace stacking
82          checkPattern( "test{ {text} , url }", new String[] { "test", null, "{text}", "url" } );
83  
84          // check quoting
85          checkPatternIllegalArgument( "unmatched brace", "{" );
86          checkPattern( "'{'", new String[] { "'{'", null } );
87          checkPattern( " ' { '.", new String[] { " ' { '.", null } );
88  
89          // unmatched quote: the actual behavior is to ignore that they are unmatched
90          checkPattern( " '", new String[] { " '", null } );
91          // but shouldn't it be different and throw an IllegalArgumentException?
92          //    checkPatternIllegalArgument( "unmatched quote", " ' " );
93          //    checkPatternIllegalArgument( "unmatched quote", " '" );
94          // impact is too important to make the change for the moment
95  
96          // check double quoting
97          checkPattern( " ''", new String[] { " '", null } );
98          checkPattern( " '' ", new String[] { " '", null } );
99          checkPattern( " ''   ", new String[] { " '", null } );
100 
101         // real world cases with quote
102         checkPattern( "project''s info", new String[] { "project'", null, "s info", null } );
103         checkPattern( "it''s a question of {chance, http://en.wikipedia.org/wiki/Chance}",
104                       new String[] { "it'", null, "s a question of ", null,
105                                      "chance", "http://en.wikipedia.org/wiki/Chance" } );
106         checkPattern( "{s'inscrire,mail@mail.com}", new String[] { "s'inscrire", "mail@mail.com" } );
107 
108         // throwing an IllegalArgumentException in case of unmatched quote would avoid the following:
109         checkPattern( "it's a question of {chance, http://en.wikipedia.org/wiki/Chance}",
110                       new String[] { "it's a question of {chance, http://en.wikipedia.org/wiki/Chance}", null } );
111 
112         checkPattern( "{}test,", new String[] { "", null, "test,", null } );
113         checkPattern( "Hi ${name}. How is it going, sir?", new String[] { "Hi ${name}. How is it going, sir?", null } );
114     }
115 }