1 16 17 18 package org.apache.jetspeed.util.template; 19 20 import java.util.Stack ; 22 23 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 import junit.awtui.TestRunner; 27 28 import org.apache.jetspeed.test.JetspeedTestCase; 30 import org.apache.jetspeed.services.resources.JetspeedResources; 31 import org.apache.jetspeed.util.template.JetspeedLink; 32 33 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 45 public class TestJetspeedLinkFactory extends JetspeedTestCase 46 { 47 51 private static TurbineConfig config = null; 52 53 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 76 public TestJetspeedLinkFactory(String name) 77 { 78 super( name ); 79 } 80 81 86 public static void main(String args[]) 87 { 88 TestRunner.main( new String [] 89 { TestJetspeedLinkFactory.class.getName() } ); 90 } 91 92 98 public static Test suite() 99 { 100 return new TestSuite( TestJetspeedLinkFactory.class ); 102 } 103 104 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 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 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 while (jsLinkStack.empty() == false) 179 JetspeedLinkFactory.putInstance( (JetspeedLink) jsLinkStack.pop()); 180 assertEquals( "Pool is full", poolCapacity, poolService.getSize(linkClassName)); 181 182 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 } 192 | Popular Tags |