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 static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
018: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
019:
020: import java.util.Collection;
021: import java.util.Map;
022:
023: import org.apache.tapestry.ComponentResources;
024: import org.apache.tapestry.internal.test.InternalBaseTestCase;
025: import org.apache.tapestry.model.ComponentModel;
026: import org.apache.tapestry.services.MetaDataLocator;
027: import org.apache.tapestry.services.PersistentFieldBundle;
028: import org.apache.tapestry.services.PersistentFieldChange;
029: import org.apache.tapestry.services.PersistentFieldManager;
030: import org.apache.tapestry.services.PersistentFieldStrategy;
031: import org.testng.annotations.Test;
032:
033: public class PersistentFieldManagerImplTest extends
034: InternalBaseTestCase {
035: @Test
036: public void post_change_with_unknown_strategy() {
037: String fieldName = "field";
038:
039: PersistentFieldStrategy strat1 = newPersistentFieldStrategy();
040: PersistentFieldStrategy strat2 = newPersistentFieldStrategy();
041: ComponentResources resources = mockComponentResources();
042: ComponentModel model = mockComponentModel();
043:
044: Map<String, PersistentFieldStrategy> strategies = newMap();
045: strategies.put("foo", strat1);
046: strategies.put("bar", strat2);
047:
048: train_getComponentModel(resources, model);
049:
050: train_getFieldPersistenceStrategy(model, fieldName,
051: "braveheart");
052:
053: replay();
054:
055: PersistentFieldManager manager = new PersistentFieldManagerImpl(
056: null, strategies);
057:
058: try {
059: manager.postChange("foo.Bar", resources, fieldName, null);
060: unreachable();
061: } catch (RuntimeException ex) {
062: assertEquals(
063: ex.getMessage(),
064: "\'braveheart\' is not a defined persistent strategy. Defined stategies: bar, foo.");
065: }
066:
067: verify();
068: }
069:
070: @Test
071: public void post_change() {
072: String pageName = "foo.Bar";
073: String nestedId = "nested";
074: String fieldName = "field";
075: String strategyName = "foo";
076:
077: ComponentResources resources = mockComponentResources();
078: ComponentModel model = mockComponentModel();
079: PersistentFieldStrategy strat = newPersistentFieldStrategy();
080: Object value = new Object();
081:
082: Map<String, PersistentFieldStrategy> strategies = newMap();
083: strategies.put(strategyName, strat);
084:
085: train_getComponentModel(resources, model);
086:
087: train_getFieldPersistenceStrategy(model, fieldName,
088: strategyName);
089:
090: train_getNestedId(resources, nestedId);
091:
092: strat.postChange(pageName, nestedId, fieldName, value);
093:
094: replay();
095:
096: PersistentFieldManager manager = new PersistentFieldManagerImpl(
097: null, strategies);
098:
099: manager.postChange(pageName, resources, fieldName, value);
100:
101: verify();
102: }
103:
104: public void strategy_name_is_case_insensitive() {
105: String pageName = "foo.Bar";
106: String nestedId = "nested";
107: String fieldName = "field";
108: String strategyName = "FOO";
109:
110: ComponentResources resources = mockComponentResources();
111: ComponentModel model = mockComponentModel();
112: PersistentFieldStrategy strat = newPersistentFieldStrategy();
113: Object value = new Object();
114:
115: Map<String, PersistentFieldStrategy> strategies = newMap();
116: strategies.put("foo", strat);
117:
118: train_getComponentModel(resources, model);
119:
120: train_getFieldPersistenceStrategy(model, fieldName,
121: strategyName);
122:
123: train_getNestedId(resources, nestedId);
124:
125: strat.postChange(pageName, nestedId, fieldName, value);
126:
127: replay();
128:
129: PersistentFieldManager manager = new PersistentFieldManagerImpl(
130: null, strategies);
131:
132: manager.postChange(pageName, resources, fieldName, value);
133:
134: verify();
135: }
136:
137: @Test
138: public void post_change_strategy_by_meta_data() {
139: String pageName = "foo.Bar";
140: String nestedId = "nested";
141: String fieldName = "field";
142: String strategyName = "foo";
143:
144: ComponentResources resources = mockComponentResources();
145: ComponentModel model = mockComponentModel();
146: PersistentFieldStrategy strat = newPersistentFieldStrategy();
147: MetaDataLocator locator = newMetaDataLocator();
148:
149: Object value = new Object();
150:
151: Map<String, PersistentFieldStrategy> strategies = newMap();
152: strategies.put(strategyName, strat);
153:
154: train_getComponentModel(resources, model);
155:
156: train_getFieldPersistenceStrategy(model, fieldName, "");
157:
158: train_findMeta(locator, PersistentFieldManagerImpl.META_KEY,
159: resources, strategyName);
160:
161: train_getNestedId(resources, nestedId);
162:
163: strat.postChange(pageName, nestedId, fieldName, value);
164:
165: replay();
166:
167: PersistentFieldManager manager = new PersistentFieldManagerImpl(
168: locator, strategies);
169:
170: manager.postChange(pageName, resources, fieldName, value);
171:
172: verify();
173: }
174:
175: @Test
176: public void post_change_with_ultimate_default_strategy() {
177: String pageName = "foo.Bar";
178: String nestedId = "nested";
179: String fieldName = "field";
180:
181: ComponentResources resources = mockComponentResources();
182: ComponentModel model = mockComponentModel();
183: MetaDataLocator locator = newMetaDataLocator();
184:
185: PersistentFieldStrategy strat = newPersistentFieldStrategy();
186: Object value = new Object();
187:
188: Map<String, PersistentFieldStrategy> strategies = newMap();
189: strategies.put(PersistentFieldManagerImpl.DEFAULT_STRATEGY,
190: strat);
191:
192: train_getComponentModel(resources, model);
193:
194: train_getFieldPersistenceStrategy(model, fieldName, "");
195:
196: train_findMeta(locator, PersistentFieldManagerImpl.META_KEY,
197: resources, PersistentFieldManagerImpl.DEFAULT_STRATEGY);
198:
199: train_getNestedId(resources, nestedId);
200:
201: strat.postChange(pageName, nestedId, fieldName, value);
202:
203: replay();
204:
205: PersistentFieldManager manager = new PersistentFieldManagerImpl(
206: locator, strategies);
207:
208: manager.postChange(pageName, resources, fieldName, value);
209:
210: verify();
211: }
212:
213: protected void train_findMeta(MetaDataLocator locator, String key,
214: ComponentResources resources, String value) {
215: expect(locator.findMeta(key, resources)).andReturn(value)
216: .atLeastOnce();
217: }
218:
219: protected MetaDataLocator newMetaDataLocator() {
220: return newMock(MetaDataLocator.class);
221: }
222:
223: private PersistentFieldStrategy newPersistentFieldStrategy() {
224: return newMock(PersistentFieldStrategy.class);
225: }
226:
227: @Test
228: public void gather_changes() {
229: Object value1 = new Object();
230: Object value2 = new Object();
231:
232: PersistentFieldStrategy strat1 = newPersistentFieldStrategy();
233:
234: Collection<PersistentFieldChange> changes1 = newList();
235: changes1.add(new PersistentFieldChangeImpl("component",
236: "field1", value1));
237:
238: PersistentFieldStrategy strat2 = newPersistentFieldStrategy();
239:
240: Collection<PersistentFieldChange> changes2 = newList();
241: changes2.add(new PersistentFieldChangeImpl("component",
242: "field2", value2));
243:
244: // We don't know the exact order the strategies will be ordered in the map,
245: // so we can't guarantee the order the strategies will be invoked.
246:
247: getMocksControl().checkOrder(false);
248:
249: expect(strat1.gatherFieldChanges("foo.Bar"))
250: .andReturn(changes1);
251:
252: expect(strat2.gatherFieldChanges("foo.Bar"))
253: .andReturn(changes2);
254:
255: replay();
256:
257: Map<String, PersistentFieldStrategy> strategies = newMap();
258:
259: strategies.put("alpha", strat1);
260: strategies.put("beta", strat2);
261:
262: PersistentFieldManager manager = new PersistentFieldManagerImpl(
263: null, strategies);
264:
265: PersistentFieldBundle bundle = manager.gatherChanges("foo.Bar");
266:
267: assertSame(bundle.getValue("component", "field1"), value1);
268: assertSame(bundle.getValue("component", "field2"), value2);
269:
270: verify();
271: }
272: }
|