View Javadoc
1   package org.apache.maven.doxia.util;
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.text.ParseException;
23  
24  import java.util.Date;
25  import java.util.GregorianCalendar;
26  
27  import org.codehaus.plexus.PlexusTestCase;
28  
29  /**
30   * Test case for <code>DoxiaUtils</code>.
31   *
32   * @author ltheussl
33   * @version $Id$
34   */
35  public class DoxiaUtilsTest
36      extends PlexusTestCase
37  {
38      /**
39       * Verify the expected results.
40       */
41      public void testIsInternalLink()
42      {
43          String link = "#anchor";
44          assertTrue( "Should be an internal link: " + link,
45              DoxiaUtils.isInternalLink( link ) );
46  
47          link = "http://maven.apache.org/index.html#anchor";
48          assertFalse( "Should NOT be an internal link: " + link,
49              DoxiaUtils.isInternalLink( link ) );
50  
51          link = "./index.html";
52          assertFalse( "Should NOT be an internal link: " + link,
53              DoxiaUtils.isInternalLink( link ) );
54      }
55  
56      /**
57       * Verify the expected results.
58       */
59      public void testIsExternalLink()
60      {
61          String link = "http://maven.apache.org/";
62          assertTrue( "Should be an external link: " + link,
63              DoxiaUtils.isExternalLink( link ) );
64  
65          link = "https://maven.apache.org/";
66          assertTrue( "Should be an external link: " + link,
67              DoxiaUtils.isExternalLink( link ) );
68  
69          link = "HTTPS://MAVEN.APACHE.ORG/";
70          assertTrue( "Should be an external link: " + link,
71              DoxiaUtils.isExternalLink( link ) );
72  
73          link = "ftp:/maven.apache.org/";
74          assertTrue( "Should be an external link: " + link,
75              DoxiaUtils.isExternalLink( link ) );
76  
77          link = "mailto:maven@apache.org";
78          assertTrue( "Should be an external link: " + link,
79              DoxiaUtils.isExternalLink( link ) );
80  
81          link = "file:/index.html";
82          assertTrue( "Should be an external link: " + link,
83              DoxiaUtils.isExternalLink( link ) );
84  
85          link = "resource_type://domain:port/filepathname?query_string#anchor";
86          assertTrue( "Should be an external link: " + link,
87              DoxiaUtils.isExternalLink( link ) );
88  
89          link = "index.html";
90          assertFalse( "Should NOT be an external link: " + link,
91              DoxiaUtils.isExternalLink( link ) );
92  
93          link = "example.pdf";
94          assertFalse( "Should NOT be an external link: " + link,
95              DoxiaUtils.isExternalLink( link ) );
96  
97          link = "./index.html";
98          assertFalse( "Should NOT be an external link: " + link,
99              DoxiaUtils.isExternalLink( link ) );
100 
101         link = "../index.html";
102         assertFalse( "Should NOT be an external link: " + link,
103             DoxiaUtils.isExternalLink( link ) );
104 
105         // Windows style separators "\" are not allowed
106 
107         link = "file:\\index.html";
108         assertFalse( "Should NOT be an external link: " + link,
109             DoxiaUtils.isExternalLink( link ) );
110 
111         link = ".\\index.html";
112         assertFalse( "Should NOT be an external link: " + link,
113             DoxiaUtils.isExternalLink( link ) );
114 
115         link = "..\\index.html";
116         assertFalse( "Should NOT be an external link: " + link,
117             DoxiaUtils.isExternalLink( link ) );
118     }
119 
120     /**
121      * Verify the expected results.
122      */
123     public void testIsLocalLink()
124     {
125         String link = "index.html";
126         assertTrue( "Should be a local link: " + link,
127             DoxiaUtils.isLocalLink( link ) );
128 
129         link = "./index.html";
130         assertTrue( "Should be a local link: " + link,
131             DoxiaUtils.isLocalLink( link ) );
132 
133         link = "../index.html";
134         assertTrue( "Should be a local link: " + link,
135             DoxiaUtils.isLocalLink( link ) );
136 
137         link = "#anchor";
138         assertFalse( "Should NOT be a local link: " + link,
139             DoxiaUtils.isLocalLink( link ) );
140 
141         link = "http://maven.apache.org/";
142         assertFalse( "Should NOT be a local link: " + link,
143             DoxiaUtils.isLocalLink( link ) );
144 
145     }
146 
147     /**
148      * Verify the expected results.
149      */
150     public void testEncodeId()
151     {
152         assertEquals( DoxiaUtils.encodeId( null ), null );
153         assertEquals( DoxiaUtils.encodeId( "" ), "a" );
154         assertEquals( DoxiaUtils.encodeId( " " ), "a" );
155         assertEquals( DoxiaUtils.encodeId( " _ " ), "a_" );
156         assertEquals( DoxiaUtils.encodeId( "1" ), "a1" );
157         assertEquals( DoxiaUtils.encodeId( "1anchor" ), "a1anchor" );
158         assertEquals( DoxiaUtils.encodeId( "_anchor" ), "a_anchor" );
159         assertEquals( DoxiaUtils.encodeId( "a b-c123 " ), "a_b-c123" );
160         assertEquals( DoxiaUtils.encodeId( "   anchor" ), "anchor" );
161         assertEquals( DoxiaUtils.encodeId( "myAnchor" ), "myAnchor" );
162         assertEquals( DoxiaUtils.encodeId( "my&Anchor" ), "my.26Anchor" );
163         assertEquals( DoxiaUtils.encodeId( "H\u00E5kon" ), "H.C3.A5kon" );
164         assertEquals( DoxiaUtils.encodeId( "H\u00E5kon", true ), "Hkon" );
165         assertEquals( DoxiaUtils.encodeId( "Theu\u00DFl" ), "Theu.C3.9Fl" );
166         assertEquals( DoxiaUtils.encodeId( "Theu\u00DFl", true ), "Theul" );
167     }
168 
169     /**
170      * Verify the expected results.
171      */
172     public void testIsValidId()
173     {
174         assertFalse( DoxiaUtils.isValidId( null ) );
175         assertFalse( DoxiaUtils.isValidId( "" ) );
176         assertFalse( DoxiaUtils.isValidId( " " ) );
177         assertFalse( DoxiaUtils.isValidId( " _ " ) );
178         assertFalse( DoxiaUtils.isValidId( "1" ) );
179         assertFalse( DoxiaUtils.isValidId( "1anchor" ) );
180         assertFalse( DoxiaUtils.isValidId( "_anchor" ) );
181         assertFalse( DoxiaUtils.isValidId( "a b-c123 " ) );
182         assertFalse( DoxiaUtils.isValidId( "   anchor" ) );
183         assertFalse( DoxiaUtils.isValidId( "my&Anchor" ) );
184         assertTrue( DoxiaUtils.isValidId( "myAnchor" ) );
185         assertTrue( DoxiaUtils.isValidId( "a_" ) );
186         assertTrue( DoxiaUtils.isValidId( "a-" ) );
187         assertTrue( DoxiaUtils.isValidId( "a:" ) );
188         assertTrue( DoxiaUtils.isValidId( "a." ) );
189         assertTrue( DoxiaUtils.isValidId( "index.html" ) );
190         assertFalse( DoxiaUtils.isValidId( "Theu\u00DFl" ) );
191         assertTrue( DoxiaUtils.isValidId( "Theu.C3.9Fl" ) );
192         assertFalse( DoxiaUtils.isValidId( "Theu%C3%9Fl" ) );
193     }
194 
195     /**
196      * Verify the expected results.
197      */
198     public void testParseDate()
199     {
200         final int year = 1973;
201         final int month = 1;
202         final int day = 27;
203 
204         try
205         {
206             final Date feb27 = new GregorianCalendar( year, month, day ).getTime();
207             assertEquals( feb27, DoxiaUtils.parseDate( "27.02.1973" ) );
208             assertEquals( feb27, DoxiaUtils.parseDate( "27. 02. 1973" ) );
209             assertEquals( feb27, DoxiaUtils.parseDate( "1973-02-27" ) );
210             assertEquals( feb27, DoxiaUtils.parseDate( "1973/02/27" ) );
211             assertEquals( feb27, DoxiaUtils.parseDate( "27 Feb 1973" ) );
212             assertEquals( feb27, DoxiaUtils.parseDate( "27 Feb. 1973" ) );
213             assertEquals( feb27, DoxiaUtils.parseDate( "Feb. 27, 1973" ) );
214             assertEquals( feb27, DoxiaUtils.parseDate( "Feb 27, '73" ) );
215             assertEquals( feb27, DoxiaUtils.parseDate( "February 27, 1973" ) );
216             assertEquals( feb27, DoxiaUtils.parseDate( "19730227" ) );
217 
218             assertEquals( new GregorianCalendar( year, 0, 1 ).getTime(), DoxiaUtils.parseDate( "1973" ) );
219 
220             final Date feb1 = new GregorianCalendar( year, 1, 1 ).getTime();
221             assertEquals( feb1, DoxiaUtils.parseDate( "February 1973" ) );
222             assertEquals( feb1, DoxiaUtils.parseDate( "Feb. 1973" ) );
223             assertEquals( feb1, DoxiaUtils.parseDate( "February '73" ) );
224             assertEquals( feb1, DoxiaUtils.parseDate( "Feb. '73" ) );
225 
226             assertNotNull( DoxiaUtils.parseDate( "Today" ) );
227             assertNotNull( DoxiaUtils.parseDate( "NOW" ) );
228         }
229         catch ( ParseException ex )
230         {
231             fail( ex.getMessage() );
232         }
233 
234         try
235         {
236             DoxiaUtils.parseDate( "yesterday" ).getTime();
237             fail();
238         }
239         catch ( ParseException ex )
240         {
241             assertNotNull( ex );
242         }
243     }
244 }