001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    package org.apache.tapestry.annotations;
015    
016    import static org.easymock.EasyMock.*;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.apache.hivemind.Location;
022    import org.apache.tapestry.enhance.EnhancementOperation;
023    import org.apache.tapestry.spec.ComponentSpecification;
024    import org.apache.tapestry.spec.IComponentSpecification;
025    import org.apache.tapestry.spec.IPropertySpecification;
026    import org.apache.tapestry.spec.PropertySpecification;
027    import org.testng.annotations.Test;
028    
029    
030    /**
031     * Tests functionality of {@link ComponentPropertyProxyWorker}.
032     */
033    @Test(sequential = true)
034    public class TestComponentPropertyProxyWorker extends BaseAnnotationTestCase
035    {
036    
037        IPropertySpecification addProperty(EnhancementOperation op, IComponentSpecification spec, Location l,
038                String propertyName) {
039            IPropertySpecification pspec = new PropertySpecification();
040    
041            pspec.setName(propertyName);
042            pspec.setPersistence("session");
043            pspec.setLocation(l);
044    
045            spec.addPropertySpecification(pspec);
046            
047            return pspec;
048        }
049        
050        
051        public void test_Generics_Excluded() {
052            Location l = newLocation();
053            EnhancementOperation op = newMock(EnhancementOperation.class);
054            checkOrder(op, false);
055    
056            IComponentSpecification spec = new ComponentSpecification();
057    
058            expect(op.getBaseClass()).andReturn(AnnotatedGenericPage.class).anyTimes();
059    
060            ComponentPropertyProxyWorker worker = new ComponentPropertyProxyWorker();
061            
062            addProperty(op, spec, l, "value");
063    
064            List<String> exclude = new ArrayList<String>();
065            exclude.add("Entity");
066            worker.setExcludedPackages(exclude);
067            
068            replay();
069    
070            worker.performEnhancement(op, spec);
071    
072            verify();
073    
074            IPropertySpecification prop = spec.getPropertySpecification("value");
075    
076            assert prop != null;
077            assert prop.isPersistent();
078            assert prop.isProxyChecked();
079            assert !prop.canProxy();
080        }
081    
082        public void test_Valid_Property() {
083            Location l = newLocation();
084            EnhancementOperation op = newMock(EnhancementOperation.class);
085            checkOrder(op, false);
086    
087            IComponentSpecification spec = new ComponentSpecification();
088    
089            expect(op.getBaseClass()).andReturn(AnnotatedGenericPersistentPage.class).anyTimes();
090    
091            ComponentPropertyProxyWorker worker = new ComponentPropertyProxyWorker();
092    
093            addProperty(op, spec, l, "object");
094    
095            List<String> exclude = new ArrayList<String>();
096            exclude.add("Entity");
097            worker.setExcludedPackages(exclude);
098    
099            replay();
100    
101            worker.performEnhancement(op, spec);
102    
103            verify();
104    
105            IPropertySpecification prop = spec.getPropertySpecification("object");
106    
107            assert prop != null;
108            assert prop.isPersistent();
109            assert prop.isProxyChecked();
110            assert prop.canProxy();
111        }
112        
113        public void test_Type_Found()
114        {
115            ComponentPropertyProxyWorker worker = new ComponentPropertyProxyWorker();
116            List<String> exclude = new ArrayList<String>();
117            exclude.add("Entity");
118            worker.setExcludedPackages(exclude);
119            
120            IPropertySpecification prop = new PropertySpecification();
121            prop.setName("value");
122            prop.setPersistence("session");
123            
124            assertEquals(worker.extractPropertyType(AnnotatedGenericPersistentPage.class, "value", prop), Persistent.class);
125            
126            prop.setGeneric(false);
127            prop.setType(null);
128            prop.setName("secondValue");
129            
130            Class type = worker.extractPropertyType(AnnotatedGenericPersistentPage.class, "secondValue", prop);
131            
132            assert type != null;
133            assert prop.isGeneric();
134            
135            assertEquals(type, Persistent.class);
136        }
137        
138        public void test_Write_Property_Non_Generic() {
139            Location l = newLocation();
140            EnhancementOperation op = newMock(EnhancementOperation.class);
141            checkOrder(op, false);
142    
143            IComponentSpecification spec = new ComponentSpecification();
144    
145            expect(op.getBaseClass()).andReturn(AnnotatedGenericPersistentPage.class).anyTimes();
146    
147            ComponentPropertyProxyWorker worker = new ComponentPropertyProxyWorker();
148            
149            IPropertySpecification p = addProperty(op, spec, l, "listValue");
150    
151            List<String> exclude = new ArrayList<String>();
152            exclude.add("Entity");
153            worker.setExcludedPackages(exclude);
154    
155            replay();
156    
157            worker.performEnhancement(op, spec);
158    
159            verify();
160    
161            IPropertySpecification prop = spec.getPropertySpecification("listValue");
162    
163            assert prop != null;
164            assert prop.isPersistent();
165            assert prop.isProxyChecked();
166            assert prop.canProxy();
167            
168            assertEquals(p.getType(), "java.util.List");
169        }
170        
171        public void test_Write_Property_Generic() {
172            Location l = newLocation();
173            EnhancementOperation op = newMock(EnhancementOperation.class);
174            checkOrder(op, false);
175    
176            IComponentSpecification spec = new ComponentSpecification();
177    
178            expect(op.getBaseClass()).andReturn(AnnotatedGenericPersistentPage.class).anyTimes();
179    
180            ComponentPropertyProxyWorker worker = new ComponentPropertyProxyWorker();
181            
182            IPropertySpecification p = addProperty(op, spec, l, "secondValue");
183    
184            List<String> exclude = new ArrayList<String>();
185            exclude.add("Entity");
186            worker.setExcludedPackages(exclude);
187    
188            replay();
189    
190            worker.performEnhancement(op, spec);
191    
192            verify();
193    
194            IPropertySpecification prop = spec.getPropertySpecification("secondValue");
195            
196            assert prop != null;
197            assert prop.isPersistent();
198            assert prop.isProxyChecked();
199            assert !prop.canProxy();
200            
201            assertEquals(p.getType(), Persistent.class.getName());
202        }
203        
204        
205        public void test_Excluded()
206        {
207            Location l = newLocation();
208            EnhancementOperation op = newOp();
209            IComponentSpecification spec = new ComponentSpecification();
210            
211            expect(op.getBaseClass()).andReturn(AnnotatedPage.class).anyTimes();
212            
213            addProperty(op, spec, l, "bean");
214            
215            ComponentPropertyProxyWorker worker = new ComponentPropertyProxyWorker();
216            
217            List exclude = new ArrayList();
218            exclude.add("Entity");
219            worker.setExcludedPackages(exclude);
220            
221            replay();
222            
223            worker.performEnhancement(op, spec);
224            
225            verify();
226            
227            IPropertySpecification prop = spec.getPropertySpecification("bean");
228            
229            assert prop != null;
230            assert prop.isPersistent();
231            assert prop.isProxyChecked();
232            assert !prop.canProxy();
233        }
234        
235        public void test_SubClass_Excluded()
236        {
237            Location l = newLocation();
238            EnhancementOperation op = newOp();
239            IComponentSpecification spec = new ComponentSpecification();
240            
241            expect(op.getBaseClass()).andReturn(AnnotatedPage.class).anyTimes();
242            
243            addProperty(op, spec, l, "subBean");
244            
245            ComponentPropertyProxyWorker worker = new ComponentPropertyProxyWorker();
246            
247            List exclude = new ArrayList();
248            exclude.add("Entity");
249            worker.setExcludedPackages(exclude);
250            
251            replay();
252            
253            worker.performEnhancement(op, spec);
254            
255            verify();
256            
257            IPropertySpecification prop = spec.getPropertySpecification("subBean");
258            
259            assert prop != null;
260            assert prop.isPersistent();
261            assert prop.isProxyChecked();
262            assert !prop.canProxy();
263        }
264    }