1 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 ; 26 import java.util.Locale ; 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 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 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 value = Messages.format("testKeyWithArgs", 1, 2); 55 String valueUnderscore = Messages._("testKeyWithArgs", 1, 2); 56 String 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 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 value = Messages.format(IdContext.id("testid"), "testKeyWithArgs", 1, 2); 74 String valueUnderscore = Messages._(IdContext.id("testid"), "testKeyWithArgs", 1, 2); 75 String 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 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 value = Messages.format(IdContext.id("testid"), "testKey"); 99 String valueUnderscore = Messages._(IdContext.id("testid"), "testKey"); 100 String 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 ("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 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 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 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 value = Messages.format("testKeyOneArg", 1); 140 String valueUnderscore = Messages._("testKeyOneArg", 1); 141 String 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 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 value = Messages.format(IdContext.id("testid"), "testKeyOneArg", 1); 159 String valueUnderscore = Messages._(IdContext.id("testid"), "testKeyOneArg", 1); 160 String 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 value = format("testKey"); 170 String valueUnderscore = _("testKey"); 171 String 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 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 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 ("de", "de"), "setThreadLocale sets correct locale"); 196 } 197 } 198 | Popular Tags |