View Javadoc
1   package org.apache.fulcrum.pool;
2   
3   
4   import static org.junit.jupiter.api.Assertions.assertEquals;
5   
6   /*
7    * Licensed to the Apache Software Foundation (ASF) under one
8    * or more contributor license agreements.  See the NOTICE file
9    * distributed with this work for additional information
10   * regarding copyright ownership.  The ASF licenses this file
11   * to you under the Apache License, Version 2.0 (the
12   * "License"); you may not use this file except in compliance
13   * with the License.  You may obtain a copy of the License at
14   *
15   *   http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing,
18   * software distributed under the License is distributed on an
19   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20   * KIND, either express or implied.  See the License for the
21   * specific language governing permissions and limitations
22   * under the License.
23   */
24  
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  
27  import org.apache.fulcrum.testcontainer.BaseUnit5Test;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  
32  /**
33   * Basic testing of the PoolService Component
34   *
35   * @author <a href="mailto:painter@apache.org">Jeffery Painter</a>
36   * @author Eric Pugh
37   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
38   *
39   */
40  public class PoolServiceTest extends BaseUnit5Test 
41  {
42  	/** Default pool service **/
43  	private PoolService poolService = null;
44  
45  	/**
46  	 * Perform pool service setup
47  	 * 
48  	 * @throws Exception generic exception
49  	 */
50  	@BeforeEach
51  	public void setUp() throws Exception 
52  	{
53          setConfigurationFileName("src/test/TestComponentConfig.xml");
54          setRoleFileName("src/test/TestRoleConfig.xml");
55          poolService = (PoolService) this.lookup(PoolService.class.getName());
56  	}
57  
58  	/**
59  	 * Class to test for Object getInstance(Class)
60  	 * 
61  	 * @throws PoolException generic exception
62  	 */
63  	@Test
64  	public void testGetInstanceClass() throws PoolException 
65  	{
66  		Object object = poolService.getInstance(StringBuilder.class);
67  		assertTrue(object instanceof StringBuilder);
68  
69  	}
70  
71  	/**
72  	 * Test adding an instance to the pool
73  	 */
74  	@Test
75  	public void testPutInstance() 
76  	{
77  		String s = "I am a string";
78  		assertEquals(0, poolService.getSize("java.lang.String"));
79  		poolService.putInstance(s);
80  		assertEquals(1, poolService.getSize("java.lang.String"));
81  
82  	}
83  
84  	/**
85  	 * Test altering pool capacity
86  	 */
87  	@Test
88  	public void testGetSetCapacity() 
89  	{
90  		assertEquals(128, poolService.getCapacity("java.lang.String"));
91  		poolService.setCapacity("java.lang.String", 278);
92  		assertEquals(278, poolService.getCapacity("java.lang.String"));
93  
94  	}
95  
96  	/**
97  	 * Test to determine current size of the pool
98  	 */
99  	@Test
100 	public void testGetSize() 
101 	{
102 		String s = "I am a string";
103 		assertEquals(0, poolService.getSize("java.lang.String"));
104 		poolService.putInstance(s);
105 		assertEquals(1, poolService.getSize("java.lang.String"));
106 
107 	}
108 
109 	/**
110 	 * Class to test for void clearPool(String)
111 	 */
112 	@Test
113 	public void testClearPoolString() 
114 	{
115 		String s = "I am a string";
116 		assertEquals(0, poolService.getSize("java.lang.String"));
117 		
118 		poolService.putInstance(s);
119 		assertEquals(1, poolService.getSize("java.lang.String"));
120 		
121 		poolService.clearPool("java.lang.String");
122 		assertEquals(0, poolService.getSize("java.lang.String"));
123 	}
124 
125 	/**
126 	 * Class to test for void clearPool()
127 	 */
128 	@Test
129 	public void testClearPool() 
130 	{
131 		String s = "I am a string";
132 		assertEquals(0, poolService.getSize("java.lang.String"));
133 		
134 		poolService.putInstance(s);
135 		poolService.putInstance(new Double(32));
136 		assertEquals(1, poolService.getSize("java.lang.String"));
137 		
138 		poolService.clearPool();
139 		assertEquals(0, poolService.getSize("java.lang.String"));
140 		assertEquals(0, poolService.getSize("java.lang.Double"));
141 	}
142 }