Source Code Cross Referenced for DefaultAcceptorManagerTestCase.java in  » Inversion-of-Control » spice-netserve » org » codehaus » spice » netserve » connection » impl » 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 » Inversion of Control » spice netserve » org.codehaus.spice.netserve.connection.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (C) The Spice Group. All rights reserved.
003:         *
004:         * This software is published under the terms of the Spice
005:         * Software License version 1.1, a copy of which has been included
006:         * with this distribution in the LICENSE.txt file.
007:         */
008:        package org.codehaus.spice.netserve.connection.impl;
009:
010:        import java.net.Socket;
011:        import java.net.InetAddress;
012:        import java.net.InetSocketAddress;
013:        import java.net.ServerSocket;
014:        import java.nio.channels.ServerSocketChannel;
015:        import java.util.Random;
016:
017:        import junit.framework.TestCase;
018:
019:        /**
020:         *
021:         * @author Peter Donald
022:         * @version $Revision: 1.2 $ $Date: 2004/03/21 23:42:59 $
023:         */
024:        public class DefaultAcceptorManagerTestCase extends TestCase {
025:            public void testConnectAndDisconnectWithTimeoutNotSet()
026:                    throws Exception {
027:                final DefaultAcceptorManager manager = new DefaultAcceptorManager();
028:                manager.setMonitor(NullAcceptorMonitor.MONITOR);
029:                manager.setSoTimeout(10);
030:                manager.setShutdownTimeout(0);
031:                final String name = "name";
032:                assertEquals("isConnected pre connect", false, manager
033:                        .isConnected(name));
034:                final ExceptOnAcceptServerSocket socket = new ExceptOnAcceptServerSocket(
035:                        true);
036:                assertEquals("socket.getSoTimeout pre connect", 0, socket
037:                        .getSoTimeout());
038:                manager
039:                        .connect(name, socket,
040:                                new MockSocketConnectionHandler());
041:                assertEquals("socket.getSoTimeout post connect", 10, socket
042:                        .getSoTimeout());
043:                assertEquals("isConnected pre disconnect", true, manager
044:                        .isConnected(name));
045:                manager.disconnect(name);
046:                assertEquals("isConnected post disconnect", false, manager
047:                        .isConnected(name));
048:            }
049:
050:            public void testShutdownAcceptors() throws Exception {
051:                final DefaultAcceptorManager manager = new DefaultAcceptorManager();
052:                manager.setMonitor(NullAcceptorMonitor.MONITOR);
053:                manager.setSoTimeout(10);
054:                final String name = "name";
055:                assertEquals("isConnected pre connect", false, manager
056:                        .isConnected(name));
057:                manager.connect(name, new ExceptOnAcceptServerSocket(true),
058:                        new MockSocketConnectionHandler());
059:                assertEquals("isConnected pre shutdownAcceptors", true, manager
060:                        .isConnected(name));
061:                manager.shutdownAcceptors();
062:                assertEquals("isConnected post shutdownAcceptors", false,
063:                        manager.isConnected(name));
064:            }
065:
066:            public void testConnectAndDisconnectWithTimeoutSet()
067:                    throws Exception {
068:                final DefaultAcceptorManager manager = new DefaultAcceptorManager();
069:                manager.setMonitor(NullAcceptorMonitor.MONITOR);
070:                manager.setSoTimeout(10);
071:                final String name = "name";
072:                assertEquals("isConnected pre connect", false, manager
073:                        .isConnected(name));
074:                final ExceptOnAcceptServerSocket socket = new ExceptOnAcceptServerSocket(
075:                        true);
076:                socket.setSoTimeout(123);
077:                assertEquals("socket.getSoTimeout pre connect", 123, socket
078:                        .getSoTimeout());
079:                manager
080:                        .connect(name, socket,
081:                                new MockSocketConnectionHandler());
082:                assertEquals("socket.getSoTimeout post connect", 123, socket
083:                        .getSoTimeout());
084:                assertEquals("isConnected pre disconnect", true, manager
085:                        .isConnected(name));
086:                manager.disconnect(name);
087:                assertEquals("isConnected post disconnect", false, manager
088:                        .isConnected(name));
089:            }
090:
091:            public void testConnectAndAcceptAndDisconnectWithTimeoutSet()
092:                    throws Exception {
093:                final DefaultAcceptorManager manager = new DefaultAcceptorManager();
094:                final RecordingAcceptorMonitor monitor = new RecordingAcceptorMonitor();
095:                manager.setMonitor(monitor);
096:                manager.setSoTimeout(10);
097:                final String name = "name";
098:                assertEquals("isConnected pre connect", false, manager
099:                        .isConnected(name));
100:                final ServerSocketChannel channel = ServerSocketChannel.open();
101:                final ServerSocket serverSocket = channel.socket();
102:                serverSocket.setReuseAddress(true);
103:                final InetAddress localAddress = InetAddress.getLocalHost();
104:                final Random random = new Random();
105:                final int port = Math.abs(random.nextInt() % 5000) + 1024;
106:                final InetSocketAddress address = new InetSocketAddress(
107:                        localAddress, port);
108:                serverSocket.bind(address);
109:                while (!serverSocket.isBound()) {
110:                    System.out.print(".");
111:                    Thread.yield();
112:                }
113:
114:                final MockSocketConnectionHandler handler = new MockSocketConnectionHandler();
115:                manager.connect(name, serverSocket, handler);
116:
117:                final Socket clientSocket = new Socket(localAddress, port);
118:                System.out.print("Socket Connecting");
119:                while (!clientSocket.isConnected()) {
120:                    System.out.print(".");
121:                    Thread.yield();
122:                }
123:                System.out.println(" - Connected");
124:                clientSocket.close();
125:
126:                //Sleep to make sure accept goes through
127:                Thread.sleep(200);
128:
129:                assertNotNull("handler.socket", handler.getSocket());
130:
131:                assertEquals("isConnected pre disconnect", true, manager
132:                        .isConnected(name));
133:                manager.disconnect(name);
134:                assertEquals("isConnected post disconnect", false, manager
135:                        .isConnected(name));
136:            }
137:
138:            public void testDuplicateConnect() throws Exception {
139:                final DefaultAcceptorManager manager = new DefaultAcceptorManager();
140:                final String name = "name";
141:                assertEquals("isConnected pre connect", false, manager
142:                        .isConnected(name));
143:                final ExceptOnAcceptServerSocket socket = new ExceptOnAcceptServerSocket(
144:                        true);
145:                manager
146:                        .connect(name, socket,
147:                                new MockSocketConnectionHandler());
148:                assertEquals("isConnected pre disconnect", true, manager
149:                        .isConnected(name));
150:                try {
151:                    manager.connect(name, new ExceptOnAcceptServerSocket(true),
152:                            new MockSocketConnectionHandler());
153:                } catch (final IllegalArgumentException iae) {
154:                    return;
155:                } finally {
156:                    socket.close();
157:                    manager.shutdownAcceptors();
158:                    assertEquals("isConnected post disconnect", false, manager
159:                            .isConnected(name));
160:                }
161:                fail("Expected to fail due to duplicate connect");
162:            }
163:
164:            public void testDisconnectNonExistent() throws Exception {
165:                final DefaultAcceptorManager manager = new DefaultAcceptorManager();
166:                try {
167:                    manager.disconnect("NonExistent");
168:                } catch (IllegalArgumentException iae) {
169:                    return;
170:                } finally {
171:                    manager.shutdownAcceptors();
172:                }
173:                fail("Expected to fail to disconnect non existent acceptor");
174:            }
175:
176:            public void testConnectWithNullName() throws Exception {
177:                final DefaultAcceptorManager manager = new DefaultAcceptorManager();
178:                try {
179:                    manager.connect(null, new ExceptOnAcceptServerSocket(true),
180:                            new MockSocketConnectionHandler());
181:                } catch (final NullPointerException npe) {
182:                    assertEquals("npe.message", "name", npe.getMessage());
183:                    return;
184:                } finally {
185:                    manager.shutdownAcceptors();
186:                }
187:                fail("expected NPE due to null name in connect");
188:            }
189:
190:            public void testConnectWithNullSocket() throws Exception {
191:                final DefaultAcceptorManager manager = new DefaultAcceptorManager();
192:                try {
193:                    manager.connect("name", null,
194:                            new MockSocketConnectionHandler());
195:                } catch (final NullPointerException npe) {
196:                    assertEquals("npe.message", "socket", npe.getMessage());
197:                    return;
198:                } finally {
199:                    manager.shutdownAcceptors();
200:                }
201:                fail("expected NPE due to null socket in connect");
202:            }
203:
204:            public void testConnectWithNullHandler() throws Exception {
205:                final DefaultAcceptorManager manager = new DefaultAcceptorManager();
206:                try {
207:                    manager.connect("name",
208:                            new ExceptOnAcceptServerSocket(true), null);
209:                } catch (final NullPointerException npe) {
210:                    assertEquals("npe.message", "handler", npe.getMessage());
211:                    return;
212:                } finally {
213:                    manager.shutdownAcceptors();
214:                }
215:                fail("expected NPE due to null handler in connect");
216:            }
217:
218:            protected void setUp() throws Exception {
219:                System.out.print("Test: " + getName());
220:            }
221:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.