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  public class MessageUtilsTest extends AbstractJsfTestCase
31  {
32  
33      private static final String DEFAULT_BUNDLE = "javax.faces.Messages";
34  
35      public MessageUtilsTest(String name)
36      {
37          super(name);
38      }
39  
40      // No longer necessary using junit 4 to run tests
41      //public static Test suite() {
42      //    return null; // keep this method or maven won't run it
43      //}
44  
45      /**
46       * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(Severity, String, Object)'
47       */
48      public void testGetMessageSeverityStringObject()
49      {
50          facesContext.getViewRoot().setLocale(Locale.ENGLISH);
51  
52          FacesMessage msg = MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,
53                  "javax.faces.component.UIInput.CONVERSION", null);
54          assertEquals("Conversion Error", msg.getSummary());
55  
56          facesContext.getViewRoot().setLocale(Locale.GERMAN);
57  
58          msg = MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,
59                  "javax.faces.component.UIInput.CONVERSION",
60                  "blubb");
61          assertEquals("Konvertierungsfehler", msg.getSummary());
62      }
63  
64      /**
65       * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(Severity, String, Object[])'
66       */
67      public void testGetMessageSeverityStringObjectArray()
68      {
69          facesContext.getViewRoot().setLocale(Locale.ENGLISH);
70  
71          FacesMessage msg = MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,
72                  "javax.faces.component.UIInput.CONVERSION", null);
73          assertEquals("Conversion Error", msg.getSummary());
74  
75          facesContext.getViewRoot().setLocale(Locale.GERMAN);
76  
77          msg = MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,
78                  "javax.faces.component.UIInput.CONVERSION", null);
79          assertEquals("Konvertierungsfehler", msg.getSummary());
80      }
81  
82      /**
83       * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(Severity, String, Object[], FacesContext)'
84       */
85      public void testGetMessageSeverityStringObjectArrayFacesContext()
86      {
87          facesContext.getViewRoot().setLocale(Locale.ENGLISH);
88  
89          FacesMessage msg = MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,
90                  "javax.faces.component.UIInput.CONVERSION", null, facesContext);
91          assertEquals("Conversion Error", msg.getSummary());
92  
93          facesContext.getViewRoot().setLocale(Locale.GERMAN);
94  
95          msg = MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,
96                  "javax.faces.component.UIInput.CONVERSION", null,
97                  facesContext);
98          assertEquals("Konvertierungsfehler", msg.getSummary());
99      }
100 
101     /**
102      * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(Locale, String, Object[])'
103      */
104     public void testGetMessageLocaleStringObjectArray()
105     {
106         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
107 
108         FacesMessage msg = org.apache.myfaces.shared.util.MessageUtils.getMessage(Locale.ENGLISH,
109                 "javax.faces.component.UIInput.CONVERSION", null);
110         assertEquals("Conversion Error", msg.getSummary());
111 
112         msg = MessageUtils.getMessage(Locale.GERMAN,
113                 "javax.faces.component.UIInput.CONVERSION", null);
114         assertEquals("Konvertierungsfehler", msg.getSummary());
115 
116     }
117 
118     /**
119      * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(FacesContext, String)'
120      */
121     public void testGetMessageFacesContextString()
122     {
123         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
124 
125         FacesMessage msg = MessageUtils.getMessage(facesContext,
126                 "javax.faces.component.UIInput.CONVERSION");
127         assertEquals("Conversion Error", msg.getSummary());
128 
129         facesContext.getViewRoot().setLocale(Locale.GERMAN);
130 
131         msg = MessageUtils.getMessage(facesContext,
132                 "javax.faces.component.UIInput.CONVERSION");
133         assertEquals("Konvertierungsfehler", msg.getSummary());
134     }
135 
136     /**
137      * Test method for 'org.apache.myfaces.shared.util.MessageUtils.getMessage(FacesContext, String, Object[])'
138      */
139     public void testGetMessageFacesContextStringObjectArray()
140     {
141         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
142 
143         FacesMessage msg = MessageUtils.getMessage(facesContext,
144                 "javax.faces.component.UIInput.CONVERSION", null);
145         assertEquals("Conversion Error", msg.getSummary());
146 
147         facesContext.getViewRoot().setLocale(Locale.GERMAN);
148 
149         msg = MessageUtils.getMessage(facesContext,
150                 "javax.faces.component.UIInput.CONVERSION", null);
151         assertEquals("Konvertierungsfehler", msg.getSummary());
152     }
153 
154     /**
155      * testGetMessageWithBundle
156      */
157     public void testGetMessageWithBundle()
158     {
159         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
160 
161         ResourceBundle bundle = ResourceBundle.getBundle(DEFAULT_BUNDLE,
162                 Locale.ENGLISH);
163         FacesMessage msg = MessageUtils.getMessage(bundle,
164                 "javax.faces.component.UIInput.CONVERSION", null);
165 
166         assertEquals("Conversion Error", msg.getSummary());
167     }
168 
169     /**
170      * testGetMessageWithBundleName
171      */
172     public void testGetMessageWithBundleName()
173     {
174         facesContext.getViewRoot().setLocale(Locale.ENGLISH);
175 
176         FacesMessage msg = MessageUtils.getMessage(DEFAULT_BUNDLE,
177                 "javax.faces.component.UIInput.CONVERSION", null);
178 
179         assertEquals("Conversion Error", msg.getSummary());
180     }
181 
182     /**
183      * testGetMessageWithBundleNameLocale
184      */
185     public void testGetMessageWithBundleNameLocale()
186     {
187         FacesMessage msg = MessageUtils.getMessage(DEFAULT_BUNDLE,
188                 Locale.GERMAN, "javax.faces.component.UIInput.CONVERSION", null);
189 
190         assertEquals("Konvertierungsfehler", msg.getSummary());
191     }
192 
193     /**
194      * testSubstituteParamsWithDELocale(
195      */
196     public void testSubstituteParamsWithDELocale() {
197         String paramString = MessageUtils.substituteParams(Locale.GERMANY, "currency {0,number,currency}", new Object[]{100});
198 
199         assertEquals("currency 100,00 \u20ac",paramString);
200     }
201 
202     /**
203      * testSubstituteParamsWithGBLocale(
204      */
205     public void testSubstituteParamsWithGBLocale() {
206         String paramString = MessageUtils.substituteParams(Locale.UK, "currency {0,number,currency}", new Object[]{100});
207 
208         System.out.println(paramString);
209         assertEquals("currency \u00a3100.00",paramString);
210     }
211 
212 }