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