Source Code Cross Referenced for TestMultiple.java in  » 6.0-JDK-Modules » j2me » com » sun » midp » links » 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.links 
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.links;
028:
029:        import com.sun.cldc.isolate.Isolate;
030:        import com.sun.midp.i3test.TestCase;
031:        import java.io.IOException;
032:        import java.io.InterruptedIOException;
033:        import java.util.Enumeration;
034:        import java.util.Hashtable;
035:        import java.util.Random;
036:
037:        /**
038:         * Tests multiple receiving or sending threads blocked on the same link.
039:         */
040:        public class TestMultiple extends TestCase {
041:
042:            /**
043:             * The number of senders or receivers.
044:             */
045:            public static final int NUM_THREADS = 10;
046:
047:            /**
048:             * Amount of time to wait for threads to complete after sending or 
049:             * receiving a message.
050:             */
051:            public static final long TIMEOUT = 100L;
052:
053:            Random rand = new Random();
054:            Object lock = new Object();
055:
056:            Hashtable doneset; // Set<Thread>
057:            Hashtable messages; // Map<Thread, LinkMessage>
058:            Hashtable throwables; // Map<Thread, Throwable>
059:
060:            /**
061:             * Common code for completion callbacks for LoggingReceiver and 
062:             * LoggingSender. Adds the thread to the done set; if non-null, adds msg 
063:             * to the map of received messages; if non-null, adds any throwables 
064:             * caught to the map of caught throwables.
065:             */
066:            public void completed(Thread thr, LinkMessage msg, Throwable exc) {
067:                synchronized (lock) {
068:                    assertFalse("shouldn't be done already", doneset
069:                            .containsKey(thr));
070:                    doneset.put(thr, thr);
071:
072:                    if (msg != null) {
073:                        messages.put(thr, msg);
074:                    }
075:
076:                    if (exc != null) {
077:                        throwables.put(thr, exc);
078:                    }
079:                }
080:            }
081:
082:            /** 
083:             * Adds a completion callback to a Receiver thread.
084:             */
085:            class LoggingReceiver extends Receiver {
086:                public LoggingReceiver(Link newlink) {
087:                    super (newlink);
088:                }
089:
090:                public void completed(LinkMessage msg, Throwable exc) {
091:                    TestMultiple.this .completed(this , msg, exc);
092:                }
093:            }
094:
095:            /** 
096:             * Adds a completion callback to a Sender thread.
097:             */
098:            class LoggingSender extends Sender {
099:                public LoggingSender(Link newlink, LinkMessage lm) {
100:                    super (newlink, lm);
101:                }
102:
103:                public void completed(LinkMessage msg, Throwable exc) {
104:                    TestMultiple.this .completed(this , msg, exc);
105:                }
106:            }
107:
108:            /**
109:             * Tests multiple receivers blocked on the same link.  Creates NUM_THREADS
110:             * threads to receive messages. Then, sends messages one by one, and
111:             * ensures that each time exactly one thread is unblocked and has received
112:             * the message just sent. Finally, after sending enough messages, ensures
113:             * that all threads have been accounted for.
114:             */
115:            void testReceivers() throws IOException {
116:                Isolate is = Isolate.currentIsolate();
117:                Link link = Link.newLink(is, is);
118:                Receiver ra[] = new LoggingReceiver[NUM_THREADS];
119:
120:                doneset = new Hashtable(NUM_THREADS);
121:                messages = new Hashtable(NUM_THREADS);
122:                throwables = new Hashtable(NUM_THREADS);
123:
124:                for (int i = 0; i < NUM_THREADS; i++) {
125:                    ra[i] = new LoggingReceiver(link);
126:                }
127:                Utils.sleep(2 * TIMEOUT);
128:
129:                for (int i = 0; i < NUM_THREADS; i++) {
130:                    String s = Integer.toString(rand.nextInt());
131:
132:                    messages.clear();
133:                    throwables.clear();
134:                    link.send(LinkMessage.newStringMessage(s));
135:                    Utils.sleep(TIMEOUT);
136:
137:                    assertEquals("iteration " + i, i + 1, doneset.size());
138:
139:                    assertEquals("one message", 1, messages.size());
140:                    for (Enumeration e = messages.elements(); e
141:                            .hasMoreElements();) {
142:                        LinkMessage msg = (LinkMessage) e.nextElement();
143:                        assertTrue("message should contain a string", msg
144:                                .containsString());
145:                        String rs = msg.extractString();
146:                        assertEquals("strings should be equal", s, rs);
147:                    }
148:
149:                    assertEquals("zero throwables", 0, throwables.size());
150:                    for (Enumeration e = throwables.elements(); e
151:                            .hasMoreElements();) {
152:                        System.out.println("### " + e.nextElement());
153:                    }
154:                }
155:
156:                for (int i = 0; i < NUM_THREADS; i++) {
157:                    assertTrue("should be done", doneset.containsKey(ra[i]));
158:                    doneset.remove(ra[i]);
159:                }
160:            }
161:
162:            /**
163:             * Tests multiple senders blocked on the same link.  Creates NUM_THREADS
164:             * threads to send messages. Then, receives messages one by one, and
165:             * ensures that each time exactly one thread is unblocked and had sent the
166:             * message just received. Finally, after receiving enough messages,
167:             * ensures that all threads have been accounted for.
168:             */
169:            void testSenders() throws IOException {
170:                Isolate is = Isolate.currentIsolate();
171:                Link link = Link.newLink(is, is);
172:                LoggingSender sa[] = new LoggingSender[NUM_THREADS];
173:
174:                for (int i = 0; i < NUM_THREADS; i++) {
175:                    String s = Integer.toString(rand.nextInt());
176:                    sa[i] = new LoggingSender(link, LinkMessage
177:                            .newStringMessage(s));
178:                }
179:                Utils.sleep(2 * TIMEOUT);
180:
181:                doneset = new Hashtable(NUM_THREADS);
182:                throwables = new Hashtable(NUM_THREADS);
183:
184:                for (int i = 0; i < NUM_THREADS; i++) {
185:                    LinkMessage rm;
186:                    String rs;
187:
188:                    messages.clear();
189:                    throwables.clear();
190:
191:                    rm = link.receive();
192:                    Utils.sleep(TIMEOUT);
193:
194:                    assertEquals("iteration " + i, i + 1, doneset.size());
195:
196:                    assertTrue("message should contain a string", rm
197:                            .containsString());
198:                    rs = rm.extractString();
199:
200:                    assertEquals("one message", 1, messages.size());
201:                    for (Enumeration e = messages.elements(); e
202:                            .hasMoreElements();) {
203:                        LinkMessage msg = (LinkMessage) e.nextElement();
204:                        String ss = msg.extractString();
205:                        assertEquals("strings should be equal", ss, rs);
206:                    }
207:
208:                    assertEquals("zero throwables", 0, throwables.size());
209:                    for (Enumeration e = throwables.elements(); e
210:                            .hasMoreElements();) {
211:                        System.out.println("### " + e.nextElement());
212:                    }
213:                }
214:
215:                for (int i = 0; i < NUM_THREADS; i++) {
216:                    assertTrue("should be done", doneset.containsKey(sa[i]));
217:                    doneset.remove(sa[i]);
218:                }
219:            }
220:
221:            /**
222:             * Runs all tests.
223:             */
224:            public void runTests() throws IOException {
225:                declare("testReceivers");
226:                testReceivers();
227:
228:                declare("testSenders");
229:                testSenders();
230:            }
231:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.