001: // Copyright 2006 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.Arrays;
018: import java.util.Locale;
019:
020: import org.apache.tapestry.ioc.MessageFormatter;
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.services.ValidationMessagesSource;
025: import org.testng.Assert;
026: import org.testng.annotations.BeforeClass;
027: import org.testng.annotations.Test;
028:
029: public class ValidationMessagesSourceImplTest extends Assert {
030: private ValidationMessagesSource _source;
031:
032: @BeforeClass
033: public void setup() {
034: Resource rootResource = new ClasspathResource("/");
035: _source = new ValidationMessagesSourceImpl(Arrays.asList(
036: "org/apache/tapestry/internal/ValidationMessages",
037: "org/apache/tapestry/internal/ValidationTestMessages"),
038: rootResource);
039: }
040:
041: @Test
042: public void builtin_message() {
043: Messages messages = _source
044: .getValidationMessages(Locale.ENGLISH);
045:
046: assertEquals(messages.format("required", "My Field"),
047: "You must provide a value for My Field.");
048: }
049:
050: @Test
051: public void contributed_message() {
052: Messages messages = _source
053: .getValidationMessages(Locale.ENGLISH);
054:
055: assertEquals(messages.get("contributed"),
056: "This message was contributed inside ValidationTestMessages.");
057: }
058:
059: @Test
060: public void localization_of_message() {
061: Messages messages = _source
062: .getValidationMessages(Locale.FRENCH);
063:
064: assertEquals(messages.get("contributed"),
065: "Zees eez Cohntributahd.");
066: }
067:
068: @Test
069: public void contains() {
070: Messages messages = _source
071: .getValidationMessages(Locale.ENGLISH);
072:
073: assertEquals(messages.contains("required"), true);
074: assertEquals(messages.contains("contributed"), true);
075: assertEquals(messages
076: .contains("this-key-does-not-exist-anywhere"), false);
077: }
078:
079: @Test
080: public void message_formatter() {
081: Messages messages = _source
082: .getValidationMessages(Locale.ENGLISH);
083:
084: MessageFormatter formatter = messages.getFormatter("required");
085:
086: assertEquals(formatter.format("My Field"),
087: "You must provide a value for My Field.");
088: }
089:
090: @Test
091: public void messages_instances_are_cached() {
092: Messages english = _source
093: .getValidationMessages(Locale.ENGLISH);
094: Messages french = _source.getValidationMessages(Locale.FRENCH);
095:
096: assertSame(_source.getValidationMessages(Locale.ENGLISH),
097: english);
098: assertSame(_source.getValidationMessages(Locale.FRENCH), french);
099: assertNotSame(french, english);
100: }
101: }
|