/* ExtResourceBundleTest.java
 * 
 * Created on: Apr 21, 2004
 * Created by: Dennis Groenendijk
 * 
 * Copyright (c) 2004, Brains2B.org
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that
 * the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and 
 * the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
 * the following disclaimer in the documentation and/or other materials provided with the distribution. 
 * 3. The name of the author may not be used to endorse or promote products derived from this software 
 * without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package test.org.brains2b.resx;

import java.util.Enumeration;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.brains2b.resx.ExtResourceBundle;

import junit.framework.TestCase;

/** [short description]
 *  [long description]
 *
 * @author <A HREF="MAILTO:dennis@brains2b.nl">dennis@brains2b.nl</A>
 * @version 0.10 [Apr 21, 2004]
 */
public class ExtResourceBundleTest extends TestCase {
	 public final static  Locale DEFAULT=new Locale("","");
	 public final static  Locale NL=new Locale("nl","");
	 public final static  Locale BE=new Locale("nl","BE");
	 public final static  Locale DE=new Locale("de","");
	/**
	 * Constructor for ExtResourceBundleTest.
	 * @param arg0
	 */
	public ExtResourceBundleTest(String arg0) {
		super(arg0);
	}

	public static void main(String[] args) {
		junit.swingui.TestRunner.run(ExtResourceBundleTest.class);
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		System.setProperty("sql.driver","oracle.jdbc.driver.OracleDriver");
		System.setProperty("sql.url", "jdbc:oracle:thin:@arthur:1521:merlin");
		System.setProperty("sql.user","SCOTT");
		System.setProperty("sql.password","MIAUW");
		super.setUp();
	}
 

	/*
	 * Test for ResourceBundle getExtBundle(String)
	 */
	public void testGetExtBundleString() {
		ResourceBundle b=ExtResourceBundle.getExtBundle("BtnLabel");
		assertEquals("Ok",b.getString("Ok"));
		assertEquals("Apply",b.getString("Apply"));
		assertEquals("Undo",b.getString("Undo")); 
		assertEquals("Cancel",b.getString("Cancel"));
		try {
			b.getString("False");
			assertTrue("Should have given an Exception",false); 
		} catch (MissingResourceException mrex) {
			assertTrue(true);
		}
	}

	/*
	 * Test for ResourceBundle getExtBundle(String, Locale)
	 */
	public void testGetExtBundleStringLocale() {
		ResourceBundle b_be=ExtResourceBundle.getExtBundle("BtnLabel",BE);
		assertEquals("Ok",b_be.getString("Ok")); // from default
		assertEquals("Herstel",b_be.getString("Undo")); // from nl
		assertEquals("Stoppen",b_be.getString("Cancel")); //from nl_BE
		
		ResourceBundle b_de=ExtResourceBundle.getExtBundle("BtnLabel",DE);
		assertEquals("Abbrechen", b_de.getString("Cancel"));
	}

	/*
	 * Test for Enumeration getKeys()
	 */
	public void testGetKeys() {
		ResourceBundle b_be=ExtResourceBundle.getExtBundle("BtnLabel",BE);
		Enumeration enu=b_be.getKeys();
		int count=0;
		while (enu.hasMoreElements()) {
			String key=(String)enu.nextElement();
			assertNotNull(key);
			assertTrue(key.equals("Ok") || key.equals("Cancel") || key.equals("Undo") || key.equals("Apply"));
			count++;
		}
		assertEquals(4,count);
	}

	public void testGetSQL() {
		ExtResourceBundle b_be=(ExtResourceBundle) ExtResourceBundle.getExtBundle("BtnLabel",BE);
		assertEquals("SELECT RESKEY, RESVALUE FROM LOCALIZED_RESOURCE WHERE DOMAIN='BTNLABEL' AND LANGUAGE='nl' AND COUNTRY='BE'",b_be.getSQL());
	}

	public void testGetBaseName() {
		ExtResourceBundle b_be=(ExtResourceBundle) ExtResourceBundle.getExtBundle("BtnLabel",BE);
		assertEquals("BTNLABEL",b_be.getBaseName());
	}

	/*
	 * Test for Locale getLocale()
	 */
	public void testGetLocale() {
		ExtResourceBundle b_default=(ExtResourceBundle) ExtResourceBundle.getExtBundle("BtnLabel",DEFAULT);
		assertEquals(DEFAULT,b_default.getLocale());
		ExtResourceBundle b_be=(ExtResourceBundle) ExtResourceBundle.getExtBundle("BtnLabel",BE);
		assertEquals(BE,b_be.getLocale());
		
	}

}
