Source Code Cross Referenced for ExpressionTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » beans » tests » java » beans » 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 » Apache Harmony Java SE » org package » org.apache.harmony.beans.tests.java.beans 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /* 
0002:         * Licensed to the Apache Software Foundation (ASF) under one or more
0003:         * contributor license agreements.  See the NOTICE file distributed with
0004:         * this work for additional information regarding copyright ownership.
0005:         * The ASF licenses this file to You under the Apache License, Version 2.0
0006:         * (the "License"); you may not use this file except in compliance with
0007:         * the License.  You may obtain a copy of the License at
0008:         * 
0009:         *     http://www.apache.org/licenses/LICENSE-2.0
0010:         * 
0011:         * Unless required by applicable law or agreed to in writing, software
0012:         * distributed under the License is distributed on an "AS IS" BASIS,
0013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         * See the License for the specific language governing permissions and
0015:         * limitations under the License.
0016:         */
0017:
0018:        package org.apache.harmony.beans.tests.java.beans;
0019:
0020:        import java.beans.DefaultPersistenceDelegate;
0021:        import java.beans.Expression;
0022:        import java.util.Arrays;
0023:        import java.util.Vector;
0024:
0025:        import junit.framework.TestCase;
0026:
0027:        import org.apache.harmony.beans.tests.support.SampleBean;
0028:
0029:        /**
0030:         * Test the class java.beans.Expression.
0031:         */
0032:        public class ExpressionTest extends TestCase {
0033:
0034:            /**
0035:             * The test checks the correct constructor is initialized
0036:             */
0037:            public void testConstructor() throws Exception {
0038:                Expression expr = new Expression(SampleBean.class, "new",
0039:                        new Object[] { "hello" });
0040:                Object result = expr.getValue();
0041:                if (result != null && result instanceof  SampleBean) {
0042:                    SampleBean bean = (SampleBean) result;
0043:                    assertEquals("hello", bean.getText());
0044:                } else {
0045:                    fail("Cannot instantiate an instance of Bean class.");
0046:                }
0047:            }
0048:
0049:            /**
0050:             * The test checks the correct static method is initialized
0051:             */
0052:            public void testStatic() throws Exception {
0053:                SampleBean theBean = new SampleBean();
0054:                Expression expr = new Expression(SampleBean.class, "create",
0055:                        new Object[] { "hello", theBean });
0056:
0057:                Object result = expr.getValue();
0058:                if (result != null && result instanceof  SampleBean) {
0059:                    SampleBean bean = (SampleBean) result;
0060:                    assertEquals("hello", bean.getText());
0061:                    assertEquals(theBean, bean.getObject());
0062:                } else {
0063:                    fail("Cannot instantiate an instance of Bean class by "
0064:                            + "static method.");
0065:                }
0066:            }
0067:
0068:            /**
0069:             * The test checks the correct getter is initialized
0070:             */
0071:            public void testGetter() throws Exception {
0072:                Expression expr = new Expression(new SampleBean("hello"),
0073:                        "getText", new Object[] {});
0074:
0075:                Object result = expr.getValue();
0076:                if (result != null && result instanceof  String) {
0077:                    assertEquals("hello", result);
0078:                } else {
0079:                    fail("Result of SampleBean.getText() call is not "
0080:                            + "of String type.");
0081:                }
0082:            }
0083:
0084:            /**
0085:             * The test checks the correct array getter is initialized
0086:             */
0087:            public void testArrayGetter() throws Exception {
0088:                int[] a = { 1, 2, 3 };
0089:                Expression expr = new Expression(a, "get",
0090:                        new Object[] { new Integer(1) });
0091:
0092:                Object result = expr.getValue();
0093:                if (result != null && result instanceof  Integer) {
0094:                    assertEquals(new Integer(2), result);
0095:                } else {
0096:                    fail("Result of array getter is not of Integer type.");
0097:                }
0098:            }
0099:
0100:            /*
0101:             * Test the constructor under normal conditions.
0102:             */
0103:            public void testConstructor_Normal() {
0104:                Object target = new MockParent();
0105:                Object arg1 = "string1";
0106:                Object arg2 = new Object();
0107:                Object arg3 = "string3";
0108:                Object arg4 = new Integer(117);
0109:                Object[] oa = new Object[] { arg1, arg2, arg3, arg4 };
0110:                Expression t = new Expression(target, "method", oa);
0111:
0112:                assertSame(target, t.getTarget());
0113:                assertSame("method", t.getMethodName());
0114:                assertSame(oa, t.getArguments());
0115:                assertSame(arg1, t.getArguments()[0]);
0116:                assertSame(arg2, t.getArguments()[1]);
0117:                assertSame(arg3, t.getArguments()[2]);
0118:                assertSame(arg4, t.getArguments()[3]);
0119:
0120:                assertEquals("<unbound>=ExpressionTest$MockParent.method("
0121:                        + "\"string1\", Object, \"string3\", Integer);", t
0122:                        .toString());
0123:            }
0124:
0125:            /*
0126:             * Test the constructor with null target.
0127:             */
0128:            public void testConstructor_NullTarget() {
0129:                Object arg = new Object();
0130:                Object[] oa = new Object[] { arg };
0131:                Expression t = new Expression(null, "method", oa);
0132:
0133:                assertSame(null, t.getTarget());
0134:                assertSame("method", t.getMethodName());
0135:                assertSame(oa, t.getArguments());
0136:                assertSame(arg, t.getArguments()[0]);
0137:            }
0138:
0139:            /*
0140:             * Test the constructor with an array target.
0141:             */
0142:            public void testConstructor_ArrayTarget() {
0143:                Object target = new MockParent();
0144:                Object arg = new Object();
0145:                Object[] oa = new Object[] { arg };
0146:                Expression t = new Expression(target, "method", oa);
0147:
0148:                assertSame(target, t.getTarget());
0149:                assertSame("method", t.getMethodName());
0150:                assertSame(oa, t.getArguments());
0151:                assertSame(arg, t.getArguments()[0]);
0152:
0153:                assertEquals(
0154:                        "<unbound>=ExpressionTest$MockParent.method(Object);",
0155:                        t.toString());
0156:            }
0157:
0158:            /*
0159:             * Test the constructor with null method name.
0160:             */
0161:            public void testConstructor_NullMethodName() {
0162:                Object target = new Object();
0163:                Object[] oa = new Object[] { new Object() };
0164:                Expression t = new Expression(target, null, oa);
0165:
0166:                assertSame(target, t.getTarget());
0167:                assertSame(null, t.getMethodName());
0168:                assertSame(oa, t.getArguments());
0169:            }
0170:
0171:            /*
0172:             * Test the constructor with the method name "new".
0173:             */
0174:            public void testConstructor_NewMethodName() {
0175:                Object target = MockObject.class;
0176:                Object[] oa = new Object[] { new Object() };
0177:                Expression t = new Expression(target, "new", oa);
0178:
0179:                assertSame(target, t.getTarget());
0180:                assertSame("new", t.getMethodName());
0181:                assertSame(oa, t.getArguments());
0182:
0183:                assertEquals("<unbound>=Class.new(Object);", t.toString());
0184:            }
0185:
0186:            /*
0187:             * Test the constructor with empty method name.
0188:             */
0189:            public void testConstructor_EmptyMethodName() {
0190:                Object target = new Object();
0191:                Object[] oa = new Object[] { new Object() };
0192:                Expression t = new Expression(target, "", oa);
0193:
0194:                assertSame(target, t.getTarget());
0195:                assertSame("", t.getMethodName());
0196:                assertSame(oa, t.getArguments());
0197:            }
0198:
0199:            /*
0200:             * Test the constructor with null arguments.
0201:             */
0202:            public void testConstructor_NullArguments() {
0203:                Object target = new MockParent();
0204:                Expression t = new Expression(target, "method", null);
0205:
0206:                assertSame(target, t.getTarget());
0207:                assertSame("method", t.getMethodName());
0208:                assertEquals(0, t.getArguments().length);
0209:
0210:                assertEquals("<unbound>=ExpressionTest$MockParent.method();", t
0211:                        .toString());
0212:            }
0213:
0214:            /*
0215:             * Test the constructor with a null argument.
0216:             */
0217:            public void testConstructor_NullArgument() {
0218:                Object target = new MockParent();
0219:                Object[] oa = new Object[] { null };
0220:                Expression t = new Expression(target, "method", oa);
0221:
0222:                assertSame(target, t.getTarget());
0223:                assertSame("method", t.getMethodName());
0224:                assertSame(oa, t.getArguments());
0225:                assertNull(t.getArguments()[0]);
0226:
0227:                assertEquals(
0228:                        "<unbound>=ExpressionTest$MockParent.method(null);", t
0229:                                .toString());
0230:            }
0231:
0232:            /*
0233:             * Test the constructor(value, ...) under normal conditions.
0234:             */
0235:            public void testConstructor_Value_Normal() throws Exception {
0236:                Object val = new Object();
0237:                Object target = new MockParent();
0238:                Object arg1 = "mama";
0239:                Object arg2 = new Object();
0240:                Object arg3 = new Object();
0241:                Object arg4 = new Long(7);
0242:                Object[] oa = new Object[] { arg1, arg2, arg3, arg4 };
0243:                Expression t = new Expression(val, target, "method", oa);
0244:
0245:                assertSame(val, t.getValue());
0246:                assertSame(target, t.getTarget());
0247:                assertSame("method", t.getMethodName());
0248:                assertSame(oa, t.getArguments());
0249:                assertSame(arg1, t.getArguments()[0]);
0250:                assertSame(arg2, t.getArguments()[1]);
0251:                assertSame(arg3, t.getArguments()[2]);
0252:                assertSame(arg4, t.getArguments()[3]);
0253:
0254:                assertEquals("Object=ExpressionTest$MockParent.method("
0255:                        + "\"mama\", Object, Object, Long);", t.toString());
0256:            }
0257:
0258:            /*
0259:             * Test the constructor(value, ...) with null target.
0260:             */
0261:            public void testConstructor_Value_NullTarget() throws Exception {
0262:                Object val = new Object();
0263:                Object arg = new Object();
0264:                Object[] oa = new Object[] { arg };
0265:                Expression t = new Expression(val, null, "method", oa);
0266:
0267:                assertSame(val, t.getValue());
0268:                assertSame(null, t.getTarget());
0269:                assertSame("method", t.getMethodName());
0270:                assertSame(oa, t.getArguments());
0271:                assertSame(arg, t.getArguments()[0]);
0272:
0273:                assertEquals("Object=null.method(Object);", t.toString());
0274:            }
0275:
0276:            /*
0277:             * Test the constructor(value, ...) with an array target.
0278:             */
0279:            public void testConstructor_Value_ArrayTarget() throws Exception {
0280:                Integer val = new Integer(69);
0281:                Object target = new Integer[] { val };
0282:                Object arg = new Integer(0);
0283:                Object[] oa = new Object[] { arg };
0284:                Expression t = new Expression(val, target, "get", oa);
0285:
0286:                assertSame(val, t.getValue());
0287:                assertSame(target, t.getTarget());
0288:                assertSame("get", t.getMethodName());
0289:                assertSame(oa, t.getArguments());
0290:                assertSame(arg, t.getArguments()[0]);
0291:
0292:                assertEquals("Integer=IntegerArray.get(Integer);", t.toString());
0293:            }
0294:
0295:            /*
0296:             * Test the constructor(value, ...) with null method name.
0297:             */
0298:            public void testConstructor_Value_NullMethodName() throws Exception {
0299:                Object val = new Object();
0300:                Object target = new Object();
0301:                Object[] oa = new Object[] { new Object() };
0302:                Expression t = new Expression(val, target, null, oa);
0303:
0304:                assertSame(val, t.getValue());
0305:                assertSame(target, t.getTarget());
0306:                assertSame(null, t.getMethodName());
0307:                assertSame(oa, t.getArguments());
0308:
0309:                assertEquals("Object=Object.null(Object);", t.toString());
0310:            }
0311:
0312:            /*
0313:             * Test the constructor(value, ...) with the method name "new".
0314:             */
0315:            public void testConstructor_Value_NewMethodName() throws Exception {
0316:                Object val = new Object();
0317:                Object target = new Object();
0318:                Object[] oa = new Object[] { new Object() };
0319:                Expression t = new Expression(val, target, "new", oa);
0320:
0321:                assertSame(val, t.getValue());
0322:                assertSame(target, t.getTarget());
0323:                assertSame("new", t.getMethodName());
0324:                assertSame(oa, t.getArguments());
0325:
0326:                assertEquals("Object=Object.new(Object);", t.toString());
0327:            }
0328:
0329:            /*
0330:             * Test the constructor(value, ...) with empty method name.
0331:             */
0332:            public void testConstructor_Value_EmptyMethodName()
0333:                    throws Exception {
0334:                Object val = new Object();
0335:                Object target = new Object();
0336:                Object[] oa = new Object[] { new Object() };
0337:                Expression t = new Expression(val, target, "", oa);
0338:
0339:                assertSame(val, t.getValue());
0340:                assertSame(target, t.getTarget());
0341:                assertSame("", t.getMethodName());
0342:                assertSame(oa, t.getArguments());
0343:
0344:                assertEquals("Object=Object.(Object);", t.toString());
0345:            }
0346:
0347:            /*
0348:             * Test the constructor(value, ...) with null arguments.
0349:             */
0350:            public void testConstructor_Value_NullArguments() throws Exception {
0351:                Object val = new Object();
0352:                Object target = new Object();
0353:                Expression t = new Expression(val, target, "method", null);
0354:
0355:                assertSame(val, t.getValue());
0356:                assertSame(target, t.getTarget());
0357:                assertSame("method", t.getMethodName());
0358:                assertEquals(0, t.getArguments().length);
0359:
0360:                assertEquals("Object=Object.method();", t.toString());
0361:            }
0362:
0363:            /*
0364:             * Test the constructor(value, ...) with a null argument.
0365:             */
0366:            public void testConstructor_Value_NullArgument() throws Exception {
0367:                Object val = new Object();
0368:                Object target = new Object();
0369:                Object[] oa = new Object[] { null };
0370:                Expression t = new Expression(val, target, "method", oa);
0371:
0372:                assertSame(val, t.getValue());
0373:                assertSame(target, t.getTarget());
0374:                assertSame("method", t.getMethodName());
0375:                assertSame(oa, t.getArguments());
0376:                assertNull(t.getArguments()[0]);
0377:
0378:                assertEquals("Object=Object.method(null);", t.toString());
0379:            }
0380:
0381:            /*
0382:             * Test the constructor(value, ...) with a null value.
0383:             */
0384:            public void testConstructor_Value_NullValue() throws Exception {
0385:                Object target = new Object();
0386:                Object[] oa = new Object[] { null };
0387:                Expression t = new Expression(null, target, "method", oa);
0388:
0389:                assertSame(null, t.getValue());
0390:                assertSame(target, t.getTarget());
0391:                assertSame("method", t.getMethodName());
0392:                assertSame(oa, t.getArguments());
0393:                assertNull(t.getArguments()[0]);
0394:
0395:                assertEquals("null=Object.method(null);", t.toString());
0396:            }
0397:
0398:            /*
0399:             * Test the setValue() method with a non-null value when the value of the
0400:             * expression is still unbounded.
0401:             */
0402:            public void testSetValue_UnboundNormal() throws Exception {
0403:                MockObject mo = new MockObject(false);
0404:                Expression t = new Expression(mo, "method", new Object[0]);
0405:                t.setValue(mo);
0406:                assertSame(mo, t.getValue());
0407:                MockObject.assertNotCalled();
0408:            }
0409:
0410:            /*
0411:             * Test the setValue() method with a null value when the value of the
0412:             * expression is still unbounded.
0413:             */
0414:            public void testSetValue_UnboundNull() throws Exception {
0415:                MockObject mo = new MockObject(false);
0416:                Expression t = new Expression(mo, "method", new Object[0]);
0417:                t.setValue(null);
0418:                assertSame(null, t.getValue());
0419:                MockObject.assertNotCalled();
0420:            }
0421:
0422:            /*
0423:             * Test the setValue() method when the value of the expression is set by the
0424:             * constructor.
0425:             */
0426:            public void testSetValue_Constructor() throws Exception {
0427:                MockObject mo = new MockObject(false);
0428:                Expression t = new Expression(mo, mo, "method", new Object[0]);
0429:                assertSame(mo, t.getValue());
0430:                MockObject.assertNotCalled();
0431:                t.setValue(null);
0432:                assertSame(null, t.getValue());
0433:                MockObject.assertNotCalled();
0434:            }
0435:
0436:            /*
0437:             * Test the setValue() method when the value of the expression is set by a
0438:             * previous call to setValue().
0439:             */
0440:            public void testSetValue_Set() throws Exception {
0441:                MockObject mo = new MockObject(false);
0442:                Expression t = new Expression(mo, "method", new Object[0]);
0443:                t.setValue(mo);
0444:                assertSame(mo, t.getValue());
0445:                MockObject.assertNotCalled();
0446:                t.setValue(null);
0447:                assertSame(null, t.getValue());
0448:                MockObject.assertNotCalled();
0449:            }
0450:
0451:            /*
0452:             * Test the setValue() method when the value of the expression is set by a
0453:             * previous call to getValue().
0454:             */
0455:            public void testSetValue_Evaluated() throws Exception {
0456:                MockObject mo = new MockObject(false);
0457:                Expression t = new Expression(mo, "method", new Object[0]);
0458:                assertEquals("method1", t.getValue());
0459:                MockObject.assertCalled("method1", new Object[0]);
0460:                t.setValue(mo);
0461:                assertSame(mo, t.getValue());
0462:                MockObject.assertNotCalled();
0463:            }
0464:
0465:            /*
0466:             * Test the getValue() method when the value of the expression is evaluated
0467:             * by a previous call to getValue().
0468:             */
0469:            public void testGetValue_Evaluated() throws Exception {
0470:                MockObject mo = new MockObject(false);
0471:                Expression t = new Expression(mo, "method", new Object[0]);
0472:                assertEquals("method1", t.getValue());
0473:                MockObject.assertCalled("method1", new Object[0]);
0474:                assertEquals("method1", t.getValue());
0475:                MockObject.assertNotCalled();
0476:            }
0477:
0478:            /*
0479:             * Test the getValue() method when the value of expression is set by the
0480:             * constructor.
0481:             */
0482:            public void testGetValue_Constructor() throws Exception {
0483:                MockObject mo = new MockObject(false);
0484:                Expression t = new Expression(mo, mo, "method", new Object[0]);
0485:                assertSame(mo, t.getValue());
0486:                MockObject.assertNotCalled();
0487:            }
0488:
0489:            /*
0490:             * Test the getValue() method when the value of expression is set by a
0491:             * previous call to setValue().
0492:             */
0493:            public void testGetValue_Set() throws Exception {
0494:                MockObject mo = new MockObject(false);
0495:                Expression t = new Expression(mo, "method", new Object[0]);
0496:                t.setValue(mo);
0497:                assertSame(mo, t.getValue());
0498:                MockObject.assertNotCalled();
0499:            }
0500:
0501:            /*
0502:             * Test the method getValue() with a normal object, a valid method name and
0503:             * valid arguments.
0504:             */
0505:            public void testGetValue_UnboundedNormalInstanceMethod()
0506:                    throws Exception {
0507:                MockObject mo = new MockObject(false);
0508:                Expression t = new Expression(mo, "method", new Object[0]);
0509:                assertEquals("method1", t.getValue());
0510:                MockObject.assertCalled("method1", new Object[0]);
0511:                t = new Expression(mo, "method", null);
0512:                assertEquals("method1", t.getValue());
0513:                MockObject.assertCalled("method1", new Object[0]);
0514:            }
0515:
0516:            /*
0517:             * Test the method getValue() with a normal object, a valid method that
0518:             * throws an exception and valid arguments.
0519:             */
0520:            public void testGetValue_UnboundedExceptionalMethod()
0521:                    throws Exception {
0522:                MockObject mo = new MockObject(false);
0523:                Expression t = new Expression(mo, "method", new Object[] {
0524:                        null, null });
0525:                try {
0526:                    t.getValue();
0527:                    fail("Should throw NullPointerException!");
0528:                } catch (NullPointerException ex) {
0529:                    // expected
0530:                }
0531:                MockObject.assertCalled("method4", new Object[] { null, null });
0532:            }
0533:
0534:            /*
0535:             * Test the method getValue() with a normal object and a non-existing method
0536:             * name.
0537:             */
0538:            public void testGetValue_UnboundedNonExistingMethod()
0539:                    throws Exception {
0540:                MockObject mo = new MockObject(false);
0541:                Expression t = new Expression(mo, "method_not_existing",
0542:                        new Object[] { null, null });
0543:                try {
0544:                    t.getValue();
0545:                    fail("Should throw NoSuchMethodException!");
0546:                } catch (NoSuchMethodException ex) {
0547:                    // expected
0548:                }
0549:            }
0550:
0551:            /*
0552:             * Test the method getValue() with a null object.
0553:             */
0554:            public void testGetValue_UnboundedNullTarget() throws Exception {
0555:                Expression t = new Expression(null, "method_not_existing",
0556:                        new Object[] { null, null });
0557:                try {
0558:                    t.getValue();
0559:                    fail("Should throw NullPointerException!");
0560:                } catch (NullPointerException ex) {
0561:                    // expected
0562:                }
0563:            }
0564:
0565:            /*
0566:             * Test the method getValue() with a null method name.
0567:             */
0568:            public void testGetValue_UnboundedNullMethodName() throws Exception {
0569:                MockObject mo = new MockObject(false);
0570:                Expression t = new Expression(mo, null, new Object[] { null,
0571:                        null });
0572:                try {
0573:                    t.getValue();
0574:                    fail("Should throw NullPointerException!");
0575:                } catch (NullPointerException ex) {
0576:                    // expected
0577:                }
0578:            }
0579:
0580:            /*
0581:             * Test the method getValue() with a normal object, a valid method and
0582:             * invalid arguments (in terms of type, numbers, etc.).
0583:             */
0584:            public void testGetValue_UnboundedInvalidArguments()
0585:                    throws Exception {
0586:                MockObject mo = new MockObject(false);
0587:                Expression t = new Expression(mo, "method", new Object[] {
0588:                        new Object(), new Object(), new Object() });
0589:                try {
0590:                    t.getValue();
0591:                    fail("Should throw NoSuchMethodException!");
0592:                } catch (NoSuchMethodException ex) {
0593:                    // expected
0594:                }
0595:            }
0596:
0597:            /*
0598:             * Test the method getValue() with a normal object, an overloaded method and
0599:             * valid arguments.
0600:             */
0601:            public void testGetValue_UnboundedOverloadedMethods()
0602:                    throws Exception {
0603:                MockObject mo = new MockObject(false);
0604:                Object[] arguments = new Object[] { new Object() };
0605:                Expression t = new Expression(mo, "method", arguments);
0606:                assertEquals("method2", t.getValue());
0607:                MockObject.assertCalled("method2", arguments);
0608:
0609:                arguments = new Object[] { "test" };
0610:                t = new Expression(mo, "method", arguments);
0611:                assertEquals("method3", t.getValue());
0612:                MockObject.assertCalled("method3", arguments);
0613:            }
0614:
0615:            /*
0616:             * Test the method getValue() with a normal object, the method name "new"
0617:             * and valid arguments.
0618:             */
0619:            public void testGetValue_UnboundedNormalConstructor()
0620:                    throws Exception {
0621:                Expression t = new Expression(MockObject.class, "new",
0622:                        new Object[0]);
0623:                t.getValue();
0624:                MockObject.assertCalled("new0", new Object[0]);
0625:                t = new Expression(MockObject.class, "new", null);
0626:                assertTrue(t.getValue() instanceof  MockObject);
0627:                MockObject.assertCalled("new0", new Object[0]);
0628:            }
0629:
0630:            /*
0631:             * Test the method getValue() with a normal object, the method name "new"
0632:             * that throws an exception and valid arguments.
0633:             */
0634:            public void testGetValue_UnboundedExceptionalConstructor()
0635:                    throws Exception {
0636:                Expression t = new Expression(MockObject.class, "new",
0637:                        new Object[] { null, null });
0638:                try {
0639:                    t.getValue();
0640:                    fail("Should throw NullPointerException!");
0641:                } catch (NullPointerException ex) {
0642:                    // expected
0643:                }
0644:                MockObject.assertCalled("new4", new Object[] { null, null });
0645:            }
0646:
0647:            /*
0648:             * Test the method getValue() with a normal object, the method name "new"
0649:             * and invalid arguments (in terms of type, numbers, etc.).
0650:             */
0651:            public void testGetValue_UnboundedNonExistingConstructor()
0652:                    throws Exception {
0653:                Expression t = new Expression(MockObject.class, "new",
0654:                        new Object[] { null, null, null });
0655:                try {
0656:                    t.getValue();
0657:                    fail("Should throw NoSuchMethodException!");
0658:                } catch (NoSuchMethodException ex) {
0659:                    // expected
0660:                }
0661:            }
0662:
0663:            /*
0664:             * Test the method getValue() with a normal object with overloaded
0665:             * constructors, the method name "new" and valid arguments. See Java
0666:             * Language Specification (15.11) for reference.
0667:             * 
0668:             * Note: decided by definition position.
0669:             */
0670:            public void testGetValue_UnboundedOverloadedConstructors()
0671:                    throws Exception {
0672:                Object[] arguments = new Object[] { new Object() };
0673:                Expression t = new Expression(MockObject.class, "new",
0674:                        arguments);
0675:                t.getValue();
0676:                MockObject.assertCalled("new2", arguments);
0677:
0678:                //FIXME: the following 2 commented assert cannot pass neither in RI nor in Harmony (HARMONY-4392),
0679:                // waiting for dev-list approval to fix Harmony implementation following spec 
0680:                arguments = new Object[] { "test" };
0681:                t = new Expression(MockObject.class, "new", arguments);
0682:                assertTrue(t.getValue() instanceof  MockObject);
0683:                //         MockObject.assertCalled("new3", arguments);
0684:
0685:                arguments = new Object[] { new Integer(1) };
0686:                t = new Expression(MockObject.class, "new", arguments);
0687:                assertTrue(t.getValue() instanceof  MockObject);
0688:                //        MockObject.assertCalled("new1-2", arguments);
0689:            }
0690:
0691:            /*
0692:             * Test the method getValue() with the Class object, a static method name
0693:             * and valid arguments.
0694:             */
0695:            public void testGetValue_UnboundedNormalStaticMethodViaClass()
0696:                    throws Exception {
0697:                Object[] arguments = new Object[] { new Object() };
0698:                Expression t = new Expression(MockObject.class, "staticMethod",
0699:                        arguments);
0700:                assertEquals("staticMethod", t.getValue());
0701:                MockObject.assertCalled("staticMethod", arguments);
0702:            }
0703:
0704:            /*
0705:             * Test the method getValue() with an object, a static method name and valid
0706:             * arguments.
0707:             */
0708:            public void testGetValue_UnboundedNormalStaticMethodViaObject()
0709:                    throws Exception {
0710:                MockObject mo = new MockObject(false);
0711:                Object[] arguments = new Object[] { new Object() };
0712:                Expression t = new Expression(mo, "staticMethod", arguments);
0713:                assertEquals("staticMethod", t.getValue());
0714:                MockObject.assertCalled("staticMethod", arguments);
0715:            }
0716:
0717:            /*
0718:             * Test the method getValue() with a Class object of a normal class that has
0719:             * a method of the same signature as Class.forName(String), a static method
0720:             * name "forName" and valid argument "string".
0721:             */
0722:            public void testGetValue_UnboundedAmbitiousStaticMethod()
0723:                    throws Exception {
0724:                Object[] arguments = new Object[] { "test" };
0725:                Expression t = new Expression(MockObject.class, "forName",
0726:                        arguments);
0727:                assertNull(t.getValue());
0728:                MockObject.assertCalled("forName", arguments);
0729:
0730:                t = new Expression(String.class, "forName",
0731:                        new Object[] { "java.lang.String" });
0732:                assertSame(String.class, t.getValue());
0733:            }
0734:
0735:            /*
0736:             * Test the method getValue() with the special method Class.forName().
0737:             */
0738:            public void testGetValue_UnboundedClassForName() throws Exception {
0739:                Object[] arguments = new String[] { Expression.class.getName() };
0740:                Expression t = new Expression(Class.class, "forName", arguments);
0741:                assertSame(Expression.class, t.getValue());
0742:
0743:                // t = new Expression(String.class, "forName", arguments);
0744:                // assertSame(this.getClass(), t.getValue());
0745:            }
0746:
0747:            /*
0748:             * Test the method getValue() with a normal array object, the method name
0749:             * "get" and valid arguments.
0750:             */
0751:            public void testGetValue_UnboundedArrayGet() throws Exception {
0752:                Object[] array = new Object[] { "test" };
0753:                Expression t = new Expression(array, "get",
0754:                        new Object[] { new Integer(0) });
0755:                assertEquals("test", t.getValue());
0756:            }
0757:
0758:            /*
0759:             * Test the method getValue() with a normal array object, the method name
0760:             * "getInt" and invalid arguments.
0761:             */
0762:            public void testGetValue_UnboundedArrayInvalidSetInt()
0763:                    throws Exception {
0764:                int[] array = new int[] { 1 };
0765:                Expression t = new Expression(array, "getInt",
0766:                        new Object[] { new Integer(0) });
0767:                try {
0768:                    t.getValue();
0769:                    fail("Should throw NoSuchMethodException!");
0770:                } catch (NoSuchMethodException ex) {
0771:                    // expected
0772:                }
0773:            }
0774:
0775:            /*
0776:             * Test the method getValue() with a normal array object, the method name
0777:             * "gets".
0778:             */
0779:            public void testGetValue_UnboundedArrayInvalidName()
0780:                    throws Exception {
0781:                Object[] array = new Object[] { "test" };
0782:                Expression t = new Expression(array, "gets", new Object[] {
0783:                        new Integer(0), new Object() });
0784:                try {
0785:                    t.getValue();
0786:                    fail("Should throw NoSuchMethodException!");
0787:                } catch (NoSuchMethodException ex) {
0788:                    // expected
0789:                }
0790:            }
0791:
0792:            /*
0793:             * Test the method getValue() with a normal object with overloaded methods
0794:             * (primitive type VS wrapper class), a valid method name and valid
0795:             * arguments.
0796:             * 
0797:             * Note: decided by definition position!
0798:             */
0799:            public void testGetValue_UnboundedPrimitiveVSWrapper()
0800:                    throws Exception {
0801:                MockObject mo = new MockObject(false);
0802:                Object[] arguments = new Object[] { new Integer(1) };
0803:                Expression t = new Expression(mo, "methodB", arguments);
0804:                assertEquals("methodB2", t.getValue());
0805:                MockObject.assertCalled("methodB2", arguments);
0806:
0807:                arguments = new Object[] { Boolean.FALSE };
0808:                t = new Expression(mo, "methodB", arguments);
0809:                assertEquals("methodB3", t.getValue());
0810:                MockObject.assertCalled("methodB3", arguments);
0811:            }
0812:
0813:            /*
0814:             * Test the method getValue() with a normal object with void method name and
0815:             * valid arguments.
0816:             */
0817:            public void testGetValue_UnboundedVoidMethod() throws Exception {
0818:                MockObject mo = new MockObject(false);
0819:                Object[] arguments = new Object[] { new Integer(1) };
0820:                Expression t = new Expression(mo, "voidMethod", arguments);
0821:                assertNull(t.getValue());
0822:                MockObject.assertCalled("voidMethod2", arguments);
0823:            }
0824:
0825:            /*
0826:             * Test the method getValue() with a protected method but within java.beans
0827:             * package.
0828:             */
0829:            public void testGetValue_ProtectedMethodWithPackage()
0830:                    throws Exception {
0831:                DefaultPersistenceDelegate dpd = new DefaultPersistenceDelegate();
0832:                Object[] arguments = new Object[] { "test", "test" };
0833:                Expression t = new Expression(dpd, "mutatesTo", arguments);
0834:                try {
0835:                    t.getValue();
0836:                    fail("Should throw NoSuchMethodException!");
0837:                } catch (NoSuchMethodException e) {
0838:                    // expected
0839:                }
0840:            }
0841:
0842:            /*
0843:             * Test the method getValue() with a method that is applicable via type
0844:             * conversion.
0845:             */
0846:            public void testGetValue_ApplicableViaTypeConversion()
0847:                    throws Exception {
0848:                MockObject mo = new MockObject(false);
0849:                // mo.methodB('c');
0850:                Object[] arguments = new Object[] { new Character((char) 1) };
0851:                Expression t = new Expression(mo, "methodB", arguments);
0852:                try {
0853:                    t.getValue();
0854:                    fail("Should throw NoSuchMethodException!");
0855:                } catch (NoSuchMethodException e) {
0856:                    // expected
0857:                }
0858:            }
0859:
0860:            public void testGetValue_returnNull() throws Exception {
0861:                MockTarget target = new MockTarget();
0862:                Expression e = new Expression(target, "aMethod",
0863:                        new Object[] {});
0864:                Object got = e.getValue();
0865:                assertTrue(MockTarget.isCalled());
0866:                got = e.getValue();
0867:                assertFalse(MockTarget.isCalled());
0868:            }
0869:
0870:            /*
0871:             * Test the method getValue() with two equal specific methods.
0872:             * 
0873:             * Note: decided by definition position! should be ambiguous.
0874:             */
0875:            // public void testGetValue_EqualSpecificMethods() throws Exception {
0876:            // MockObject mo = new MockObject(false);
0877:            // Object[] arguments = new Object[] { new MockObject(false),
0878:            // new MockObject(false) };
0879:            // Expression t = new Expression(mo, "equalSpecificMethod", arguments);
0880:            // assertEquals("equalSpecificMethod1", t.getValue());
0881:            // MockObject.assertCalled("equalSpecificMethod1", arguments);
0882:            // }
0883:            /*
0884:             * Test the method getValue() with two equal specific methods but one
0885:             * declaring thrown exception.
0886:             * 
0887:             * Note: decided by definition position! should call the one with exception.
0888:             */
0889:            // public void testGetValue_EqualSpecificMethodsException() throws Exception
0890:            // {
0891:            // MockObject mo = new MockObject(false);
0892:            // Object[] arguments = new Object[] { new MockObject(false),
0893:            // new MockObject(false), new Object() };
0894:            // Expression t = new Expression(mo, "equalSpecificMethod", arguments);
0895:            // assertEquals("equalSpecificMethod3", t.getValue());
0896:            // MockObject.assertCalled("equalSpecificMethod3", arguments);
0897:            // }
0898:            /*
0899:             * Super class of MockObject.
0900:             */
0901:            public static class MockParent {
0902:
0903:                protected static String calledMethod = null;
0904:
0905:                protected static Vector<Object> receivedArguments = new Vector<Object>();
0906:
0907:                public Object method() {
0908:                    reset();
0909:                    calledMethod = "method1";
0910:                    return calledMethod;
0911:                }
0912:
0913:                protected Object method(Boolean o) {
0914:                    reset();
0915:                    calledMethod = "method1-1";
0916:                    receivedArguments.add(o);
0917:                    return calledMethod;
0918:                }
0919:
0920:                public Object method(Integer o) {
0921:                    reset();
0922:                    calledMethod = "method1-2";
0923:                    receivedArguments.add(o);
0924:                    return calledMethod;
0925:                }
0926:
0927:                public Object method(Object o) {
0928:                    reset();
0929:                    calledMethod = "method2";
0930:                    receivedArguments.add(o);
0931:                    return calledMethod;
0932:                }
0933:
0934:                public Object method(String o) {
0935:                    reset();
0936:                    calledMethod = "method3";
0937:                    receivedArguments.add(o);
0938:                    return calledMethod;
0939:                }
0940:
0941:                public Object method(Object o, Object o2) {
0942:                    reset();
0943:                    calledMethod = "method4";
0944:                    receivedArguments.add(o);
0945:                    receivedArguments.add(o2);
0946:                    throw new NullPointerException();
0947:                }
0948:
0949:                public Object method(Object o, Object o2, Object o3, Object o4) {
0950:                    reset();
0951:                    calledMethod = "method5";
0952:                    receivedArguments.add(o);
0953:                    receivedArguments.add(o2);
0954:                    receivedArguments.add(o3);
0955:                    receivedArguments.add(o4);
0956:                    return calledMethod;
0957:                }
0958:
0959:                public static void reset() {
0960:                    receivedArguments.clear();
0961:                    calledMethod = null;
0962:                }
0963:            }
0964:
0965:            public static class MockTarget {
0966:                static int called = 0;
0967:
0968:                static int base = 0;
0969:
0970:                public Object aMethod() { // should return null on first call
0971:                    called++;
0972:                    return null;
0973:                }
0974:
0975:                public static boolean isCalled() {
0976:                    boolean result = !(base == called);
0977:                    base = called;
0978:                    return result;
0979:                }
0980:
0981:            }
0982:
0983:            /*
0984:             * Mock object.
0985:             */
0986:            public static class MockObject extends MockParent {
0987:
0988:                public MockObject() {
0989:                    reset();
0990:                    calledMethod = "new0";
0991:                }
0992:
0993:                public MockObject(boolean testingConstructor) {
0994:                    reset();
0995:                    if (testingConstructor) {
0996:                        calledMethod = "new1";
0997:                    }
0998:                }
0999:
1000:                public MockObject(String o) {
1001:                    reset();
1002:                    calledMethod = "new3";
1003:                    receivedArguments.add(o);
1004:                }
1005:
1006:                public MockObject(Object o) {
1007:                    reset();
1008:                    calledMethod = "new2";
1009:                    receivedArguments.add(o);
1010:                }
1011:
1012:                public MockObject(Integer o) {
1013:                    reset();
1014:                    calledMethod = "new1-2";
1015:                    receivedArguments.add(o);
1016:                }
1017:
1018:                public MockObject(Object o, Object o2) {
1019:                    reset();
1020:                    calledMethod = "new4";
1021:                    receivedArguments.add(o);
1022:                    receivedArguments.add(o2);
1023:                    throw new NullPointerException();
1024:                }
1025:
1026:                // public Object methodB(Integer i) {
1027:                // reset();
1028:                // calledMethod = "methodB1";
1029:                // receivedArguments.add(i);
1030:                // return calledMethod;
1031:                // }
1032:
1033:                public Object methodB(int i) {
1034:                    reset();
1035:                    calledMethod = "methodB2";
1036:                    receivedArguments.add(new Integer(i));
1037:                    return calledMethod;
1038:                }
1039:
1040:                public Object methodB(boolean b) {
1041:                    reset();
1042:                    calledMethod = "methodB3";
1043:                    receivedArguments.add(new Boolean(b));
1044:                    return calledMethod;
1045:                }
1046:
1047:                // public Object methodB(Boolean b) {
1048:                // reset();
1049:                // calledMethod = "methodB4";
1050:                // receivedArguments.add(b);
1051:                // return calledMethod;
1052:                // }
1053:
1054:                public Object voidMethod(Object o) {
1055:                    reset();
1056:                    calledMethod = "voidMethod";
1057:                    receivedArguments.add(o);
1058:                    return "voidMethod";
1059:                }
1060:
1061:                public void voidMethod(Integer o) {
1062:                    reset();
1063:                    calledMethod = "voidMethod2";
1064:                    receivedArguments.add(o);
1065:                }
1066:
1067:                public static Object staticMethod(Object o) {
1068:                    reset();
1069:                    calledMethod = "staticMethod";
1070:                    receivedArguments.add(o);
1071:                    return calledMethod;
1072:                }
1073:
1074:                // public Object equalSpecificMethod(MockObject o, MockParent p) {
1075:                // reset();
1076:                // calledMethod = "equalSpecificMethod1";
1077:                // receivedArguments.add(o);
1078:                // receivedArguments.add(p);
1079:                // return calledMethod;
1080:                // }
1081:
1082:                // public Object equalSpecificMethod(MockParent p, MockObject o) {
1083:                // reset();
1084:                // calledMethod = "equalSpecificMethod2";
1085:                // receivedArguments.add(p);
1086:                // receivedArguments.add(o);
1087:                // return calledMethod;
1088:                // }
1089:
1090:                // public Object equalSpecificMethod(MockObject o, MockParent p, Object
1091:                // o2) {
1092:                // reset();
1093:                // calledMethod = "equalSpecificMethod3";
1094:                // receivedArguments.add(o);
1095:                // receivedArguments.add(p);
1096:                // receivedArguments.add(o2);
1097:                // return calledMethod;
1098:                // }
1099:
1100:                // public Object equalSpecificMethod(MockParent p, MockObject o, Object
1101:                // o2)
1102:                // throws Exception {
1103:                // reset();
1104:                // calledMethod = "equalSpecificMethod4";
1105:                // receivedArguments.add(p);
1106:                // receivedArguments.add(o);
1107:                // receivedArguments.add(o2);
1108:                // return calledMethod;
1109:                // }
1110:
1111:                public static Class<?> forName(String o) {
1112:                    reset();
1113:                    calledMethod = "forName";
1114:                    receivedArguments.add(o);
1115:                    return null;
1116:                }
1117:
1118:                public static void assertCalled(String methodName,
1119:                        Object[] arguments) {
1120:                    assertEquals(methodName, calledMethod);
1121:                    assertTrue(Arrays.equals(arguments, receivedArguments
1122:                            .toArray()));
1123:                    reset();
1124:                }
1125:
1126:                public static void assertNotCalled() {
1127:                    assertNull(calledMethod);
1128:                    assertTrue(receivedArguments.isEmpty());
1129:                }
1130:            }
1131:
1132:            public void testSubExpression() throws Exception {
1133:                MyExpression my_e = new MyExpression();
1134:                my_e.setTarget(new Target());
1135:                my_e.setArguments(new Object[] {});
1136:                my_e.setMethodName("aMethod");
1137:                my_e.execute();
1138:                assertEquals("haha", my_e.getValue());
1139:            }
1140:
1141:            private static class MyExpression extends java.beans.Expression {
1142:
1143:                private Object target = null;
1144:
1145:                private Object args[] = new Object[] { new Object() };
1146:
1147:                private String name = "";
1148:
1149:                public MyExpression() {
1150:                    super (null, null, null);
1151:                }
1152:
1153:                public void setTarget(Object t) {
1154:                    target = t;
1155:                }
1156:
1157:                public Object getTarget() {
1158:                    return target;
1159:                }
1160:
1161:                public void setArguments(Object[] a) {
1162:                    args = a;
1163:                }
1164:
1165:                public Object[] getArguments() {
1166:                    return args;
1167:                }
1168:
1169:                public void setMethodName(String n) {
1170:                    name = n;
1171:                }
1172:
1173:                public String getMethodName() {
1174:                    return name;
1175:                }
1176:
1177:                public void setValue(Object value) {
1178:                    super .setValue(value);
1179:                }
1180:
1181:                public Object getValue() {
1182:                    return "haha";
1183:                }
1184:
1185:            }
1186:
1187:            public static class Target {
1188:
1189:                public Object aMethod() {
1190:                    return "haha";
1191:                }
1192:            }
1193:        }
w__w_w___._ja___va__2___s___.c_o_m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.