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 junit.framework.Test;
23  import junit.framework.TestSuite;
24  
25  import org.apache.commons.lang.ArrayUtils;
26  import org.apache.turbine.services.pull.PullService;
27  import org.apache.turbine.services.pull.TurbinePull;
28  import org.apache.turbine.test.BaseTurbineTest;
29  import org.apache.velocity.context.Context;
30  
31  
32  public class UIToolTest
33          extends BaseTurbineTest
34  {
35      public UIToolTest(String name)
36              throws Exception
37      {
38          super(name, "conf/test/TurbineResources.properties");
39      }
40  
41      public static Test suite()
42      {
43          return new TestSuite(UIToolTest.class);
44      }
45  
46      private UITool getTool()
47      {
48          PullService pullService = TurbinePull.getService();
49          assertNotNull(pullService);
50  
51          Context globalContext = pullService.getGlobalContext();
52          assertNotNull(globalContext);
53  
54          return (UITool) globalContext.get("ui");
55      }
56  
57      public void testTool()
58      {
59          UITool ui = getTool();
60          assertNotNull(ui);
61      }
62  
63      public void testCssSlashes()
64      {
65          UITool ui = getTool();
66  
67          String cssUrl = ui.getStylecss();
68          assertEquals("CSS URL does not match", "http:///conf/test/turbine-resources/turbine-skins/myskin/skins.css", cssUrl);
69      }
70  
71      public void testImageSlashes()
72      {
73          UITool ui = getTool();
74  
75          String img = "myimage.gif";
76  
77          String imgUrl = ui.image(img);
78          assertEquals("CSS URL does not match", "http:///conf/test/turbine-resources/turbine-skins/myskin/turbine-images/" + img, imgUrl);
79  
80          String img2 = "foo/myimage.gif";
81  
82          String imgUrl2 = ui.image(img2);
83          assertEquals("CSS URL does not match", "http:///conf/test/turbine-resources/turbine-skins/myskin/turbine-images/" + img2, imgUrl2);
84  
85          String img3 = "/foo/myimage.gif";
86  
87          String imgUrl3 = ui.image(img3);
88          assertEquals("CSS URL does not match", "http:///conf/test/turbine-resources/turbine-skins/myskin/turbine-images" + img3, imgUrl3);
89      }
90  
91      public void testPathologicalCases()
92      {
93      	UITool ui = getTool();
94  
95      	String img = "";
96          String imgUrl = ui.image(img);
97          assertEquals("Could not strip empty String", "http:///conf/test/turbine-resources/turbine-skins/myskin/turbine-images/", imgUrl);
98  
99      	img = "/";
100         imgUrl = ui.image(img);
101         assertEquals("Could not strip single Slash", "http:///conf/test/turbine-resources/turbine-skins/myskin/turbine-images/", imgUrl);
102 
103     	img = "//";
104         imgUrl = ui.image(img);
105         assertEquals("Could not strip double Slash", "http:///conf/test/turbine-resources/turbine-skins/myskin/turbine-images/", imgUrl);
106     }
107 
108     public void testGetSkinNames()
109     {
110         UITool ui = getTool();
111 
112         String[] skinNames = ui.getSkinNames();
113         // Remove the ".svn" dir that may be present.
114         skinNames = (String[]) ArrayUtils.removeElement(skinNames, ".svn");
115         assertEquals(2, skinNames.length);
116 
117         assertTrue(ArrayUtils.contains(skinNames, "myotherskin"));
118         assertTrue(ArrayUtils.contains(skinNames, "myskin"));
119     }
120 
121     public void testSkinValues()
122     {
123         UITool ui = getTool();
124 
125         // Default skin
126         //skin_property_1 = skin_property_1_my_skin
127         assertEquals("skin_property_1_my_skin", ui.get("skin_property_1"));
128         
129         ui.setSkin("myotherskin");
130         //skin_property_1 = skin_property_1_my_other_skin
131         assertEquals("skin_property_1_my_other_skin", ui.get("skin_property_1"));
132     }
133 }