KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > enhance > TestEnhancementOperation


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.enhance;
16
17 import java.lang.reflect.Modifier JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.hivemind.ClassResolver;
25 import org.apache.hivemind.Location;
26 import org.apache.hivemind.impl.DefaultClassResolver;
27 import org.apache.hivemind.service.BodyBuilder;
28 import org.apache.hivemind.service.ClassFab;
29 import org.apache.hivemind.service.ClassFactory;
30 import org.apache.hivemind.service.MethodSignature;
31 import org.apache.hivemind.service.impl.ClassFactoryImpl;
32 import org.apache.hivemind.test.HiveMindTestCase;
33 import org.apache.tapestry.AbstractComponent;
34 import org.apache.tapestry.BaseComponent;
35 import org.apache.tapestry.IComponent;
36 import org.apache.tapestry.IPage;
37 import org.apache.tapestry.components.Insert;
38 import org.apache.tapestry.event.PageDetachListener;
39 import org.apache.tapestry.event.PageValidateListener;
40 import org.apache.tapestry.link.ServiceLink;
41 import org.apache.tapestry.services.ComponentConstructor;
42 import org.apache.tapestry.spec.IComponentSpecification;
43 import org.easymock.MockControl;
44 import org.easymock.internal.ArrayMatcher;
45
46 /**
47  * Tests for {@link org.apache.tapestry.enhance.EnhancementOperationImpl}.
48  *
49  * @author Howard M. Lewis Ship
50  * @since 4.0
51  */

52 public class TestEnhancementOperation extends HiveMindTestCase
53 {
54     protected void setUp() throws Exception JavaDoc
55     {
56         super.setUp();
57
58         EnhancementOperationImpl._uid = 97;
59     }
60
61     public abstract static class Fixture
62     {
63         public abstract String JavaDoc getStringProperty();
64
65         public abstract boolean isBooleanProperty();
66
67         public abstract boolean getFlagProperty();
68     }
69
70     public abstract static class ValidatingComponent extends AbstractComponent implements
71             PageValidateListener
72     {
73     }
74
75     public abstract static class GetClassReferenceFixture
76     {
77         public abstract Class JavaDoc getClassReference();
78     }
79
80     public static class MissingConstructorFixture
81     {
82         public MissingConstructorFixture(String JavaDoc foo)
83         {
84             //
85
}
86     }
87
88     public abstract static class UnclaimedAbstractPropertiesFixture
89     {
90         public abstract String JavaDoc getReadOnly();
91
92         public abstract void setWriteOnly(String JavaDoc value);
93
94         public void setConcrete(int i)
95         {
96             //
97
}
98
99         public int getConcrete()
100         {
101             return -1;
102         }
103     }
104
105     public void testClaimedProperty()
106     {
107         EnhancementOperationImpl eo = new EnhancementOperationImpl();
108
109         eo.claimProperty("foo");
110         eo.claimProperty("bar");
111
112         try
113         {
114             eo.claimProperty("foo");
115             unreachable();
116         }
117         catch (ApplicationRuntimeException ex)
118         {
119             assertEquals(EnhanceMessages.claimedProperty("foo"), ex.getMessage());
120         }
121     }
122
123     private ClassFactory newClassFactory()
124     {
125         return newClassFactory(BaseComponent.class);
126     }
127
128     private ClassFactory newClassFactory(Class JavaDoc baseClass)
129     {
130         return newClassFactory(baseClass, newClassFab());
131     }
132
133     private ClassFactory newClassFactory(Class JavaDoc baseClass, ClassFab classFab)
134     {
135         MockControl control = newControl(ClassFactory.class);
136         ClassFactory factory = (ClassFactory) control.getMock();
137
138         String JavaDoc className = baseClass.getName();
139         int dotx = className.lastIndexOf('.');
140         String JavaDoc baseName = className.substring(dotx + 1);
141
142         factory.newClass("$" + baseName + "_97", baseClass);
143         control.setReturnValue(classFab);
144
145         return factory;
146     }
147
148     private ClassFab newClassFab()
149     {
150         return (ClassFab) newMock(ClassFab.class);
151     }
152
153     private IComponentSpecification newSpec()
154     {
155         return (IComponentSpecification) newMock(IComponentSpecification.class);
156     }
157
158     public void testConstructorAndAccessors()
159     {
160         IComponentSpecification spec = newSpec();
161         ClassFactory cf = newClassFactory();
162
163         replayControls();
164
165         EnhancementOperation eo = new EnhancementOperationImpl(new DefaultClassResolver(), spec,
166                 BaseComponent.class, cf);
167
168         assertSame(BaseComponent.class, eo.getBaseClass());
169
170         verifyControls();
171     }
172
173     public void testCheckImplementsNoInterface()
174     {
175         IComponentSpecification spec = newSpec();
176         ClassFactory cf = newClassFactory();
177
178         replayControls();
179
180         EnhancementOperation eo = new EnhancementOperationImpl(new DefaultClassResolver(), spec,
181                 BaseComponent.class, cf);
182
183         assertEquals(false, eo.implementsInterface(PageValidateListener.class));
184
185         verifyControls();
186     }
187
188     public void testCheckImplementsClassImplements()
189     {
190         IComponentSpecification spec = newSpec();
191         ClassFactory cf = newClassFactory(ValidatingComponent.class);
192
193         replayControls();
194
195         EnhancementOperation eo = new EnhancementOperationImpl(new DefaultClassResolver(), spec,
196                 ValidatingComponent.class, cf);
197
198         assertEquals(true, eo.implementsInterface(PageValidateListener.class));
199
200         verifyControls();
201     }
202
203     public void testCheckImplementsNoMatchForAddedInterfaces()
204     {
205         IComponentSpecification spec = newSpec();
206
207         MockControl factoryc = newControl(ClassFactory.class);
208         ClassFactory factory = (ClassFactory) factoryc.getMock();
209
210         MockControl classfabc = newControl(ClassFab.class);
211         ClassFab classfab = (ClassFab) classfabc.getMock();
212
213         factory.newClass("$BaseComponent_97", BaseComponent.class);
214         factoryc.setReturnValue(classfab);
215
216         classfab.addInterface(PageDetachListener.class);
217
218         replayControls();
219
220         EnhancementOperation eo = new EnhancementOperationImpl(new DefaultClassResolver(), spec,
221                 BaseComponent.class, factory);
222
223         eo.extendMethodImplementation(
224                 PageDetachListener.class,
225                 EnhanceUtils.PAGE_DETACHED_SIGNATURE,
226                 "foo();");
227
228         assertEquals(false, eo.implementsInterface(PageValidateListener.class));
229
230         verifyControls();
231     }
232
233     public void testCheckImplementsMatchAddedInterfaces()
234     {
235         IComponentSpecification spec = newSpec();
236
237         MockControl factoryc = newControl(ClassFactory.class);
238         ClassFactory factory = (ClassFactory) factoryc.getMock();
239
240         MockControl classfabc = newControl(ClassFab.class);
241         ClassFab classfab = (ClassFab) classfabc.getMock();
242
243         factory.newClass("$BaseComponent_97", BaseComponent.class);
244         factoryc.setReturnValue(classfab);
245
246         classfab.addInterface(PageDetachListener.class);
247
248         replayControls();
249
250         EnhancementOperation eo = new EnhancementOperationImpl(new DefaultClassResolver(), spec,
251                 BaseComponent.class, factory);
252
253         eo.extendMethodImplementation(
254                 PageDetachListener.class,
255                 EnhanceUtils.PAGE_DETACHED_SIGNATURE,
256                 "foo();");
257
258         assertEquals(true, eo.implementsInterface(PageDetachListener.class));
259
260         verifyControls();
261     }
262
263     public void testValidatePropertyNew()
264     {
265         IComponentSpecification spec = newSpec();
266         ClassFactory cf = newClassFactory();
267
268         replayControls();
269
270         EnhancementOperation eo = new EnhancementOperationImpl(new DefaultClassResolver(), spec,
271                 BaseComponent.class, cf);
272
273         // Validates because BaseComponent doesn't have such a property.
274

275         eo.validateProperty("abraxis", Set JavaDoc.class);
276
277         verifyControls();
278     }
279
280     public void testValidatePropertyMatches()
281     {
282         IComponentSpecification spec = newSpec();
283         ClassFactory cf = newClassFactory();
284
285         replayControls();
286
287         EnhancementOperation eo = new EnhancementOperationImpl(new DefaultClassResolver(), spec,
288                 BaseComponent.class, cf);
289
290         // Validates because BaseComponent inherits a page property of type IPage
291

292         eo.validateProperty("page", IPage.class);
293
294         verifyControls();
295     }
296
297     public void testValidatePropertyMismatch()
298     {
299         IComponentSpecification spec = newSpec();
300         ClassFactory cf = newClassFactory();
301
302         replayControls();
303
304         EnhancementOperation eo = new EnhancementOperationImpl(new DefaultClassResolver(), spec,
305                 BaseComponent.class, cf);
306
307         // Validates because BaseComponent inherits a page property of type IPage
308

309         try
310         {
311             eo.validateProperty("page", String JavaDoc.class);
312             unreachable();
313         }
314         catch (ApplicationRuntimeException ex)
315         {
316             assertEquals(EnhanceMessages.propertyTypeMismatch(
317                     BaseComponent.class,
318                     "page",
319                     IPage.class,
320                     String JavaDoc.class), ex.getMessage());
321         }
322
323         verifyControls();
324     }
325
326     public void testConvertTypeName()
327     {
328         IComponentSpecification spec = newSpec();
329         ClassFactory cf = newClassFactory();
330
331         replayControls();
332
333         EnhancementOperation eo = new EnhancementOperationImpl(new DefaultClassResolver(), spec,
334                 BaseComponent.class, cf);
335
336         assertSame(boolean.class, eo.convertTypeName("boolean"));
337         assertSame(float[].class, eo.convertTypeName("float[]"));
338         assertSame(double[][].class, eo.convertTypeName("double[][]"));
339
340         assertSame(Set JavaDoc.class, eo.convertTypeName(Set JavaDoc.class.getName()));
341
342         assertSame(List JavaDoc[].class, eo.convertTypeName(List JavaDoc.class.getName() + "[]"));
343
344         try
345         {
346             eo.convertTypeName("package.DoesNotExist");
347             unreachable();
348         }
349         catch (ApplicationRuntimeException ex)
350         {
351             // Ignore message, it's from HiveMind anyway.
352
}
353
354         verifyControls();
355
356     }
357
358     public void testAddField()
359     {
360         IComponentSpecification spec = newSpec();
361
362         MockControl cfc = newControl(ClassFactory.class);
363         ClassFactory cf = (ClassFactory) cfc.getMock();
364
365         ClassFab fab = (ClassFab) newMock(ClassFab.class);
366
367         cf.newClass("$BaseComponent_97", BaseComponent.class);
368
369         cfc.setReturnValue(fab);
370
371         fab.addField("fred", String JavaDoc.class);
372
373         replayControls();
374
375         EnhancementOperation eo = new EnhancementOperationImpl(new DefaultClassResolver(), spec,
376                 BaseComponent.class, cf);
377
378         eo.addField("fred", String JavaDoc.class);
379
380         verifyControls();
381     }
382
383     public void testAddInjectedField()
384     {
385         IComponentSpecification spec = newSpec();
386
387         MockControl cfc = newControl(ClassFactory.class);
388         ClassFactory cf = (ClassFactory) cfc.getMock();
389
390         MockControl fabc = newControl(ClassFab.class);
391         ClassFab fab = (ClassFab) fabc.getMock();
392
393         cf.newClass("$BaseComponent_97", BaseComponent.class);
394
395         cfc.setReturnValue(fab);
396
397         // String because "FRED_VALUE" is a String
398

399         fab.addField("fred", String JavaDoc.class);
400
401         replayControls();
402
403         EnhancementOperationImpl eo = new EnhancementOperationImpl(new DefaultClassResolver(),
404                 spec, BaseComponent.class, cf);
405
406         assertEquals("fred", eo.addInjectedField("fred", String JavaDoc.class, "FRED_VALUE"));
407
408         verifyControls();
409
410         HashMap JavaDoc map = new HashMap JavaDoc();
411
412         fab.addField("fred$0", Map JavaDoc.class);
413
414         replayControls();
415
416         assertEquals("fred$0", eo.addInjectedField("fred", Map JavaDoc.class, map));
417
418         verifyControls();
419
420         BodyBuilder body = new BodyBuilder();
421         body.begin();
422         body.addln("fred = $1;");
423         body.addln("fred$0 = $2;");
424         body.end();
425
426         fab.addConstructor(new Class JavaDoc[]
427         { String JavaDoc.class, Map JavaDoc.class }, null, body.toString());
428         fabc.setMatcher(new ArrayMatcher());
429
430         replayControls();
431
432         eo.finalizeEnhancedClass();
433
434         verifyControls();
435     }
436
437     public void testAddMethod()
438     {
439         Class JavaDoc baseClass = Insert.class;
440         MethodSignature sig = new MethodSignature(void.class, "frob", null, null);
441
442         IComponentSpecification spec = newSpec();
443
444         MockControl cfc = newControl(ClassFactory.class);
445         ClassFactory cf = (ClassFactory) cfc.getMock();
446
447         MockControl fabc = newControl(ClassFab.class);
448         ClassFab fab = (ClassFab) fabc.getMock();
449
450         // We force the uid to 97 in setUp()
451

452         cf.newClass("$Insert_97", baseClass);
453
454         cfc.setReturnValue(fab);
455
456         fab.addMethod(Modifier.PUBLIC, sig, "method body");
457         fabc.setReturnValue(null);
458
459         replayControls();
460
461         EnhancementOperation eo = new EnhancementOperationImpl(new DefaultClassResolver(), spec,
462                 baseClass, cf);
463
464         eo.addMethod(Modifier.PUBLIC, sig, "method body");
465
466         verifyControls();
467     }
468
469     public void testGetAccessorMethodName()
470     {
471         IComponentSpecification spec = newSpec();
472         ClassFactory cf = newClassFactory(Fixture.class);
473
474         replayControls();
475
476         EnhancementOperation eo = new EnhancementOperationImpl(new DefaultClassResolver(), spec,
477                 Fixture.class, cf);
478
479         assertEquals("getStringProperty", eo.getAccessorMethodName("stringProperty"));
480         assertEquals("isBooleanProperty", eo.getAccessorMethodName("booleanProperty"));
481         assertEquals("getFlagProperty", eo.getAccessorMethodName("flagProperty"));
482         assertEquals("getUnknownProperty", eo.getAccessorMethodName("unknownProperty"));
483
484         verifyControls();
485     }
486
487     /**
488      * On this test, instead of mocking up everything, we actually use the raw implementations to
489      * construct a new class; the class gets a class reference passed to it in its constructor.
490      */

491
492     public void testGetClassReference() throws Exception JavaDoc
493     {
494         Location l = newLocation();
495         MockControl specControl = newControl(IComponentSpecification.class);
496
497         IComponentSpecification spec = (IComponentSpecification) specControl.getMock();
498
499         spec.getLocation();
500         specControl.setReturnValue(l);
501
502         replayControls();
503
504         EnhancementOperationImpl eo = new EnhancementOperationImpl(new DefaultClassResolver(),
505                 spec, GetClassReferenceFixture.class, new ClassFactoryImpl());
506
507         // This does two things; it creates a new field, and it sets up a new constructor
508
// parameter to inject the class value (Map.class) into each new instance.
509

510         String JavaDoc ref = eo.getClassReference(Map JavaDoc.class);
511         String JavaDoc ref2 = eo.getClassReference(Map JavaDoc.class);
512
513         eo.addMethod(Modifier.PUBLIC, new MethodSignature(Class JavaDoc.class, "getClassReference", null,
514                 null), "return " + ref + ";");
515
516         ComponentConstructor cc = eo.getConstructor();
517
518         GetClassReferenceFixture f = (GetClassReferenceFixture) cc.newInstance();
519
520         assertSame(Map JavaDoc.class, f.getClassReference());
521
522         verifyControls();
523
524         assertSame(ref, ref2);
525     }
526
527     public void testGetArrayClassReference() throws Exception JavaDoc
528     {
529         MockControl specControl = newControl(IComponentSpecification.class);
530
531         IComponentSpecification spec = (IComponentSpecification) specControl.getMock();
532
533         replayControls();
534
535         EnhancementOperationImpl eo = new EnhancementOperationImpl(new DefaultClassResolver(),
536                 spec, GetClassReferenceFixture.class, new ClassFactoryImpl());
537
538         String JavaDoc ref = eo.getClassReference(int[].class);
539
540         assertTrue(ref.indexOf('[') < 0);
541
542         verifyControls();
543     }
544
545     /**
546      * Really a test for {@link org.apache.tapestry.enhance.ComponentConstructorImpl};
547      * {@link #testGetClassReference()}tests the success case, just want to fill in the failure.
548      */

549
550     public void testComponentConstructorFailure()
551     {
552         Location l = newLocation();
553
554         ComponentConstructor cc = new ComponentConstructorImpl(BaseComponent.class
555                 .getConstructors()[0], new Object JavaDoc[]
556         { "unexpected" }, "<classfab>", l);
557
558         try
559         {
560             cc.newInstance();
561             unreachable();
562         }
563         catch (ApplicationRuntimeException ex)
564         {
565             assertExceptionSubstring(
566                     ex,
567                     "Unable to instantiate instance of class org.apache.tapestry.BaseComponent");
568             assertSame(l, ex.getLocation());
569         }
570     }
571
572     public void testGetPropertyType()
573     {
574         IComponentSpecification spec = newSpec();
575         ClassFactory cf = newClassFactory();
576
577         replayControls();
578
579         EnhancementOperation eo = new EnhancementOperationImpl(new DefaultClassResolver(), spec,
580                 BaseComponent.class, cf);
581
582         assertEquals(Map JavaDoc.class, eo.getPropertyType("assets"));
583         assertEquals(IPage.class, eo.getPropertyType("page"));
584
585         // Doesn't exist, so returns null
586

587         assertNull(eo.getPropertyType("foosball"));
588
589         verifyControls();
590     }
591
592     public void testFindUnclaimedAbstractProperties()
593     {
594         ClassResolver cr = (ClassResolver) newMock(ClassResolver.class);
595         IComponentSpecification spec = newSpec();
596         ClassFactory cf = newClassFactory(UnclaimedAbstractPropertiesFixture.class);
597
598         replayControls();
599
600         EnhancementOperation eo = new EnhancementOperationImpl(cr, spec,
601                 UnclaimedAbstractPropertiesFixture.class, cf);
602
603         List JavaDoc l = eo.findUnclaimedAbstractProperties();
604
605         assertEquals(2, l.size());
606         assertEquals(true, l.contains("readOnly"));
607         assertEquals(true, l.contains("writeOnly"));
608
609         eo.claimProperty("readOnly");
610
611         l = eo.findUnclaimedAbstractProperties();
612
613         assertEquals(1, l.size());
614         assertEquals(true, l.contains("writeOnly"));
615
616         eo.claimProperty("writeOnly");
617
618         l = eo.findUnclaimedAbstractProperties();
619
620         assertEquals(true, l.isEmpty());
621
622         verifyControls();
623     }
624
625     public void testGetNewMethod()
626     {
627         ClassResolver cr = new DefaultClassResolver();
628         MockControl specc = newControl(IComponentSpecification.class);
629         IComponentSpecification spec = (IComponentSpecification) specc.getMock();
630
631         MockControl cfc = newControl(ClassFactory.class);
632         ClassFactory cf = (ClassFactory) cfc.getMock();
633         MockControl fabc = newControl(ClassFab.class);
634         ClassFab fab = (ClassFab) fabc.getMock();
635
636         fab.addInterface(PageDetachListener.class);
637
638         cf.newClass("$BaseComponent_97", BaseComponent.class);
639         cfc.setReturnValue(fab);
640
641         replayControls();
642
643         EnhancementOperationImpl eo = new EnhancementOperationImpl(cr, spec, BaseComponent.class,
644                 cf);
645
646         MethodSignature sig = EnhanceUtils.PAGE_DETACHED_SIGNATURE;
647
648         eo.extendMethodImplementation(PageDetachListener.class, sig, "some-code();");
649
650         verifyControls();
651
652         replayControls();
653
654         // Check that repeated calls do not
655
// keep adding methods.
656

657         eo.extendMethodImplementation(PageDetachListener.class, sig, "more-code();");
658
659         verifyControls();
660
661         fab.addMethod(Modifier.PUBLIC, sig, "{\n some-code();\n more-code();\n}\n");
662         fabc.setReturnValue(null);
663
664         fab.createClass();
665         fabc.setReturnValue(BaseComponent.class);
666
667         spec.getLocation();
668         specc.setReturnValue(null);
669
670         replayControls();
671
672         eo.getConstructor();
673
674         verifyControls();
675     }
676
677     public void testGetExistingMethod()
678     {
679         ClassResolver cr = new DefaultClassResolver();
680         MockControl specc = newControl(IComponentSpecification.class);
681         IComponentSpecification spec = (IComponentSpecification) specc.getMock();
682
683         MockControl cfc = newControl(ClassFactory.class);
684         ClassFactory cf = (ClassFactory) cfc.getMock();
685         MockControl fabc = newControl(ClassFab.class);
686         ClassFab fab = (ClassFab) fabc.getMock();
687
688         cf.newClass("$BaseComponent_97", BaseComponent.class);
689         cfc.setReturnValue(fab);
690
691         replayControls();
692
693         EnhancementOperationImpl eo = new EnhancementOperationImpl(cr, spec, BaseComponent.class,
694                 cf);
695
696         MethodSignature sig = EnhanceUtils.FINISH_LOAD_SIGNATURE;
697
698         eo.extendMethodImplementation(IComponent.class, sig, "some-code();");
699
700         verifyControls();
701
702         fab.addMethod(Modifier.PUBLIC, sig, "{\n super.finishLoad($$);\n some-code();\n}\n");
703         fabc.setReturnValue(null);
704
705         fab.createClass();
706         fabc.setReturnValue(BaseComponent.class);
707
708         spec.getLocation();
709         specc.setReturnValue(null);
710
711         replayControls();
712
713         eo.getConstructor();
714
715         verifyControls();
716     }
717
718     public void testGetExistingProtectedMethod()
719     {
720         ClassResolver cr = new DefaultClassResolver();
721         MockControl specc = newControl(IComponentSpecification.class);
722         IComponentSpecification spec = (IComponentSpecification) specc.getMock();
723
724         MockControl cfc = newControl(ClassFactory.class);
725         ClassFactory cf = (ClassFactory) cfc.getMock();
726         MockControl fabc = newControl(ClassFab.class);
727         ClassFab fab = (ClassFab) fabc.getMock();
728
729         cf.newClass("$BaseComponent_97", BaseComponent.class);
730         cfc.setReturnValue(fab);
731
732         replayControls();
733
734         EnhancementOperationImpl eo = new EnhancementOperationImpl(cr, spec, BaseComponent.class,
735                 cf);
736
737         // A protected method
738
MethodSignature sig = EnhanceUtils.CLEANUP_AFTER_RENDER_SIGNATURE;
739
740         eo.extendMethodImplementation(IComponent.class, sig, "some-code();");
741
742         verifyControls();
743
744         fab.addMethod(
745                 Modifier.PUBLIC,
746                 sig,
747                 "{\n super.cleanupAfterRender($$);\n some-code();\n}\n");
748         fabc.setReturnValue(null);
749
750         fab.createClass();
751         fabc.setReturnValue(BaseComponent.class);
752
753         spec.getLocation();
754         specc.setReturnValue(null);
755
756         replayControls();
757
758         eo.getConstructor();
759
760         verifyControls();
761     }
762
763     public static abstract class ExistingAbstractMethodFixture extends BaseComponent implements
764             PageDetachListener
765     {
766         //
767
}
768
769     public void getExistingAbstractMethod()
770     {
771         ClassResolver cr = new DefaultClassResolver();
772         MockControl specc = newControl(IComponentSpecification.class);
773         IComponentSpecification spec = (IComponentSpecification) specc.getMock();
774
775         MockControl cfc = newControl(ClassFactory.class);
776         ClassFactory cf = (ClassFactory) cfc.getMock();
777         MockControl fabc = newControl(ClassFab.class);
778         ClassFab fab = (ClassFab) fabc.getMock();
779
780         cf.newClass("$ExitingAbstractMethodFixture_97", ExistingAbstractMethodFixture.class);
781         cfc.setReturnValue(fab);
782
783         replayControls();
784
785         EnhancementOperationImpl eo = new EnhancementOperationImpl(cr, spec,
786                 ExistingAbstractMethodFixture.class, cf);
787
788         MethodSignature sig = EnhanceUtils.PAGE_DETACHED_SIGNATURE;
789
790         eo.extendMethodImplementation(PageDetachListener.class, sig, "some-code();");
791
792         verifyControls();
793
794         fab.addMethod(Modifier.PUBLIC, sig, "{\n some-code();\n}\n");
795         fabc.setReturnValue(null);
796
797         fab.createClass();
798         fabc.setReturnValue(BaseComponent.class);
799
800         spec.getLocation();
801         specc.setReturnValue(null);
802
803         replayControls();
804
805         eo.getConstructor();
806
807         verifyControls();
808     }
809
810     /**
811      * This seems to pass on the command line, but fail inside Eclipse. I think Eclipse's Java
812      * compiler works a little different from Java's ... in this example (TODO: Create test fixture
813      * classes for this test) the getTarget() method doesn't show up as a declared public method
814      * when Eclipse compiles the code, but does when JDK compiles the code.
815      */

816     public void testPropertyInheritedFromInterface()
817     {
818         IComponentSpecification spec = newSpec();
819         ClassFactory cf = newClassFactory(ServiceLink.class);
820
821         replayControls();
822
823         EnhancementOperation eo = new EnhancementOperationImpl(new DefaultClassResolver(), spec,
824                 ServiceLink.class, cf);
825
826         assertEquals(String JavaDoc.class, eo.getPropertyType("target"));
827
828         verifyControls();
829     }
830
831     public void testConstructorFailure()
832     {
833         IComponentSpecification spec = newSpec();
834
835         MockControl control = newControl(ClassFab.class);
836         ClassFab classFab = (ClassFab) control.getMock();
837
838         Throwable JavaDoc t = new RuntimeException JavaDoc("Inconceivable!");
839
840         classFab.createClass();
841         control.setThrowable(t);
842
843         ClassFactory cf = newClassFactory(ServiceLink.class, classFab);
844
845         replayControls();
846
847         EnhancementOperationImpl eo = new EnhancementOperationImpl(new DefaultClassResolver(),
848                 spec, ServiceLink.class, cf);
849
850         try
851         {
852             eo.getConstructor();
853             unreachable();
854         }
855         catch (ApplicationRuntimeException ex)
856         {
857             assertEquals(
858                     "Failure enhancing class org.apache.tapestry.link.ServiceLink: Inconceivable!",
859                     ex.getMessage());
860             assertSame(classFab, ex.getComponent());
861             assertSame(t, ex.getRootCause());
862         }
863
864         verifyControls();
865     }
866 }
Popular Tags