001package org.apache.turbine.services.pull.tools;
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
022
023import static org.junit.Assert.assertEquals;
024import static org.junit.Assert.assertNotNull;
025import static org.junit.Assert.assertTrue;
026
027import java.util.HashMap;
028import java.util.Map;
029
030import org.apache.commons.lang.ArrayUtils;
031import org.apache.turbine.TurbineConstants;
032import org.apache.turbine.services.jsonrpc.JsonrpcServicelTest;
033import org.apache.turbine.services.pull.PullService;
034import org.apache.turbine.services.pull.TurbinePull;
035import org.apache.turbine.test.BaseTestCase;
036import org.apache.turbine.util.TurbineConfig;
037import org.apache.velocity.context.Context;
038import org.junit.AfterClass;
039import org.junit.BeforeClass;
040import org.junit.Test;
041import org.junit.runner.RunWith;
042import org.junit.runners.Suite;
043
044@RunWith(Suite.class)
045@Suite.SuiteClasses({
046    JsonrpcServicelTest.class
047    })
048public class UIToolTest
049        extends BaseTestCase
050{
051    
052    private static TurbineConfig turbineConfig = null;
053    private static PullService pullService = null;
054    private static UITool ui = null;
055
056    
057    @BeforeClass
058    public static void setUp() throws Exception {
059
060
061        Map<String, String> initParams = new HashMap<String, String>();
062        initParams.put(TurbineConfig.PROPERTIES_PATH_KEY, "/conf/test/CompleteTurbineResources.properties"); // "conf/test/TurbineResources.properties"
063        initParams.put(TurbineConstants.LOGGING_ROOT_KEY, "target/test-logs");
064
065        turbineConfig = new TurbineConfig(".", initParams);
066        turbineConfig.initialize();
067        
068        //pullService = (PullService)TurbineServices.getInstance().getService(PullService.SERVICE_NAME);
069        pullService = TurbinePull.getService();
070        assertNotNull(pullService);
071        Context globalContext = pullService.getGlobalContext();
072        assertNotNull(globalContext);
073
074        ui = (UITool) globalContext.get("ui");
075    }
076    
077    @AfterClass
078    public static void destroy() throws Exception {
079        turbineConfig.dispose();
080        ui=null;
081    }
082
083    @Test
084    public void testTool()
085    {
086        assertNotNull(ui);
087    }
088    
089    @Test
090    public void testCssSlashes()
091    {
092
093        ui.setSkin("myskin");
094
095        String cssUrl = ui.getStylecss();
096        assertEquals("CSS URL does not match", "http:///conf/test/turbine-resources/turbine-skins/myskin/skins.css", cssUrl);
097    }
098    
099    @Test
100    public void testImageSlashes()
101    {
102        ui.setSkin("myskin");
103
104        String img = "myimage.gif";
105
106        String imgUrl = ui.image(img);
107        assertEquals("CSS URL does not match", "http:///conf/test/turbine-resources/turbine-skins/myskin/turbine-images/" + img, imgUrl);
108
109        String img2 = "foo/myimage.gif";
110
111        String imgUrl2 = ui.image(img2);
112        assertEquals("CSS URL does not match", "http:///conf/test/turbine-resources/turbine-skins/myskin/turbine-images/" + img2, imgUrl2);
113
114        String img3 = "/foo/myimage.gif";
115
116        String imgUrl3 = ui.image(img3);
117        assertEquals("CSS URL does not match", "http:///conf/test/turbine-resources/turbine-skins/myskin/turbine-images" + img3, imgUrl3);
118    }
119    @Test
120    public void testPathologicalCases()
121    {
122        ui.setSkin("myskin");
123
124        String img = "";
125        String imgUrl = ui.image(img);
126        assertEquals("Could not strip empty String", "http:///conf/test/turbine-resources/turbine-skins/myskin/turbine-images/", imgUrl);
127
128        img = "/";
129        imgUrl = ui.image(img);
130        assertEquals("Could not strip single Slash", "http:///conf/test/turbine-resources/turbine-skins/myskin/turbine-images/", imgUrl);
131
132        img = "//";
133        imgUrl = ui.image(img);
134        assertEquals("Could not strip double Slash", "http:///conf/test/turbine-resources/turbine-skins/myskin/turbine-images/", imgUrl);
135    }
136    @Test
137    public void testGetSkinNames()
138    {
139
140        String[] skinNames = ui.getSkinNames();
141        // Remove the ".svn" dir that may be present.
142        skinNames = (String[]) ArrayUtils.removeElement(skinNames, ".svn");
143        assertEquals(2, skinNames.length);
144
145        assertTrue(ArrayUtils.contains(skinNames, "myotherskin"));
146        assertTrue(ArrayUtils.contains(skinNames, "myskin"));
147    }
148    @Test
149    public void testSkinValues()
150    {
151
152        // Default skin
153        //skin_property_1 = skin_property_1_my_skin
154        assertEquals("skin_property_1_my_skin", ui.get("skin_property_1"));
155        
156        ui.setSkin("myotherskin");
157        //skin_property_1 = skin_property_1_my_other_skin
158        assertEquals("skin_property_1_my_other_skin", ui.get("skin_property_1"));
159    }
160}