View Javadoc

1   /*
2    * Copyright 2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.myfaces.shared.util;
17  
18  import java.util.Locale;
19  import java.util.ResourceBundle;
20  
21  import javax.faces.application.FacesMessage;
22  
23  import junit.framework.Test;
24  import org.apache.myfaces.shared.util.MessageUtils;
25  import org.apache.myfaces.test.base.AbstractJsfTestCase;
26  
27  /**
28   * TestCase for MessageUtils
29   *
30   * @author Stephan Strittmatter
31   */
32  public class MessageUtilsTest extends AbstractJsfTestCase
33  {
34  
35      private static final String DEFAULT_BUNDLE = "javax.faces.Messages";
36  
37      public MessageUtilsTest(String name)
38      {
39          super(name);
40      }
41  
42      // No longer necessary using junit 4 to run tests
43      //public static Test suite() {
44      //    return null; // keep this method or maven won't run it
45      //}
46  
47      /**
48       * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(Severity, String, Object)'
49       */
50      public void testGetMessageSeverityStringObject()
51      {
52          facesContext.getViewRoot().setLocale(Locale.ENGLISH);
53  
54          FacesMessage msg = MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,
55                  "javax.faces.component.UIInput.CONVERSION", null);
56          assertEquals("Conversion Error", msg.getSummary());
57  
58          facesContext.getViewRoot().setLocale(Locale.GERMAN);
59  
60          msg = MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,
61                  "javax.faces.component.UIInput.CONVERSION",
62                  "blubb");
63          assertEquals("Konvertierungsfehler", msg.getSummary());
64      }
65  
66      /**
67       * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(Severity, String, Object[])'
68       */
69      public void testGetMessageSeverityStringObjectArray()
70      {
71          facesContext.getViewRoot().setLocale(Locale.ENGLISH);
72  
73          FacesMessage msg = MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,
74                  "javax.faces.component.UIInput.CONVERSION", null);
75          assertEquals("Conversion Error", msg.getSummary());
76  
77          facesContext.getViewRoot().setLocale(Locale.GERMAN);
78  
79          msg = MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,
80                  "javax.faces.component.UIInput.CONVERSION", null);
81          assertEquals("Konvertierungsfehler", msg.getSummary());
82      }
83  
84      /**
85       * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(Severity, String, Object[], FacesContext)'
86       */
87      public void testGetMessageSeverityStringObjectArrayFacesContext()
88      {
89          facesContext.getViewRoot().setLocale(Locale.ENGLISH);
90  
91          FacesMessage msg = MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,
92                  "javax.faces.component.UIInput.CONVERSION", null, facesContext);
93          assertEquals("Conversion Error", msg.getSummary());
94  
95          facesContext.getViewRoot().setLocale(Locale.GERMAN);
96  
97          msg = MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,
98                  "javax.faces.component.UIInput.CONVERSION", null,
99                  facesContext);
100         assertEquals("Konvertierungsfehler", msg.getSummary());
101     }
102 
103     /**
104      * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(Locale, String, Object[])'
105      */
106     public void testGetMessageLocaleStringObjectArray()
107     {
108         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
109 
110         FacesMessage msg = org.apache.myfaces.shared.util.MessageUtils.getMessage(Locale.ENGLISH,
111                 "javax.faces.component.UIInput.CONVERSION", null);
112         assertEquals("Conversion Error", msg.getSummary());
113 
114         msg = MessageUtils.getMessage(Locale.GERMAN,
115                 "javax.faces.component.UIInput.CONVERSION", null);
116         assertEquals("Konvertierungsfehler", msg.getSummary());
117 
118     }
119 
120     /**
121      * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(FacesContext, String)'
122      */
123     public void testGetMessageFacesContextString()
124     {
125         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
126 
127         FacesMessage msg = MessageUtils.getMessage(facesContext,
128                 "javax.faces.component.UIInput.CONVERSION");
129         assertEquals("Conversion Error", msg.getSummary());
130 
131         facesContext.getViewRoot().setLocale(Locale.GERMAN);
132 
133         msg = MessageUtils.getMessage(facesContext,
134                 "javax.faces.component.UIInput.CONVERSION");
135         assertEquals("Konvertierungsfehler", msg.getSummary());
136     }
137 
138     /**
139      * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(FacesContext, String, Object[])'
140      */
141     public void testGetMessageFacesContextStringObjectArray()
142     {
143         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
144 
145         FacesMessage msg = MessageUtils.getMessage(facesContext,
146                 "javax.faces.component.UIInput.CONVERSION", null);
147         assertEquals("Conversion Error", msg.getSummary());
148 
149         facesContext.getViewRoot().setLocale(Locale.GERMAN);
150 
151         msg = MessageUtils.getMessage(facesContext,
152                 "javax.faces.component.UIInput.CONVERSION", null);
153         assertEquals("Konvertierungsfehler", msg.getSummary());
154     }
155 
156     /**
157      * testGetMessageWithBundle
158      */
159     public void testGetMessageWithBundle()
160     {
161         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
162 
163         ResourceBundle bundle = ResourceBundle.getBundle(DEFAULT_BUNDLE,
164                 Locale.ENGLISH);
165         FacesMessage msg = MessageUtils.getMessage(bundle,
166                 "javax.faces.component.UIInput.CONVERSION", null);
167 
168         assertEquals("Conversion Error", msg.getSummary());
169     }
170 
171     /**
172      * testGetMessageWithBundleName
173      */
174     public void testGetMessageWithBundleName()
175     {
176         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
177 
178         FacesMessage msg = MessageUtils.getMessage(DEFAULT_BUNDLE,
179                 "javax.faces.component.UIInput.CONVERSION", null);
180 
181         assertEquals("Conversion Error", msg.getSummary());
182     }
183 
184     /**
185      * testGetMessageWithBundleNameLocale
186      */
187     public void testGetMessageWithBundleNameLocale()
188     {
189         FacesMessage msg = MessageUtils.getMessage(DEFAULT_BUNDLE,
190                 Locale.GERMAN, "javax.faces.component.UIInput.CONVERSION", null);
191 
192         assertEquals("Konvertierungsfehler", msg.getSummary());
193     }
194 
195     /**
196      * testSubstituteParamsWithDELocale(
197      */
198     public void testSubstituteParamsWithDELocale() {
199         String paramString = MessageUtils.substituteParams(Locale.GERMANY, "currency {0,number,currency}", new Object[]{100});
200 
201         assertEquals("currency 100,00 \u20ac",paramString);
202     }
203 
204     /**
205      * testSubstituteParamsWithGBLocale(
206      */
207     public void testSubstituteParamsWithGBLocale() {
208         String paramString = MessageUtils.substituteParams(Locale.UK, "currency {0,number,currency}", new Object[]{100});
209 
210         System.out.println(paramString);
211         assertEquals("currency \u00a3100.00",paramString);
212     }
213 
214 }