/**
 * 
 * Copyright 2016, JEDLSoft, All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.ilib.test;

import java.util.HashMap;
import java.util.Map;

import com.ilib.IResourceBundle;
import com.ilib.IResourceBundle.MissingType;
import com.ilib.IString;
import com.ilib.IlibLocale;

import junit.framework.TestCase;


/**
 * @author edwin
 *
 */
public class IResourceBundleTest extends TestCase
{
	public void testConstructorNotNull()
	{
		IResourceBundle rb = new IResourceBundle();
		assertNotNull(rb);
	}
	
	public void testConstructorResourcesNotNull()
	{
		IResourceBundle rb = new IResourceBundle("java/src/com/ilib/test/res");
		assertNotNull(rb);
	}
	
	public void testNullLocale()
	{
		IResourceBundle rb = new IResourceBundle("java/src/com/ilib/test/res", null);
		assertNotNull(rb);
		
		assertEquals("en-US", rb.getLocale().toString());
	}
	
	public void testEmptyLocale()
	{
		IlibLocale l = new IlibLocale("");
		IResourceBundle rb = new IResourceBundle("java/src/com/ilib/test/res", l);
		assertNotNull(rb);
		
		assertEquals("en-US", rb.getLocale().toString());
	}

	public void testConstructorResourcesAndLocale()
	{
		IlibLocale l = new IlibLocale("fr-FR");
		IResourceBundle rb = new IResourceBundle("java/src/com/ilib/test/res", l);
		assertNotNull(rb);
	}

	public void testContainsSourceTrue()
	{
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", new IlibLocale("fr-FR"));
		assertNotNull(resBundle);
		
		String result = resBundle.getString("Signing in...");
		assertEquals("Connexion en cours...", result.toString());
	}

	public void testContainsSourceFalse()
	{
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", new IlibLocale("fr-FR"));
		assertNotNull(resBundle);
		
		String result = resBundle.getString("Services Palm");
		assertEquals("Services Palm", result.toString());
		assertNotSame("Palm Services", result.toString());
	}

	public void testGetLocStringBaseLocale1()
	{
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", new IlibLocale("es-ES"));
		assertNotNull(resBundle);
		
		String result = resBundle.getString("Opt out failed. Try later");
		assertEquals("Falló la opción de no participar. Intentarlo más tarde.", result.toString());
	}

	public void testGetLocStringNonBaseLocale1()
	{
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", new IlibLocale("es-MX"));
		assertNotNull(resBundle);
		
		String result = resBundle.getString("Opt out failed. Try later");
		assertEquals("Falló la opción de no participar. Intenta más tarde", result.toString());
		assertNotSame("Falló la opción de no participar. Intentarlo más tarde.", result.toString());
	}
	
	public void testGetLocStringBaseLocale2()
	{
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", new IlibLocale("es-ES"));
		assertNotNull(resBundle);
		
		String result = resBundle.getString("Done");
		assertEquals("Aceptar", result.toString());
	}

	public void testGetLocStringNonBaseLocale2()
	{
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", new IlibLocale("es-MX"));
		assertNotNull(resBundle);
		
		String result = resBundle.getString("Done");
		assertEquals("Listo", result.toString());
		assertNotSame("Aceptar", result.toString());
	}
	
	public void testGetLocStringBaseLocale3()
	{
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", new IlibLocale("zh-Hans-CN"));
		assertNotNull(resBundle);
		
		String result = resBundle.getString("Keep Backup On");
		assertEquals("保持备份打开", result.toString());
	}

	public void testGetLocStringNonBaseLocale3()
	{
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", new IlibLocale("zh-HK"));
		assertNotNull(resBundle);
		
		String result = resBundle.getString("Keep Backup On");
		assertEquals("保持備份打開", result.toString());
		assertNotSame("保持备份打开", result.toString());
	}
	
	public void testGetLocStringWithExistedKey1()
	{
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", new IlibLocale("es-ES"));
		assertNotNull(resBundle);
		
		IString result = new IString(resBundle.getString("A verification email was sent to {email}."));
		Map<String, String> values = new HashMap<>();
		values.put("email", "https://www.google.com.ua");
		assertEquals("Se envió un mensaje de verificación a https://www.google.com.ua.", result.format(values));
	}

	public void testGetLocStringWithExistedKey2()
	{
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", new IlibLocale("es-MX"));
		assertNotNull(resBundle);
		
		IString result = new IString(resBundle.getString("A verification email was sent to {email}."));
		Map<String, String> values = new HashMap<>();
		values.put("email", "www.sourceforge.net");
		assertNotSame("Se envió un mensaje de verificación a www.sourceforge.net.", result.format(values));
		assertEquals("Se ha enviado un mensaje de verificación a www.sourceforge.net.", result.format(values));
	}

	public void testGetLocStringWithNonExistedKey1()
	{
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", new IlibLocale("it-IT"));
		assertNotNull(resBundle);
		
		String result = resBundle.getString("Email Sent", "emailsent");
		//assertEquals("Email Sent", result.toString());
		assertEquals("emailsent", result.toString());
	}

	public void testGetLocStringWithNonExistedKey2()
	{
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", new IlibLocale("it-IT"));
		assertNotNull(resBundle);
		
		String result = resBundle.getString("You must use a valid email address format.", "usevalidemail");
		//assertEquals("You must use a valid email address format.", result.toString());
		assertEquals("usevalidemail", result.toString());
	}
	
	public void testGetLocaleWithResourcesGermany()
	{
		final IlibLocale locale = new IlibLocale("de-DE");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		assertNotNull(resBundle);

		assertEquals(locale.toString(), resBundle.getLocale().toString());
	}

	public void testGetLocaleWithResourcesNetherlands()
	{
		final IlibLocale locale = new IlibLocale("nl-NL");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		assertNotNull(resBundle);

		assertEquals(locale.toString(), resBundle.getLocale().toString());
	}
	
	public void testGetStringDefaultPseudo()
	{
		final IlibLocale locale = new IlibLocale("zxx");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		assertNotNull(resBundle);

		assertEquals("Ïñvàľíð Ňëţŵõŕķ Ňàmë9876543210", resBundle.getString("Invalid Network Name").toString());
	}

	public void testGetStringCyrlPseudo()
	{
		final IlibLocale locale = new IlibLocale("zxx-Cyrl-RU");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		assertNotNull(resBundle);

		assertEquals("Инвалид Нэтwорк Намэ9876543210", resBundle.getString("Invalid Network Name").toString());
	}

	public void testGetStringPseudo()
	{
		final IlibLocale locale = new IlibLocale("de-DE");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		assertNotNull(resBundle);

		assertEquals("àçţüàľ šţàţë fõŕ Ŵífí: 6543210", resBundle.getStringPseudo("actual state for Wifi: ").toString());
	}

	public void testGetStringPseudoMissing()
	{
		final IlibLocale locale = new IlibLocale("de-DE");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		resBundle.setMissingType(MissingType.PSEUDO);
		assertNotNull(resBundle);

		assertEquals("Nicht aktualisieren", resBundle.getString("Don't Update").toString());
		assertEquals("Ðõñ'ţ Úþðàţë àñ ëmàíľ6543210", resBundle.getString("Don't Update an email").toString());

	}

	public void testGetStringPseudoMissingLengthenFalse()
	{
		final IlibLocale locale = new IlibLocale("de-DE");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		resBundle.setMissingType(MissingType.PSEUDO);
		resBundle.setLengthen(false);
		assertNotNull(resBundle);

		assertEquals("Ðõñ'ţ Úþðàţë àñ ëmàíľ", resBundle.getString("Don't Update an email").toString());
		assertNotSame("Ðõñ'ţ Úþðàţë àñ ëmàíľ6543210", resBundle.getString("Don't Update an email").toString());
	}
	
	public void testGetStringKeyValueNull()
	{
		final IlibLocale locale = new IlibLocale("de-DE");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		assertNotNull(resBundle);

		assertNull(resBundle.getString(null));
	}

	public void testGetStringEmptyMissing()
	{
		final IlibLocale locale = new IlibLocale("de-DE");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		resBundle.setMissingType(MissingType.EMPTY);
		assertNotNull(resBundle);

		assertEquals("", resBundle.getString("Don't Update an email").toString());
	}

	public void testGetStringSameTargetAndSourceLocalesKeySource()
	{
		final IlibLocale locale = new IlibLocale("uk-UA");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		resBundle.setMissingType(MissingType.EMPTY);
		assertNotNull(resBundle);

		assertEquals("Здоровенькі були!", resBundle.getString("Здоровенькі були!", "Cheers!").toString());
	}
	
	public void testGetStringSameTargetAndSourceLocalesSource()
	{
		final IlibLocale locale = new IlibLocale("uk-UA");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		resBundle.setMissingType(MissingType.EMPTY);
		assertNotNull(resBundle);

		assertEquals("Здоровенькі були!", resBundle.getString("Здоровенькі були!").toString());
	}
	
	public void testGetPseudoStringLatnScript()
	{
		final IlibLocale locale = new IlibLocale("en-GB");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		resBundle.setMissingType(MissingType.PSEUDO);
		assertNotNull(resBundle);

		assertEquals("Ëñğàğëmëñţ þõíñţ!76543210", resBundle.getString("Engagement point!").toString());
	}

	public void testGetPseudoStringCyrlScript()
	{
		final IlibLocale locale = new IlibLocale("uk-UA");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		resBundle.setMissingType(MissingType.PSEUDO);
		assertNotNull(resBundle);

		assertEquals("Энгагэмэнт поинт!76543210", resBundle.getString("Engagement point!").toString());
	}

	public void testGetPseudoStringHebrScript()
	{
		final IlibLocale locale = new IlibLocale("he-IL");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		resBundle.setMissingType(MissingType.PSEUDO);
		assertNotNull(resBundle);

		assertEquals("ֶנגַגֶמֶנט פִֹנט!76543210", resBundle.getString("Engagement point!").toString());
	}
	
	public void testGetPseudoStringHansScript()
	{
		final IlibLocale locale = new IlibLocale("zh-HK");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		resBundle.setMissingType(MissingType.PSEUDO);
		resBundle.setLengthen(false);
		assertNotNull(resBundle);

		assertEquals("俄尼个阿个俄们俄尼推 琶夥意尼推!",resBundle.getString("Engagement point!").toString());
	}
	
	public void testGetStringPseudoTypeJava()
	{
		final IlibLocale locale = new IlibLocale("de-DE");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		resBundle.setType(IResourceBundle.JAVA_TYPE);
		assertNotNull(resBundle);

		assertEquals("àçţüàľ šţàţë fõŕ Ŵífí: {0} 6543210", resBundle.getStringPseudo("actual state for Wifi: {0}").toString());
	}
	
	public void testGetStringPseudoTypeJavaSkipEscapedUnicode()
	{
		final IlibLocale locale = new IlibLocale("de-DE");
		IResourceBundle resBundle = new IResourceBundle("java/src/com/ilib/test/res", locale);
		resBundle.setType(IResourceBundle.JAVA_TYPE);
		assertNotNull(resBundle);

		assertEquals("àçţüàľ šţàţë fõŕ Ŵífí: \u00A06543210", resBundle.getStringPseudo("actual state for Wifi: \u00A0").toString());
	}

}
