KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > admin > jmx > ConnectionPoolMBeanTest


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool.admin.jmx;
7
8 import org.logicalcobwebs.proxool.ProxoolConstants;
9 import org.logicalcobwebs.proxool.ProxoolDriver;
10 import org.logicalcobwebs.proxool.ProxoolException;
11 import org.logicalcobwebs.proxool.ProxoolFacade;
12
13 import javax.management.Attribute JavaDoc;
14 import javax.management.AttributeList JavaDoc;
15 import javax.management.MBeanServer JavaDoc;
16 import javax.management.MBeanServerFactory JavaDoc;
17 import javax.management.Notification JavaDoc;
18 import javax.management.NotificationFilter JavaDoc;
19 import javax.management.NotificationFilterSupport JavaDoc;
20 import javax.management.NotificationListener JavaDoc;
21 import javax.management.ObjectName JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Properties JavaDoc;
24
25
26 /**
27  * Test {@link org.logicalcobwebs.proxool.admin.jmx.ConnectionPoolMBean}.
28  *
29  * @version $Revision: 1.10 $, $Date: 2003/10/20 07:40:44 $
30  * @author Christian Nedregaard (christian_nedregaard@email.com)
31  * @author $Author: chr32 $ (current maintainer)
32  * @since Proxool 0.8
33  */

34 public class ConnectionPoolMBeanTest extends AbstractJMXTest {
35     private MBeanServer JavaDoc mBeanServer;
36     private boolean notified;
37
38     /**
39      * @see junit.framework.TestCase#TestCase(java.lang.String)
40      */

41     public ConnectionPoolMBeanTest(String JavaDoc s) {
42         super(s);
43     }
44
45     /**
46      * Test that an attribute can be fetched from the MBean.
47      * @throws java.lang.Exception if an error occours.
48      */

49     public void testGetAttribute() throws Exception JavaDoc {
50         final String JavaDoc alias = "testGetAttribute";
51         createBasicPool(alias);
52         final ObjectName JavaDoc objectName = ProxoolJMXHelper.getObjectName(alias);
53         final String JavaDoc fatalSql = (String JavaDoc) this.mBeanServer.getAttribute(objectName,
54                 ProxoolJMXHelper.getValidIdentifier(ProxoolConstants.FATAL_SQL_EXCEPTION));
55         assertTrue("Expected fatalSQLException to be '" + alias + "', but it was '" + fatalSql
56                 + "'. ", fatalSql.equals(alias));
57         ProxoolFacade.removeConnectionPool(alias);
58     }
59
60     /**
61      * Test that a list attributes can be fetched from the MBean.
62      * @throws java.lang.Exception if an error occours.
63      */

64     public void testGetAttributes() throws Exception JavaDoc {
65         final String JavaDoc alias = "testGetAttributes";
66         createBasicPool(alias);
67         final String JavaDoc fatalSQLAttribute = ProxoolJMXHelper.getValidIdentifier(ProxoolConstants.FATAL_SQL_EXCEPTION);
68         final String JavaDoc aliasAttribute = ProxoolJMXHelper.getValidIdentifier(ProxoolConstants.ALIAS);
69         final ObjectName JavaDoc objectName = ProxoolJMXHelper.getObjectName(alias);
70         final AttributeList JavaDoc attributes = this.mBeanServer.getAttributes(objectName, new String JavaDoc[]{
71             fatalSQLAttribute,
72             aliasAttribute
73         });
74         String JavaDoc fatalSqlValue = null;
75         String JavaDoc aliasValue = null;
76         Iterator JavaDoc attributeIterator = attributes.iterator();
77         while (attributeIterator.hasNext()) {
78             Attribute JavaDoc attribute = (Attribute JavaDoc) attributeIterator.next();
79             if (attribute.getName().equals(aliasAttribute)) {
80                 aliasValue = (String JavaDoc) attribute.getValue();
81             } else if (attribute.getName().equals(fatalSQLAttribute)) {
82                 fatalSqlValue = (String JavaDoc) attribute.getValue();
83             }
84         }
85         assertNotNull("The value for the alias attribute is missing.", aliasValue);
86         assertNotNull("The value for the fatalSQLException attribute is missing.", fatalSqlValue);
87         assertTrue("Expeted alias to have value '" + aliasValue + "' but the value was '" + aliasValue + ".",
88                 aliasValue.equals(alias));
89         assertTrue("Expexted fatalSQLException to have value '" + alias + "' but the value was '" + fatalSqlValue + ".",
90                 fatalSqlValue.equals(alias));
91         ProxoolFacade.removeConnectionPool(alias);
92     }
93
94     /**
95      * Test that an attribute can be fetched from the MBean.
96      * @throws java.lang.Exception if an error occours.
97      */

98     public void testSetAttribute() throws Exception JavaDoc {
99         String JavaDoc alias = "testSetAttribute";
100         createBasicPool(alias);
101         final ObjectName JavaDoc objectName = ProxoolJMXHelper.getObjectName(alias);
102         final String JavaDoc fatalSQLAttributeName = ProxoolJMXHelper.getValidIdentifier(ProxoolConstants.FATAL_SQL_EXCEPTION);
103         final String JavaDoc newValue = "dingo";
104         this.mBeanServer.setAttribute(objectName,
105                 new Attribute JavaDoc(fatalSQLAttributeName, newValue));
106         String JavaDoc fatalSQLAttribtueValue = (String JavaDoc) mBeanServer.getAttribute(objectName, fatalSQLAttributeName);
107         // check that value vas registered by the bean.
108
assertTrue("Expexted fatalSQLException JMX attribtue to have value '" + newValue + "' but the value was '"
109                 + fatalSQLAttribtueValue + "'.",
110                 fatalSQLAttribtueValue.equals(newValue));
111         // check that the bean updated the pool.
112
final String JavaDoc proxoolProopertyValue = (String JavaDoc) ProxoolFacade.getConnectionPoolDefinition(alias)
113                 .getFatalSqlExceptions().toArray()[0];
114         assertTrue("Expexted fatal-sql-exception Proxool property to have value '"
115                 + newValue + "' but the value was '" + proxoolProopertyValue + "'.",
116                 proxoolProopertyValue.equals(newValue));
117         // check that string properites can be deleted.
118
this.mBeanServer.setAttribute(objectName,
119                 new Attribute JavaDoc(fatalSQLAttributeName, ""));
120         fatalSQLAttribtueValue = (String JavaDoc) mBeanServer.getAttribute(objectName, fatalSQLAttributeName);
121         assertTrue("Expexted fatal-sql-exception Proxool property to be empty "
122             + " but the value was '" + fatalSQLAttribtueValue + "'.", "".equals(fatalSQLAttribtueValue));
123         ProxoolFacade.removeConnectionPool(alias);
124     }
125
126     public void testSetAttributes() throws Exception JavaDoc {
127         String JavaDoc alias = "testSetAttributes";
128         createBasicPool(alias);
129         final ObjectName JavaDoc objectName = ProxoolJMXHelper.getObjectName(alias);
130         final String JavaDoc fatalSQLAttributeName = ProxoolJMXHelper.getValidIdentifier(ProxoolConstants.FATAL_SQL_EXCEPTION);
131         final String JavaDoc testSQLAttributeName = ProxoolJMXHelper.getValidIdentifier(ProxoolConstants.HOUSE_KEEPING_TEST_SQL);
132         final String JavaDoc newValue = "dingo";
133         // test when updated through JMX.
134
final AttributeList JavaDoc attributeList = new AttributeList JavaDoc();
135         attributeList.add(new Attribute JavaDoc(fatalSQLAttributeName, newValue));
136         attributeList.add(new Attribute JavaDoc(testSQLAttributeName, newValue));
137         this.mBeanServer.setAttributes(objectName, attributeList);
138         final String JavaDoc fatalSQLAttribtueValue = (String JavaDoc) mBeanServer.getAttribute(objectName, fatalSQLAttributeName);
139         final String JavaDoc testSQLAttribtueValue = (String JavaDoc) mBeanServer.getAttribute(objectName, testSQLAttributeName);
140         // check that values vas registered by the bean.
141
assertTrue("Expexted fatalSQLException JMX attribtue to have value '" + newValue + "' but the value was '"
142                 + fatalSQLAttribtueValue + "'.",
143                 fatalSQLAttribtueValue.equals(newValue));
144         assertTrue("Expexted housekeeperTestSQL JMX attribtue to have value '" + newValue + "' but the value was '"
145                 + testSQLAttribtueValue + "'.",
146                 testSQLAttribtueValue.equals(newValue));
147         // check that the bean updated the pool.
148
final String JavaDoc fatalSQLProxoolPropertyValue = (String JavaDoc) ProxoolFacade.getConnectionPoolDefinition(alias)
149                 .getFatalSqlExceptions().toArray()[0];
150         final String JavaDoc testSQLProxoolPropertyValue = ProxoolFacade.getConnectionPoolDefinition(alias)
151                 .getHouseKeepingTestSql();
152         assertTrue("Expexted fatal-sql-exception Proxool property to have value '"
153                 + newValue + "' but the value was '" + fatalSQLProxoolPropertyValue + ".",
154                 fatalSQLProxoolPropertyValue.equals(newValue));
155         assertTrue("Expexted housekeeper-test-sql Proxool property to have value '"
156                 + newValue + "' but the value was '" + testSQLProxoolPropertyValue + ".",
157                 testSQLProxoolPropertyValue.equals(newValue));
158         ProxoolFacade.removeConnectionPool(alias);
159     }
160
161     public void testInvokeShutown() throws Exception JavaDoc {
162         final String JavaDoc alias = "testInvokeShutown";
163         createBasicPool(alias);
164         final ObjectName JavaDoc objectName = ProxoolJMXHelper.getObjectName(alias);
165         this.mBeanServer.invoke(objectName, "shutdown", new Object JavaDoc[0], new String JavaDoc[0]);
166         try {
167             ProxoolFacade.removeConnectionPool(alias);
168             fail("Removal of pool alias should have failed, because it should have already be removed.");
169         } catch (ProxoolException e) {
170             // we want this
171
}
172     }
173
174     public void testNotification() throws Exception JavaDoc {
175         String JavaDoc alias = "testNotification";
176         Properties JavaDoc info = createBasicPool(alias);
177         final NotificationListener JavaDoc notificationListener = new TestNotificationListener();
178         final ObjectName JavaDoc objectName = ProxoolJMXHelper.getObjectName(alias);
179         this.mBeanServer.addNotificationListener(objectName, notificationListener, getFilter(), notificationListener);
180         // test when updated through JMX.
181
this.mBeanServer.setAttribute(objectName,
182                 new Attribute JavaDoc(ProxoolJMXHelper.getValidIdentifier(ProxoolConstants.FATAL_SQL_EXCEPTION), "dingo"));
183         assertTrue("We did not get notified when updating through JMX.", this.notified);
184         this.notified = false;
185         // test when updated through ProxoolFacade
186
info = (Properties JavaDoc) info.clone();
187         info.put(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, "1");
188         ProxoolFacade.updateConnectionPool(ProxoolConstants.PROPERTY_PREFIX + alias, info);
189         assertTrue("We did not get notified when updating through ProxoolFacade.", this.notified);
190         ProxoolFacade.removeConnectionPool(alias);
191     }
192
193     private class TestNotificationListener implements NotificationListener JavaDoc {
194         public void handleNotification(Notification JavaDoc notification, Object JavaDoc handBack) {
195             if (handBack.equals(this)) {
196                 notified = true;
197             } else {
198                 fail("Got notification with unknown handback.");
199             }
200         }
201     }
202
203     private NotificationFilter JavaDoc getFilter() {
204         final NotificationFilterSupport JavaDoc notificationFilter = new NotificationFilterSupport JavaDoc();
205         notificationFilter.enableType(ConnectionPoolMBean.NOTIFICATION_TYPE_DEFINITION_UPDATED);
206         return notificationFilter;
207     }
208
209     /**
210      * Calls {@link org.logicalcobwebs.proxool.AbstractProxoolTest#setUp}
211      * @see junit.framework.TestCase#setUp
212      */

213     protected void setUp() throws Exception JavaDoc {
214         this.notified = false;
215         Class.forName(ProxoolDriver.class.getName());
216         this.mBeanServer = MBeanServerFactory.createMBeanServer();
217         super.setUp();
218     }
219
220     /**
221      * Calls {@link org.logicalcobwebs.proxool.AbstractProxoolTest#tearDown}
222      * @see junit.framework.TestCase#setUp
223      */

224     protected void tearDown() throws Exception JavaDoc {
225         MBeanServerFactory.releaseMBeanServer(this.mBeanServer);
226         this.mBeanServer = null;
227         super.tearDown();
228     }
229 }
230
231 /*
232  Revision history:
233  $Log: ConnectionPoolMBeanTest.java,v $
234  Revision 1.10 2003/10/20 07:40:44 chr32
235  Improved tests.
236
237  Revision 1.9 2003/05/06 23:17:12 chr32
238  Moving JMX tests back in from sandbox.
239
240  Revision 1.1 2003/03/07 16:35:18 billhorsman
241  moved jmx stuff into sandbox until it is tested
242
243  Revision 1.7 2003/03/04 10:58:45 billhorsman
244  checkstyle
245
246  Revision 1.6 2003/03/04 10:24:41 billhorsman
247  removed try blocks around each test
248
249  Revision 1.5 2003/03/03 17:09:09 billhorsman
250  all tests now extend AbstractProxoolTest
251
252  Revision 1.4 2003/03/03 11:12:06 billhorsman
253  fixed licence
254
255  Revision 1.3 2003/03/01 15:27:25 billhorsman
256  checkstyle
257
258  Revision 1.2 2003/02/28 12:53:59 billhorsman
259  move database to db directory and use constants where possible
260
261  Revision 1.1 2003/02/26 19:03:43 chr32
262  Init rev.
263
264  */

265
Popular Tags