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.hivemind.Resource;
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 org.testng.annotations.Test;
026    
027    /**
028     * Tests for {@link org.apache.tapestry.annotations.InitialValueAnnotationWorker}.
029     * 
030     * @author Howard M. Lewis Ship
031     */
032    @Test
033    public class InitialValueAnnotationWorkerTest extends BaseAnnotationTestCase
034    {
035        public void testCanEnhance()
036        {
037            InitialValueAnnotationWorker worker = new InitialValueAnnotationWorker();
038            
039            assertEquals(false, worker.canEnhance(findMethod(AnnotatedPage.class, "getMapBean")));
040            assertEquals(true, worker.canEnhance(findMethod(
041                    AnnotatedPage.class,
042                    "getPropertyWithInitialValue")));
043        }
044    
045        public void testEnhanceNonMatch()
046        {
047            EnhancementOperation op = newOp();
048            IComponentSpecification spec = newSpec();
049            Method method = findMethod(AnnotatedPage.class, "getMapBean");
050            Resource resource = newResource(AnnotatedPage.class);
051    
052            replay();
053    
054            InitialValueAnnotationWorker worker = new InitialValueAnnotationWorker();
055    
056            worker.peformEnhancement(op, spec, method, resource);
057    
058            verify();
059        }
060    
061        public void testEnhancePersistAnnotationPresent()
062        {
063            EnhancementOperation op = newOp();
064            IComponentSpecification spec = newSpec();
065            Method method = findMethod(AnnotatedPage.class, "getPersistentPropertyWithInitialValue");
066            Resource resource = newResource(AnnotatedPage.class);
067    
068            replay();
069    
070            InitialValueAnnotationWorker worker = new InitialValueAnnotationWorker();
071    
072            worker.peformEnhancement(op, spec, method, resource);
073    
074            verify();
075        }
076    
077        public void testJustInitialValue()
078        {
079            EnhancementOperation op = newOp();
080            IComponentSpecification spec = new ComponentSpecification();
081            Method method = findMethod(AnnotatedPage.class, "getPropertyWithInitialValue");
082            Resource resource = newResource(AnnotatedPage.class);
083    
084            replay();
085    
086            InitialValueAnnotationWorker worker = new InitialValueAnnotationWorker();
087    
088            worker.peformEnhancement(op, spec, method, resource);
089    
090            verify();
091    
092            IPropertySpecification ps = spec.getPropertySpecification("propertyWithInitialValue");
093    
094            assertEquals("propertyWithInitialValue", ps.getName());
095            assertEquals(false, ps.isPersistent());
096            assertEquals("fred", ps.getInitialValue());
097    
098            Location expectedLocation = newMethodLocation(
099                    AnnotatedPage.class,
100                    method,
101                    InitialValue.class);
102    
103            assertEquals(expectedLocation, ps.getLocation());
104        }
105    }