1   /*
2    * Copyright 2000-2001,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  
18  package org.apache.jetspeed.util.template;
19  
20  // Java imports
21  import java.util.Stack;
22  
23  // Cactus and Junit imports
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  import junit.awtui.TestRunner;
27  
28  // Jetspeed imports
29  import org.apache.jetspeed.test.JetspeedTestCase;
30  import org.apache.jetspeed.services.resources.JetspeedResources;
31  import org.apache.jetspeed.util.template.JetspeedLink;
32  
33  // Turbine imports
34  import org.apache.turbine.services.pool.PoolService;
35  import org.apache.turbine.services.TurbineServices;
36  import org.apache.turbine.util.TurbineConfig;
37  import org.apache.turbine.util.StringUtils;
38  
39  /***
40   * TestTurbineCache
41   *
42   * @author <a href="paulsp@apache.org">Paul Spencer</a>
43   * @version $Id: TestJetspeedLinkFactory.java,v 1.1 2004/04/07 22:02:42 jford Exp $
44   */
45  public class TestJetspeedLinkFactory extends JetspeedTestCase
46  {
47      /***
48       * Configuration object to run Turbine outside a servlet container
49       * ( uses turbine.properties )
50       */
51      private static TurbineConfig config = null;
52      
53      /***
54       * Sets up TurbineConfig using the system property:
55       * <pre>turbine.properties</pre>
56       */
57      static
58      {
59          try
60          {
61              config = new TurbineConfig( "webapp",
62              "/WEB-INF/conf/TurbineResources.properties");
63              config.init();
64          }
65          catch (Exception e)
66          {
67              fail(StringUtils.stackTrace(e));
68          }
69      }
70      
71      /***
72       * Defines the testcase name for JUnit.
73       *
74       * @param name the testcase's name.
75       */
76      public TestJetspeedLinkFactory(String name)
77      {
78          super( name );
79      }
80      
81      /***
82       * Start the tests.
83       *
84       * @param args the arguments. Not used
85       */
86      public static void main(String args[])
87      {
88          TestRunner.main( new String[]
89          { TestJetspeedLinkFactory.class.getName() } );
90      }
91      
92      /***
93       * Creates the test suite.
94       *
95       * @return a test suite (<code>TestSuite</code>) that includes all methods
96       *         starting with "test"
97       */
98      public static Test suite()
99      {
100         // All methods starting with "test" will be executed in the test suite.
101         return new TestSuite( TestJetspeedLinkFactory.class );
102     }
103     
104     /***
105      * Simple test that get a JetspeedLink object
106      *
107      * @throws Exception
108      */
109     public void testSimpleGet() throws Exception
110     {
111         JetspeedLink jsLink = JetspeedLinkFactory.getInstance();
112         System.out.println("Class return by JetspeedLinkFactory: " + jsLink.getClass().getName());
113         assertNotNull( "Got JetspeedLink", jsLink);
114         assertEquals( "Created class defined by tools.request.jslink"
115         , JetspeedResources.getString("tool.request.jslink"
116         , "org.apache.jetspeed.util.template.BaseJetspeedLink"), jsLink.getClass().getName());
117         assertTrue( "Class instance of JetspeedLink", (jsLink instanceof JetspeedLink));
118     }
119     
120     /***
121      * Simple test that gets and put a JetspeedLink object
122      *
123      * @throws Exception
124      */
125     public void testGetandPut() throws Exception
126     {
127         PoolService poolService = (PoolService) TurbineServices.
128         getInstance().getService(PoolService.SERVICE_NAME);
129         JetspeedLink jsLink = null;
130         int beforeSize;
131         
132         for (int counter = 0; counter < 10; counter++)
133         {
134             jsLink = JetspeedLinkFactory.getInstance();
135             assertNotNull( "Get loop - Got JetspeedLink", jsLink);
136             assertTrue( "Get loop - jsLink instance of JetspeedLink", (jsLink instanceof JetspeedLink));
137         }
138         String linkClassName = jsLink.getClass().getName();
139         jsLink = null;
140         
141         for (int counter = 0; counter < 10; counter++)
142         {
143             jsLink = JetspeedLinkFactory.getInstance();
144             assertNotNull( "Get/put loop - Got JetspeedLink", jsLink);
145             assertTrue( "Get/put loop - jsLink instance of JetspeedLink", (jsLink instanceof JetspeedLink));
146             beforeSize = poolService.getSize( linkClassName);
147             JetspeedLinkFactory.putInstance(jsLink);
148             assertTrue( "Class saved in pool", (beforeSize < poolService.getSize( linkClassName)));
149             jsLink = null;
150         }
151     }
152     public void testFillPool() throws Exception
153     {
154         Stack jsLinkStack = new Stack();
155         JetspeedLink jsLink = null;
156 
157         PoolService poolService = (PoolService) TurbineServices.
158         getInstance().getService(PoolService.SERVICE_NAME);
159         int poolCapacity;
160 
161         jsLink = JetspeedLinkFactory.getInstance();
162         String linkClassName = jsLink.getClass().getName();
163         poolCapacity = poolService.getCapacity( linkClassName);
164 
165         System.out.println("Class Name  " + linkClassName);
166         System.out.println("Pool Capacity " + poolCapacity);
167 
168         // Fill stack with objects
169         for (int counter = 0; counter < poolCapacity; counter++)
170         {
171             jsLink = JetspeedLinkFactory.getInstance();
172             assertNotNull( "Get loop - Got JetspeedLink", jsLink);
173             assertTrue( "Get loop - jsLink instance of JetspeedLink", (jsLink instanceof JetspeedLink));
174             jsLinkStack.push(jsLink);
175         }
176         
177         // Fill up the pool
178         while (jsLinkStack.empty() == false)
179             JetspeedLinkFactory.putInstance( (JetspeedLink) jsLinkStack.pop());
180         assertEquals( "Pool is full", poolCapacity, poolService.getSize(linkClassName));
181         
182         // Empty pool
183         for (int counter = 0; counter < poolCapacity; counter++)
184         {
185             jsLink = JetspeedLinkFactory.getInstance();
186             assertNotNull( "Get loop - Got JetspeedLink", jsLink);
187             assertTrue( "Get loop - jsLink instance of JetspeedLink", (jsLink instanceof JetspeedLink));
188         }
189         assertEquals( "Pool is empty", 0, poolService.getSize(linkClassName));
190     }
191 }