001    // Copyright 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.checkOrder;
018    import static org.easymock.EasyMock.expect;
019    
020    import org.apache.tapestry.BaseComponentTestCase;
021    import org.apache.tapestry.IMarkupWriter;
022    import org.apache.tapestry.INamespace;
023    import org.apache.tapestry.IPage;
024    import org.apache.tapestry.IRequestCycle;
025    import org.testng.annotations.Test;
026    
027    /**
028     * Tests for {@link org.apache.tapestry.services.impl.BaseTagWriter}.
029     * 
030     * @author Howard M. Lewis Ship
031     * @since 4.0
032     */
033    @Test
034    public class TestBaseTagWriter extends BaseComponentTestCase
035    {
036        private IMarkupWriter newWriter(String url)
037        {
038            IMarkupWriter writer = newMock(IMarkupWriter.class);
039    
040            writer.beginEmpty("base");
041            writer.attribute("href", url);
042            writer.printRaw("<!--[if IE]></base><![endif]-->");
043            writer.println();
044    
045            return writer;
046        }
047    
048        private INamespace newNamespace(String id)
049        {
050            INamespace ns = newMock(INamespace.class);
051            checkOrder(ns, false);
052            
053            expect(ns.getId()).andReturn(id);
054    
055            return ns;
056        }
057    
058        private IPage newPage(INamespace ns, String pageName)
059        {
060            IPage page = newPage();
061    
062            expect(page.getNamespace()).andReturn(ns);
063    
064            if (pageName != null)
065            {
066                expect(page.getPageName()).andReturn(pageName);
067            }
068    
069            return page;
070        }
071    
072        private IRequestCycle newRequestCycle(IPage page, String url)
073        {
074            IRequestCycle cycle = newCycle();
075    
076            expect(cycle.getPage()).andReturn(page);
077    
078            expect(cycle.getAbsoluteURL(url)).andReturn("http://foo.com/context" + url);
079    
080            return cycle;
081        }
082    
083        private void run(IMarkupWriter writer, IRequestCycle cycle)
084        {
085            replay();
086            
087            new BaseTagWriter().render(writer, cycle);
088    
089            verify();
090        }
091        
092        public void testNotApplicationNamespace()
093        {
094            INamespace ns = newNamespace("library");
095            IPage page = newPage(ns, null);
096            IRequestCycle cycle = newRequestCycle(page, "/");
097            IMarkupWriter writer = newWriter("http://foo.com/context/");
098    
099            run(writer, cycle);
100        }
101        
102        public void testInRoot()
103        {
104            INamespace ns = newNamespace("library");
105            IPage page = newPage(ns, null);
106            IRequestCycle cycle = newRequestCycle(page, "/");
107            IMarkupWriter writer = newWriter("http://foo.com/context/");
108    
109            run(writer, cycle);
110        }
111        
112        public void testInSubFolder()
113        {
114            INamespace ns = newNamespace(null);
115            IPage page = newPage(ns, "admin/AdminMenu");
116            IRequestCycle cycle = newRequestCycle(page, "/admin/");
117            IMarkupWriter writer = newWriter("http://foo.com/context/admin/");
118            
119            run(writer, cycle);   
120        }
121    }