Re: Wickettester change locale of request

2009-10-16 Thread Per Newgro

Thanks Pieter,

right in place i found the same solution :-).

Cause of our problem is that creation of WicketTester is not following a 
(maybe common-base)
wicket rule: "Let the user override defaults by providing protected 
creation methods" e.g. newRequest().


All is done in MockWebApplication Constructor by using new Constructors 
for the
MockHttpServletRequest. A second barrier is that the 
MockHttpServletRequest only provides
default initialization of header attributes by usage of 
setDefaultHeaders which is private.
Another way of providing locales in http headers would be if 
MockHttpServletRequest.addHeader(String, String)
would exchange values or even better provide a setHeader(String, String) 
which is implemented
that way. But there is still the problem with unaccessible instance 
creation of requests.


So the solution for me would be:
1. add method to MockWebApplication
protected MockHttpServletRequest newMockHttpServletRequest(Application 
application, ServletSession servletSession, ServletContext context) {

 return new MockHttpServletRequest(application, servletSession, context);
}
2. call new Method in Constructor
servletRequest = newMockHttpServletRequest(this.application, 
servletSession, context);

3. make setDefaultHeaders() in MockHttpServletRequest protected.

But the third change could have drawbacks i don't see now :-).

Cheers
Per

-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: Wickettester change locale of request

2009-10-16 Thread Pieter Degraeuwe
Hi,

Ik had the same problem. I digged in the code and found that the Mock
implementation of the request used -as you say- the default Locale.
Unfortunately, there was no way to set de Locale on that mock request...

I ended up to set the default locale using Locale.setDefault(getLocale())
when I create the WicketTester instance. (getLocale() can then be overridden
by concerned Test)
This might not be the most elegant solution, but it certainly does its
job

On Thu, Oct 15, 2009 at 1:45 PM, Per Newgro  wrote:

> Hi
>
> i would like to test my code on new session creation in application.
> A task in this process is to validate the resulting session locale.
> Because the session locale will be taken from the request i have to provide
> my test value in the Wickettester.servletRequest attribute.
>
> But how can i set it manually to avoid usage of system default locale?
>
> MyApplication
> public Session newSession(Request request, Response response) {
>  MySession session = new MySession(request);
>  if (isInvalid(session.getLocale())) {
>session.setLocale(Locale.ENGLISH);
>  }
>  return session;
> }
>
> private boolean isInvalid(Locale locale) {
>  return isUndefined(new Locale(locale.getLanguage()))
>  && isUndefined(locale);
> }
>
> private boolean isUndefined(Locale locale) {
>  return !_locales.contains(locale);
> }
>
> MyApplicationTest
>
> @Test
> public void invalidLanguageLocale() {
>  assertEquals(Locale.CHINESE,
>prepareSession(toHtmlHeaderString(Locale.ENGLISH)).getLocale());
> }
>
> private String toHtmlHeaderString(Locale locale) {
>  String result = locale.getLanguage();
>  if (locale.getCountry() != null
>   && locale.getCountry().trim().length() > 0) {
>result = result + "-" + locale.getCountry();
>  }
>  return result;
> }
>
> private Session prepareSession(String locale) {
>  _tester.setupRequestAndResponse();
>  MockHttpServletRequest request = _tester.getServletRequest();
>  request.addHeader("Accept-Language", locale);
>  Session session = (Session)
>_testee.newSession(_tester.getWicketRequest(),
>_tester.getWicketResponse());
>  return session;
> }
>
> --
> GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
> Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
>
> -
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>


-- 
Pieter Degraeuwe
Systemworks bvba
Belgiëlaan 61
9070 Destelbergen
GSM: +32 (0)485/68.60.85
Email: pieter.degrae...@systemworks.be
visit us at http://www.systemworks.be


Wickettester change locale of request

2009-10-15 Thread Per Newgro
Hi

i would like to test my code on new session creation in application.
A task in this process is to validate the resulting session locale.
Because the session locale will be taken from the request i have to provide my 
test value in the Wickettester.servletRequest attribute.

But how can i set it manually to avoid usage of system default locale?

MyApplication
public Session newSession(Request request, Response response) {
  MySession session = new MySession(request);
  if (isInvalid(session.getLocale())) {
session.setLocale(Locale.ENGLISH);
  }
  return session;
}

private boolean isInvalid(Locale locale) {
  return isUndefined(new Locale(locale.getLanguage())) 
  && isUndefined(locale);
}

private boolean isUndefined(Locale locale) {
  return !_locales.contains(locale);
}

MyApplicationTest

@Test
public void invalidLanguageLocale() {
  assertEquals(Locale.CHINESE, 
prepareSession(toHtmlHeaderString(Locale.ENGLISH)).getLocale());
}

private String toHtmlHeaderString(Locale locale) {
  String result = locale.getLanguage();
  if (locale.getCountry() != null 
   && locale.getCountry().trim().length() > 0) {
result = result + "-" + locale.getCountry();
  }
  return result;
}

private Session prepareSession(String locale) {
  _tester.setupRequestAndResponse();
  MockHttpServletRequest request = _tester.getServletRequest();
  request.addHeader("Accept-Language", locale);
  Session session = (Session) 
_testee.newSession(_tester.getWicketRequest(), 
_tester.getWicketResponse());
  return session;
}

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org