Source Code Cross Referenced for StateListenerTest.java in  » Database-JDBC-Connection-Pool » proxool » org » logicalcobwebs » proxool » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database JDBC Connection Pool » proxool » org.logicalcobwebs.proxool 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * This software is released under a licence similar to the Apache Software Licence.
003:         * See org.logicalcobwebs.proxool.package.html for details.
004:         * The latest version is available at http://proxool.sourceforge.net
005:         */
006:        package org.logicalcobwebs.proxool;
007:
008:        import org.apache.commons.logging.Log;
009:        import org.apache.commons.logging.LogFactory;
010:
011:        import java.sql.Connection;
012:        import java.sql.DriverManager;
013:        import java.sql.SQLException;
014:        import java.util.Properties;
015:
016:        /**
017:         * Test that registering a {@link ConfigurationListenerIF} with the {@link ProxoolFacade}
018:         * works.
019:         *
020:         * @version $Revision: 1.14 $, $Date: 2006/01/18 14:40:06 $
021:         * @author Christian Nedregaard (christian_nedregaard@email.com)
022:         * @author $Author: billhorsman $ (current maintainer)
023:         * @since Proxool 0.7
024:         */
025:        public class StateListenerTest extends AbstractProxoolTest {
026:
027:            private static final Log LOG = LogFactory
028:                    .getLog(StateListenerTest.class);
029:
030:            /**
031:             * @see junit.framework.TestCase#TestCase
032:             */
033:            public StateListenerTest(String s) {
034:                super (s);
035:            }
036:
037:            /**
038:             * Test whether we can add a state listener and that it receives
039:             * notification of change of state
040:             */
041:            public void testAddStateListener() throws Exception {
042:
043:                String testName = "addStateListener";
044:                String alias = testName;
045:
046:                String url = TestHelper.buildProxoolUrl(alias,
047:                        TestConstants.HYPERSONIC_DRIVER,
048:                        TestConstants.HYPERSONIC_TEST_URL);
049:                Properties info = new Properties();
050:                info.setProperty(ProxoolConstants.USER_PROPERTY,
051:                        TestConstants.HYPERSONIC_USER);
052:                info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
053:                        TestConstants.HYPERSONIC_PASSWORD);
054:                info
055:                        .setProperty(
056:                                ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY,
057:                                "1");
058:                info
059:                        .setProperty(
060:                                ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY,
061:                                "0");
062:                info.setProperty(
063:                        ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY,
064:                        "1000");
065:                info
066:                        .setProperty(
067:                                ProxoolConstants.OVERLOAD_WITHOUT_REFUSAL_LIFETIME_PROPERTY,
068:                                "6000");
069:                ProxoolFacade.registerConnectionPool(url, info);
070:
071:                assertEquals("maximumConnectionCount", 1, ProxoolFacade
072:                        .getConnectionPoolDefinition(alias)
073:                        .getMaximumConnectionCount());
074:
075:                StateResultMonitor srm = new StateResultMonitor();
076:                ProxoolFacade.addStateListener(alias, srm);
077:
078:                assertEquals("maximumConnectionCount", 1, ProxoolFacade
079:                        .getConnectionPoolDefinition(alias)
080:                        .getMaximumConnectionCount());
081:
082:                Connection c1 = DriverManager.getConnection(url);
083:
084:                // Test BUSY
085:                srm.setExpectedUpState(StateListenerIF.STATE_BUSY);
086:                assertEquals("Timeout waiting for BUSY", ResultMonitor.SUCCESS,
087:                        srm.getResult());
088:
089:                assertEquals("maximumConnectionCount", 1, ProxoolFacade
090:                        .getConnectionPoolDefinition(alias)
091:                        .getMaximumConnectionCount());
092:
093:                try {
094:                    Connection c2 = DriverManager.getConnection(url);
095:                    fail("Didn't expect second connection since maximumConnectionCount is 1");
096:                } catch (SQLException e) {
097:                    // We expect a refusal here
098:                    // Log message only so we don't get a worrying stack trace
099:                    LOG.debug("Ignoring expected refusal: " + e.getMessage());
100:                }
101:
102:                // Test Overloaded
103:                srm.setExpectedUpState(StateListenerIF.STATE_OVERLOADED);
104:                assertEquals("Timeout waiting for OVERLOADED",
105:                        ResultMonitor.SUCCESS, srm.getResult());
106:
107:                // Test Busy again
108:                srm.setExpectedUpState(StateListenerIF.STATE_BUSY);
109:                assertEquals("Timeout waiting for BUSY", ResultMonitor.SUCCESS,
110:                        srm.getResult());
111:
112:                // Test Quiet again
113:                c1.close();
114:                srm.setExpectedUpState(StateListenerIF.STATE_QUIET);
115:                assertEquals("Timeout waiting for QUIET",
116:                        ResultMonitor.SUCCESS, srm.getResult());
117:
118:                // Bogus definition -> should be down
119:                try {
120:                    ProxoolFacade.updateConnectionPool("proxool." + alias
121:                            + ":blah:foo", null);
122:                } catch (ProxoolException e) {
123:                    LOG
124:                            .debug(
125:                                    "Ignoring expected exception when trying to register a pool with a bogus driver",
126:                                    e);
127:                }
128:                srm.setExpectedUpState(StateListenerIF.STATE_DOWN);
129:                assertEquals("Timeout waiting for DOWN", ResultMonitor.SUCCESS,
130:                        srm.getResult());
131:
132:            }
133:
134:            class TestStateListener implements  StateListenerIF {
135:
136:                private boolean somethingHappened;
137:
138:                private int upState;
139:
140:                public void upStateChanged(int newUpState) {
141:                    LOG.debug("upState: " + upState + " -> " + newUpState);
142:                    upState = newUpState;
143:                    somethingHappened = true;
144:                }
145:
146:                boolean isSomethingHappened() {
147:                    return somethingHappened;
148:                }
149:
150:                int getUpState() {
151:                    return upState;
152:                }
153:
154:                void reset() {
155:                    upState = 0;
156:                    somethingHappened = false;
157:                }
158:
159:                void waitForSomethingToHappen(int stateToWaitFor) {
160:
161:                    long start = System.currentTimeMillis();
162:                    while (!somethingHappened) {
163:                        if (upState == stateToWaitFor) {
164:                            if (!somethingHappened) {
165:                                LOG.error("Waiting for state = "
166:                                        + stateToWaitFor
167:                                        + " but it's already at that state");
168:                                break;
169:                            }
170:                        }
171:                        try {
172:                            Thread.sleep(500);
173:                        } catch (InterruptedException e) {
174:                            LOG.error("Awoken", e);
175:                        }
176:                        if (System.currentTimeMillis() - start > 30000) {
177:                            fail("Timeout waiting for something to happen");
178:                        }
179:                    }
180:
181:                }
182:
183:                int zgetNextState(int stateToWaitFor) {
184:                    waitForSomethingToHappen(stateToWaitFor);
185:                    somethingHappened = false;
186:                    return upState;
187:                }
188:            }
189:        }
190:
191:        /*
192:         Revision history:
193:         $Log: StateListenerTest.java,v $
194:         Revision 1.14  2006/01/18 14:40:06  billhorsman
195:         Unbundled Jakarta's Commons Logging.
196:
197:         Revision 1.13  2005/05/04 16:03:23  billhorsman
198:         Now catches ProxoolException when pool is updated with bogus driver.
199:
200:         Revision 1.12  2004/06/02 21:05:19  billhorsman
201:         Don't log worrying stack traces for expected exceptions.
202:
203:         Revision 1.11  2003/03/04 10:58:44  billhorsman
204:         checkstyle
205:
206:         Revision 1.10  2003/03/04 10:24:40  billhorsman
207:         removed try blocks around each test
208:
209:         Revision 1.9  2003/03/03 17:09:06  billhorsman
210:         all tests now extend AbstractProxoolTest
211:
212:         Revision 1.8  2003/03/03 11:12:05  billhorsman
213:         fixed licence
214:
215:         Revision 1.7  2003/03/02 00:37:23  billhorsman
216:         more robust
217:
218:         Revision 1.6  2003/03/01 15:27:24  billhorsman
219:         checkstyle
220:
221:         Revision 1.5  2003/03/01 15:24:09  billhorsman
222:         tweaked properties
223:
224:         Revision 1.4  2003/02/28 17:41:13  billhorsman
225:         more robust wait for state change
226:
227:         Revision 1.3  2003/02/27 18:01:48  billhorsman
228:         completely rethought the test structure. it's now
229:         more obvious. no new tests yet though.
230:
231:         Revision 1.2  2003/02/26 16:05:50  billhorsman
232:         widespread changes caused by refactoring the way we
233:         update and redefine pool definitions.
234:
235:         Revision 1.1  2003/02/19 23:07:57  billhorsman
236:         new test
237:
238:         Revision 1.2  2003/02/19 15:14:22  billhorsman
239:         fixed copyright (copy and paste error,
240:         not copyright change)
241:
242:         Revision 1.1  2003/02/19 13:47:31  chr32
243:         Added configuration listener test.
244:
245:         Revision 1.2  2003/02/18 16:58:12  chr32
246:         Checkstyle.
247:
248:         Revision 1.1  2003/02/18 16:51:20  chr32
249:         Added tests for ConnectionListeners.
250:
251:         */
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.