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 static org.easymock.EasyMock.eq;
018: import static org.easymock.EasyMock.isA;
019:
020: import java.util.Collections;
021: import java.util.Map;
022:
023: import org.apache.tapestry.internal.test.InternalBaseTestCase;
024: import org.apache.tapestry.internal.util.Holder;
025: import org.apache.tapestry.services.ApplicationStateContribution;
026: import org.apache.tapestry.services.ApplicationStateCreator;
027: import org.apache.tapestry.services.ApplicationStateManager;
028: import org.apache.tapestry.services.ApplicationStatePersistenceStrategy;
029: import org.apache.tapestry.services.ApplicationStatePersistenceStrategySource;
030: import org.easymock.EasyMock;
031: import org.easymock.IAnswer;
032: import org.testng.annotations.Test;
033:
034: public class ApplicationStateManagerImplTest extends
035: InternalBaseTestCase {
036: @SuppressWarnings("unchecked")
037: @Test
038: public void get_from_configured_aso() {
039: String strategyName = "ethereal";
040: ApplicationStatePersistenceStrategy strategy = mockApplicationStatePersistenceStrategy();
041: ApplicationStatePersistenceStrategySource source = mockApplicationStatePersistenceStrategySource();
042: Class asoClass = ReadOnlyBean.class;
043: ApplicationStateCreator<ReadOnlyBean> creator = mockApplicationStateCreator();
044: ReadOnlyBean aso = new ReadOnlyBean();
045:
046: Map<Class, ApplicationStateContribution> configuration = Collections
047: .singletonMap(asoClass,
048: new ApplicationStateContribution(strategyName,
049: creator));
050:
051: train_get(source, strategyName, strategy);
052:
053: train_get(strategy, asoClass, creator, aso);
054:
055: replay();
056:
057: ApplicationStateManager manager = new ApplicationStateManagerImpl(
058: configuration, source);
059:
060: assertSame(manager.get(asoClass), aso);
061:
062: verify();
063: }
064:
065: @SuppressWarnings("unchecked")
066: @Test
067: public void check_exists_when_null() {
068: String strategyName = "ethereal";
069: ApplicationStatePersistenceStrategy strategy = mockApplicationStatePersistenceStrategy();
070: ApplicationStatePersistenceStrategySource source = mockApplicationStatePersistenceStrategySource();
071: Class asoClass = ReadOnlyBean.class;
072: ApplicationStateCreator<ReadOnlyBean> creator = mockApplicationStateCreator();
073:
074: Map<Class, ApplicationStateContribution> configuration = Collections
075: .singletonMap(asoClass,
076: new ApplicationStateContribution(strategyName,
077: creator));
078:
079: train_get(source, strategyName, strategy);
080: train_exists(strategy, asoClass, false);
081:
082: replay();
083:
084: ApplicationStateManager manager = new ApplicationStateManagerImpl(
085: configuration, source);
086:
087: assertFalse(manager.exists(asoClass));
088:
089: verify();
090: }
091:
092: @SuppressWarnings("unchecked")
093: @Test
094: public void check_exists_when_true() {
095: String strategyName = "ethereal";
096: ApplicationStatePersistenceStrategy strategy = mockApplicationStatePersistenceStrategy();
097: ApplicationStatePersistenceStrategySource source = mockApplicationStatePersistenceStrategySource();
098: Class asoClass = ReadOnlyBean.class;
099: ApplicationStateCreator<ReadOnlyBean> creator = mockApplicationStateCreator();
100:
101: Map<Class, ApplicationStateContribution> configuration = Collections
102: .singletonMap(asoClass,
103: new ApplicationStateContribution(strategyName,
104: creator));
105:
106: train_get(source, strategyName, strategy);
107: train_exists(strategy, asoClass, true);
108:
109: replay();
110:
111: ApplicationStateManager manager = new ApplicationStateManagerImpl(
112: configuration, source);
113:
114: assertTrue(manager.exists(asoClass));
115:
116: verify();
117: }
118:
119: @SuppressWarnings("unchecked")
120: @Test
121: public void set_configured_aso() {
122: String strategyName = "ethereal";
123: ApplicationStatePersistenceStrategy strategy = mockApplicationStatePersistenceStrategy();
124: ApplicationStatePersistenceStrategySource source = mockApplicationStatePersistenceStrategySource();
125: Class asoClass = ReadOnlyBean.class;
126: Object aso = new ReadOnlyBean();
127:
128: Map<Class, ApplicationStateContribution> configuration = Collections
129: .singletonMap(asoClass,
130: new ApplicationStateContribution(strategyName));
131:
132: train_get(source, strategyName, strategy);
133:
134: strategy.set(asoClass, aso);
135:
136: replay();
137:
138: ApplicationStateManager manager = new ApplicationStateManagerImpl(
139: configuration, source);
140:
141: manager.set(asoClass, aso);
142:
143: verify();
144: }
145:
146: @SuppressWarnings("unchecked")
147: @Test
148: public void get_from_unconfigured_aso() {
149: ApplicationStatePersistenceStrategy strategy = mockApplicationStatePersistenceStrategy();
150: ApplicationStatePersistenceStrategySource source = mockApplicationStatePersistenceStrategySource();
151: Class asoClass = ReadOnlyBean.class;
152: final Holder holder = new Holder();
153:
154: train_get(source, ApplicationStateManagerImpl.DEFAULT_STRATEGY,
155: strategy);
156:
157: IAnswer answer = new IAnswer() {
158: public Object answer() throws Throwable {
159: ApplicationStateCreator creator = (ApplicationStateCreator) EasyMock
160: .getCurrentArguments()[1];
161:
162: Object aso = creator.create();
163:
164: holder.put(aso);
165:
166: return aso;
167: }
168: };
169:
170: expect(
171: strategy.get(eq(asoClass),
172: isA(ApplicationStateCreator.class))).andAnswer(
173: answer);
174:
175: replay();
176:
177: Map<Class, ApplicationStateContribution> configuration = Collections
178: .emptyMap();
179:
180: ApplicationStateManager manager = new ApplicationStateManagerImpl(
181: configuration, source);
182:
183: Object actual = manager.get(asoClass);
184:
185: assertSame(actual, holder.get());
186:
187: verify();
188: }
189: }
|