View Javadoc

    1   package org.apache.turbine.services.pull.tools;
    2
    3   /*
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *   http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21
    22  import static org.junit.Assert.assertEquals;
    23  import static org.junit.Assert.assertNotNull;
    24  import static org.junit.Assert.assertTrue;
    25
    26  import java.util.HashMap;
    27  import java.util.Map;
    28
    29  import org.apache.commons.lang.ArrayUtils;
    30  import org.apache.turbine.TurbineConstants;
    31  import org.apache.turbine.services.TurbineServices;
    32  import org.apache.turbine.services.jsonrpc.JsonrpcServicelTest;
    33  import org.apache.turbine.services.pull.PullService;
    34  import org.apache.turbine.test.BaseTestCase;
    35  import org.apache.turbine.util.TurbineConfig;
    36  import org.apache.velocity.context.Context;
    37  import org.junit.AfterClass;
    38  import org.junit.BeforeClass;
    39  import org.junit.Test;
    40  import org.junit.runner.RunWith;
    41  import org.junit.runners.Suite;
    42
    43  @RunWith(Suite.class)
    44  @Suite.SuiteClasses({ JsonrpcServicelTest.class })
    45  public class UIToolTest extends BaseTestCase
    46  {
    47
    48      private static TurbineConfig turbineConfig = null;
    49      private static PullService pullService = null;
    50      private static UITool ui = null;
    51
    52      @BeforeClass
    53      public static void setUp() throws Exception
    54      {
    55          Map<String, String> initParams = new HashMap<String, String>();
    56          initParams.put(TurbineConfig.PROPERTIES_PATH_KEY, "/conf/test/CompleteTurbineResources.properties"); // "conf/test/TurbineResources.properties"
    57          initParams.put(TurbineConstants.LOGGING_ROOT_KEY, "target/test-logs");
    58
    59          turbineConfig = new TurbineConfig(".", initParams);
    60          turbineConfig.initialize();
    61
    62          pullService = (PullService)TurbineServices.getInstance().getService(PullService.SERVICE_NAME);
    63          assertNotNull(pullService);
    64          Context globalContext = pullService.getGlobalContext();
    65          assertNotNull(globalContext);
    66
    67          ui = (UITool) globalContext.get("ui");
    68      }
    69
    70      @AfterClass
    71      public static void destroy() throws Exception
    72      {
    73          turbineConfig.dispose();
    74          ui = null;
    75      }
    76
    77      @Test
    78      public void testTool()
    79      {
    80          assertNotNull(ui);
    81      }
    82
    83      @Test
    84      public void testCssSlashes()
    85      {
    86
    87          ui.setSkin("myskin");
    88
    89          String cssUrl = ui.getStylecss();
    90          assertEquals("CSS URL does not match", "http:///conf/test/turbine-resources/turbine-skins/myskin/skins.css", cssUrl);
    91      }
    92
    93      @Test
    94      public void testImageSlashes()
    95      {
    96          ui.setSkin("myskin");
    97
    98          String img = "myimage.gif";
    99
    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 }