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.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
018:
019: import java.util.Collections;
020: import java.util.Map;
021:
022: import org.apache.tapestry.ComponentResources;
023: import org.apache.tapestry.internal.test.InternalBaseTestCase;
024: import org.apache.tapestry.model.ComponentModel;
025: import org.apache.tapestry.services.MetaDataLocator;
026: import org.testng.annotations.Test;
027:
028: public class MetaDataLocatorImplTest extends InternalBaseTestCase {
029: @Test
030: public void found_in_component() {
031: ComponentResources resources = mockComponentResources();
032: ComponentModel model = mockComponentModel();
033:
034: String key = "foo.bar";
035: String value = "zaphod";
036: String completeId = "foo.Bar:baz";
037:
038: train_getCompleteId(resources, completeId);
039: train_getComponentModel(resources, model);
040: train_getMeta(model, key, value);
041:
042: replay();
043:
044: Map<String, String> configuration = Collections.emptyMap();
045:
046: MetaDataLocator locator = new MetaDataLocatorImpl(configuration);
047:
048: assertSame(locator.findMeta(key, resources), value);
049:
050: verify();
051:
052: // And check that it's cached:
053:
054: train_getCompleteId(resources, completeId);
055:
056: replay();
057:
058: assertSame(locator.findMeta(key, resources), value);
059:
060: verify();
061: }
062:
063: @Test
064: public void found_in_container() {
065: ComponentResources resources = mockComponentResources();
066: ComponentResources containerResources = mockComponentResources();
067: ComponentModel model = mockComponentModel();
068: ComponentModel containerModel = mockComponentModel();
069:
070: String key = "foo.bar";
071: String value = "zaphod";
072: String completeId = "foo.Bar:baz";
073:
074: train_getCompleteId(resources, completeId);
075: train_getComponentModel(resources, model);
076: train_getMeta(model, key, null);
077: train_getContainerResources(resources, containerResources);
078: train_getComponentModel(containerResources, containerModel);
079: train_getMeta(containerModel, key, value);
080:
081: replay();
082:
083: Map<String, String> configuration = Collections.emptyMap();
084:
085: MetaDataLocator locator = new MetaDataLocatorImpl(configuration);
086:
087: assertSame(locator.findMeta(key, resources), value);
088:
089: verify();
090: }
091:
092: @Test
093: public void found_via_high_level_default() {
094: ComponentResources resources = mockComponentResources();
095: ComponentModel model = mockComponentModel();
096:
097: String key = "foo.bar";
098: String value = "zaphod";
099: String completeId = "Bar";
100: String logicalPageName = completeId;
101:
102: train_getCompleteId(resources, completeId);
103: train_getComponentModel(resources, model);
104: train_getMeta(model, key, null);
105: train_getContainerResources(resources, null);
106:
107: train_getPageName(resources, logicalPageName);
108:
109: replay();
110:
111: Map<String, String> configuration = newMap();
112: configuration.put(key, value);
113:
114: MetaDataLocator locator = new MetaDataLocatorImpl(configuration);
115:
116: assertSame(locator.findMeta(key, resources), value);
117:
118: verify();
119:
120: // And check that it's cached:
121:
122: train_getCompleteId(resources, completeId);
123:
124: replay();
125:
126: assertSame(locator.findMeta(key, resources), value);
127:
128: verify();
129: }
130:
131: @Test
132: public void default_matching_is_case_insensitive() {
133: ComponentResources resources = mockComponentResources();
134: ComponentModel model = mockComponentModel();
135:
136: String key = "foo.bar";
137: String value = "zaphod";
138: String completeId = "foo.Bar";
139:
140: train_getCompleteId(resources, completeId);
141: train_getComponentModel(resources, model);
142: train_getMeta(model, key, null);
143: train_getContainerResources(resources, null);
144:
145: train_getPageName(resources, "foo/Bar");
146:
147: replay();
148:
149: Map<String, String> configuration = newMap();
150: configuration.put(key.toUpperCase(), value);
151:
152: MetaDataLocator locator = new MetaDataLocatorImpl(configuration);
153:
154: assertSame(locator.findMeta(key, resources), value);
155:
156: verify();
157:
158: // And check that it's cached:
159:
160: train_getCompleteId(resources, completeId);
161:
162: replay();
163:
164: assertSame(locator.findMeta(key, resources), value);
165:
166: verify();
167: }
168:
169: @Test
170: public void subfolder_default_overrides_high_level_default() {
171: ComponentResources resources = mockComponentResources();
172: ComponentModel model = mockComponentModel();
173:
174: String key = "foo.bar";
175: String value = "zaphod";
176: String completeId = "foo.Bar";
177:
178: train_getCompleteId(resources, completeId);
179: train_getComponentModel(resources, model);
180: train_getMeta(model, key, null);
181: train_getContainerResources(resources, null);
182:
183: train_getPageName(resources, "foo/Bar");
184:
185: replay();
186:
187: Map<String, String> configuration = newMap();
188: configuration.put(key, "xxx");
189: configuration.put("foo:" + key, value);
190:
191: MetaDataLocator locator = new MetaDataLocatorImpl(configuration);
192:
193: assertSame(locator.findMeta(key, resources), value);
194:
195: verify();
196:
197: // And check that it's cached:
198:
199: train_getCompleteId(resources, completeId);
200:
201: replay();
202:
203: assertSame(locator.findMeta(key, resources), value);
204:
205: verify();
206: }
207:
208: @Test
209: public void test_cache_cleared() {
210: ComponentResources resources = mockComponentResources();
211: ComponentModel model = mockComponentModel();
212:
213: String key = "foo.bar";
214: String value = "zaphod";
215: String completeId = "foo.Bar:baz";
216:
217: train_getCompleteId(resources, completeId);
218: train_getComponentModel(resources, model);
219: train_getMeta(model, key, value);
220:
221: replay();
222:
223: Map<String, String> configuration = Collections.emptyMap();
224:
225: MetaDataLocatorImpl locator = new MetaDataLocatorImpl(
226: configuration);
227:
228: assertSame(locator.findMeta(key, resources), value);
229:
230: verify();
231:
232: // And check that it's cached:
233:
234: train_getCompleteId(resources, completeId);
235: train_getComponentModel(resources, model);
236: train_getMeta(model, key, value);
237:
238: replay();
239:
240: locator.objectWasInvalidated();
241:
242: assertSame(locator.findMeta(key, resources), value);
243:
244: verify();
245: }
246: }
|