001package org.apache.maven.wagon.shared.http;
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
024import java.net.MalformedURLException;
025import java.net.URISyntaxException;
026
027public class EncodingUtilTest
028    extends TestCase
029{
030    public void testEncodeURLWithTrailingSlash()
031    {
032        String encodedURL = EncodingUtil.encodeURLToString( "https://host:1234/test", "demo/" );
033
034        assertEquals( "https://host:1234/test/demo/", encodedURL );
035    }
036
037    public void testEncodeURLWithSpaces()
038        throws URISyntaxException, MalformedURLException
039    {
040        String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/path with spaces" );
041
042        assertEquals( "file://host:1/path%20with%20spaces", encodedURL );
043    }
044
045    public void testEncodeURLWithSpacesInPath()
046        throws URISyntaxException, MalformedURLException
047    {
048        String encodedURL = EncodingUtil.encodeURLToString( "file://host:1", "path with spaces" );
049
050        assertEquals( "file://host:1/path%20with%20spaces", encodedURL );
051    }
052
053    public void testEncodeURLWithSpacesInBothBaseAndPath()
054        throws URISyntaxException, MalformedURLException
055    {
056        String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/with%20a", "path with spaces" );
057
058        assertEquals( "file://host:1/with%20a/path%20with%20spaces", encodedURL );
059    }
060
061    public void testEncodeURLWithSlashes1()
062        throws URISyntaxException, MalformedURLException
063    {
064        String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/basePath", "a", "b", "c" );
065
066        assertEquals( "file://host:1/basePath/a/b/c", encodedURL );
067
068        encodedURL = EncodingUtil.encodeURLToString( "file://host:1/basePath", "a/b/c" );
069
070        assertEquals( "file://host:1/basePath/a/b/c", encodedURL );
071    }
072
073    public void testEncodeURLWithSlashes2()
074        throws URISyntaxException, MalformedURLException
075    {
076        String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/basePath/", "a", "b", "c" );
077
078        assertEquals( "file://host:1/basePath/a/b/c", encodedURL );
079
080        encodedURL = EncodingUtil.encodeURLToString( "file://host:1/basePath/", "a/b/c" );
081
082        assertEquals( "file://host:1/basePath/a/b/c", encodedURL );
083    }
084
085    public void testEncodeURLWithSlashes3()
086            throws URISyntaxException, MalformedURLException
087    {
088        String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/basePath/", new String[0] );
089
090        assertEquals( "file://host:1/basePath/", encodedURL );
091    }
092
093    public void testEncodeURLWithSlashes4()
094        throws URISyntaxException, MalformedURLException
095    {
096        String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/basePath", new String[0] );
097
098        assertEquals( "file://host:1/basePath", encodedURL );
099    }
100
101    public void testEncodeURLWithSlashes5()
102        throws URISyntaxException, MalformedURLException
103    {
104        String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/basePath",
105                                                            "a/1", "b/1", "c/1" );
106
107        assertEquals( "file://host:1/basePath/a%2F1/b%2F1/c%2F1", encodedURL );
108    }
109
110    public void testEncodeURLWithSlashes6()
111        throws URISyntaxException, MalformedURLException
112    {
113        String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/", new String[0] );
114
115        assertEquals( "file://host:1/", encodedURL );
116    }
117
118    public void testEncodeURLWithSlashes7()
119        throws URISyntaxException, MalformedURLException
120    {
121        String encodedURL = EncodingUtil.encodeURLToString( "file://host:1", new String[0] );
122
123        assertEquals( "file://host:1", encodedURL );
124    }
125
126    public void testEncodeURLWithNonLatin()
127            throws URISyntaxException, MalformedURLException
128    {
129        String encodedURL = EncodingUtil.encodeURLToString( "file://host:1", "пипец/" );
130
131        assertEquals( "file://host:1/%D0%BF%D0%B8%D0%BF%D0%B5%D1%86/", encodedURL );
132    }
133}