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.enhance;
016    
017    import org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.hivemind.Location;
019    import org.apache.hivemind.service.MethodSignature;
020    import org.apache.tapestry.engine.state.ApplicationStateManager;
021    import org.apache.tapestry.spec.InjectSpecification;
022    import org.apache.tapestry.spec.InjectSpecificationImpl;
023    import org.testng.annotations.Test;
024    
025    import java.lang.reflect.Modifier;
026    
027    /**
028     * Tests for {@link org.apache.tapestry.enhance.InjectStateFlagWorker}.
029     * 
030     * @author Howard M. Lewis Ship
031     * @since 4.0
032     */
033    @Test
034    public class InjectStateFlagWorkerTest extends BaseEnhancementTestCase
035    {
036        public void testNoExistingProperty()
037        {
038            Location l = newLocation();
039            ApplicationStateManager asm = newApplicationStateManager();
040    
041            InjectSpecification is = new InjectSpecificationImpl();
042            is.setProperty("fred");
043            is.setObject("fredASO");
044            is.setLocation(l);
045    
046            EnhancementOperation op = newOp();
047    
048            trainGetPropertyType(op, "fred", null);
049    
050            op.claimReadonlyProperty("fred");
051    
052            trainAddInjectedField(
053                    op,
054                    "_$applicationStateManager",
055                    ApplicationStateManager.class,
056                    asm,
057                    "_$asm");
058    
059            trainGetAccessorMethodName(op, "fred", "isFred");
060    
061            MethodSignature sig = new MethodSignature(boolean.class, "isFred", null, null);
062    
063            String code = "{\n  return _$asm.exists(\"fredASO\");\n}\n";
064    
065            op.addMethod(Modifier.PUBLIC, sig, code, l);
066    
067            replay();
068    
069            InjectStateFlagWorker worker = new InjectStateFlagWorker();
070    
071            worker.setApplicationStateManager(asm);
072    
073            worker.performEnhancement(op, is);
074    
075            verify();
076        }
077    
078        public void testWithExistingProperty()
079        {
080            Location l = newLocation();
081            ApplicationStateManager asm = newApplicationStateManager();
082    
083            InjectSpecification is = new InjectSpecificationImpl();
084            is.setProperty("fred");
085            is.setObject("fredASO");
086            is.setLocation(l);
087    
088            EnhancementOperation op = newOp();
089    
090            trainGetPropertyType(op, "fred", boolean.class);
091    
092            op.claimReadonlyProperty("fred");
093    
094            trainAddInjectedField(
095                    op,
096                    "_$applicationStateManager",
097                    ApplicationStateManager.class,
098                    asm,
099                    "_$asm");
100    
101            trainGetAccessorMethodName(op, "fred", "getFred");
102    
103            MethodSignature sig = new MethodSignature(boolean.class, "getFred", null, null);
104    
105            String code = "{\n  return _$asm.exists(\"fredASO\");\n}\n";
106    
107            op.addMethod(Modifier.PUBLIC, sig, code, l);
108    
109            replay();
110    
111            InjectStateFlagWorker worker = new InjectStateFlagWorker();
112    
113            worker.setApplicationStateManager(asm);
114    
115            worker.performEnhancement(op, is);
116    
117            verify();
118        }
119    
120        public void testWithExistingPropertyWrongType()
121        {
122            Location l = newLocation();
123            ApplicationStateManager asm = newApplicationStateManager();
124    
125            InjectSpecification is = new InjectSpecificationImpl();
126            is.setProperty("fred");
127            is.setObject("fredASO");
128            is.setLocation(l);
129    
130            EnhancementOperation op = newOp();
131    
132            trainGetPropertyType(op, "fred", int.class);
133    
134            replay();
135    
136            InjectStateFlagWorker worker = new InjectStateFlagWorker();
137    
138            worker.setApplicationStateManager(asm);
139    
140            try
141            {
142                worker.performEnhancement(op, is);
143                unreachable();
144            }
145            catch (ApplicationRuntimeException ex)
146            {
147                assertEquals("Property fred must be type boolean for this type of injection.", ex.getMessage());
148                assertSame(l, ex.getLocation());
149            }
150    
151            verify();
152        }
153    }