Source Code Cross Referenced for TestInterruptedIO.java in  » 6.0-JDK-Modules » j2me » com » sun » midp » io » j2me » socket » 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 » 6.0 JDK Modules » j2me » com.sun.midp.io.j2me.socket 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * 	
003:         *
004:         * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006:         * 
007:         * This program is free software; you can redistribute it and/or
008:         * modify it under the terms of the GNU General Public License version
009:         * 2 only, as published by the Free Software Foundation.
010:         * 
011:         * This program is distributed in the hope that it will be useful, but
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014:         * General Public License version 2 for more details (a copy is
015:         * included at /legal/license.txt).
016:         * 
017:         * You should have received a copy of the GNU General Public License
018:         * version 2 along with this work; if not, write to the Free Software
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA
021:         * 
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023:         * Clara, CA 95054 or visit www.sun.com if you need additional
024:         * information or have any questions.
025:         */
026:
027:        package com.sun.midp.io.j2me.socket;
028:
029:        import java.io.*;
030:        import javax.microedition.io.*;
031:        import com.sun.midp.i3test.*;
032:
033:        /**
034:         * Test of SocketConnection function that unblocks a pending read
035:         * when the connection is closed.
036:         */
037:        public class TestInterruptedIO extends TestCase implements  Runnable {
038:
039:            /**
040:             * URI to which the test will try to open connections.
041:             * Use any URI visible from your network, for example,
042:             * "socket://www.sun.com:80" will do in most cases;
043:             * "socket://localhost:80" will do if your computer is
044:             * running an http server. Please, contact your network
045:             * administrator if you have a problem that you cannot
046:             * resolve yourself.
047:             */
048:            static private final String otherSideUri =
049:            //"socket://www.sun.com:80";
050:            "socket://localhost:80";
051:
052:            /** SocketConnection being tested. */
053:            SocketConnection connection;
054:
055:            /** InputStream for the blocking read. */
056:            InputStream is;
057:
058:            /** Thread that is blocked on the read. */
059:            Thread t1;
060:
061:            /** Records if the expected InterruptedIOException is thrown. */
062:            boolean exceptionThrown = false;
063:
064:            /** Records if the thread exited. */
065:            boolean done = false;
066:
067:            /**
068:             * Run method to open the InputStream and block on a read.
069:             * When the stream/connection is closed the read should 
070:             * unblock with an InterruptedIOException.
071:             */
072:            public void run() {
073:                try {
074:                    is = connection.openInputStream();
075:
076:                    // Notify indicating the InputStream is open
077:                    synchronized (this ) {
078:                        notifyAll();
079:                    }
080:
081:                    // Block on a read
082:                    int ch = is.read();
083:                    assertTrue("read should not have succeeded", false);
084:                } catch (InterruptedIOException ioe) {
085:                    assertNotNull("Caught InterruptedIOException", ioe);
086:                    exceptionThrown = true;
087:                } catch (IOException ioe) {
088:                    assertNull("Unexpected IOException", ioe);
089:                } finally {
090:                    synchronized (this ) {
091:                        done = true;
092:                        notifyAll();
093:                    }
094:                }
095:            }
096:
097:            /**
098:             * Open the connection and start the thread to call the run
099:             * method.  It waits until the InputStream is open before returning.
100:             * @return false if testing cannot be continued
101:             */
102:            boolean setUp() {
103:                // Create a socket connection 
104:                try {
105:                    connection = (SocketConnection) Connector
106:                            .open(otherSideUri);
107:                    //OutputStream os = connection.openOutputStream();
108:                    //os.write("GET /midlets/midlet.jad HTTP/1.0\n\n".getBytes());
109:                } catch (IOException ioe) {
110:                    assertNull("Exception during socket open", ioe);
111:                }
112:
113:                assertNotNull("Verify socket open", connection);
114:                if (null == connection) {
115:                    fail("Could not open connection to "
116:                            + otherSideUri
117:                            + ".  If the host name cannot be resolved, you may "
118:                            + "need to change the i3test source to use a host name that"
119:                            + " is visible from your network.");
120:                    return false;
121:                }
122:
123:                // Launch a thread which would be blocked for some I/O operation
124:                // for above socket connection
125:                t1 = new Thread(this );
126:                t1.start();
127:
128:                // Wait for the InputStream to have been opened
129:                synchronized (this ) {
130:                    while (is == null) {
131:                        try {
132:                            wait(200);
133:                        } catch (InterruptedException e) {
134:                            // Ignore interrupt
135:                        }
136:                    }
137:                }
138:                return true;
139:            }
140:
141:            /**
142:             * Close the open connection.
143:             */
144:            void closeConnection() {
145:                try {
146:                    if (is != null) {
147:                        is.close();
148:                        is = null;
149:                    }
150:                    if (connection != null) {
151:                        connection.close();
152:                        connection = null;
153:                    }
154:                } catch (IOException e) {
155:                    assertNull("Exception closing a socket", e);
156:                    e.printStackTrace();
157:                }
158:            }
159:
160:            /**
161:             * This test creates another thread that tries to close a socket
162:             * connection that is in use by first thread. It is supposed to throw 
163:             * InterruptedIOException in this case.
164:             */
165:            void testForException() {
166:                closeConnection();
167:
168:                synchronized (this ) {
169:                    if (!done) {
170:                        try {
171:                            wait(2000);
172:                        } catch (InterruptedException e) {
173:                            System.out.println("Catch interrupt");
174:                        }
175:                    }
176:                }
177:
178:                assertTrue("Verify InterruptedIOException", exceptionThrown);
179:            }
180:
181:            /**
182:             * Clean up after the test. 
183:             */
184:            void tearDown() {
185:                closeConnection();
186:            }
187:
188:            /**
189:             * Run the test by setting up the connection, and starting
190:             * a thread to block on a read then  forcing the 
191:             * connection to be closed and checking that exception occurred.
192:             */
193:            public void runTests() {
194:                declare("Test InterruptedIOException");
195:
196:                boolean setupSuccess = setUp();
197:
198:                if (setupSuccess) {
199:                    testForException();
200:
201:                    tearDown();
202:                }
203:            }
204:
205:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.