1 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 ; 23 import java.util.Locale ; 24 25 import api.cintoo.messages.MessageHandler; 26 27 import static org.easymock.EasyMock.*; 28 29 public class TestMessageHandler { 30 private String threadFormat; 31 private MessageHandler messageHandler; 32 private BundleManager mockBundleManager; 33 private Locale en_locale; 34 private ResourceBundle test_en; 35 36 @Configuration(beforeTestMethod = true) 37 public void setUp() { 38 39 en_locale = new Locale ("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 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 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 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 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 other = new Thread () { 101 public void run() { 102 threadFormat = messageHandler.format("testKey"); 103 } 104 }; 105 other.start(); 106 try { 107 other.join(); 108 } catch (InterruptedException e) { 109 } 111 Assert.assertEquals(threadFormat, "testEnValue", "Correct key returned in thread"); 112 } 113 114 @Test 115 public void testThreadLocale() { 116 Locale de_locale = new Locale ("de", "de"); 117 ResourceBundle 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 other = new Thread () { 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 e) { 135 } 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 |