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    
015    package org.apache.tapestry.form.translator;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import org.apache.tapestry.IMarkupWriter;
020    import org.apache.tapestry.IRequestCycle;
021    import org.apache.tapestry.form.FormComponentContributorContext;
022    import org.apache.tapestry.form.FormComponentContributorTestCase;
023    import org.apache.tapestry.form.IFormComponent;
024    import org.apache.tapestry.json.JSONObject;
025    import org.apache.tapestry.valid.ValidatorException;
026    import org.testng.annotations.AfterMethod;
027    import org.testng.annotations.Test;
028    
029    /**
030     * Test case for {@link StringTranslator}.
031     * 
032     * @author Paul Ferraro
033     * @since 4.0
034     */
035    @Test(sequential=true)
036    public class TestStringTranslator extends FormComponentContributorTestCase
037    {
038        private StringTranslator _translator = new StringTranslator();
039    
040        @AfterMethod
041        public void reset()
042        {
043            _translator.setTrim(false);
044            _translator.setEmpty(null);
045            _translator.setMessage(null);
046        }
047        
048        public void test_Format()
049        {
050            replay();
051    
052            String result = _translator.format(_component, null, "Test this");
053    
054            assertEquals("Test this", result);
055    
056            verify();
057        }
058    
059        public void test_Null_Format()
060        {
061            replay();
062    
063            String result = _translator.format(_component, null, null);
064    
065            assertEquals("", result);
066    
067            verify();
068        }
069    
070        public void test_Parse()
071        {
072            replay();
073    
074            try
075            {
076                String result = (String) _translator.parse(_component, null, "Test this");
077    
078                assertEquals("Test this", result);
079            }
080            catch (ValidatorException e)
081            {
082                unreachable();
083            }
084            finally
085            {
086                verify();
087            }
088        }
089    
090        public void test_Trimmed_Parse()
091        {
092            _translator.setTrim(true);
093    
094            replay();
095    
096            try
097            {
098                String result = (String) _translator.parse(_component, null, " Test this ");
099    
100                assertEquals("Test this", result);
101            }
102            catch (ValidatorException e)
103            {
104                unreachable();
105            }
106            finally
107            {
108                verify();
109            }
110        }
111    
112        public void test_Empty_Parse()
113        {
114            replay();
115    
116            try
117            {
118                String result = (String) _translator.parse(_component, null, "");
119    
120                assertEquals(null, result);
121            }
122            catch (ValidatorException e)
123            {
124                unreachable();
125            }
126            finally
127            {
128                verify();
129            }
130        }
131    
132        public void test_Custom_Empty_Parse()
133        {
134            _translator.setEmpty("");
135    
136            replay();
137    
138            try
139            {
140                String result = (String) _translator.parse(_component, null, "");
141    
142                assertEquals("", result);
143            }
144            catch (ValidatorException e)
145            {
146                unreachable();
147            }
148            finally
149            {
150                verify();
151            }
152        }
153    
154        public void test_Render_Contribution()
155        {
156            replay();
157    
158            _translator.renderContribution(null, _cycle, null, _component);
159    
160            verify();
161        }
162    
163        public void test_Trim_Render_Contribution()
164        {
165            IMarkupWriter writer = newWriter();
166            IRequestCycle cycle = newCycle();
167            
168            JSONObject json = new JSONObject();
169            
170            FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
171            
172            expect(context.getProfile()).andReturn(json);
173            
174            IFormComponent field = newFieldWithClientId("myfield");
175            
176            replay();
177    
178            Translator t = new StringTranslator("trim");
179    
180            t.renderContribution(writer, cycle, context, field);
181    
182            verify();
183            
184            assertEquals("{\"trim\":[\"myfield\"]}",
185                    json.toString());
186        }
187    
188    }