KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cintoo > messages > TestMessageHandler


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 org.testng.annotations.*;
19 import org.testng.Assert;
20 import api.cintoo.messages.bundle.BundleManager;
21
22 import java.util.ResourceBundle JavaDoc;
23 import java.util.Locale JavaDoc;
24
25 import api.cintoo.messages.MessageHandler;
26
27 import static org.easymock.EasyMock.*;
28
29 public class TestMessageHandler {
30   private String JavaDoc threadFormat;
31   private MessageHandler messageHandler;
32   private BundleManager mockBundleManager;
33   private Locale JavaDoc en_locale;
34   private ResourceBundle JavaDoc test_en;
35
36   @Configuration(beforeTestMethod = true)
37   public void setUp() {
38
39     en_locale = new Locale JavaDoc("en", "");
40     test_en = ResourceBundle.getBundle("test", en_locale);
41
42     mockBundleManager = createMock(BundleManager.class);
43     messageHandler = new DefaultMessageHandler(mockBundleManager);
44
45     mockBundleManager.setBundle("test");
46     expect(mockBundleManager.getBundle(en_locale)).andReturn(test_en);
47   }
48
49   @Configuration(afterTestMethod = true)
50   public void tearDown() {
51     verify(mockBundleManager);
52   }
53
54   @Test
55   public void testLocaleBeforeBundle() {
56     replay(mockBundleManager);
57
58     messageHandler.setLocale("en", "");
59     messageHandler.setBundle("test");
60     String JavaDoc value = messageHandler.format("testKey");
61     Assert.assertEquals(value, "testEnValue", "Correct key returned when locale is set before cintoo.cintoo.bundle");
62   }
63
64   @Test
65   public void testThreadLocaleBeforeBundle() {
66     replay(mockBundleManager);
67
68     messageHandler.setThreadLocale("en", "");
69     messageHandler.setBundle("test");
70     String JavaDoc value = messageHandler.format("testKey");
71     Assert.assertEquals(value, "testEnValue", "Correct key returned when locale is set before cintoo.cintoo.bundle");
72   }
73
74   @Test
75   public void testBundleBeforeLocale() {
76     replay(mockBundleManager);
77
78     messageHandler.setBundle("test");
79     messageHandler.setLocale("en", "");
80     String JavaDoc value = messageHandler.format("testKey");
81     Assert.assertEquals(value, "testEnValue", "Correct key returned when cintoo.cintoo.bundle is set before locale");
82   }
83
84   @Test
85   public void testThreadBundleBeforeLocale() {
86     replay(mockBundleManager);
87
88     messageHandler.setBundle("test");
89     messageHandler.setThreadLocale("en", "");
90     String JavaDoc value = messageHandler.format("testKey");
91     Assert.assertEquals(value, "testEnValue", "Correct key returned when cintoo.cintoo.bundle is set before locale");
92   }
93
94   @Test
95   public void testSingletonLocale() {
96     replay(mockBundleManager);
97
98     messageHandler.setLocale("en", "");
99     messageHandler.setBundle("test");
100     Thread JavaDoc other = new Thread JavaDoc() {
101       public void run() {
102         threadFormat = messageHandler.format("testKey");
103       }
104     };
105     other.start();
106     try {
107       other.join();
108     } catch (InterruptedException JavaDoc e) {
109       //
110
}
111     Assert.assertEquals(threadFormat, "testEnValue", "Correct key returned in thread");
112   }
113
114   @Test
115   public void testThreadLocale() {
116     Locale JavaDoc de_locale = new Locale JavaDoc("de", "de");
117     ResourceBundle JavaDoc test_de = ResourceBundle.getBundle("test", de_locale);
118
119     expect(mockBundleManager.getBundle(de_locale)).andReturn(test_de);
120     replay(mockBundleManager);
121
122     messageHandler.setBundle("test");
123     messageHandler.setThreadLocale("de", "de");
124
125     Thread JavaDoc other = new Thread JavaDoc() {
126       public void run() {
127         messageHandler.setThreadLocale("en", "");
128         threadFormat = messageHandler.format("testKey");
129       }
130     };
131     other.start();
132     try {
133       other.join();
134     } catch (InterruptedException JavaDoc e) {
135       //
136
}
137
138     Assert.assertEquals(threadFormat, "testEnValue", "Correct key returned in second thread with ThreadLocal");
139     Assert.assertEquals(messageHandler.format("testKey"), "testValue", "Correct key returned in first thread with ThreadLocal");
140   }
141 }
142
Popular Tags