2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.shale.clay.config;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  
26  import org.apache.shale.clay.config.beans.AttributeBean;
27  import org.apache.shale.clay.config.beans.ComponentBean;
28  import org.apache.shale.clay.config.beans.SymbolBean;
29  
30  public class LimitedJspxTestCase extends AbstractTestCaseConfig {
31  
32      // Construct a new instance of this test case.
33      public LimitedJspxTestCase(String name) {
34          super(name);
35      }
36  
37      // Return the tests included in this test case.
38      public static Test suite() {
39  
40          return (new TestSuite(LimitedJspxTestCase.class));
41      }
42  
43      public void testJpx() {
44          loadConfigFile(null);
45  
46          ComponentBean document = htmlTemplateConfigBean.getElement("/org/apache/shale/clay/config/limited-jspx.html");
47          assertNotNull(document);
48  
49          assertEquals("void component count", 2, countVoidComponents(document));
50  
51          List clayIncludes = findClayJspxInclude(document);
52          assertEquals("includes", 2, clayIncludes.size());
53  
54          Iterator ci = clayIncludes.iterator();
55          while (ci.hasNext()) {
56              ComponentBean clayInclude = (ComponentBean) ci.next();
57              if (clayInclude.getSymbol("@file") != null) {
58  
59                  SymbolBean symbol = clayInclude.getSymbol("@file");
60                  assertNotNull("file symbol", symbol);
61                  assertEquals("/org/apache/shale/clay/config/address1.html", symbol.getValue());
62  
63                  AttributeBean attr = clayInclude.getAttribute("clayJsfid");
64                  assertNotNull("clayJsfid", attr);
65                  assertEquals("@file", attr.getValue());
66  
67  
68              } else if (clayInclude.getSymbol("@page") != null) {
69  
70                  SymbolBean symbol = clayInclude.getSymbol("@page");
71                  assertNotNull("file symbol", symbol);
72                  assertEquals("/org/apache/shale/clay/config/address1.html", symbol.getValue());
73  
74                  AttributeBean attr = clayInclude.getAttribute("clayJsfid");
75                  assertNotNull("clayJsfid", attr);
76                  assertEquals("@page", attr.getValue());
77  
78                  for (int i = 0; i < 5; i++) {
79                      symbol = clayInclude.getSymbol("@symbol" + i);
80                      assertNotNull("@symbol" + i, symbol);
81                      assertEquals("value" + i, symbol.getValue());
82                  }
83  
84              } else {
85                  assertFalse("invalid include mapping", true);
86              }
87          }
88  
89      }
90  
91      // counts the number of void components in the tree
92      public int countVoidComponents(ComponentBean bean) {
93          int voidcnt = 0;
94  
95          if (bean.getJsfid().equals("void")) {
96              voidcnt++;
97          }
98  
99          Iterator ci = bean.getChildren().iterator();
100         while (ci.hasNext()) {
101             ComponentBean child = (ComponentBean) ci.next();
102             voidcnt += countVoidComponents(child);
103         } 
104 
105         return voidcnt;
106     }
107 
108     // find beans with a jsfid equal to "clay"
109     public List findClayJspxInclude(ComponentBean bean) {
110         List beans = new ArrayList();
111 
112         if (bean.getJsfid().equals("clay")) {
113             beans.add(bean);
114         }
115         Iterator ci = bean.getChildren().iterator();
116         while (ci.hasNext()) {
117             beans.addAll(findClayJspxInclude((ComponentBean) ci.next()));
118         }        
119 
120         return beans;
121     }
122 }