001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import java.io.UnsupportedEncodingException;
018:
019: import javax.servlet.http.HttpServletRequest;
020: import javax.servlet.http.HttpSession;
021:
022: import org.apache.tapestry.internal.test.InternalBaseTestCase;
023: import org.apache.tapestry.services.Request;
024: import org.apache.tapestry.services.Session;
025: import org.testng.annotations.Test;
026:
027: public class RequestImplTest extends InternalBaseTestCase {
028: @Test
029: public void get_session_doesnt_exist() {
030: HttpServletRequest sr = mockHttpServletRequest();
031:
032: train_getSession(sr, false, null);
033:
034: replay();
035:
036: Request request = new RequestImpl(sr);
037:
038: assertNull(request.getSession(false));
039:
040: verify();
041: }
042:
043: @Test
044: public void force_session_create() {
045: HttpServletRequest sr = mockHttpServletRequest();
046: HttpSession ss = mockHttpSession();
047:
048: train_getSession(sr, true, ss);
049:
050: train_getAttribute(ss, "foo", "bar");
051:
052: replay();
053:
054: Request request = new RequestImpl(sr);
055: Session session = request.getSession(true);
056:
057: assertEquals(session.getAttribute("foo"), "bar");
058:
059: verify();
060: }
061:
062: @Test
063: public void set_encoding_success() throws Exception {
064: HttpServletRequest sr = mockHttpServletRequest();
065:
066: String encoding = "the-encoding";
067:
068: sr.setCharacterEncoding(encoding);
069:
070: replay();
071:
072: new RequestImpl(sr).setEncoding(encoding);
073:
074: verify();
075: }
076:
077: @Test
078: public void set_encoding_failure() throws Exception {
079: HttpServletRequest sr = mockHttpServletRequest();
080:
081: String encoding = "the-encoding";
082: UnsupportedEncodingException exception = new UnsupportedEncodingException(
083: "Oops.");
084:
085: sr.setCharacterEncoding(encoding);
086: getMocksControl().andThrow(exception);
087:
088: replay();
089:
090: try {
091: new RequestImpl(sr).setEncoding(encoding);
092: unreachable();
093: } catch (RuntimeException ex) {
094: assertSame(ex.getCause(), exception);
095: }
096:
097: verify();
098:
099: }
100: }
|