org.springframework.context.i18n.LocaleContext源码实例Demo

类org.springframework.context.i18n.LocaleContext源码实例Demo

下面列出了org.springframework.context.i18n.LocaleContext 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Test
public void testLocaleAndTimeZone() {
    String value = LOCALE_CUSTOM + " " + TIMEZONE_CUSTOM.getID();
    Cookie cookie = new Cookie(DEFAULT_COOKIE_NAME, value);
    when(request.getCookies()).thenReturn(new Cookie[]{cookie});

    LocaleContext context = resolver.resolveLocaleContext(request);

    assertThat(context).isNotNull();
    assertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.class);
    Locale locale = ((TimeZoneAwareLocaleContext) context).getLocale();
    TimeZone zone = ((TimeZoneAwareLocaleContext) context).getTimeZone();
    assertThat(locale).isEqualTo(LOCALE_CUSTOM);
    assertThat(zone).isEqualTo(TIMEZONE_CUSTOM);

    List<Event> events = recorder.play();
    assertThat(events).isEmpty();
}
 
@Override
public LocaleContext resolveLocaleContext(final HttpServletRequest request) {
	parseLocaleCookieIfNecessary(request);
	return new TimeZoneAwareLocaleContext() {
		@Override
		@Nullable
		public Locale getLocale() {
			return (Locale) request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME);
		}
		@Override
		@Nullable
		public TimeZone getTimeZone() {
			return (TimeZone) request.getAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME);
		}
	};
}
 
@Override
public void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response,
		@Nullable LocaleContext localeContext) {

	Assert.notNull(response, "HttpServletResponse is required for CookieLocaleResolver");

	Locale locale = null;
	TimeZone timeZone = null;
	if (localeContext != null) {
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
		addCookie(response,
				(locale != null ? toLocaleValue(locale) : "-") + (timeZone != null ? '/' + timeZone.getID() : ""));
	}
	else {
		removeCookie(response);
	}
	request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
			(locale != null ? locale : determineDefaultLocale(request)));
	request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
			(timeZone != null ? timeZone : determineDefaultTimeZone(request)));
}
 
@Override
public LocaleContext resolveLocaleContext(final HttpServletRequest request) {
	return new TimeZoneAwareLocaleContext() {
		@Override
		public Locale getLocale() {
			Locale locale = (Locale) WebUtils.getSessionAttribute(request, localeAttributeName);
			if (locale == null) {
				locale = determineDefaultLocale(request);
			}
			return locale;
		}
		@Override
		@Nullable
		public TimeZone getTimeZone() {
			TimeZone timeZone = (TimeZone) WebUtils.getSessionAttribute(request, timeZoneAttributeName);
			if (timeZone == null) {
				timeZone = determineDefaultTimeZone(request);
			}
			return timeZone;
		}
	};
}
 
/**
 * Create a HttpPost for the given configuration.
 * <p>The default implementation creates a standard HttpPost with
 * "application/x-java-serialized-object" as "Content-Type" header.
 * @param config the HTTP invoker configuration that specifies the
 * target service
 * @return the HttpPost instance
 * @throws java.io.IOException if thrown by I/O methods
 */
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException {
	HttpPost httpPost = new HttpPost(config.getServiceUrl());

	RequestConfig requestConfig = createRequestConfig(config);
	if (requestConfig != null) {
		httpPost.setConfig(requestConfig);
	}

	LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
	if (localeContext != null) {
		Locale locale = localeContext.getLocale();
		if (locale != null) {
			httpPost.addHeader(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale));
		}
	}

	if (isAcceptGzipEncoding()) {
		httpPost.addHeader(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
	}

	return httpPost;
}
 
@Test
public void testResolveLocaleContextWithInvalidLocaleOnErrorDispatch() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.GERMAN);
	request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, new ServletException());
	Cookie cookie = new Cookie("LanguageKoekje", "++ GMT+1");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+2"));
	resolver.setCookieName("LanguageKoekje");
	LocaleContext loc = resolver.resolveLocaleContext(request);
	assertEquals(Locale.GERMAN, loc.getLocale());
	assertTrue(loc instanceof TimeZoneAwareLocaleContext);
	assertEquals(TimeZone.getTimeZone("GMT+2"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
 
源代码7 项目: jhipster   文件: AngularCookieLocaleResolver.java
/** {@inheritDoc} */
@Override
public LocaleContext resolveLocaleContext(final HttpServletRequest request) {
    parseAngularCookieIfNecessary(request);
    return new TimeZoneAwareLocaleContext() {
        @Override
        public Locale getLocale() {
            return (Locale) request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME);
        }

        @Override
        public TimeZone getTimeZone() {
            return (TimeZone) request.getAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME);
        }
    };
}
 
@Override
public LocaleContext resolveLocaleContext(final HttpServletRequest request) {
	return new TimeZoneAwareLocaleContext() {
		@Override
		public Locale getLocale() {
			Locale locale = (Locale) WebUtils.getSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME);
			if (locale == null) {
				locale = determineDefaultLocale(request);
			}
			return locale;
		}
		@Override
		public TimeZone getTimeZone() {
			TimeZone timeZone = (TimeZone) WebUtils.getSessionAttribute(request, TIME_ZONE_SESSION_ATTRIBUTE_NAME);
			if (timeZone == null) {
				timeZone = determineDefaultTimeZone(request);
			}
			return timeZone;
		}
	};
}
 
/**
 * Create a HttpPost for the given configuration.
 * <p>The default implementation creates a standard HttpPost with
 * "application/x-java-serialized-object" as "Content-Type" header.
 * @param config the HTTP invoker configuration that specifies the
 * target service
 * @return the HttpPost instance
 * @throws java.io.IOException if thrown by I/O methods
 */
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException {
	HttpPost httpPost = new HttpPost(config.getServiceUrl());

	RequestConfig requestConfig = createRequestConfig(config);
	if (requestConfig != null) {
		httpPost.setConfig(requestConfig);
	}

	LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
	if (localeContext != null) {
		Locale locale = localeContext.getLocale();
		if (locale != null) {
			httpPost.addHeader(HTTP_HEADER_ACCEPT_LANGUAGE, locale.toLanguageTag());
		}
	}

	if (isAcceptGzipEncoding()) {
		httpPost.addHeader(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
	}

	return httpPost;
}
 
@Override
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) {
	Locale locale = null;
	TimeZone timeZone = null;
	if (localeContext != null) {
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
		addCookie(response, (locale != null ? locale : "-") + (timeZone != null ? ' ' + timeZone.getID() : ""));
	}
	else {
		removeCookie(response);
	}
	request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
			(locale != null ? locale: determineDefaultLocale(request)));
	request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
			(timeZone != null ? timeZone : determineDefaultTimeZone(request)));
}
 
源代码11 项目: java-technology-stack   文件: RequestContext.java
public RequestContext(ServerWebExchange exchange, Map<String, Object> model, MessageSource messageSource,
		@Nullable RequestDataValueProcessor dataValueProcessor) {

	Assert.notNull(exchange, "ServerWebExchange is required");
	Assert.notNull(model, "Model is required");
	Assert.notNull(messageSource, "MessageSource is required");
	this.exchange = exchange;
	this.model = model;
	this.messageSource = messageSource;

	LocaleContext localeContext = exchange.getLocaleContext();
	Locale locale = localeContext.getLocale();
	this.locale = (locale != null ? locale : Locale.getDefault());
	TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext ?
			((TimeZoneAwareLocaleContext) localeContext).getTimeZone() : null);
	this.timeZone = (timeZone != null ? timeZone : TimeZone.getDefault());

	this.defaultHtmlEscape = null;  // TODO
	this.dataValueProcessor = dataValueProcessor;
}
 
/**
 * Prepare the given HTTP connection.
 * <p>The default implementation specifies POST as method,
 * "application/x-java-serialized-object" as "Content-Type" header,
 * and the given content length as "Content-Length" header.
 * @param connection the HTTP connection to prepare
 * @param contentLength the length of the content to send
 * @throws IOException if thrown by HttpURLConnection methods
 * @see java.net.HttpURLConnection#setRequestMethod
 * @see java.net.HttpURLConnection#setRequestProperty
 */
protected void prepareConnection(HttpURLConnection connection, int contentLength) throws IOException {
	if (this.connectTimeout >= 0) {
		connection.setConnectTimeout(this.connectTimeout);
	}
	if (this.readTimeout >= 0) {
		connection.setReadTimeout(this.readTimeout);
	}
	connection.setDoOutput(true);
	connection.setRequestMethod(HTTP_METHOD_POST);
	connection.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType());
	connection.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength));

	LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
	if (localeContext != null) {
		Locale locale = localeContext.getLocale();
		if (locale != null) {
			connection.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale));
		}
	}
	if (isAcceptGzipEncoding()) {
		connection.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
	}
}
 
@Override
public LocaleContext resolveLocaleContext(final HttpServletRequest request) {
	parseLocaleCookieIfNecessary(request);
	return new TimeZoneAwareLocaleContext() {
		@Override
		@Nullable
		public Locale getLocale() {
			return (Locale) request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME);
		}
		@Override
		@Nullable
		public TimeZone getTimeZone() {
			return (TimeZone) request.getAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME);
		}
	};
}
 
@Override
public LocaleContext resolveLocaleContext(final HttpServletRequest request) {
	return new TimeZoneAwareLocaleContext() {
		@Override
		public Locale getLocale() {
			Locale locale = (Locale) WebUtils.getSessionAttribute(request, localeAttributeName);
			if (locale == null) {
				locale = determineDefaultLocale(request);
			}
			return locale;
		}
		@Override
		@Nullable
		public TimeZone getTimeZone() {
			TimeZone timeZone = (TimeZone) WebUtils.getSessionAttribute(request, timeZoneAttributeName);
			if (timeZone == null) {
				timeZone = determineDefaultTimeZone(request);
			}
			return timeZone;
		}
	};
}
 
@Test
public void testResolveLocaleContextWithInvalidLocaleOnErrorDispatch() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.GERMAN);
	request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, new ServletException());
	Cookie cookie = new Cookie("LanguageKoekje", "++ GMT+1");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+2"));
	resolver.setCookieName("LanguageKoekje");
	LocaleContext loc = resolver.resolveLocaleContext(request);
	assertEquals(Locale.GERMAN, loc.getLocale());
	assertTrue(loc instanceof TimeZoneAwareLocaleContext);
	assertEquals(TimeZone.getTimeZone("GMT+2"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
 
源代码16 项目: jhipster   文件: AngularCookieLocaleResolverTest.java
@Test
public void testLocaleAndTimeZoneWithQuotes() {
    String value = resolver.quote(LOCALE_CUSTOM.toString() + " " + TIMEZONE_CUSTOM.getID());
    Cookie cookie = new Cookie(DEFAULT_COOKIE_NAME, value);
    when(request.getCookies()).thenReturn(new Cookie[]{cookie});

    LocaleContext context = resolver.resolveLocaleContext(request);

    assertThat(context).isNotNull();
    assertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.class);
    Locale locale = ((TimeZoneAwareLocaleContext) context).getLocale();
    TimeZone zone = ((TimeZoneAwareLocaleContext) context).getTimeZone();
    assertThat(locale).isEqualTo(LOCALE_CUSTOM);
    assertThat(zone).isEqualTo(TIMEZONE_CUSTOM);

    List<Event> events = recorder.play();
    assertThat(events).isEmpty();
}
 
/**
 * Create a HttpPost for the given configuration.
 * <p>The default implementation creates a standard HttpPost with
 * "application/x-java-serialized-object" as "Content-Type" header.
 * @param config the HTTP invoker configuration that specifies the
 * target service
 * @return the HttpPost instance
 * @throws java.io.IOException if thrown by I/O methods
 */
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException {
	HttpPost httpPost = new HttpPost(config.getServiceUrl());

	RequestConfig requestConfig = createRequestConfig(config);
	if (requestConfig != null) {
		httpPost.setConfig(requestConfig);
	}

	LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
	if (localeContext != null) {
		Locale locale = localeContext.getLocale();
		if (locale != null) {
			httpPost.addHeader(HTTP_HEADER_ACCEPT_LANGUAGE, locale.toLanguageTag());
		}
	}

	if (isAcceptGzipEncoding()) {
		httpPost.addHeader(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
	}

	return httpPost;
}
 
/**
 * Prepare the given HTTP connection.
 * <p>The default implementation specifies POST as method,
 * "application/x-java-serialized-object" as "Content-Type" header,
 * and the given content length as "Content-Length" header.
 * @param connection the HTTP connection to prepare
 * @param contentLength the length of the content to send
 * @throws IOException if thrown by HttpURLConnection methods
 * @see java.net.HttpURLConnection#setRequestMethod
 * @see java.net.HttpURLConnection#setRequestProperty
 */
protected void prepareConnection(HttpURLConnection connection, int contentLength) throws IOException {
	if (this.connectTimeout >= 0) {
		connection.setConnectTimeout(this.connectTimeout);
	}
	if (this.readTimeout >= 0) {
		connection.setReadTimeout(this.readTimeout);
	}

	connection.setDoOutput(true);
	connection.setRequestMethod(HTTP_METHOD_POST);
	connection.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType());
	connection.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength));

	LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
	if (localeContext != null) {
		Locale locale = localeContext.getLocale();
		if (locale != null) {
			connection.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, locale.toLanguageTag());
		}
	}

	if (isAcceptGzipEncoding()) {
		connection.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
	}
}
 
源代码19 项目: jhipster   文件: AngularCookieLocaleResolverTest.java
@Test
public void testPresets() {
    when(request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME)).thenReturn(LOCALE_DEFAULT);
    when(request.getAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME)).thenReturn(TIMEZONE_DEFAULT);

    LocaleContext context = resolver.resolveLocaleContext(request);

    assertThat(context).isNotNull();
    assertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.class);
    Locale locale = ((TimeZoneAwareLocaleContext) context).getLocale();
    TimeZone zone = ((TimeZoneAwareLocaleContext) context).getTimeZone();

    assertThat(locale).isNotNull();
    assertThat(locale).isEqualTo(LOCALE_DEFAULT);
    assertThat(zone).isEqualTo(TIMEZONE_DEFAULT);

    List<Event> events = recorder.play();
    assertThat(events).isEmpty();
}
 
源代码20 项目: lams   文件: SimpleHttpInvokerRequestExecutor.java
/**
 * Prepare the given HTTP connection.
 * <p>The default implementation specifies POST as method,
 * "application/x-java-serialized-object" as "Content-Type" header,
 * and the given content length as "Content-Length" header.
 * @param connection the HTTP connection to prepare
 * @param contentLength the length of the content to send
 * @throws IOException if thrown by HttpURLConnection methods
 * @see java.net.HttpURLConnection#setRequestMethod
 * @see java.net.HttpURLConnection#setRequestProperty
 */
protected void prepareConnection(HttpURLConnection connection, int contentLength) throws IOException {
	if (this.connectTimeout >= 0) {
		connection.setConnectTimeout(this.connectTimeout);
	}
	if (this.readTimeout >= 0) {
		connection.setReadTimeout(this.readTimeout);
	}

	connection.setDoOutput(true);
	connection.setRequestMethod(HTTP_METHOD_POST);
	connection.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType());
	connection.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength));

	LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
	if (localeContext != null) {
		Locale locale = localeContext.getLocale();
		if (locale != null) {
			connection.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale));
		}
	}

	if (isAcceptGzipEncoding()) {
		connection.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
	}
}
 
@Nullable
private TimeZone getTimeZone(LocaleContext localeContext) {
	TimeZone timeZone = null;
	if (localeContext instanceof TimeZoneAwareLocaleContext) {
		timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
	}
	return timeZone;
}
 
源代码22 项目: spring4-understanding   文件: FrameworkServlet.java
private void resetContextHolders(HttpServletRequest request,
		LocaleContext prevLocaleContext, RequestAttributes previousAttributes) {

	LocaleContextHolder.setLocaleContext(prevLocaleContext, this.threadContextInheritable);
	RequestContextHolder.setRequestAttributes(previousAttributes, this.threadContextInheritable);
	if (logger.isTraceEnabled()) {
		logger.trace("Cleared thread-bound request context: " + request);
	}
}
 
源代码23 项目: spring-analysis-note   文件: FrameworkServlet.java
private void initContextHolders(HttpServletRequest request,
		@Nullable LocaleContext localeContext, @Nullable RequestAttributes requestAttributes) {

	if (localeContext != null) {
		LocaleContextHolder.setLocaleContext(localeContext, this.threadContextInheritable);
	}
	if (requestAttributes != null) {
		RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
	}
}
 
源代码24 项目: spring-analysis-note   文件: DispatcherServlet.java
/**
 * Build a LocaleContext for the given request, exposing the request's primary locale as current locale.
 * <p>The default implementation uses the dispatcher's LocaleResolver to obtain the current locale,
 * which might change during a request.
 * @param request current HTTP request
 * @return the corresponding LocaleContext
 */
@Override
protected LocaleContext buildLocaleContext(final HttpServletRequest request) {
	LocaleResolver lr = this.localeResolver;
	if (lr instanceof LocaleContextResolver) {
		return ((LocaleContextResolver) lr).resolveLocaleContext(request);
	}
	else {
		return () -> (lr != null ? lr.resolveLocale(request) : request.getLocale());
	}
}
 
源代码25 项目: lams   文件: SessionLocaleResolver.java
@Override
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) {
	Locale locale = null;
	TimeZone timeZone = null;
	if (localeContext != null) {
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	WebUtils.setSessionAttribute(request, this.localeAttributeName, locale);
	WebUtils.setSessionAttribute(request, this.timeZoneAttributeName, timeZone);
}
 
源代码26 项目: spring-analysis-note   文件: FixedLocaleResolver.java
@Override
public LocaleContext resolveLocaleContext(HttpServletRequest request) {
	return new TimeZoneAwareLocaleContext() {
		@Override
		@Nullable
		public Locale getLocale() {
			return getDefaultLocale();
		}
		@Override
		public TimeZone getTimeZone() {
			return getDefaultTimeZone();
		}
	};
}
 
@Test
public void testResolveLocaleContext() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	Cookie cookie = new Cookie("LanguageKoekje", "nl");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setCookieName("LanguageKoekje");
	LocaleContext loc = resolver.resolveLocaleContext(request);
	assertEquals("nl", loc.getLocale().getLanguage());
	assertTrue(loc instanceof TimeZoneAwareLocaleContext);
	assertNull(((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
 
@Test
public void testResolveLocaleContextWithTimeZone() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	Cookie cookie = new Cookie("LanguageKoekje", "nl GMT+1");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setCookieName("LanguageKoekje");
	LocaleContext loc = resolver.resolveLocaleContext(request);
	assertEquals("nl", loc.getLocale().getLanguage());
	assertTrue(loc instanceof TimeZoneAwareLocaleContext);
	assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
 
@Test
public void testResolveLocaleContextWithoutCookieAndDefaultLocale() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setDefaultLocale(Locale.GERMAN);
	resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+1"));

	LocaleContext loc = resolver.resolveLocaleContext(request);
	assertEquals(Locale.GERMAN, loc.getLocale());
	assertTrue(loc instanceof TimeZoneAwareLocaleContext);
	assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
 
@Test
public void testResolveLocaleContextWithCookieWithoutLocale() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);
	Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, "");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();

	LocaleContext loc = resolver.resolveLocaleContext(request);
	assertEquals(request.getLocale(), loc.getLocale());
	assertTrue(loc instanceof TimeZoneAwareLocaleContext);
	assertNull(((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
 
源代码评论
动弹
沙发等你来抢
 类方法
 同包方法