1 15 package org.apache.tapestry.services.impl; 16 17 import java.util.Locale ; 18 19 import org.apache.hivemind.service.ThreadLocale; 20 import org.apache.hivemind.test.HiveMindTestCase; 21 import org.apache.tapestry.TapestryConstants; 22 import org.apache.tapestry.services.CookieSource; 23 import org.apache.tapestry.web.WebRequest; 24 import org.easymock.MockControl; 25 26 32 public class TestRequestLocaleManager extends HiveMindTestCase 33 { 34 private ThreadLocale newThreadLocale() 35 { 36 return (ThreadLocale) newMock(ThreadLocale.class); 37 } 38 39 private ThreadLocale newThreadLocale(Locale locale) 40 { 41 MockControl control = newControl(ThreadLocale.class); 42 ThreadLocale threadLocale = (ThreadLocale) control.getMock(); 43 44 threadLocale.getLocale(); 45 control.setReturnValue(locale); 46 47 return threadLocale; 48 } 49 50 public void testSuppliedByRequest() 51 { 52 MockControl sourceControl = newControl(CookieSource.class); 53 CookieSource source = (CookieSource) sourceControl.getMock(); 54 55 MockControl requestControl = newControl(WebRequest.class); 56 WebRequest request = (WebRequest) requestControl.getMock(); 57 58 ThreadLocale tl = newThreadLocale(); 59 60 62 source.readCookieValue(TapestryConstants.LOCALE_COOKIE_NAME); 63 sourceControl.setReturnValue(null); 64 65 request.getLocale(); 66 requestControl.setReturnValue(Locale.JAPANESE); 67 68 tl.setLocale(Locale.JAPANESE); 69 70 replayControls(); 71 72 RequestLocaleManagerImpl manager = new RequestLocaleManagerImpl(); 73 manager.setCookieSource(source); 74 manager.setRequest(request); 75 manager.setThreadLocale(tl); 76 77 Locale actual = manager.extractLocaleForCurrentRequest(); 78 79 assertEquals(Locale.JAPANESE, actual); 80 81 verifyControls(); 82 } 83 84 private void attempt(String localeName, Locale expectedLocale) 85 { 86 MockControl sourceControl = newControl(CookieSource.class); 87 CookieSource source = (CookieSource) sourceControl.getMock(); 88 89 ThreadLocale tl = newThreadLocale(); 90 91 93 source.readCookieValue(TapestryConstants.LOCALE_COOKIE_NAME); 94 sourceControl.setReturnValue(localeName); 95 96 tl.setLocale(expectedLocale); 97 98 replayControls(); 99 100 RequestLocaleManagerImpl manager = new RequestLocaleManagerImpl(); 101 manager.setCookieSource(source); 102 manager.setThreadLocale(tl); 103 104 Locale actual = manager.extractLocaleForCurrentRequest(); 105 106 assertEquals(expectedLocale, actual); 107 108 verifyControls(); 109 } 110 111 public void testJustLanguage() 112 { 113 attempt("en", Locale.ENGLISH); 114 } 115 116 public void testLanguageAndCountry() 117 { 118 attempt("fr_FR", Locale.FRANCE); 119 } 120 121 public void testWithVariant() 122 { 123 attempt("en_US_Bahstohn", new Locale ("en", "US", "Bahstohn")); 124 } 125 126 public void testPersist() 127 { 128 Locale locale = Locale.SIMPLIFIED_CHINESE; 129 130 CookieSource source = (CookieSource) newMock(CookieSource.class); 131 ThreadLocale threadLocale = newThreadLocale(locale); 132 133 135 source.writeCookieValue(TapestryConstants.LOCALE_COOKIE_NAME, locale.toString()); 136 137 replayControls(); 138 139 RequestLocaleManagerImpl m = new RequestLocaleManagerImpl(); 140 m.setCookieSource(source); 141 m.setThreadLocale(threadLocale); 142 143 m.persistLocale(); 144 145 verifyControls(); 146 } 147 148 public void testPersistNoChange() 149 { 150 MockControl sourceControl = newControl(CookieSource.class); 151 CookieSource source = (CookieSource) sourceControl.getMock(); 152 153 MockControl requestControl = newControl(WebRequest.class); 154 WebRequest request = (WebRequest) requestControl.getMock(); 155 156 MockControl tlc = newControl(ThreadLocale.class); 157 ThreadLocale tl = (ThreadLocale) tlc.getMock(); 158 159 161 source.readCookieValue(TapestryConstants.LOCALE_COOKIE_NAME); 162 sourceControl.setReturnValue(null); 163 164 request.getLocale(); 165 requestControl.setReturnValue(Locale.JAPANESE); 166 167 tl.setLocale(Locale.JAPANESE); 168 169 replayControls(); 170 171 RequestLocaleManagerImpl manager = new RequestLocaleManagerImpl(); 172 manager.setCookieSource(source); 173 manager.setRequest(request); 174 manager.setThreadLocale(tl); 175 176 Locale actual = manager.extractLocaleForCurrentRequest(); 177 178 assertEquals(Locale.JAPANESE, actual); 179 180 verifyControls(); 181 182 tl.getLocale(); 183 tlc.setReturnValue(Locale.JAPANESE); 184 185 replayControls(); 186 187 189 manager.persistLocale(); 190 191 verifyControls(); 192 } 193 194 public void testGetLocaleValuesAreCached() 195 { 196 RequestLocaleManagerImpl manager = new RequestLocaleManagerImpl(); 197 198 Locale l1 = manager.getLocale("en"); 199 Locale l2 = manager.getLocale("en"); 200 201 assertSame(l1, l2); 202 } 203 204 207 208 public void testFilterDisabled() 209 { 210 RequestLocaleManagerImpl manager = new RequestLocaleManagerImpl(); 211 212 Locale l = manager.filterRequestedLocale("en"); 213 214 assertEquals(Locale.ENGLISH, l); 215 } 216 217 220 221 public void testFilterEnabled() 222 { 223 RequestLocaleManagerImpl manager = new RequestLocaleManagerImpl(); 224 manager.setAcceptedLocales("en,fr"); 225 manager.initializeService(); 226 227 assertEquals(Locale.ENGLISH, manager.filterRequestedLocale("en")); 228 assertEquals(Locale.ENGLISH, manager.filterRequestedLocale("en_US")); 229 assertEquals(Locale.FRENCH, manager.filterRequestedLocale("fr")); 230 assertEquals(Locale.FRENCH, manager.filterRequestedLocale("fr_FR")); 231 232 234 assertEquals(Locale.ENGLISH, manager.filterRequestedLocale("foo_bar_BAZ")); 235 } 236 } | Popular Tags |