001package org.apache.maven.doxia.util;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.text.ParseException;
023
024import java.util.Date;
025import java.util.GregorianCalendar;
026
027import org.codehaus.plexus.PlexusTestCase;
028
029/**
030 * Test case for <code>DoxiaUtils</code>.
031 *
032 * @author ltheussl
033 * @version $Id$
034 */
035public class DoxiaUtilsTest
036    extends PlexusTestCase
037{
038    /**
039     * Verify the expected results.
040     */
041    public void testIsInternalLink()
042    {
043        String link = "#anchor";
044        assertTrue( "Should be an internal link: " + link,
045            DoxiaUtils.isInternalLink( link ) );
046
047        link = "http://maven.apache.org/index.html#anchor";
048        assertFalse( "Should NOT be an internal link: " + link,
049            DoxiaUtils.isInternalLink( link ) );
050
051        link = "./index.html";
052        assertFalse( "Should NOT be an internal link: " + link,
053            DoxiaUtils.isInternalLink( link ) );
054    }
055
056    /**
057     * Verify the expected results.
058     */
059    public void testIsExternalLink()
060    {
061        String link = "http://maven.apache.org/";
062        assertTrue( "Should be an external link: " + link,
063            DoxiaUtils.isExternalLink( link ) );
064
065        link = "https://maven.apache.org/";
066        assertTrue( "Should be an external link: " + link,
067            DoxiaUtils.isExternalLink( link ) );
068
069        link = "HTTPS://MAVEN.APACHE.ORG/";
070        assertTrue( "Should be an external link: " + link,
071            DoxiaUtils.isExternalLink( link ) );
072
073        link = "ftp:/maven.apache.org/";
074        assertTrue( "Should be an external link: " + link,
075            DoxiaUtils.isExternalLink( link ) );
076
077        link = "mailto:maven@apache.org";
078        assertTrue( "Should be an external link: " + link,
079            DoxiaUtils.isExternalLink( link ) );
080
081        link = "file:/index.html";
082        assertTrue( "Should be an external link: " + link,
083            DoxiaUtils.isExternalLink( link ) );
084
085        link = "resource_type://domain:port/filepathname?query_string#anchor";
086        assertTrue( "Should be an external link: " + link,
087            DoxiaUtils.isExternalLink( link ) );
088
089        link = "index.html";
090        assertFalse( "Should NOT be an external link: " + link,
091            DoxiaUtils.isExternalLink( link ) );
092
093        link = "example.pdf";
094        assertFalse( "Should NOT be an external link: " + link,
095            DoxiaUtils.isExternalLink( link ) );
096
097        link = "./index.html";
098        assertFalse( "Should NOT be an external link: " + link,
099            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}