1 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 ; 14 import javax.management.AttributeList ; 15 import javax.management.MBeanServer ; 16 import javax.management.MBeanServerFactory ; 17 import javax.management.Notification ; 18 import javax.management.NotificationFilter ; 19 import javax.management.NotificationFilterSupport ; 20 import javax.management.NotificationListener ; 21 import javax.management.ObjectName ; 22 import java.util.Iterator ; 23 import java.util.Properties ; 24 25 26 34 public class ConnectionPoolMBeanTest extends AbstractJMXTest { 35 private MBeanServer mBeanServer; 36 private boolean notified; 37 38 41 public ConnectionPoolMBeanTest(String s) { 42 super(s); 43 } 44 45 49 public void testGetAttribute() throws Exception { 50 final String alias = "testGetAttribute"; 51 createBasicPool(alias); 52 final ObjectName objectName = ProxoolJMXHelper.getObjectName(alias); 53 final String fatalSql = (String ) 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 64 public void testGetAttributes() throws Exception { 65 final String alias = "testGetAttributes"; 66 createBasicPool(alias); 67 final String fatalSQLAttribute = ProxoolJMXHelper.getValidIdentifier(ProxoolConstants.FATAL_SQL_EXCEPTION); 68 final String aliasAttribute = ProxoolJMXHelper.getValidIdentifier(ProxoolConstants.ALIAS); 69 final ObjectName objectName = ProxoolJMXHelper.getObjectName(alias); 70 final AttributeList attributes = this.mBeanServer.getAttributes(objectName, new String []{ 71 fatalSQLAttribute, 72 aliasAttribute 73 }); 74 String fatalSqlValue = null; 75 String aliasValue = null; 76 Iterator attributeIterator = attributes.iterator(); 77 while (attributeIterator.hasNext()) { 78 Attribute attribute = (Attribute ) attributeIterator.next(); 79 if (attribute.getName().equals(aliasAttribute)) { 80 aliasValue = (String ) attribute.getValue(); 81 } else if (attribute.getName().equals(fatalSQLAttribute)) { 82 fatalSqlValue = (String ) 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 98 public void testSetAttribute() throws Exception { 99 String alias = "testSetAttribute"; 100 createBasicPool(alias); 101 final ObjectName objectName = ProxoolJMXHelper.getObjectName(alias); 102 final String fatalSQLAttributeName = ProxoolJMXHelper.getValidIdentifier(ProxoolConstants.FATAL_SQL_EXCEPTION); 103 final String newValue = "dingo"; 104 this.mBeanServer.setAttribute(objectName, 105 new Attribute (fatalSQLAttributeName, newValue)); 106 String fatalSQLAttribtueValue = (String ) mBeanServer.getAttribute(objectName, fatalSQLAttributeName); 107 assertTrue("Expexted fatalSQLException JMX attribtue to have value '" + newValue + "' but the value was '" 109 + fatalSQLAttribtueValue + "'.", 110 fatalSQLAttribtueValue.equals(newValue)); 111 final String proxoolProopertyValue = (String ) 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 this.mBeanServer.setAttribute(objectName, 119 new Attribute (fatalSQLAttributeName, "")); 120 fatalSQLAttribtueValue = (String ) 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 { 127 String alias = "testSetAttributes"; 128 createBasicPool(alias); 129 final ObjectName objectName = ProxoolJMXHelper.getObjectName(alias); 130 final String fatalSQLAttributeName = ProxoolJMXHelper.getValidIdentifier(ProxoolConstants.FATAL_SQL_EXCEPTION); 131 final String testSQLAttributeName = ProxoolJMXHelper.getValidIdentifier(ProxoolConstants.HOUSE_KEEPING_TEST_SQL); 132 final String newValue = "dingo"; 133 final AttributeList attributeList = new AttributeList (); 135 attributeList.add(new Attribute (fatalSQLAttributeName, newValue)); 136 attributeList.add(new Attribute (testSQLAttributeName, newValue)); 137 this.mBeanServer.setAttributes(objectName, attributeList); 138 final String fatalSQLAttribtueValue = (String ) mBeanServer.getAttribute(objectName, fatalSQLAttributeName); 139 final String testSQLAttribtueValue = (String ) mBeanServer.getAttribute(objectName, testSQLAttributeName); 140 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 final String fatalSQLProxoolPropertyValue = (String ) ProxoolFacade.getConnectionPoolDefinition(alias) 149 .getFatalSqlExceptions().toArray()[0]; 150 final String 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 { 162 final String alias = "testInvokeShutown"; 163 createBasicPool(alias); 164 final ObjectName objectName = ProxoolJMXHelper.getObjectName(alias); 165 this.mBeanServer.invoke(objectName, "shutdown", new Object [0], new String [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 } 172 } 173 174 public void testNotification() throws Exception { 175 String alias = "testNotification"; 176 Properties info = createBasicPool(alias); 177 final NotificationListener notificationListener = new TestNotificationListener(); 178 final ObjectName objectName = ProxoolJMXHelper.getObjectName(alias); 179 this.mBeanServer.addNotificationListener(objectName, notificationListener, getFilter(), notificationListener); 180 this.mBeanServer.setAttribute(objectName, 182 new Attribute (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 info = (Properties ) 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 { 194 public void handleNotification(Notification notification, Object 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 getFilter() { 204 final NotificationFilterSupport notificationFilter = new NotificationFilterSupport (); 205 notificationFilter.enableType(ConnectionPoolMBean.NOTIFICATION_TYPE_DEFINITION_UPDATED); 206 return notificationFilter; 207 } 208 209 213 protected void setUp() throws Exception { 214 this.notified = false; 215 Class.forName(ProxoolDriver.class.getName()); 216 this.mBeanServer = MBeanServerFactory.createMBeanServer(); 217 super.setUp(); 218 } 219 220 224 protected void tearDown() throws Exception { 225 MBeanServerFactory.releaseMBeanServer(this.mBeanServer); 226 this.mBeanServer = null; 227 super.tearDown(); 228 } 229 } 230 231 265 | Popular Tags |