001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.cli2.resource;
020
021import java.util.Locale;
022import java.util.MissingResourceException;
023import java.util.ResourceBundle;
024
025import junit.framework.TestCase;
026
027/**
028 * A utility class used to provide internationalisation support.
029 *
030 * @author John Keyes
031 */
032public class ResourceHelperTest extends TestCase {
033    /** system property */
034    private static final String PROP_LOCALE = "org.apache.commons.cli2.resource.bundle";
035
036    private static ResourceHelper helper;
037
038    /** resource bundle */
039    private ResourceBundle bundle;
040
041    public void setUp() {
042        System.setProperty(PROP_LOCALE, "org.apache.commons.cli2.resource.TestBundle");
043        helper = ResourceHelper.getResourceHelper();
044    }
045    
046    public void tearDown() {
047        System.setProperty(PROP_LOCALE, "org.apache.commons.cli2.resource.CLIMessageBundle_en_US.properties");
048    }
049    
050    /**
051     * Create a new ResourceHelper for the specified class.
052     */
053    public ResourceHelperTest() {
054        super("ResourceHelperTest");
055    }
056    
057    public void testOverridden() {
058        assertEquals("wrong message", "The class name \"ResourceHelper\" is invalid.", helper.getMessage("ClassValidator.bad.classname", "ResourceHelper"));
059    }
060    
061    public void testNewMessage1Param() {
062        assertEquals("wrong message", "Some might say we will find a brighter day.", helper.getMessage("test.message"));
063    }
064
065    public void testNewMessage2Params() {
066        assertEquals("wrong message", "Some might say we will find a brighter day.", helper.getMessage("test.message", "Some"));
067    }
068
069    public void testNewMessage3Params() {
070        assertEquals("wrong message", "Some might say we will find a brighter day.", helper.getMessage("test.message", "Some", "might"));
071    }
072
073    public void testNewMessage4Params() {
074        assertEquals("wrong message", "Some might say we will find a brighter day.", helper.getMessage("test.message", "Some", "might", "say"));
075    }
076
077    public void testDefaultBundle() {
078        System.setProperty(PROP_LOCALE, "madeupname.properties");
079        helper = ResourceHelper.getResourceHelper();
080        assertEquals("wrong message", "The class name \"ResourceHelper\" is invalid.", helper.getMessage("ClassValidator.bad.classname", "ResourceHelper"));
081    }
082}