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.util.Locale;
018:
019: import org.apache.tapestry.internal.test.InternalBaseTestCase;
020: import org.apache.tapestry.internal.util.URLChangeTracker;
021: import org.apache.tapestry.ioc.Messages;
022: import org.apache.tapestry.ioc.Resource;
023: import org.apache.tapestry.ioc.internal.util.ClasspathResource;
024: import org.apache.tapestry.model.ComponentModel;
025: import org.apache.tapestry.services.ComponentMessagesSource;
026: import org.testng.annotations.Test;
027:
028: /**
029: * Tests {@link ComponentMessagesSourceImpl} as well as {@link MessagesSourceImpl} (which contains
030: * code refactored out of CMSI).
031: */
032: @Test(sequential=true)
033: public class ComponentMessagesSourceImplTest extends
034: InternalBaseTestCase {
035: // With control of the tracker, we can force changes as if underlying files were changed.
036:
037: private static final String SIMPLE_COMPONENT_CLASS_NAME = "org.apache.tapestry.internal.services.SimpleComponent";
038:
039: private final URLChangeTracker _tracker = new URLChangeTracker();
040:
041: private final Resource _simpleComponentResource = new ClasspathResource(
042: "org/apache/tapestry/internal/services/SimpleComponent.class");
043:
044: private final ComponentMessagesSourceImpl _source = new ComponentMessagesSourceImpl(
045: _simpleComponentResource, "AppCatalog.properties", _tracker);
046:
047: @Test
048: public void simple_component() {
049: ComponentModel model = mockComponentModel();
050:
051: train_getComponentClassName(model, SIMPLE_COMPONENT_CLASS_NAME);
052:
053: train_getBaseResource(model, _simpleComponentResource);
054:
055: train_getParentModel(model, null);
056:
057: replay();
058:
059: forceCacheClear();
060:
061: Messages messages = _source.getMessages(model, Locale.ENGLISH);
062:
063: assertEquals(messages.get("color"), "color");
064: assertEquals(messages.get("framework"), "Tapestry");
065:
066: // Check normal caching
067:
068: assertSame(_source.getMessages(model, Locale.ENGLISH), messages);
069:
070: // Now, force a cache clear and retry.
071:
072: forceCacheClear();
073:
074: Messages messages2 = _source.getMessages(model, Locale.ENGLISH);
075:
076: // Check that a new Messages was created
077:
078: assertNotSame(messages2, messages);
079:
080: assertEquals(messages2.get("color"), "color");
081: assertEquals(messages2.get("framework"), "Tapestry");
082:
083: verify();
084: }
085:
086: private void forceCacheClear() {
087: _tracker.forceChange();
088: _source.checkForUpdates();
089: }
090:
091: @Test
092: public void per_language_messages_override() {
093: ComponentModel model = mockComponentModel();
094:
095: train_getComponentClassName(model, SIMPLE_COMPONENT_CLASS_NAME);
096:
097: train_getBaseResource(model, _simpleComponentResource);
098:
099: train_getParentModel(model, null);
100:
101: replay();
102:
103: forceCacheClear();
104:
105: Messages messages = _source.getMessages(model, Locale.UK);
106:
107: assertEquals(messages.get("color"), "colour");
108: assertEquals(messages.get("framework"), "Tapestry");
109:
110: verify();
111: }
112:
113: @Test
114: public void messages_keys_are_case_insensitive() {
115: ComponentModel model = mockComponentModel();
116:
117: train_getComponentClassName(model, SIMPLE_COMPONENT_CLASS_NAME);
118:
119: train_getBaseResource(model, _simpleComponentResource);
120:
121: train_getParentModel(model, null);
122:
123: replay();
124:
125: forceCacheClear();
126:
127: Messages messages = _source.getMessages(model, Locale.UK);
128:
129: assertEquals(messages.get("COlor"), "colour");
130: assertEquals(messages.get("Framework"), "Tapestry");
131:
132: verify();
133: }
134:
135: @Test
136: public void subclass_inherits_base_class_messages() {
137: ComponentModel model = mockComponentModel();
138: ComponentModel parent = mockComponentModel();
139:
140: train_getComponentClassName(model,
141: "org.apache.tapestry.internal.services.SubclassComponent");
142:
143: train_getBaseResource(
144: model,
145: new ClasspathResource(
146: "org/apache/tapestry/internal/services/SubclassComponent.class"));
147:
148: train_getParentModel(model, parent);
149:
150: train_getComponentClassName(parent, SIMPLE_COMPONENT_CLASS_NAME);
151:
152: train_getBaseResource(parent, _simpleComponentResource);
153:
154: train_getParentModel(parent, null);
155:
156: replay();
157:
158: forceCacheClear();
159:
160: Messages messages = _source.getMessages(model, Locale.ENGLISH);
161:
162: assertEquals(messages.get("color"), "color");
163: assertEquals(messages.get("framework"), "Tapestry");
164: assertEquals(messages.get("source"), "SubclassComponent");
165: assertEquals(messages.get("metal"), "steel");
166: assertEquals(messages.get("app-catalog-source"), "AppCatalog");
167: assertEquals(messages.get("app-catalog-overridden"),
168: "Overridden by Component");
169:
170: messages = _source.getMessages(model, Locale.UK);
171:
172: assertEquals(messages.get("color"), "colour");
173: assertEquals(messages.get("framework"), "Tapestry");
174: assertEquals(messages.get("source"), "SubclassComponent");
175: assertEquals(messages.get("metal"), "aluminium");
176:
177: verify();
178: }
179:
180: @Test
181: public void no_app_catalog() {
182: ComponentModel model = mockComponentModel();
183: ComponentModel parent = mockComponentModel();
184:
185: train_getComponentClassName(model,
186: "org.apache.tapestry.internal.services.SubclassComponent");
187:
188: train_getBaseResource(
189: model,
190: new ClasspathResource(
191: "org/apache/tapestry/internal/services/SubclassComponent.class"));
192:
193: train_getParentModel(model, parent);
194:
195: train_getComponentClassName(parent, SIMPLE_COMPONENT_CLASS_NAME);
196:
197: train_getBaseResource(parent, _simpleComponentResource);
198:
199: train_getParentModel(parent, null);
200:
201: replay();
202:
203: forceCacheClear();
204:
205: ComponentMessagesSource source = new ComponentMessagesSourceImpl(
206: _simpleComponentResource, "NoSuchAppCatalog.properties");
207:
208: Messages messages = source.getMessages(model, Locale.ENGLISH);
209:
210: assertEquals(messages.get("color"), "color");
211: assertEquals(messages.get("app-catalog-source"),
212: "[[missing key: app-catalog-source]]");
213: assertEquals(messages.get("app-catalog-overridden"),
214: "Overridden by Component");
215:
216: verify();
217: }
218: }
|