001package org.apache.maven.doxia.module.apt;
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 junit.framework.TestCase;
023
024/**
025 * Test AptUtils.
026 *
027 * @author ltheussl
028 * @version $Id$
029 */
030public class AptUtilsTest
031        extends TestCase
032{
033    /**
034     * Test of isExternalLink method, of class AptUtils.
035     */
036    public void testIsExternalLink()
037    {
038        String link = "http://maven.apache.org/";
039        assertTrue( "Should be an external link: " + link,
040            AptUtils.isExternalLink( link ) );
041
042        link = "https://maven.apache.org/";
043        assertTrue( "Should be an external link: " + link,
044            AptUtils.isExternalLink( link ) );
045
046        link = "HTTPS://MAVEN.APACHE.ORG/";
047        assertTrue( "Should be an external link: " + link,
048            AptUtils.isExternalLink( link ) );
049
050        link = "ftp:/maven.apache.org/";
051        assertTrue( "Should be an external link: " + link,
052            AptUtils.isExternalLink( link ) );
053
054        link = "mailto:maven@apache.org";
055        assertTrue( "Should be an external link: " + link,
056            AptUtils.isExternalLink( link ) );
057
058        link = "file:/index.html";
059        assertTrue( "Should be an external link: " + link,
060            AptUtils.isExternalLink( link ) );
061
062        link = "resource_type://domain:port/filepathname?query_string#anchor";
063        assertTrue( "Should be an external link: " + link,
064            AptUtils.isExternalLink( link ) );
065
066        link = "index.html";
067        assertFalse( "Should NOT be an external link: " + link,
068            AptUtils.isExternalLink( link ) );
069
070        link = "example.pdf";
071        assertFalse( "Should NOT be an external link: " + link,
072            AptUtils.isExternalLink( link ) );
073
074        link = "./index.html";
075        assertFalse( "Should NOT be an external link: " + link,
076            AptUtils.isExternalLink( link ) );
077
078        link = "../index.html";
079        assertFalse( "Should NOT be an external link: " + link,
080            AptUtils.isExternalLink( link ) );
081
082        // Windows style separators "\" are not allowed
083
084        link = "file:\\index.html";
085        assertFalse( "Should NOT be an external link: " + link,
086            AptUtils.isExternalLink( link ) );
087
088        link = ".\\index.html";
089        assertFalse( "Should NOT be an external link: " + link,
090            AptUtils.isExternalLink( link ) );
091
092        link = "..\\index.html";
093        assertFalse( "Should NOT be an external link: " + link,
094            AptUtils.isExternalLink( link ) );
095    }
096
097    /**
098     * Test of isInternalLink method, of class AptUtils.
099     */
100    public void testIsInternalLink()
101    {
102        String link = "index.html";
103        assertTrue( "Should be an internal link: " + link, AptUtils.isInternalLink( link ) );
104        link = "file:/index.html";
105        assertFalse( "Should NOT be an internal link: " + link, AptUtils.isInternalLink( link ) );
106        link = "./index.html";
107        assertFalse( "Should NOT be an internal link: " + link, AptUtils.isInternalLink( link ) );
108    }
109
110    /**
111     * Test of isLocalLink method, of class AptUtils.
112     */
113    public void testIsLocalLink()
114    {
115        String link = "/index.html";
116        assertTrue( "Should be a local link: " + link, AptUtils.isLocalLink( link ) );
117
118        link = "./index.html";
119        assertTrue( "Should be a local link: " + link, AptUtils.isLocalLink( link ) );
120
121        link = "../index.html";
122        assertTrue( "Should be a local link: " + link, AptUtils.isLocalLink( link ) );
123
124        link = "index.html";
125        assertFalse( "Should NOT be a local link: " + link, AptUtils.isLocalLink( link ) );
126
127        link = ".\\index.html";
128        assertFalse( "Should NOT be a local link: " + link, AptUtils.isLocalLink( link ) );
129
130        link = "\\index.html";
131        assertFalse( "Should NOT be a local link: " + link, AptUtils.isLocalLink( link ) );
132
133        link = "..\\index.html";
134        assertFalse( "Should NOT be a local link: " + link, AptUtils.isLocalLink( link ) );
135    }
136
137    /**
138     * Test of encodeAnchor method, of class AptUtils.
139     */
140    public void testEncodeAnchor()
141    {
142        assertNull( AptUtils.encodeAnchor( null ) );
143        assertEquals( "a123_:_.-aBc", AptUtils.encodeAnchor( " 12!3 :_.&-a)Bc " ) );
144    }
145
146    /**
147     * Test of encodeFragment method, of class AptUtils.
148     */
149    public void testEncodeFragment()
150    {
151        assertNull( AptUtils.encodeFragment( null ) );
152        assertEquals( "abc0d", AptUtils.encodeFragment( "a B&c0)D" ) );
153    }
154
155    /**
156     * Test of linkToKey method, of class AptUtils.
157     */
158    public void testLinkToKey()
159    {
160        assertEquals( "abc56au", AptUtils.linkToKey( "aB$%C56 a&\\/'U" ) );
161    }
162}