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.form;
015    
016    import org.apache.tapestry.IForm;
017    import org.apache.tapestry.IMarkupWriter;
018    import org.apache.tapestry.IRequestCycle;
019    import org.apache.tapestry.valid.IValidationDelegate;
020    import org.apache.tapestry.valid.ValidatorException;
021    import static org.easymock.EasyMock.expect;
022    import org.testng.annotations.Test;
023    
024    /**
025     * Tests {@link PropertySelection}.
026     * 
027     */
028    @Test(sequential = true)
029    public class TestPropertySelection extends BaseFormComponentTestCase
030    {
031        private static final String SYSTEM_NEWLINE= (String)java.security.AccessController.doPrivileged(
032                new sun.security.action.GetPropertyAction("line.separator"));
033        
034        public void test_Rewind()
035        {
036            ValidatableFieldSupport vfs = newMock(ValidatableFieldSupport.class);
037    
038            IPropertySelectionModel model = new StringPropertySelectionModel(new String[] { "One", "Two", "Three" });
039            
040            PropertySelection component = newInstance(PropertySelection.class, 
041                    new Object[]  { 
042                "validatableFieldSupport", vfs,
043                "model", model
044            });
045            
046            IRequestCycle cycle = newCycle();
047            
048            IForm form = newMock(IForm.class);
049            
050            IMarkupWriter writer = newWriter();
051            
052            IValidationDelegate delegate = newDelegate();
053            
054            expect(cycle.renderStackPush(component)).andReturn(component);
055            
056            trainGetForm(cycle, form);
057            trainWasPrerendered(form, writer, component, false);
058            trainGetDelegate(form, delegate);
059    
060            delegate.setFormComponent(component);
061    
062            trainGetElementId(form, component, "barney");
063            trainIsRewinding(form, true);
064            
065            trainGetParameter(cycle, "barney", "1");
066            
067            try
068            {
069                vfs.validate(component, writer, cycle, model.translateValue("1"));
070            }
071            catch (ValidatorException e)
072            {
073                unreachable();
074            }
075            
076            expect(cycle.renderStackPop()).andReturn(component);
077            
078            replay();
079    
080            component.render(writer, cycle);
081    
082            verify();
083            
084            assertEquals(component.getValue(), "Two");
085        }
086        
087        public void test_Render()
088        {
089            ValidatableFieldSupport vfs = newMock(ValidatableFieldSupport.class);
090            
091            IRequestCycle cycle = newCycle();
092            IForm form = newMock(IForm.class);
093    
094            IMarkupWriter writer = newBufferWriter();
095    
096            MockDelegate delegate = new MockDelegate();
097    
098            IPropertySelectionModel model = new StringPropertySelectionModel(new String[] { "One", "Two", "Three" }, 
099                    new boolean[] {false, false, true});
100            
101            PropertySelection component = newInstance(PropertySelection.class,
102                                                      "id", "hannah",
103                                                      "validatableFieldSupport", vfs,
104                                                      "model", model,
105                                                      "value", "One",
106                                                      "optionRenderer", DefaultOptionRenderer.DEFAULT_INSTANCE);
107            
108            expect(cycle.renderStackPush(component)).andReturn(component);
109            
110            trainGetForm(cycle, form);
111            trainWasPrerendered(form, writer, component, false);
112            trainGetDelegate(form, delegate);
113            
114            delegate.setFormComponent(component);
115    
116            trainGetElementId(form, component, "hannah");
117            trainIsRewinding(form, false);
118            trainIsRewinding(cycle, false);
119            
120            form.setFormFieldUpdating(true);
121            
122            delegate.setFormComponent(component);
123            vfs.renderContributions(component, writer, cycle);
124            
125            expect(cycle.renderStackPop()).andReturn(component);
126            
127            replay();
128    
129            component.render(writer, cycle);
130    
131            verify();
132    
133            assertBuffer("<span class=\"prefix\"><select name=\"hannah\" id=\"hannah\" class=\"validation-delegate\">" + SYSTEM_NEWLINE +
134                    "<option value=\"0\" selected=\"selected\">One</option>" + SYSTEM_NEWLINE +
135                    "<option value=\"1\">Two</option>" + SYSTEM_NEWLINE +
136                    "<option value=\"2\" disabled=\"disabled\">Three</option>" + SYSTEM_NEWLINE +
137                    "</select></span>");
138        }
139    }