KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cintoo > messages > TestMessages


1 /*
2  * Copyright 2006 cintoo, Berlin, Germany
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 cintoo.messages;
17
18 import api.cintoo.messages.Messages;
19 import cintoo.messages.context.IdContext;
20 import cintoo.messages.context.PackageContext;
21 import cintoo.messages.bundle.xml.XmlResourceBundle;
22
23 import static api.cintoo.messages.Messages.*;
24
25 import java.util.ResourceBundle JavaDoc;
26 import java.util.Locale JavaDoc;
27
28 import org.testng.annotations.*;
29 import org.testng.Assert;
30
31
32 public class TestMessages {
33
34   @Test
35   public void testLocaleBeforeBundle() {
36     Messages.setLocale("en", "");
37     Messages.setBundle("test");
38     String JavaDoc value = Messages.format("testKey");
39     Assert.assertEquals(value, "testEnValue", "Correct key returned when locale is set before cintoo.cintoo.bundle");
40   }
41
42   @Test
43   public void testFormatWithArgs() {
44     Messages.setLocale("en", "");
45     Messages.setBundle("test");
46     String JavaDoc value = Messages.format("testKeyWithArgs", 1, 2);
47     Assert.assertEquals(value, "testEnValue 1 2", "Correct formating called");
48   }
49
50   @Test
51   public void testFormatWithArgsShortcuts() {
52     Messages.setLocale("en", "");
53     Messages.setBundle("test");
54     String JavaDoc value = Messages.format("testKeyWithArgs", 1, 2);
55     String JavaDoc valueUnderscore = Messages._("testKeyWithArgs", 1, 2);
56     String JavaDoc valueDollar = Messages.$("testKeyWithArgs", 1, 2);
57     Assert.assertEquals(value, valueUnderscore, "Correct formating for _()");
58     Assert.assertEquals(value, valueDollar, "Correct formating for $()");
59   }
60
61   @Test
62   public void testFormatWithContextAndArgs() {
63     Messages.setLocale("en", "");
64     Messages.setBundle("test", IdContext.id("testid"));
65     String JavaDoc value = Messages.format(IdContext.id("testid"), "testKeyWithArgs", 1, 2);
66     Assert.assertEquals(value, "testEnValue 1 2", "Correct formating called");
67   }
68
69   @Test
70   public void testFormatWithContextAndArgsShortcuts() {
71     Messages.setLocale("en", "");
72     Messages.setBundle("test", IdContext.id("testid"));
73     String JavaDoc value = Messages.format(IdContext.id("testid"), "testKeyWithArgs", 1, 2);
74     String JavaDoc valueUnderscore = Messages._(IdContext.id("testid"), "testKeyWithArgs", 1, 2);
75     String JavaDoc valueDollar = Messages.$(IdContext.id("testid"), "testKeyWithArgs", 1, 2);
76     Assert.assertEquals(value, valueUnderscore, "Correct formating for _()");
77     Assert.assertEquals(value, valueDollar, "Correct formating for $()");
78    }
79
80   @Test
81   public void testSetBundleWithString() {
82     Messages.setBundle("test", "a.b");
83     Assert.assertTrue(Messages.getBundle(PackageContext.string("a.b")) != null, "Messages returns bundle");
84   }
85
86   @Test
87   public void testFormatWithContext() {
88     Messages.setLocale("en", "");
89     Messages.setBundle("test", IdContext.id("testid"));
90     String JavaDoc value = Messages.format(IdContext.id("testid"), "testKey");
91     Assert.assertEquals(value, "testEnValue", "Correct formating called");
92   }
93
94   @Test
95   public void testFormatWithContextShortcuts() {
96     Messages.setLocale("en", "");
97     Messages.setBundle("test", IdContext.id("testid"));
98     String JavaDoc value = Messages.format(IdContext.id("testid"), "testKey");
99     String JavaDoc valueUnderscore = Messages._(IdContext.id("testid"), "testKey");
100     String JavaDoc valueDollar = Messages._(IdContext.id("testid"), "testKey");
101     Assert.assertEquals(value, valueUnderscore, "Correct formating for _()");
102     Assert.assertEquals(value, valueDollar, "Correct formating for $()");
103   }
104
105   @Test
106   public void testGetLocale() {
107     Messages.setLocale("de", "de");
108     Assert.assertEquals(new Locale JavaDoc("de", "de"), Messages.getLocale(), "Messages returns correct locale");
109   }
110
111   @Test
112   public void test_WithContext() {
113     Messages.setLocale("en", "");
114     Messages.setBundle("test", IdContext.id("testid"));
115     String JavaDoc value = _(IdContext.id("testid"), "testKey");
116     Assert.assertEquals(value, "testEnValue", "Correct formating called");
117   }
118
119   @Test
120   public void test$WithContext() {
121     Messages.setLocale("en", "");
122     Messages.setBundle("test", IdContext.id("testid"));
123     String JavaDoc value = $(IdContext.id("testid"), "testKey");
124     Assert.assertEquals(value, "testEnValue", "Correct formating called");
125   }
126
127   @Test
128   public void testFormatWithOneArg() {
129     Messages.setLocale("en", "");
130     Messages.setBundle("test");
131     String JavaDoc value = Messages.format("testKeyOneArg", 1);
132     Assert.assertEquals(value, "1 testEnValue", "Correct formating called");
133   }
134
135   @Test
136   public void testFormatWithOneArgShortcuts() {
137     Messages.setLocale("en", "");
138     Messages.setBundle("test");
139     String JavaDoc value = Messages.format("testKeyOneArg", 1);
140     String JavaDoc valueUnderscore = Messages._("testKeyOneArg", 1);
141     String JavaDoc valueDollar = Messages.$("testKeyOneArg", 1);
142     Assert.assertEquals(value, valueUnderscore, "Correct formating for _()");
143     Assert.assertEquals(value, valueDollar, "Correct formating for $()");
144   }
145
146   @Test
147   public void testFormatWithOneArgAndContext() {
148     Messages.setLocale("en", "");
149     Messages.setBundle("test", IdContext.id("testid"));
150     String JavaDoc value = Messages.format(IdContext.id("testid"), "testKeyOneArg", 1);
151     Assert.assertEquals(value, "1 testEnValue", "Correct formating called");
152   }
153
154   @Test
155   public void testFormatWithOneArgAndContextShortcuts() {
156     Messages.setLocale("en", "");
157     Messages.setBundle("test", IdContext.id("testid"));
158     String JavaDoc value = Messages.format(IdContext.id("testid"), "testKeyOneArg", 1);
159     String JavaDoc valueUnderscore = Messages._(IdContext.id("testid"), "testKeyOneArg", 1);
160     String JavaDoc valueDollar = Messages.$(IdContext.id("testid"), "testKeyOneArg", 1);
161     Assert.assertEquals(value, valueUnderscore, "Correct formating for _()");
162     Assert.assertEquals(value, valueDollar, "Correct formating for $()");
163   }
164
165   @Test
166   public void testOneKeyShortcuts() {
167     Messages.setLocale("en", "");
168     Messages.setBundle("test");
169     String JavaDoc value = format("testKey");
170     String JavaDoc valueUnderscore = _("testKey");
171     String JavaDoc valueDollar = $("testKey");
172     Assert.assertEquals(value, valueUnderscore, "Correct formating for _()");
173     Assert.assertEquals(value, valueDollar, "Correct formating for $()");
174   }
175
176   @Test
177   public void test_() {
178     Messages.setLocale("en", "");
179     Messages.setBundle("test");
180     String JavaDoc value = _("testKey");
181     Assert.assertEquals(value, "testEnValue", "Correct key returned for _");
182   }
183
184   @Test
185   public void test$() {
186     Messages.setLocale("en", "");
187     Messages.setBundle("test");
188     String JavaDoc value = $("testKey");
189     Assert.assertEquals(value, "testEnValue", "Correct key returned for _");
190   }
191
192   @Test
193   public void testSetThreadLocale() {
194     Messages.setThreadLocale("de", "de");
195     Assert.assertEquals(Messages.getLocale(), new Locale JavaDoc("de", "de"), "setThreadLocale sets correct locale");
196   }
197 }
198
Popular Tags