001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.services.impl;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import org.apache.tapestry.junit.TapestryTestCase;
020    import org.apache.tapestry.services.AbsoluteURLBuilder;
021    import org.apache.tapestry.web.WebRequest;
022    import org.testng.annotations.Test;
023    
024    /**
025     * Tests for {@link org.apache.tapestry.services.impl.AbsoluteURLBuilderImpl}.
026     * 
027     * @author Howard M. Lewis Ship
028     * @since 4.0
029     */
030    @Test
031    public class TestAbsoluteURLBuilder extends TapestryTestCase
032    {
033        private void attempt(String expected, String URI, String scheme, String server, int port)
034        {
035            AbsoluteURLBuilder b = new AbsoluteURLBuilderImpl();
036    
037            String actual = b.constructURL(URI, scheme, server, port);
038    
039            assertEquals(expected, actual);
040        }
041    
042        private void attemptDefault(String expected, String URI, String scheme, String server, int port)
043        {
044            WebRequest request = newMock(WebRequest.class);
045    
046            expect(request.getScheme()).andReturn(scheme);
047    
048            expect(request.getServerName()).andReturn(server);
049    
050            expect(request.getServerPort()).andReturn(port);
051    
052            replay();
053    
054            AbsoluteURLBuilderImpl b = new AbsoluteURLBuilderImpl();
055            b.setRequest(request);
056    
057            String actual = b.constructURL(URI);
058    
059            assertEquals(expected, actual);
060    
061            verify();
062        }
063    
064        public void testURIIncludesServer()
065        {
066            attempt("https://foo/bar/baz", "//foo/bar/baz", "https", "SERVER", 100);
067        }
068    
069        public void testPortZero()
070        {
071            attempt("http://foo/bar/baz", "/bar/baz", "http", "foo", 0);
072        }
073    
074        public void testNoLeadingSlash()
075        {
076            attempt("http://foo/bar/baz", "bar/baz", "http", "foo", 0);
077        }
078    
079        public void testPortNonZero()
080        {
081            attempt("http://foo.com:1024/bar/baz", "/bar/baz", "http", "foo.com", 1024);
082        }
083    
084        public void testDefaultsForPort80()
085        {
086            attemptDefault("http://foo/bar/baz", "/bar/baz", "http", "foo", 80);
087        }
088    
089        public void testDefault()
090        {
091            attemptDefault("http://foo:8080/bar/baz", "/bar/baz", "http", "foo", 8080);
092        }
093    }