|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 the original author or authors. |
| 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 | + |
| 17 | +package org.springframework.context.i18n; |
| 18 | + |
| 19 | +import java.util.Locale; |
| 20 | +import java.util.TimeZone; |
| 21 | + |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import static org.junit.Assert.*; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Juergen Hoeller |
| 28 | + */ |
| 29 | +public class LocaleContextHolderTests { |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testSetLocaleContext() { |
| 33 | + LocaleContext lc = new SimpleLocaleContext(Locale.GERMAN); |
| 34 | + LocaleContextHolder.setLocaleContext(lc); |
| 35 | + assertSame(lc, LocaleContextHolder.getLocaleContext()); |
| 36 | + assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); |
| 37 | + assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); |
| 38 | + |
| 39 | + lc = new SimpleLocaleContext(Locale.GERMANY); |
| 40 | + LocaleContextHolder.setLocaleContext(lc); |
| 41 | + assertSame(lc, LocaleContextHolder.getLocaleContext()); |
| 42 | + assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); |
| 43 | + assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); |
| 44 | + |
| 45 | + LocaleContextHolder.resetLocaleContext(); |
| 46 | + assertNull(LocaleContextHolder.getLocaleContext()); |
| 47 | + assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); |
| 48 | + assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testSetTimeZoneAwareLocaleContext() { |
| 53 | + LocaleContext lc = new SimpleTimeZoneAwareLocaleContext(Locale.GERMANY, TimeZone.getTimeZone("GMT+1")); |
| 54 | + LocaleContextHolder.setLocaleContext(lc); |
| 55 | + assertSame(lc, LocaleContextHolder.getLocaleContext()); |
| 56 | + assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); |
| 57 | + assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); |
| 58 | + |
| 59 | + LocaleContextHolder.resetLocaleContext(); |
| 60 | + assertNull(LocaleContextHolder.getLocaleContext()); |
| 61 | + assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); |
| 62 | + assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testSetLocale() { |
| 67 | + LocaleContextHolder.setLocale(Locale.GERMAN); |
| 68 | + assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); |
| 69 | + assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); |
| 70 | + assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); |
| 71 | + assertEquals(Locale.GERMAN, LocaleContextHolder.getLocaleContext().getLocale()); |
| 72 | + |
| 73 | + LocaleContextHolder.setLocale(Locale.GERMANY); |
| 74 | + assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); |
| 75 | + assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); |
| 76 | + assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); |
| 77 | + assertEquals(Locale.GERMANY, LocaleContextHolder.getLocaleContext().getLocale()); |
| 78 | + |
| 79 | + LocaleContextHolder.setLocale(null); |
| 80 | + assertNull(LocaleContextHolder.getLocaleContext()); |
| 81 | + assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); |
| 82 | + assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testSetTimeZone() { |
| 87 | + LocaleContextHolder.setTimeZone(TimeZone.getTimeZone("GMT+1")); |
| 88 | + assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); |
| 89 | + assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); |
| 90 | + assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); |
| 91 | + assertNull(LocaleContextHolder.getLocaleContext().getLocale()); |
| 92 | + assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); |
| 93 | + |
| 94 | + LocaleContextHolder.setTimeZone(TimeZone.getTimeZone("GMT+2")); |
| 95 | + assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); |
| 96 | + assertEquals(TimeZone.getTimeZone("GMT+2"), LocaleContextHolder.getTimeZone()); |
| 97 | + assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); |
| 98 | + assertNull(LocaleContextHolder.getLocaleContext().getLocale()); |
| 99 | + assertEquals(TimeZone.getTimeZone("GMT+2"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); |
| 100 | + |
| 101 | + LocaleContextHolder.setTimeZone(null); |
| 102 | + assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); |
| 103 | + assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); |
| 104 | + assertNull(LocaleContextHolder.getLocaleContext()); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testSetLocaleAndSetTimeZoneMixed() { |
| 109 | + LocaleContextHolder.setLocale(Locale.GERMANY); |
| 110 | + assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); |
| 111 | + assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); |
| 112 | + assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); |
| 113 | + assertEquals(Locale.GERMANY, LocaleContextHolder.getLocaleContext().getLocale()); |
| 114 | + |
| 115 | + LocaleContextHolder.setTimeZone(TimeZone.getTimeZone("GMT+1")); |
| 116 | + assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); |
| 117 | + assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); |
| 118 | + assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); |
| 119 | + assertEquals(Locale.GERMANY, LocaleContextHolder.getLocaleContext().getLocale()); |
| 120 | + assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); |
| 121 | + |
| 122 | + LocaleContextHolder.setLocale(Locale.GERMAN); |
| 123 | + assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); |
| 124 | + assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); |
| 125 | + assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); |
| 126 | + assertEquals(Locale.GERMAN, LocaleContextHolder.getLocaleContext().getLocale()); |
| 127 | + assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); |
| 128 | + |
| 129 | + LocaleContextHolder.setTimeZone(null); |
| 130 | + assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); |
| 131 | + assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); |
| 132 | + assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); |
| 133 | + assertEquals(Locale.GERMAN, LocaleContextHolder.getLocaleContext().getLocale()); |
| 134 | + |
| 135 | + LocaleContextHolder.setTimeZone(TimeZone.getTimeZone("GMT+2")); |
| 136 | + assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); |
| 137 | + assertEquals(TimeZone.getTimeZone("GMT+2"), LocaleContextHolder.getTimeZone()); |
| 138 | + assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); |
| 139 | + assertEquals(Locale.GERMAN, LocaleContextHolder.getLocaleContext().getLocale()); |
| 140 | + assertEquals(TimeZone.getTimeZone("GMT+2"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); |
| 141 | + |
| 142 | + LocaleContextHolder.setLocale(null); |
| 143 | + assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); |
| 144 | + assertEquals(TimeZone.getTimeZone("GMT+2"), LocaleContextHolder.getTimeZone()); |
| 145 | + assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); |
| 146 | + assertNull(LocaleContextHolder.getLocaleContext().getLocale()); |
| 147 | + assertEquals(TimeZone.getTimeZone("GMT+2"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); |
| 148 | + |
| 149 | + LocaleContextHolder.setTimeZone(null); |
| 150 | + assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); |
| 151 | + assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); |
| 152 | + assertNull(LocaleContextHolder.getLocaleContext()); |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments