    1   /*
    2    * Copyright 2004-2009 the Seasar Foundation and the Others.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
    13   * either express or implied. See the License for the specific language
    14   * governing permissions and limitations under the License.
    15   */
    16  package org.seasar.cubby.controller.impl;
    17
    18  import static org.junit.Assert.assertEquals;
    19
    20  import java.util.Enumeration;
    21  import java.util.Locale;
    22  import java.util.Map;
    23  import java.util.ResourceBundle;
    24
    25  import org.junit.Test;
    26  import org.seasar.cubby.controller.MessagesBehaviour;
    27
    28  public class DefaultMessagesBehaviourTest {
    29
    30  	@Test
    31  	public void defaultBaseName() {
    32  		DefaultMessagesBehaviour behaviour = new DefaultMessagesBehaviour();
    33  		assertEquals(DefaultMessagesBehaviour.DEFAULT_RESOURCE_NAME, behaviour
    34  				.getBaseName());
    35  	}
    36
    37  	@Test
    38  	public void baseName() {
    39  		DefaultMessagesBehaviour behaviour = new DefaultMessagesBehaviour();
    40  		String customBaseName = "base";
    41  		behaviour.setBaseName(customBaseName);
    42  		assertEquals(customBaseName, behaviour.getBaseName());
    43  	}
    44
    45  	@Test
    46  	public void getBundleWithDefaultLocale() {
    47  		Locale defaultLocale = Locale.getDefault();
    48  		Locale.setDefault(Locale.JAPANESE);
    49  		MessagesBehaviour behaviour = new DefaultMessagesBehaviour();
    50  		ResourceBundle bundle = behaviour.getBundle(null);
    51  		assertEquals("{0}は{1}以下選択してください。", bundle
    52  				.getString("valid.arrayMaxSize"));
    53  		Locale.setDefault(defaultLocale);
    54  		assertEquals(Locale.JAPANESE, bundle.getLocale());
    55  	}
    56
    57  	@Test
    58  	public void getBundleWithJapaneseLocale() {
    59  		MessagesBehaviour behaviour = new DefaultMessagesBehaviour();
    60  		ResourceBundle bundle = behaviour.getBundle(Locale.JAPANESE);
    61  		assertEquals("{0}は{1}以下選択してください。", bundle
    62  				.getString("valid.arrayMaxSize"));
    63  		assertEquals(Locale.JAPANESE, bundle.getLocale());
    64  	}
    65
    66  	@Test
    67  	public void getBundleWithEnglishLocale() {
    68  		MessagesBehaviour behaviour = new DefaultMessagesBehaviour();
    69  		ResourceBundle bundle = behaviour.getBundle(Locale.ENGLISH);
    70  		assertEquals("{0} : selects <= {1}.", bundle
    71  				.getString("valid.arrayMaxSize"));
    72  		assertEquals(Locale.ENGLISH, bundle.getLocale());
    73  	}
    74
    75  	@Test
    76  	public void testToMap() {
    77  		MessagesBehaviour behaviour = new DefaultMessagesBehaviour();
    78  		ResourceBundle bundle = behaviour.getBundle(Locale.ENGLISH);
    79  		Map<String, Object> map = behaviour.toMap(bundle);
    80  		int bundleSize = 0;
    81  		for (Enumeration<String> keys = bundle.getKeys(); keys
    82  				.hasMoreElements();) {
    83  			String key = keys.nextElement();
    84  			assertEquals(bundle.getObject(key), map.get(key));
    85  			bundleSize++;
    86  		}
    87  		assertEquals(bundleSize, map.size());
    88
    89  	}
    90
    91  }