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.enhance.EnhancementOperation;
021    import org.apache.tapestry.spec.ComponentSpecification;
022    import org.apache.tapestry.spec.IComponentSpecification;
023    import org.apache.tapestry.spec.InjectSpecification;
024    import org.testng.annotations.Test;
025    
026    /**
027     * Test for the "simple" annotation workers, that collect basic information and update the component
028     * specification.
029     * 
030     * @author Howard Lewis Ship
031     * @since 4.0
032     */
033    @Test
034    public class TestSimpleAnnotationWorkers extends BaseAnnotationTestCase
035    {
036        public void test_Inject_Page()
037        {
038            Location l = newLocation();
039            IComponentSpecification spec = execute(new InjectPageAnnotationWorker(), "getMyPage", l);
040    
041            InjectSpecification is = (InjectSpecification) spec.getInjectSpecifications().get(0);
042    
043            assertEquals("myPage", is.getProperty());
044            assertEquals("page", is.getType());
045            assertEquals("SomePageName", is.getObject());
046            assertSame(l, is.getLocation());
047        }
048    
049        public void test_Inject_Meta()
050        {
051            Location l = newLocation();
052            IComponentSpecification spec = execute(new InjectMetaAnnotationWorker(), "getMetaFred", l);
053    
054            InjectSpecification is = (InjectSpecification) spec.getInjectSpecifications().get(0);
055    
056            assertEquals("metaFred", is.getProperty());
057            assertEquals("meta", is.getType());
058            assertEquals("fred", is.getObject());
059            assertSame(l, is.getLocation());
060    
061        }
062        
063        public void test_Inject_Meta_NoValue()
064        {
065            Location l = newLocation();
066            IComponentSpecification spec = execute(new InjectMetaAnnotationWorker(), "getPageTitle", l);
067    
068            InjectSpecification is = (InjectSpecification) spec.getInjectSpecifications().get(0);
069    
070            assertEquals("pageTitle", is.getProperty());
071            assertEquals("meta", is.getType());
072            assertEquals("page-title", is.getObject());
073            assertSame(l, is.getLocation());
074    
075        }    
076    
077        public void test_Inject_Script()
078        {
079            Location l = newLocation();
080            IComponentSpecification spec = execute(new InjectScriptAnnotationWorker(), "getScript", l);
081    
082            InjectSpecification is = (InjectSpecification) spec.getInjectSpecifications().get(0);
083    
084            assertEquals("script", is.getProperty());
085            assertEquals("script", is.getType());
086            assertEquals("foo.script", is.getObject());
087            assertSame(l, is.getLocation());
088        }
089    
090        public void test_Inject_State()
091        {
092            Location l = newLocation();
093            IComponentSpecification spec = execute(new InjectStateAnnotationWorker(), "getBarney", l);
094    
095            InjectSpecification is = (InjectSpecification) spec.getInjectSpecifications().get(0);
096    
097            assertEquals("barney", is.getProperty());
098            assertEquals("state", is.getType());
099            assertEquals("barneyASO", is.getObject());
100            assertSame(l, is.getLocation());
101        }
102        
103        public void test_Inject_State_NoValue()
104        {
105            Location l = newLocation();
106            IComponentSpecification spec = execute(new InjectStateAnnotationWorker(), "getMyVisit", l);
107    
108            InjectSpecification is = (InjectSpecification) spec.getInjectSpecifications().get(0);
109    
110            assertEquals("myVisit", is.getProperty());
111            assertEquals("state", is.getType());
112            assertEquals("my-visit", is.getObject());
113            assertSame(l, is.getLocation());
114        }    
115    
116        public void test_Inject_State_Flag()
117        {
118            Location l = newLocation();
119            IComponentSpecification spec = execute(
120                    new InjectStateFlagAnnotationWorker(),
121                    "getBarneyExists",
122                    l);
123    
124            InjectSpecification is = (InjectSpecification) spec.getInjectSpecifications().get(0);
125    
126            assertEquals("barneyExists", is.getProperty());
127            assertEquals("state-flag", is.getType());
128            assertEquals("barneyASO", is.getObject());
129            assertSame(l, is.getLocation());
130        }
131        
132        public void test_Inject_State_Flag_NoValue()
133        {
134            Location l = newLocation();
135            IComponentSpecification spec = execute(
136                    new InjectStateFlagAnnotationWorker(),
137                    "getMyVisitExists",
138                    l);
139    
140            InjectSpecification is = (InjectSpecification) spec.getInjectSpecifications().get(0);
141    
142            assertEquals("myVisitExists", is.getProperty());
143            assertEquals("state-flag", is.getType());
144            assertEquals("my-visit", is.getObject());
145            assertSame(l, is.getLocation());
146        }    
147    
148        private IComponentSpecification execute(MethodAnnotationEnhancementWorker worker,
149                String methodName, Location location)
150        {
151            EnhancementOperation op = newOp();
152            IComponentSpecification spec = new ComponentSpecification();
153    
154            Method method = findMethod(AnnotatedPage.class, methodName);
155    
156            replay();
157    
158            worker.performEnhancement(op, spec, method, location);
159    
160            verify();
161    
162            return spec;
163        }
164    }