Source Code Cross Referenced for TestSipAddress.java in  » 6.0-JDK-Modules » j2me » javax » microedition » sip » 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 » javax.microedition.sip 
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 javax.microedition.sip;
028:
029:        import com.sun.midp.i3test.TestCase;
030:
031:        /**
032:         * Tests for SipAddress class.
033:         *
034:         */
035:        public class TestSipAddress extends TestCase {
036:
037:            /** Error message returned when SipAddress cannot be created. */
038:            private final String errCreateMsg = "Cannot create SipAddress: ";
039:
040:            /**
041:             * Body of the test 1.
042:             *
043:             * Test constructors of SipAddress class.
044:             */
045:            void Test1() {
046:                try {
047:                    // try to create SipAddress object with null argument
048:                    SipAddress testAddress = new SipAddress(null);
049:                    fail("NullPointerException wasn't caused");
050:                } catch (java.lang.NullPointerException e) {
051:                    // assertTrue("IllegalArgumentException was caused", true);
052:                } catch (Throwable e) {
053:                    fail("" + e + " was caused");
054:                }
055:
056:                try {
057:                    // try to create SipAddress object with wrong scheme argument
058:                    SipAddress testAddress = new SipAddress(
059:                            "http://www.sun.com");
060:                    fail("IllegalArgumentException wasn't caused");
061:                } catch (java.lang.IllegalArgumentException e) {
062:                    // assertTrue("IllegalArgumentException was caused", true);
063:                } catch (Throwable e) {
064:                    fail("" + e + " was caused");
065:                }
066:
067:                try {
068:                    // try to create SipAddress object with empty user part
069:                    SipAddress testAddress = new SipAddress("sip:@www.sun.com");
070:                    fail("IllegalArgumentException wasn't caused");
071:                } catch (java.lang.IllegalArgumentException e) {
072:                    // assertTrue("IllegalArgumentException was caused", true);
073:                } catch (Throwable e) {
074:                    fail("" + e + " was caused");
075:                }
076:
077:                try {
078:                    // try to create SipAddress object with wrong port value
079:                    SipAddress testAddress = new SipAddress(
080:                            "sip:user@www.sun.com:abcd");
081:                    fail("IllegalArgumentException wasn't caused");
082:                } catch (java.lang.IllegalArgumentException e) {
083:                    assertTrue("IllegalArgumentException was caused", true);
084:                } catch (Throwable e) {
085:                    fail("" + e + " was caused");
086:                }
087:
088:                try {
089:                    // try to create SipAddress object and check all its parts
090:                    SipAddress testAddress = new SipAddress("DisplName",
091:                            "sips:user:PassW@host:1234;par1=val1;par2=val2");
092:                    assertTrue("Scheme", testAddress.getScheme()
093:                            .equalsIgnoreCase("sips"));
094:                    assertTrue("User", testAddress.getUser().equals(
095:                            "user:PassW"));
096:                    assertTrue("Host", testAddress.getHost().equalsIgnoreCase(
097:                            "host"));
098:                    assertTrue("Port", testAddress.getPort() == 1234);
099:                    assertTrue("Parameter 1", testAddress.getParameter("par1")
100:                            .equalsIgnoreCase("val1"));
101:                    assertTrue("Parameter 2", testAddress.getParameter("par2")
102:                            .equalsIgnoreCase("val2"));
103:                    assertTrue("DisplayName", testAddress.getDisplayName()
104:                            .equalsIgnoreCase("DisplName"));
105:                } catch (Throwable e) {
106:                    fail("" + e + " was caused");
107:                }
108:            }
109:
110:            /**
111:             * Body of the test 2.
112:             *
113:             * Test checks URI parts. 
114:             * @see ABNF in RFC3261, p. 228
115:             */
116:            void Test2() {
117:                try {
118:                    SipAddress testAddress = new SipAddress("sip:test.org:5060");
119:                    int testPort = testAddress.getPort();
120:                    assertEquals("Returned port number should have been 5060",
121:                            testPort, 5060);
122:                } catch (Throwable e) {
123:                    fail("\n" + e
124:                            + " was caused due to server SIPURI with port=5060");
125:                }
126:
127:                try {
128:                    SipAddress testAddress = new SipAddress("sip:test.org");
129:                    int testPort = testAddress.getPort();
130:
131:                    /*
132:                     * Per JSR180 specs, if port number is not set, getPort()
133:                     * should return 5060
134:                     */
135:                    assertEquals("Returned port number should have been 5060",
136:                            testPort, 5060);
137:                } catch (Throwable e) {
138:                    fail("\n" + e
139:                            + " was caused due to non-existent port number");
140:                }
141:
142:                try {
143:                    /*
144:                     * The valid Contact address "*" is accepted in SipAddress.
145:                     * In this case all properties will be null and port number is 0.
146:                     * Yet toString() method will return the value "*".
147:                     */
148:                    SipAddress testAddress = new SipAddress("*");
149:                    int testPort = testAddress.getPort();
150:                    assertEquals("Returned port number should have been 0",
151:                            testPort, 0);
152:
153:                    String strAddr = testAddress.toString();
154:                    assertTrue("toString() returned '" + strAddr
155:                            + "' instead of '*'.", strAddr.equals("*"));
156:                } catch (Throwable e) {
157:                    fail("\n" + e + " was caused due to wild-card (*) address");
158:                }
159:            }
160:
161:            /**
162:             * Body of the test 3.
163:             *
164:             * Test SipAddress methods: setDisplayName()/getDisplayName().
165:             */
166:            void Test3() {
167:                SipAddress sa = null;
168:
169:                try {
170:                    sa = new SipAddress("sip:test.org:5090");
171:                } catch (Exception e) {
172:                    fail(errCreateMsg + e);
173:                }
174:
175:                // Testing set/getDisplayName()
176:                String name = sa.getDisplayName();
177:                assertTrue("getDisplayName() returned '" + name
178:                        + "' instead of null.", name == null);
179:
180:                String testName = "Test Name";
181:                sa.setDisplayName(testName);
182:                name = sa.getDisplayName();
183:                assertTrue("getDisplayName() returned '" + name
184:                        + "' after setDisplayName().", name.equals(testName));
185:
186:                sa.setDisplayName("");
187:                name = sa.getDisplayName();
188:                assertTrue("getDisplayName() returned '" + name
189:                        + "' after setDisplayName(\"\").", name == null);
190:            }
191:
192:            /**
193:             * Body of the test 4.
194:             *
195:             * Test SipAddress methods: setScheme()/getScheme().
196:             */
197:            void Test4() {
198:                SipAddress sa = null;
199:
200:                try {
201:                    sa = new SipAddress("sip:test.org:5090");
202:                } catch (Exception e) {
203:                    fail(errCreateMsg + e);
204:                }
205:
206:                // Testing set/getScheme()
207:                String scheme = sa.getScheme();
208:                assertTrue("getScheme() returned '" + scheme
209:                        + "' instead of 'sip'.", scheme.equals("sip"));
210:
211:                // scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
212:                try {
213:                    // TODO: maybe create a test for unknown scheme?
214:                    sa = new SipAddress("Test Name <tel:123456>");
215:                } catch (Exception e) {
216:                    fail("Cannot create SipAddress: " + e);
217:                }
218:
219:                // TODO: implement changing of object's type, not only a scheme's name!
220:                sa.setScheme("sips");
221:                scheme = sa.getScheme();
222:                assertTrue("getScheme() returned '" + scheme
223:                        + "' instead of 'sips'.", scheme.equals("sips"));
224:            }
225:
226:            /**
227:             * Body of the test 5.
228:             *
229:             * Test SipAddress methods: setUser()/getUser().
230:             */
231:            void Test5() {
232:                SipAddress sa = null;
233:
234:                try {
235:                    sa = new SipAddress("sip:alice:secretword@atlanta.com;"
236:                            + "transport=tcp");
237:                } catch (Exception e) {
238:                    fail(errCreateMsg + e);
239:                }
240:
241:                String user = sa.getUser();
242:                assertTrue("getuser() returned '" + user
243:                        + "' instead of 'alice'.", user
244:                        .equals("alice:secretword"));
245:
246:                String testUser = "User_Bob";
247:                try {
248:                    sa.setUser(testUser);
249:                } catch (Exception e) {
250:                    fail("setUser() has thrown an exception:" + e);
251:                }
252:
253:                user = sa.getUser();
254:                assertTrue("getUser() returned '" + user + "' instead of '"
255:                        + testUser + "'.", user.equals(testUser));
256:            }
257:
258:            /**
259:             * Body of the test 6.
260:             *
261:             * Test SipAddress methods: setURI()/getURI().
262:             */
263:            void Test6() {
264:                SipAddress sa = null;
265:
266:                try {
267:                    sa = new SipAddress("tel:123456");
268:                } catch (Exception e) {
269:                    fail(errCreateMsg + e);
270:                }
271:
272:                // Testing set/getURI()
273:                String uri = sa.getURI();
274:                assertTrue("getURI() returned '" + uri
275:                        + "' instead of 'tel:123456'.", uri
276:                        .equals("tel:123456"));
277:
278:                // Check that the URI parameters are ignored
279:                String testUri = "sip:test.org:5060";
280:                try {
281:                    sa.setURI(testUri + ";param1=test");
282:                } catch (Exception e) {
283:                    fail("setURI() has thrown an exception: " + e);
284:                }
285:
286:                uri = sa.getURI();
287:                assertTrue("getURI() returned '" + uri + "' instead of '"
288:                        + testUri + "'.", uri.equals(testUri));
289:
290:                // Try to set an invalid URI.
291:                testUri = "invalid uri";
292:                try {
293:                    sa.setURI(testUri);
294:                    fail("setURI(): IAE was not thrown.");
295:                } catch (IllegalArgumentException iae) {
296:                } catch (Exception e) {
297:                    fail("setURI() has thrown '" + e + "' instead of IAE.");
298:                }
299:            }
300:
301:            /**
302:             * Body of the test 7.
303:             *
304:             * Test SipAddress methods: setHost()/getHost() & setPort()/getPort().
305:             */
306:            void Test7() {
307:                SipAddress sa = null;
308:
309:                try {
310:                    sa = new SipAddress(
311:                            "sip:alice@atlanta.com:5070;transport=tcp");
312:                } catch (Exception e) {
313:                    fail(errCreateMsg + e);
314:                }
315:
316:                String testHost = "atlanta.com";
317:                String host = sa.getHost();
318:                assertTrue("getHost() has returned '" + host + "' instead of '"
319:                        + testHost + "'", host.equals(testHost));
320:
321:                int port = sa.getPort();
322:                assertTrue("getPort() has returned '" + port
323:                        + "' instead of 5070.", port == 5070);
324:
325:                String val;
326:                String hostNames[] = { "test.com", "10.0.0.1",
327:                        "www.test.spb.com" };
328:
329:                for (int i = 0; i < hostNames.length; i++) {
330:                    try {
331:                        sa.setHost(hostNames[i]);
332:                    } catch (Exception e) {
333:                        fail("setHost(" + hostNames[i]
334:                                + ") has thrown an exception: " + e);
335:                    }
336:
337:                    val = sa.getHost();
338:                    assertTrue("getHost() has returned '" + val
339:                            + "' instead of '" + hostNames[i] + "'.", val
340:                            .equals(hostNames[i]));
341:                } // end for
342:
343:                String wrongNames[] = { null, "" };
344:
345:                for (int i = 0; i < wrongNames.length; i++) {
346:                    try {
347:                        sa.setHost(wrongNames[i]);
348:                        fail("setHost(" + wrongNames + ") didn't throw IAE");
349:                    } catch (Exception e) {
350:                    }
351:                } // end for
352:
353:                // Testing setPort()/ getPort()...
354:                //
355:                // setPort() sets the port number of the SIP address.
356:                // Valid range is 0-65535, where 0 means that the port number
357:                // is removed from the address URI.
358:                //
359:                // getPort() returns the port number of the SIP address.
360:                // If port number is not set, return 5060.
361:                // If the address is wildcard "*" return 0.
362:                try {
363:                    sa.setPort(5090);
364:                } catch (Exception e) {
365:                    fail("setPort(5090) has thrown an exception: " + e);
366:                }
367:
368:                port = sa.getPort();
369:                assertTrue("getPort() has returned '" + port
370:                        + "' instead of 5090.", port == 5090);
371:
372:                // Check that setPort(0) removes the port number from the URI.
373:                try {
374:                    sa.setPort(0);
375:                } catch (Exception e) {
376:                    fail("setPort(0) has thrown an exception: " + e);
377:                }
378:
379:                port = sa.getPort();
380:                assertTrue("getPort() has returned '" + port
381:                        + "' instead of 5060.", port == 5060);
382:
383:                // Trying to set an invalid port number...
384:                try {
385:                    sa.setPort(70000);
386:                    fail("setPort(70000) didn't throw IAE.");
387:                } catch (IllegalArgumentException iae) {
388:                } catch (Exception e) {
389:                    fail("setPort(70000) has thrown '" + e
390:                            + "' instead of IAE.");
391:                }
392:
393:                // Check that the default port number is 5060.
394:                try {
395:                    sa = new SipAddress("sip:alice@atlanta.com;transport=tcp");
396:                } catch (Exception e) {
397:                    fail(errCreateMsg + e);
398:                }
399:
400:                port = sa.getPort();
401:                assertTrue("getPort() has returned '" + port
402:                        + "' instead of 5060.", port == 5060);
403:
404:                // Check that if SipAddress is '*', getPort() returns 0.
405:                try {
406:                    sa = new SipAddress("*");
407:                } catch (Exception e) {
408:                    fail(errCreateMsg + e);
409:                }
410:
411:                port = sa.getPort();
412:                assertTrue("getPort() has returned '" + port
413:                        + "' instead of 0.", port == 0);
414:            }
415:
416:            /**
417:             * Body of the test 8.
418:             *
419:             * Test SipAddress methods: setParameter()/getParameter()/
420:             *                          removeParameter()/getParameterNames().
421:             */
422:            void Test8() {
423:                SipAddress sa = null;
424:
425:                try {
426:                    sa = new SipAddress("sip:test.org:5090");
427:                } catch (Exception e) {
428:                    fail(errCreateMsg + e);
429:                }
430:
431:                String val;
432:                String paramList[][] = {
433:                        // parameter's name, value to set, value that
434:                        // must be returned by getParameter()
435:                        { "param1", "value1", "value1" },
436:                        { "param1", "value_2", "value_2" },
437:                        { "param2", "test%20value", "test%20value" },
438:                        { "param3", null, "" } };
439:                int paramNum = 3;
440:
441:                // Testing set/getParameter()...
442:                // setParameter() sets the named URI parameter to the specified value.
443:                // If the value is null the parameter is interpreted as a parameter
444:                // without value. Existing parameter will be overwritten, otherwise
445:                // the parameter is added.
446:                for (int i = 0; i < paramList.length; i++) {
447:                    try {
448:                        sa.setParameter(paramList[i][0], paramList[i][1]);
449:                    } catch (Exception e) {
450:                        fail("setParameter(" + paramList[i][0] + ", "
451:                                + paramList[i][1]
452:                                + ") has thrown an exception: " + e);
453:                    }
454:
455:                    val = sa.getParameter(paramList[i][0]);
456:                    assertTrue("getParameter() has returned '" + val
457:                            + "' instead of '" + paramList[i][2] + "'.", val
458:                            .equals(paramList[i][2]));
459:                } // end for
460:
461:                // getParameter() returns empty string for parameters without value
462:                // and null if the parameter is not defined.
463:                val = sa.getParameter("unexisting");
464:                assertTrue("getParameter() has returned '" + val
465:                        + "' instead of 'null'.", val == null);
466:
467:                // Testing getParameterNames()...
468:                // It returns a string array of parameter names or null if the address
469:                // does not have any parameters.
470:                String name;
471:                String[] paramNames = sa.getParameterNames();
472:                assertTrue("getParameterNames() has returned "
473:                        + paramNames.length + " parameters instead of "
474:                        + paramNum + ".", paramNames.length == paramNum);
475:
476:                for (int i = 0; i < paramNames.length; i++) {
477:                    name = "param" + (i + 1);
478:
479:                    boolean isValid = false;
480:
481:                    for (int j = 0; j < paramNames.length; j++) {
482:                        if (paramNames[j].equals(name)) {
483:                            isValid = true;
484:                            break;
485:                        }
486:                    }
487:
488:                    // Check that getParameterNames() has returned the correct names.
489:                    assertTrue("getParameterNames() has returned '" + name
490:                            + "' parameter but it doesn't exist.", isValid);
491:                }
492:
493:                // Testing removeParameter()...
494:                for (int i = 0; i < paramNames.length; i++) {
495:                    name = "param" + (i + 1);
496:                    sa.removeParameter(name);
497:
498:                    val = sa.getParameter(name);
499:                    assertTrue("getParameter() has returned '" + val
500:                            + "' instead of 'null'.", val == null);
501:                }
502:
503:                paramNames = sa.getParameterNames();
504:                int len = (paramNames != null) ? paramNames.length : 0;
505:
506:                assertTrue("getParameterNames() has returned " + len
507:                        + " parameter(s) instead of null.", paramNames == null);
508:            }
509:
510:            /**
511:             * Body of the test 9.
512:             *
513:             * Test SipAddress methods: toString().
514:             */
515:            void Test9() {
516:                SipAddress sa = null;
517:                String testUriReordered = "sips:alice@atlanta.com?priority=urgent&subject=project%20x";
518:                String[] testUris = {
519:                        "sips:alice@atlanta.com?subject=project%20x&priority=urgent",
520:                        "Alice <sip:alice@atlanta.com>",
521:                        "sip:alice@atlanta.com",
522:                        "sip:alice:secretword@atlanta.com;transport=tcp",
523:                        "The Name <sip:alice:secretword@atlanta.com:6000;transport=tcp>",
524:                        "sip:+1-212-555-1212:1234@gateway.com;user=phone",
525:                        "sips:1212@gateway.com",
526:                        "sip:alice@192.0.2.4",
527:                        "sip:atlanta.com;method=REGISTER?to=alice%40atlanta.com",
528:                        "sip:alice;day=tuesday@atlanta.com" };
529:
530:                // toString() returns a fully qualified SIP address,
531:                // with display name, URI and URI parameters.
532:                // If display name is not specified only a SIP URI is returned.
533:                // If the port is not explicitly set (to 5060 or other value)
534:                // it will be omitted from the address URI in returned String.
535:                for (int i = 0; i < testUris.length; i++) {
536:                    try {
537:                        sa = new SipAddress(testUris[i]);
538:                    } catch (Exception e) {
539:                        fail(errCreateMsg + e);
540:                    }
541:
542:                    String strAddr = sa.toString();
543:                    boolean isValid;
544:
545:                    if (i == 0) {
546:                        // The first test URL has two parameters. Their order
547:                        // is not defined in the spec, so they can be reordered.
548:                        isValid = strAddr.equals(testUris[i])
549:                                || strAddr.equals(testUriReordered);
550:                    } else {
551:                        isValid = strAddr.equals(testUris[i]);
552:                    }
553:
554:                    assertTrue("toString() has returned '" + strAddr
555:                            + "' instead of '" + testUris[i] + "'.", isValid);
556:                }
557:
558:                // "*" address was tested in Test2().
559:            }
560:
561:            /**
562:             * Run the tests
563:             */
564:            public void runTests() {
565:                declare("Constructor SipAddress test");
566:                Test1();
567:
568:                declare("URI parsing test");
569:                Test2();
570:
571:                declare("setDisplayName()/getDisplayName()");
572:                Test3();
573:
574:                declare("setScheme()/getScheme()");
575:                Test4();
576:
577:                declare("setUser()/getUser()");
578:                Test5();
579:
580:                declare("setURI()/getURI()");
581:                Test6();
582:
583:                declare("setHost()/getHost() & setPort()/getPort()");
584:                Test7();
585:
586:                declare("setParameter()/getParameter()/removeParameter()/"
587:                        + "getParameterNames()");
588:                Test8();
589:
590:                declare("toString()");
591:                Test9();
592:            }
593:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.