001: // Copyright 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 org.apache.tapestry.internal.test.InternalBaseTestCase;
018: import org.apache.tapestry.services.ApplicationStateCreator;
019: import org.apache.tapestry.services.ApplicationStatePersistenceStrategy;
020: import org.apache.tapestry.services.Request;
021: import org.apache.tapestry.services.Session;
022: import org.testng.annotations.Test;
023:
024: public class SessionApplicationStatePersistenceStrategyTest extends
025: InternalBaseTestCase {
026: @SuppressWarnings("unchecked")
027: @Test
028: public void get_aso_already_exists() {
029: Request request = mockRequest();
030: Session session = mockSession();
031: Class asoClass = ReadOnlyBean.class;
032: Object aso = new ReadOnlyBean();
033: String key = "aso:" + asoClass.getName();
034: ApplicationStateCreator creator = mockApplicationStateCreator();
035:
036: train_getSession(request, true, session);
037: train_getAttribute(session, key, aso);
038:
039: replay();
040:
041: ApplicationStatePersistenceStrategy strategy = new SessionApplicationStatePersistenceStrategy(
042: request);
043:
044: assertSame(strategy.get(asoClass, creator), aso);
045:
046: verify();
047: }
048:
049: @SuppressWarnings("unchecked")
050: @Test
051: public void check_exists_does_not_create_session() {
052: Request request = mockRequest();
053: Class asoClass = ReadOnlyBean.class;
054:
055: train_getSession(request, false, null);
056:
057: replay();
058:
059: ApplicationStatePersistenceStrategy strategy = new SessionApplicationStatePersistenceStrategy(
060: request);
061:
062: assertFalse(strategy.exists(asoClass));
063:
064: verify();
065: }
066:
067: @SuppressWarnings("unchecked")
068: @Test
069: public void get_aso_needs_to_be_created() {
070: Request request = mockRequest();
071: Session session = mockSession();
072: Class asoClass = ReadOnlyBean.class;
073: Object aso = new ReadOnlyBean();
074: String key = "aso:" + asoClass.getName();
075: ApplicationStateCreator creator = mockApplicationStateCreator();
076:
077: // First for exists()
078: train_getSession(request, false, session);
079: train_getAttribute(session, key, null);
080:
081: // Second for get()
082: train_getSession(request, true, session);
083: train_getAttribute(session, key, null);
084:
085: train_create(creator, aso);
086:
087: session.setAttribute(key, aso);
088:
089: // Then for exists() after
090: train_getSession(request, false, session);
091: train_getAttribute(session, key, aso);
092:
093: replay();
094:
095: ApplicationStatePersistenceStrategy strategy = new SessionApplicationStatePersistenceStrategy(
096: request);
097:
098: assertFalse(strategy.exists(asoClass));
099:
100: assertSame(strategy.get(asoClass, creator), aso);
101:
102: assertTrue(strategy.exists(asoClass));
103:
104: verify();
105: }
106:
107: @SuppressWarnings("unchecked")
108: @Test
109: public void set_aso() {
110: Request request = mockRequest();
111: Session session = mockSession();
112: Class asoClass = ReadOnlyBean.class;
113: Object aso = new ReadOnlyBean();
114: String key = "aso:" + asoClass.getName();
115:
116: train_getSession(request, true, session);
117: session.setAttribute(key, aso);
118:
119: replay();
120:
121: ApplicationStatePersistenceStrategy strategy = new SessionApplicationStatePersistenceStrategy(
122: request);
123:
124: strategy.set(asoClass, aso);
125:
126: verify();
127: }
128: }
|