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;
016:
017: import java.io.Closeable;
018: import java.io.IOException;
019: import java.util.ArrayList;
020: import java.util.Arrays;
021: import java.util.Collections;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.TreeMap;
026: import java.util.regex.Pattern;
027:
028: import org.apache.tapestry.OptionModel;
029: import org.apache.tapestry.PropertyConduit;
030: import org.apache.tapestry.SelectModel;
031: import org.apache.tapestry.beaneditor.Order;
032: import org.apache.tapestry.internal.test.InternalBaseTestCase;
033: import org.apache.tapestry.ioc.Messages;
034: import org.apache.tapestry.ioc.services.ClassFactory;
035: import org.apache.tapestry.ioc.services.ClassPropertyAdapter;
036: import org.apache.tapestry.ioc.services.PropertyAccess;
037: import org.apache.tapestry.ioc.services.TypeCoercer;
038: import org.testng.annotations.AfterClass;
039: import org.testng.annotations.BeforeClass;
040: import org.testng.annotations.DataProvider;
041: import org.testng.annotations.Test;
042:
043: public class TapestryInternalUtilsTest extends InternalBaseTestCase {
044: private ClassFactory _classFactory;
045:
046: private PropertyAccess _access;
047:
048: @BeforeClass
049: public void setup() {
050: _classFactory = getService("ClassFactory", ClassFactory.class);
051: _access = getService("PropertyAccess", PropertyAccess.class);
052: }
053:
054: @AfterClass
055: public void cleanup() {
056: _access = null;
057: _classFactory = null;
058: }
059:
060: @Test
061: public void close_null_is_noop() {
062: TapestryInternalUtils.close(null);
063: }
064:
065: @Test
066: public void close_success() throws Exception {
067: Closeable c = newMock(Closeable.class);
068:
069: c.close();
070:
071: replay();
072:
073: TapestryInternalUtils.close(c);
074:
075: verify();
076: }
077:
078: @Test
079: public void close_ignores_exceptions() throws Exception {
080: Closeable c = newMock(Closeable.class);
081:
082: c.close();
083: setThrowable(new IOException());
084:
085: replay();
086:
087: TapestryInternalUtils.close(c);
088:
089: verify();
090: }
091:
092: @Test(dataProvider="to_user_presentable")
093: public void to_user_presentable(String input, String expected) {
094: assertEquals(TapestryInternalUtils.toUserPresentable(input),
095: expected);
096: }
097:
098: @DataProvider(name="to_user_presentable")
099: public Object[][] to_user_presentable_data() {
100: return new Object[][] { { "hello", "Hello" },
101: { "userId", "User Id" }, { "useHTML", "Use HTML" },
102: { "underscored_name", "Underscored Name" }, };
103: }
104:
105: @Test
106: public void map_from_keys_and_values() {
107: Map<String, String> map = TapestryInternalUtils
108: .mapFromKeysAndValues("fred", "flintstone", "barney",
109: "rubble");
110:
111: assertEquals(map.size(), 2);
112: assertEquals(map.get("fred"), "flintstone");
113: assertEquals(map.get("barney"), "rubble");
114: }
115:
116: @Test
117: public void string_to_option_model_just_label() {
118: OptionModel model = TapestryInternalUtils
119: .toOptionModel("Just A Label");
120:
121: assertEquals(model.getLabel(), "Just A Label");
122: assertEquals(model.getValue(), "Just A Label");
123: }
124:
125: @Test
126: public void string_to_option_model() {
127: OptionModel model = TapestryInternalUtils
128: .toOptionModel("my-value=Some Label");
129:
130: assertEquals(model.getLabel(), "Some Label");
131: assertEquals(model.getValue(), "my-value");
132: }
133:
134: @Test
135: public void string_to_option_models() {
136: List<OptionModel> options = TapestryInternalUtils
137: .toOptionModels("UK,USA,DE=Germany");
138:
139: assertEquals(options.size(), 3);
140:
141: assertEquals(options.get(0).getLabel(), "UK");
142: assertEquals(options.get(0).getValue(), "UK");
143:
144: assertEquals(options.get(1).getLabel(), "USA");
145: assertEquals(options.get(1).getValue(), "USA");
146:
147: assertEquals(options.get(2).getLabel(), "Germany");
148: assertEquals(options.get(2).getValue(), "DE");
149: }
150:
151: @Test
152: public void map_entry_to_option_model() {
153: Map<String, String> map = Collections.singletonMap("key",
154: "value");
155: Map.Entry entry = map.entrySet().iterator().next();
156: OptionModel model = TapestryInternalUtils.toOptionModel(entry);
157:
158: assertEquals(model.getLabel(), "value");
159: assertEquals(model.getValue(), "key");
160: }
161:
162: @Test
163: public void map_to_option_models() {
164: Map<Integer, String> map = new TreeMap<Integer, String>();
165: map.put(1, "A");
166: map.put(2, null);
167: map.put(3, "C");
168:
169: List<OptionModel> options = TapestryInternalUtils
170: .toOptionModels(map);
171:
172: assertEquals(options.size(), 3);
173:
174: assertEquals(options.get(0).getLabel(), "A");
175: assertEquals(options.get(0).getValue(), 1);
176:
177: assertEquals(options.get(1).getLabel(), "");
178: assertEquals(options.get(1).getValue(), 2);
179:
180: assertEquals(options.get(2).getLabel(), "C");
181: assertEquals(options.get(2).getValue(), 3);
182: }
183:
184: @Test
185: public void null_map_key_is_null_option_value() {
186:
187: Map<Integer, String> map = new HashMap<Integer, String>();
188: map.put(null, "Label");
189:
190: List<OptionModel> options = TapestryInternalUtils
191: .toOptionModels(map);
192:
193: assertEquals(options.size(), 1);
194:
195: assertEquals(options.get(0).getLabel(), "Label");
196: assertEquals(options.get(0).getValue(), null);
197: }
198:
199: @Test
200: public void object_to_option_model() {
201: Object object = new Integer(27);
202: OptionModel model = TapestryInternalUtils.toOptionModel(object);
203:
204: assertEquals(model.getLabel(), "27");
205: assertEquals(model.getValue(), "27");
206: }
207:
208: @Test
209: public void list_to_option_models() {
210: List<String> list = new ArrayList<String>();
211: list.add("A");
212: list.add(null);
213: list.add("C");
214:
215: List<OptionModel> options = TapestryInternalUtils
216: .toOptionModels(list);
217:
218: assertEquals(options.size(), 3);
219:
220: assertEquals(options.get(0).getLabel(), "A");
221: assertEquals(options.get(0).getValue(), "A");
222:
223: assertEquals(options.get(1).getLabel(), "");
224: assertEquals(options.get(1).getValue(), "");
225:
226: assertEquals(options.get(2).getLabel(), "C");
227: assertEquals(options.get(2).getValue(), "C");
228: }
229:
230: @Test
231: public void whitespace_around_terms_is_trimmed() {
232: List<OptionModel> options = TapestryInternalUtils
233: .toOptionModels(" UK , USA , DE=Germany ");
234:
235: assertEquals(options.size(), 3);
236:
237: assertEquals(options.get(0).getLabel(), "UK");
238: assertEquals(options.get(0).getValue(), "UK");
239:
240: assertEquals(options.get(1).getLabel(), "USA");
241: assertEquals(options.get(1).getValue(), "USA");
242:
243: assertEquals(options.get(2).getLabel(), "Germany");
244: assertEquals(options.get(2).getValue(), "DE");
245: }
246:
247: @Test
248: public void string_to_select_model_type_coercion_integration() {
249: TypeCoercer coercer = getService(TypeCoercer.class);
250:
251: SelectModel selectModel = coercer.coerce(
252: " UK , USA , DE=Germany ", SelectModel.class);
253:
254: assertNull(selectModel.getOptionGroups());
255: assertEquals(selectModel.getOptions().size(), 3);
256:
257: // Waste of effort to re-test each individual option model.
258: }
259:
260: @Test
261: public void parse_key_value() {
262: KeyValue kv = TapestryInternalUtils.parseKeyValue("foo=bar");
263:
264: assertEquals(kv.getKey(), "foo");
265: assertEquals(kv.getValue(), "bar");
266: }
267:
268: @Test
269: public void bad_format_key_value_pair() {
270: String input = "abraxas";
271:
272: try {
273: TapestryInternalUtils.parseKeyValue(input);
274: unreachable();
275: } catch (IllegalArgumentException ex) {
276: assertEquals(ex.getMessage(), InternalMessages
277: .badKeyValue(input));
278: }
279: }
280:
281: @Test
282: public void whitespace_trimmed_for_key_value() {
283: KeyValue kv = TapestryInternalUtils
284: .parseKeyValue(" mykey = myvalue ");
285:
286: assertEquals(kv.getKey(), "mykey");
287: assertEquals(kv.getValue(), "myvalue");
288: }
289:
290: @Test
291: public void default_order_no_annotation() {
292: PropertyConduit conduit = mockPropertyConduit();
293:
294: train_getAnnotation(conduit, Order.class, null);
295:
296: replay();
297:
298: assertEquals(TapestryInternalUtils.defaultOrder(conduit), 0);
299:
300: verify();
301: }
302:
303: @Test
304: public void default_order_with_annotation() {
305: PropertyConduit conduit = mockPropertyConduit();
306: Order order = newMock(Order.class);
307:
308: train_getAnnotation(conduit, Order.class, order);
309:
310: expect(order.value()).andReturn(99);
311:
312: replay();
313:
314: assertEquals(TapestryInternalUtils.defaultOrder(conduit), 99);
315:
316: verify();
317: }
318:
319: @Test
320: public void extract_id_from_property_expression() {
321: assertEquals(TapestryInternalUtils
322: .extractIdFromPropertyExpression("simpleName"),
323: "simpleName");
324: assertEquals(
325: TapestryInternalUtils
326: .extractIdFromPropertyExpression("complex.name().withStuff"),
327: "complexnamewithStuff");
328: assertEquals(
329: TapestryInternalUtils
330: .extractIdFromPropertyExpression("number99.withABullet"),
331: "number99withABullet");
332: }
333:
334: @Test
335: public void default_label_key_found() {
336: Messages messages = mockMessages();
337: train_contains(messages, "myid-label", true);
338: train_get(messages, "myid-label", "My Id");
339:
340: replay();
341:
342: assertEquals(TapestryInternalUtils.defaultLabel("myid",
343: messages, "myid-name-not-used"), "My Id");
344:
345: verify();
346: }
347:
348: @Test
349: public void default_label_from_name() {
350: Messages messages = mockMessages();
351:
352: stub_contains(messages, false);
353:
354: replay();
355:
356: assertEquals(TapestryInternalUtils.defaultLabel(
357: "foobarbazbiff", messages, "foo.bar().baz.biff()"),
358: "Biff");
359:
360: verify();
361: }
362:
363: @Test
364: public void property_order_basic() {
365: ClassPropertyAdapter adapter = _access
366: .getAdapter(DataBean.class);
367:
368: List<String> names = adapter.getPropertyNames();
369:
370: names.remove("class");
371:
372: List<String> sorted = TapestryInternalUtils.orderProperties(
373: adapter, _classFactory, names);
374:
375: assertEquals(sorted, Arrays.asList("firstName", "lastName",
376: "age"));
377: }
378:
379: @Test
380: public void property_order_on_subclass() {
381: ClassPropertyAdapter adapter = _access
382: .getAdapter(DataBeanSubclass.class);
383:
384: List<String> names = adapter.getPropertyNames();
385:
386: names.remove("class");
387:
388: List<String> sorted = TapestryInternalUtils.orderProperties(
389: adapter, _classFactory, names);
390:
391: // Subclass properties listed after superclass properties, as desired.
392:
393: assertEquals(sorted, Arrays.asList("firstName", "lastName",
394: "age", "street", "city", "state", "zip"));
395: }
396:
397: @Test
398: public void properties_with_order_annotation_filtered() {
399: ClassPropertyAdapter adapter = _access
400: .getAdapter(PropertyOrderBean.class);
401:
402: List<String> names = adapter.getPropertyNames();
403:
404: names.remove("class");
405:
406: List<String> sorted = TapestryInternalUtils.orderProperties(
407: adapter, _classFactory, names);
408:
409: // Property third has an explicit @Order
410:
411: assertEquals(sorted, Arrays.asList("first", "second"));
412: }
413:
414: @Test
415: public void null_equals_null() {
416: assertTrue(TapestryInternalUtils.isEqual(null, null));
417: }
418:
419: @Test
420: public void non_null_never_equals_null() {
421: assertFalse(TapestryInternalUtils.isEqual(this , null));
422: }
423:
424: @Test
425: public void same_is_equal() {
426: assertTrue(TapestryInternalUtils.isEqual(this , this ));
427: }
428:
429: @Test
430: public void is_equal_with_objects() {
431: String left = "left";
432: String right = "right";
433:
434: assertFalse(TapestryInternalUtils.isEqual(left, right));
435: assertTrue(TapestryInternalUtils
436: .isEqual(left, new String(left)));
437: }
438:
439: @Test
440: public void type_coersion_string_to_pattern() {
441: TypeCoercer coercer = getObject(TypeCoercer.class, null);
442:
443: String input = "\\s+";
444:
445: Pattern pattern = coercer.coerce(input, Pattern.class);
446:
447: assertEquals(pattern.toString(), input);
448: }
449:
450: }
|