Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Fix timezone config handling (#4427)
Browse files Browse the repository at this point in the history
Signed-off-by: Henning Treu <henning.treu@telekom.de>
  • Loading branch information
htreu authored and kaikreuzer committed Oct 17, 2017
1 parent 22d37f2 commit e49df6c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 51 deletions.
Expand Up @@ -7,15 +7,18 @@
*/
package org.eclipse.smarthome.core.internal.i18n;

import static org.eclipse.smarthome.core.internal.i18n.I18nProviderImpl.*;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import java.time.ZoneId;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import org.eclipse.smarthome.core.library.types.PointType;
import org.junit.Before;
Expand All @@ -33,12 +36,6 @@
*/
public class I18nProviderImplTest {

private static final String CONFIG_LOCATION = "location";
private static final String CONFIG_LANGUAGE = "language";
private static final String CONFIG_SCRIPT = "script";
private static final String CONFIG_REGION = "region";
private static final String CONFIG_VARIANT = "variant";

private static final String LOCATION_ZERO = "0,0";
private static final String LOCATION_DARMSTADT = "49.876733,8.666809,1";
private static final String LOCATION_HAMBURG = "53.588231,9.920082,5";
Expand All @@ -55,6 +52,8 @@ public class I18nProviderImplTest {
private static final String VARIANT_DE = "1996";
private static final String VARIANT_RU = "";

private static final String TIMEZONE_GMT9 = "Etc/GMT-9";

// LocationProvider translationProvider;
I18nProviderImpl i18nProviderImpl;
ConfigurationAdmin configAdmin;
Expand All @@ -72,7 +71,7 @@ public class I18nProviderImplTest {
public void setup() {

initMocks(this);
buildInitialConfig();
initialConfig = buildInitialConfig();
when(componentContext.getProperties()).thenReturn(initialConfig);
when(componentContext.getBundleContext()).thenReturn(bundleContext);
when(bundleContext.getBundles()).thenReturn(new Bundle[] { bundle });
Expand All @@ -90,10 +89,11 @@ public void assertThatConfigurationWasSet() {

assertThat(location.toString(), is(LOCATION_ZERO));

assertThat(setLocale.getLanguage(), is(initialConfig.get(CONFIG_LANGUAGE)));
assertThat(setLocale.getScript(), is(initialConfig.get(CONFIG_SCRIPT)));
assertThat(setLocale.getCountry(), is(initialConfig.get(CONFIG_REGION)));
assertThat(setLocale.getVariant(), is(initialConfig.get(CONFIG_VARIANT)));
assertThat(setLocale.getLanguage(), is(initialConfig.get(LANGUAGE)));
assertThat(setLocale.getScript(), is(initialConfig.get(SCRIPT)));
assertThat(setLocale.getCountry(), is(initialConfig.get(REGION)));
assertThat(setLocale.getVariant(), is(initialConfig.get(VARIANT)));
assertThat(i18nProviderImpl.getTimeZone(), is(ZoneId.of(TIMEZONE_GMT9)));
}

@Test
Expand All @@ -104,13 +104,14 @@ public void assertThatDefaultLocaleWillBeUsed() {
Locale setLocale = i18nProviderImpl.getLocale();

assertNull(location);
assertThat(i18nProviderImpl.getTimeZone(), is(TimeZone.getDefault().toZoneId()));
assertThat(setLocale, is(Locale.getDefault()));
}

@Test
public void assertThatDefaultLocaleWillBeUsedAndLocationIsSet() {
Hashtable<String, Object> conf = new Hashtable<String, Object>();
conf.put(CONFIG_LOCATION, LOCATION_DARMSTADT);
Hashtable<String, Object> conf = new Hashtable<>();
conf.put(LOCATION, LOCATION_DARMSTADT);
i18nProviderImpl.modified(conf);

PointType location = i18nProviderImpl.getLocation();
Expand All @@ -129,10 +130,19 @@ public void assertThatActivateSetsLocaleAndLocation() {

assertThat(location.toString(), is(LOCATION_ZERO));

assertThat(setLocale.getLanguage(), is(initialConfig.get(CONFIG_LANGUAGE)));
assertThat(setLocale.getScript(), is(initialConfig.get(CONFIG_SCRIPT)));
assertThat(setLocale.getCountry(), is(initialConfig.get(CONFIG_REGION)));
assertThat(setLocale.getVariant(), is(initialConfig.get(CONFIG_VARIANT)));
assertThat(setLocale.getLanguage(), is(initialConfig.get(LANGUAGE)));
assertThat(setLocale.getScript(), is(initialConfig.get(SCRIPT)));
assertThat(setLocale.getCountry(), is(initialConfig.get(REGION)));
assertThat(setLocale.getVariant(), is(initialConfig.get(VARIANT)));
}

@Test
public void assertThatInvalidTimeZoneFallsbackToDefaultTimeZone() {
Hashtable<String, Object> conf = new Hashtable<>();
conf.put(TIMEZONE, "invalid");
i18nProviderImpl.modified(conf);

assertThat(i18nProviderImpl.getTimeZone(), is(TimeZone.getDefault().toZoneId()));
}

@Test
Expand All @@ -152,21 +162,25 @@ public void assertThatConfigurationChangeWorks() {
assertThat(setLocale.getVariant(), is(VARIANT_RU));
}

private void buildInitialConfig() {
initialConfig.put(CONFIG_LOCATION, LOCATION_ZERO);
initialConfig.put(CONFIG_LANGUAGE, LANGUAGE_DE);
initialConfig.put(CONFIG_SCRIPT, SCRIPT_DE);
initialConfig.put(CONFIG_REGION, REGION_DE);
initialConfig.put(CONFIG_VARIANT, VARIANT_DE);
private Dictionary<String, Object> buildInitialConfig() {
Dictionary<String, Object> conf = new Hashtable<>();
conf.put(LOCATION, LOCATION_ZERO);
conf.put(LANGUAGE, LANGUAGE_DE);
conf.put(SCRIPT, SCRIPT_DE);
conf.put(REGION, REGION_DE);
conf.put(VARIANT, VARIANT_DE);
conf.put(TIMEZONE, TIMEZONE_GMT9);

return conf;
}

private Hashtable<String, Object> buildRUConfig() {
Hashtable<String, Object> conf = new Hashtable<>();
conf.put(CONFIG_LOCATION, LOCATION_HAMBURG);
conf.put(CONFIG_LANGUAGE, LANGUAGE_RU);
conf.put(CONFIG_SCRIPT, SCRIPT_RU);
conf.put(CONFIG_REGION, REGION_RU);
conf.put(CONFIG_VARIANT, VARIANT_RU);
conf.put(LOCATION, LOCATION_HAMBURG);
conf.put(LANGUAGE, LANGUAGE_RU);
conf.put(SCRIPT, SCRIPT_RU);
conf.put(REGION, REGION_RU);
conf.put(VARIANT, VARIANT_RU);
return conf;
}

Expand Down
Expand Up @@ -17,9 +17,9 @@
public interface TimeZoneProvider {

/**
* Provides access to the time zone property.
* Gets the configured time zone as {@link ZoneId} or the system default time zone if not configured properly.
*
* @return the time zone set in the user interface and if there isn't one, the default time zone of the operating system
* @return the configured time zone as {@link ZoneId} or the system default time zone if not configured properly.
*/
ZoneId getTimeZone();
}
Expand Up @@ -8,6 +8,7 @@
package org.eclipse.smarthome.core.internal.i18n;

import java.text.MessageFormat;
import java.time.DateTimeException;
import java.time.ZoneId;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -58,21 +59,21 @@ public class I18nProviderImpl implements TranslationProvider, LocaleProvider, Lo
private final Logger logger = LoggerFactory.getLogger(getClass());

// LocaleProvider
private static final String LANGUAGE = "language";
private static final String SCRIPT = "script";
private static final String REGION = "region";
private static final String VARIANT = "variant";
static final String LANGUAGE = "language";
static final String SCRIPT = "script";
static final String REGION = "region";
static final String VARIANT = "variant";
private Locale locale;

// TranslationProvider
private ResourceBundleTracker resourceBundleTracker;

// LocationProvider
private static final String LOCATION = "location";
static final String LOCATION = "location";
private PointType location;

// TimeZoneProvider
private static final String TIMEZONE = "timezone";
static final String TIMEZONE = "timezone";
private ZoneId timeZone;

@Activate
Expand All @@ -91,19 +92,19 @@ protected void deactivate(ComponentContext componentContext) {

@Modified
protected synchronized void modified(Map<String, Object> config) {
final String language = (String) config.get(LANGUAGE);
final String script = (String) config.get(SCRIPT);
final String region = (String) config.get(REGION);
final String variant = (String) config.get(VARIANT);
final String location = (String) config.get(LOCATION);
final ZoneId timeZone = (ZoneId) config.get(TIMEZONE);

setTimeZone(timeZone);
final String language = toStringOrNull(config.get(LANGUAGE));
final String script = toStringOrNull(config.get(SCRIPT));
final String region = toStringOrNull(config.get(REGION));
final String variant = toStringOrNull(config.get(VARIANT));
final String location = toStringOrNull(config.get(LOCATION));
final String zoneId = toStringOrNull(config.get(TIMEZONE));

setTimeZone(zoneId);
setLocation(location);

if (StringUtils.isEmpty(language)) {
// at least the language must be defined otherwise the system default locale is used
logger.debug("No language set, fallback to default system locale");
logger.debug("No language set, falling back to the default locale");
locale = null;
return;
}
Expand Down Expand Up @@ -142,6 +143,10 @@ protected synchronized void modified(Map<String, Object> config) {
logger.info("Locale set to {}, Location set to {}, Time zone set to {}", locale, this.location, this.timeZone);
}

private String toStringOrNull(Object value) {
return value == null ? null : value.toString();
}

private void setLocation(final String location) {
if (location != null) {
try {
Expand All @@ -153,13 +158,17 @@ private void setLocation(final String location) {
}
}

private void setTimeZone(final ZoneId timeZone) {
if (timeZone != null) {
private void setTimeZone(final String zoneId) {
if (StringUtils.isBlank(zoneId)) {
timeZone = TimeZone.getDefault().toZoneId();
logger.debug("No time zone set, falling back to the default time zone '{}'.", timeZone.toString());
} else {
try {
this.timeZone = TimeZone.getTimeZone(timeZone).toZoneId();
} catch (IllegalArgumentException e) {
this.timeZone = TimeZone.getDefault().toZoneId();
logger.warn("Could not set a new time zone, the system's default time zone will be used ", timeZone, e.getMessage());
timeZone = ZoneId.of(zoneId);
} catch (DateTimeException e) {
timeZone = TimeZone.getDefault().toZoneId();
logger.warn("Error setting time zone '{}', falling back to the default time zone '{}': {}", zoneId,
timeZone.toString(), e.getMessage());
}
}
}
Expand Down

0 comments on commit e49df6c

Please sign in to comment.