001    // Copyright 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    
015    package org.apache.tapestry.annotations;
016    
017    import java.lang.reflect.Method;
018    
019    import org.apache.hivemind.Location;
020    import org.apache.tapestry.engine.IPropertySource;
021    import org.apache.tapestry.enhance.EnhancementOperation;
022    import org.apache.tapestry.spec.ComponentSpecification;
023    import org.apache.tapestry.spec.IComponentSpecification;
024    import org.apache.tapestry.spec.IPropertySpecification;
025    import static org.easymock.EasyMock.expect;
026    import org.testng.annotations.Test;
027    
028    /**
029     * Tests for {@link org.apache.tapestry.annotations.PersistAnnotationWorker}.
030     * 
031     * @author Howard M. Lewis Ship
032     * @since 4.0
033     */
034    @Test
035    public class PersistAnnotationWorkerTest extends AnnotationEnhancementWorkerTest
036    {
037        public void testDefaultStrategy()
038        {
039            Location l = newLocation();
040    
041            EnhancementOperation op = newOp();
042            IComponentSpecification spec = new ComponentSpecification();
043                IPropertySource propertySource = trainPropertySource();
044    
045                replay();
046    
047            Method m = findMethod(AnnotatedPage.class, "getPersistentProperty");
048    
049            PersistAnnotationWorker worker = new PersistAnnotationWorker();
050                worker.setPropertySource(propertySource);
051                worker.performEnhancement(op, spec, m, l);
052    
053            verify();
054    
055            IPropertySpecification ps = spec.getPropertySpecification("persistentProperty");
056    
057            assertEquals("session", ps.getPersistence());
058            assertEquals("persistentProperty", ps.getName());
059            assertSame(l, ps.getLocation());
060            assertNull(ps.getInitialValue());
061        }
062    
063            public void testStrategySpecified()
064        {
065            Location l = newLocation();
066    
067            EnhancementOperation op = newOp();
068            IComponentSpecification spec = new ComponentSpecification();
069                IPropertySource propertySource = trainPropertySource();
070    
071                replay();
072    
073            Method m = findMethod(AnnotatedPage.class, "getClientPersistentProperty");
074    
075            PersistAnnotationWorker worker = new PersistAnnotationWorker();
076                worker.setPropertySource(propertySource);
077                worker.performEnhancement(op, spec, m, l);
078    
079            verify();
080    
081            IPropertySpecification ps = spec.getPropertySpecification("clientPersistentProperty");
082    
083            assertEquals("client", ps.getPersistence());
084            assertEquals("clientPersistentProperty", ps.getName());
085            assertSame(l, ps.getLocation());
086            assertNull(ps.getInitialValue());
087        }
088    
089        public void testWithInitialValue()
090        {
091            Location l = newLocation();
092    
093            EnhancementOperation op = newOp();
094            IComponentSpecification spec = new ComponentSpecification();
095                IPropertySource propertySource = trainPropertySource();
096    
097                replay();
098    
099            Method m = findMethod(AnnotatedPage.class, "getPersistentPropertyWithInitialValue");
100    
101            PersistAnnotationWorker worker = new PersistAnnotationWorker();
102                worker.setPropertySource(propertySource);
103                worker.performEnhancement(op, spec, m, l);
104    
105            verify();
106    
107            IPropertySpecification ps = spec
108                    .getPropertySpecification("persistentPropertyWithInitialValue");
109    
110            assertEquals("session", ps.getPersistence());
111            assertEquals("persistentPropertyWithInitialValue", ps.getName());
112            assertSame(l, ps.getLocation());
113            assertEquals("user.naturalName", ps.getInitialValue());
114        }
115    
116            private IPropertySource trainPropertySource()
117            {
118                    IPropertySource propertySource = newMock(IPropertySource.class);
119                    expect(propertySource.getPropertyValue(PersistAnnotationWorker.DEFAULT_PROPERTY_PERSISTENCE_STRATEGY)).andReturn("session");
120                    return propertySource;
121            }
122    }