1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.services.registry;
18  
19  import java.util.Iterator;
20  
21  // Junit imports
22  import junit.framework.Test;
23  import junit.framework.TestSuite;
24  
25  // Jetspeed imports
26  import org.apache.jetspeed.test.JetspeedTestCase;
27  import org.apache.jetspeed.om.registry.*;
28  
29  import org.apache.turbine.util.TurbineConfig;
30  import org.apache.turbine.util.StringUtils;
31  
32  import org.apache.jetspeed.services.Registry;
33  import org.apache.jetspeed.om.registry.base.CategoryIterator;
34  
35  /***
36   * TestRegistryCategories
37   *
38   * @author <a href="taylor@apache.org">David Sean Taylor</a>
39   * @version $Id: TestRegistryCategories.java,v 1.1 2004/04/07 22:02:42 jford Exp $
40   */
41  
42  public class TestRegistryCategories extends JetspeedTestCase {
43  
44      /***
45       * Defines the testcase name for JUnit.
46       *
47       * @param name the testcase's name.
48       */
49      public TestRegistryCategories( String name ) {
50          super( name );
51      }
52      
53      /***
54       * Start the tests.
55       *
56       * @param args the arguments. Not used
57       */
58      public static void main(String args[]) {
59          junit.awtui.TestRunner.main( new String[] { TestRegistryCategories.class.getName() } );
60      }
61   
62      public void setup() {
63          System.out.println("Setup: Testing categories of Registry");
64       }
65      /***
66       * Creates the test suite.
67       *
68       * @return a test suite (<code>TestSuite</code>) that includes all methods
69       *         starting with "test"
70       */
71      public static Test suite() {
72          // All methods starting with "test" will be executed in the test suite.
73          return new TestSuite( TestRegistryCategories.class );
74      }
75  
76      /***
77       * Tests categories
78       * @throws Exception
79       */
80      public void testCategories() throws Exception 
81      {
82          try
83          {
84              PortletRegistry registry = (PortletRegistry)Registry.get(Registry.PORTLET);
85  
86              PortletEntry pe = (PortletEntry)registry.getEntry("RSS");
87              assertNotNull(pe);
88  
89              Iterator iterator = registry.findPortletsByCategory("rss");
90              int count = 0;
91              init();
92              PortletEntry rss;
93              while (iterator.hasNext())
94              {
95                  rss = (PortletEntry)iterator.next();
96                  print(iterator, rss);
97                  count++;
98              }
99              System.out.println("[RSS] count = " + count);
100             System.out.println("------------------");            
101             assertEquals( "RSS Count", 10, count);
102             //assertEquals( "RSS Count", 2, count);
103 
104             registry.removeEntry(pe);
105             iterator = registry.findPortletsByCategory("rss");
106             assertTrue(iterator.hasNext());
107             //assertTrue(iterator.hasNext());
108             //assertTrue(iterator.hasNext());
109             //assertTrue(iterator.hasNext());
110             //assertTrue(iterator.hasNext());
111               
112             registry.addEntry(pe);
113             iterator = registry.findPortletsByCategory("rss");
114             assertTrue(iterator.hasNext());
115             rss = (PortletEntry)iterator.next();
116             Iterator cats = rss.listCategories();
117             assertNotNull(cats);
118             assertTrue(cats.hasNext());
119             Category cat = (Category)cats.next();
120             assertTrue(cat.getName().startsWith("rss") || cat.getName().startsWith("news"));
121 
122 
123             iterator = registry.listByCategory();
124             count = 0;
125             init();
126             while (iterator.hasNext())
127             {
128                 PortletEntry entry = (PortletEntry)iterator.next();
129             //    print(iterator, entry);
130                 count++;
131             }
132             System.out.println("[Everything] count = " + count);
133             System.out.println("------------------");
134 
135             iterator = registry.findPortletsByCategory("General");
136             count = 0;
137             init();
138             while (iterator.hasNext())
139             {
140                 PortletEntry entry = (PortletEntry)iterator.next();
141                 //print(iterator, entry);
142                 count++;
143             }
144             System.out.println("[General] count = " + count);
145             System.out.println("------------------");
146 
147             iterator = registry.findPortletsByGroupCategory("base", "news.xml.rss");
148             count = 0;
149             init();
150             while (iterator.hasNext())
151             {
152                 PortletEntry entry = (PortletEntry)iterator.next();
153                 //print(iterator, entry);
154                 count++;
155             }
156             System.out.println("[base][xml.rss] count = " + count);
157             System.out.println("------------------");
158 
159             iterator = registry.findPortletsByCategory("");
160             count = 0;
161             init();
162             while (iterator.hasNext())
163             {
164                 PortletEntry entry = (PortletEntry)iterator.next();
165                 //print(iterator, entry);
166                 count++;
167             }
168             System.out.println("[] count = " + count);
169             System.out.println("------------------");
170 
171             iterator = registry.findPortletsByGroupCategory("", "");
172             count = 0;
173             init();
174             while (iterator.hasNext())
175             {
176                 PortletEntry entry = (PortletEntry)iterator.next();
177                 //print(iterator, entry);
178                 count++;
179             }
180             System.out.println("[][] count = " + count);
181             System.out.println("------------------");
182 
183             iterator = registry.findPortletsByGroupCategory("Jetspeed", "");
184             count = 0;
185             init();
186             while (iterator.hasNext())
187             {
188                 PortletEntry entry = (PortletEntry)iterator.next();
189                 //print(iterator, entry);
190                 count++;
191             }
192             System.out.println("[Jetspeed][] count = " + count);
193             System.out.println("------------------");
194 
195             iterator = registry.findPortletsByCategory("news");
196             count = 0;
197             init();
198             while (iterator.hasNext())
199             {
200                 PortletEntry entry = (PortletEntry)iterator.next();
201                 //print(iterator, entry);
202                 count++;
203             }
204             System.out.println("[news] count = " + count);
205             System.out.println("------------------");
206 
207            
208 //            SortedMap sm = tree.tailMap("sports");
209 //            Iterator it = sm.keySet().iterator();
210         }
211         catch (Exception e)
212         {
213             String errmsg = "Error in category test: " + e.toString();
214            // e.printStackTrace();
215            assertNotNull(errmsg, null);
216         }
217     }
218 
219     private String lastGroup = "";
220     private String lastCategory = "";
221 
222     private void init()
223     {
224         lastGroup = "";
225         lastCategory = "";
226     }
227 
228     private void print(Iterator iterator, PortletEntry entry)
229     {
230         String group = ((CategoryIterator)iterator).getGroup();
231         String category = ((CategoryIterator)iterator).getCategory();
232         if (!lastGroup.equals(group))
233         {
234             System.out.println("Group: [" + group + "]");
235             lastGroup = group;
236         }
237         if (!lastCategory.equals(category))
238         {
239             System.out.println("....Cat: [" + category + "]");
240             lastCategory = category;
241         }
242         System.out.println("........" + entry.getName());
243      }
244     
245     /***
246      * Tests IdentityElement unmarshaling entryset base stuff
247      * @throws Exception
248      */
249 
250 
251     /*
252       Configuration object to run Turbine outside a servlet container
253       ( uses turbine.properties )
254     */
255     private static TurbineConfig config = null;
256     
257     /*    
258       Sets up TurbineConfig using the system property:
259       <pre>turbine.properties</pre>
260     */
261     static
262     {
263         try
264         {
265            config = new TurbineConfig( "webapp", "/WEB-INF/conf/TurbineResources.properties");
266            config.init();
267         }
268         catch (Exception e)
269         {
270             fail(StringUtils.stackTrace(e));
271         }
272     }
273 }