<commit_before>/*******************************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.report.engine.api;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import junit.framework.TestCase;

import com.ibm.icu.util.ULocale;
;
/**
 * 
 */

public class ParameterConverterTest extends TestCase
{

	/*
	 * test for formating a string
	 */
	public void testStringFormat( )
	{
		ULocale locDef = ULocale.getDefault( );
		ReportParameterConverter rpc;

		rpc = new ReportParameterConverter( "@@@@", locDef );
		assertEquals( "1234fggggg", rpc.format( "1234fggggg" ) );
		rpc = new ReportParameterConverter( "@@@@!", locDef );
		assertEquals( "1234fggggg", rpc.format( "1234fggggg" ) );

		rpc = new ReportParameterConverter( "@@@@!", locDef );
		assertEquals( "123 ", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "@@@@!!!", locDef );
		assertEquals( "123 ", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "@@@@", locDef );
		assertEquals( "123456", rpc.format( "123456" ) );
		rpc = new ReportParameterConverter( "@@@@!", locDef );
		assertEquals( "123456", rpc.format( "123456" ) );
		rpc = new ReportParameterConverter( "(@@)@@@", locDef );
		assertEquals( "(  )123", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "&&&&!", locDef );
		assertEquals( "123", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "&&&&&&", locDef );
		assertEquals( "123", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "@@@&!", locDef );
		assertEquals( "123", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "&@@@&!", locDef );
		assertEquals( "123 ", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "@@@&", locDef );
		assertEquals( " 123", rpc.format( "123" ) );

		rpc = new ReportParameterConverter( "@@@@@aaa!", locDef );
		assertEquals( "123  aaa", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "@@aaa!", locDef );
		assertEquals( "12aaa3", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "@@aaa&&&!", locDef );
		assertEquals( "12aaa3", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "@@@@@aaa>!", locDef );
		assertEquals( "123  aaa", rpc.format( "123" ) );

		rpc = new ReportParameterConverter( "@@@@<!", locDef );
		assertEquals( "1234fggggg", rpc.format( "1234fggggg" ) );
		rpc = new ReportParameterConverter( "@@@@<!", locDef );
		assertEquals( "1234fggggg", rpc.format( "1234fggGgG" ) );
		rpc = new ReportParameterConverter( "@@@@>!", locDef );
		assertEquals( "1234FGGGGG", rpc.format( "1234fggggg" ) );
		rpc = new ReportParameterConverter( "@@@@&&&>!", locDef );
		assertEquals( "1234FGGGGG", rpc.format( "1234fggggg" ) );

		rpc = new ReportParameterConverter( "@@@@&&@@@<<<>>>@@@&>", locDef );
		assertEquals( "   1234FGGGGG", rpc.format( "1234fggggg" ) );

		rpc = new ReportParameterConverter( "!", locDef );
		assertEquals( "123", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "", locDef );
		assertEquals( "123", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( ">", locDef );
		assertEquals( "123AAA", rpc.format( "123aaA" ) );

		rpc = new ReportParameterConverter( "^^^\"!", locDef );
		assertEquals( "^^^\"123", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "^^^&YY&&&!", locDef );
		assertEquals( "^^^1YY23", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "^^^&YY@@@!", locDef );
		assertEquals( "^^^1YY23 ", rpc.format( "123" ) );

		// test for SSN
		rpc = new ReportParameterConverter( "@@@-@@-@@@@!", locDef );
		assertEquals( "600-00-03274", rpc.format( "6000003274" ) );
		// test for zipcode+4
		rpc = new ReportParameterConverter( "@@@@@-@@@@!", locDef );
		assertEquals( "94305-0110", rpc.format( "943050110" ) );
		rpc = new ReportParameterConverter( "@@@@@-@@@@", locDef );
		assertEquals( "94305-0110", rpc.format( "943050110" ) );
		// test for zipcode
		rpc = new ReportParameterConverter( "@@@@@!", locDef );
		assertEquals( "94305", rpc.format( "94305" ) );
		rpc = new ReportParameterConverter( "@@@@@", locDef );
		assertEquals( "94305", rpc.format( "94305" ) );
		// test for phonenumber
		rpc = new ReportParameterConverter( "(@@@)-@@@-@@@@!", locDef );
		assertEquals( "(650)-837-2345,", rpc.format( "6508372345," ) );
		rpc = new ReportParameterConverter( "(@@@)-@@@-@@@@", locDef );
		assertEquals( "(650)-837-2345", rpc.format( "6508372345" ) );
	}

	/*
	 * test for formating a data
	 */
	public void testDateFormat( )
	{
		ULocale locDef = ULocale.getDefault( ), locale;
		ReportParameterConverter rpc;
		Calendar dateCal;
		Date date;

		locale = new ULocale( "en", "us" );
		dateCal = Calendar.getInstance( locDef.toLocale( ) );
		dateCal.set( 1998, 8, 13, 20, 1, 44 );
		date = dateCal.getTime( );
		locale = ULocale.ITALY;
		rpc = new ReportParameterConverter( "MM/dd/yy KK:mm aa", locale );
		assertEquals( "09/13/98 08:01 p.", rpc.format( date ) );
		rpc = new ReportParameterConverter( "Long Date", locale );
		assertEquals( "13 settembre 1998", rpc.format( date ) );

		locale = new ULocale( "en", "us" );
		dateCal = Calendar.getInstance( locDef.toLocale( ) );
		dateCal.set( 1998, 8, 13, 20, 1, 44 );
		date = dateCal.getTime( );
		rpc = new ReportParameterConverter( "MM/dd/yyyy hh:mm:ss a", locale );
		assertEquals( "09/13/1998 08:01:44 PM", rpc.format( date ) );

		// test the instance of locale
		dateCal = Calendar.getInstance( locDef.toLocale( ) );
		dateCal.set( 1998, 8, 13, 20, 1, 44 );
		date = dateCal.getTime( );

		locale = new ULocale( "en", "us" );
		rpc = new ReportParameterConverter( "Long Date", locale );
		assertEquals( "September 13, 1998", rpc.format( date ) );
		rpc = new ReportParameterConverter( "D", locale );
		assertEquals( "September 13, 1998", rpc.format( date ) );
		rpc = new ReportParameterConverter( "Medium Date", locale );
		assertEquals( "Sep 13, 1998", rpc.format( date ) );
		rpc = new ReportParameterConverter( "Short Date", locale );
		assertEquals( "9/13/98", rpc.format( date ) );
		rpc = new ReportParameterConverter( "d", locale );
		assertEquals( "9/13/98", rpc.format( date ) );
		rpc = new ReportParameterConverter( "Long Time", locale );
		assertEquals( true, rpc.format( date ).startsWith( "8:01:44 PM GMT+" ) );
		rpc = new ReportParameterConverter( "T", locale );
		assertEquals( true, rpc.format( date ).startsWith( "8:01:44 PM GMT+" ) );

		SimpleDateFormat javaSample = (SimpleDateFormat) java.text.DateFormat.getDateTimeInstance( java.text.DateFormat.LONG,
				java.text.DateFormat.SHORT,
				locale.toLocale( ) );
		rpc = new ReportParameterConverter( "f", locale );
		assertEquals( javaSample.format( date ), rpc.format( date ) );

		rpc = new ReportParameterConverter( "General Date", locale );
		assertEquals( true, rpc.format( date ).startsWith( "September 13, 1998 8:01:44 PM GMT+" ) );
		rpc = new ReportParameterConverter( "Short Time", locale );
		assertEquals( "20:01", rpc.format( date ) );
		rpc = new ReportParameterConverter( "Medium Time", locale );
		assertEquals( "8:01:44 PM", rpc.format( date ) );
	}

	/*
	 * test for formating a number or other obj
	 */
	public void testOtherFormat( )
	{
		ULocale locDef = ULocale.getDefault( );
		ReportParameterConverter rpc;

		NumberFormat number = NumberFormat.getInstance( Locale.getDefault( ) );
		number.setGroupingUsed( false );
		rpc = new ReportParameterConverter( null, locDef.toLocale( ) );
		assertEquals( number.format( 1002.234 ),
				rpc.format( new Double( 1002.234 ) ) );

		// test format with different style.<commit_msg>modify the golden string in order to solve the failure when in different local TimeZone.<commit_after>/*******************************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.report.engine.api;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import junit.framework.TestCase;

import com.ibm.icu.util.ULocale;
;
/**
 * 
 */

public class ParameterConverterTest extends TestCase
{

	/*
	 * test for formating a string
	 */
	public void testStringFormat( )
	{
		ULocale locDef = ULocale.getDefault( );
		ReportParameterConverter rpc;

		rpc = new ReportParameterConverter( "@@@@", locDef );
		assertEquals( "1234fggggg", rpc.format( "1234fggggg" ) );
		rpc = new ReportParameterConverter( "@@@@!", locDef );
		assertEquals( "1234fggggg", rpc.format( "1234fggggg" ) );

		rpc = new ReportParameterConverter( "@@@@!", locDef );
		assertEquals( "123 ", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "@@@@!!!", locDef );
		assertEquals( "123 ", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "@@@@", locDef );
		assertEquals( "123456", rpc.format( "123456" ) );
		rpc = new ReportParameterConverter( "@@@@!", locDef );
		assertEquals( "123456", rpc.format( "123456" ) );
		rpc = new ReportParameterConverter( "(@@)@@@", locDef );
		assertEquals( "(  )123", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "&&&&!", locDef );
		assertEquals( "123", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "&&&&&&", locDef );
		assertEquals( "123", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "@@@&!", locDef );
		assertEquals( "123", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "&@@@&!", locDef );
		assertEquals( "123 ", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "@@@&", locDef );
		assertEquals( " 123", rpc.format( "123" ) );

		rpc = new ReportParameterConverter( "@@@@@aaa!", locDef );
		assertEquals( "123  aaa", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "@@aaa!", locDef );
		assertEquals( "12aaa3", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "@@aaa&&&!", locDef );
		assertEquals( "12aaa3", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "@@@@@aaa>!", locDef );
		assertEquals( "123  aaa", rpc.format( "123" ) );

		rpc = new ReportParameterConverter( "@@@@<!", locDef );
		assertEquals( "1234fggggg", rpc.format( "1234fggggg" ) );
		rpc = new ReportParameterConverter( "@@@@<!", locDef );
		assertEquals( "1234fggggg", rpc.format( "1234fggGgG" ) );
		rpc = new ReportParameterConverter( "@@@@>!", locDef );
		assertEquals( "1234FGGGGG", rpc.format( "1234fggggg" ) );
		rpc = new ReportParameterConverter( "@@@@&&&>!", locDef );
		assertEquals( "1234FGGGGG", rpc.format( "1234fggggg" ) );

		rpc = new ReportParameterConverter( "@@@@&&@@@<<<>>>@@@&>", locDef );
		assertEquals( "   1234FGGGGG", rpc.format( "1234fggggg" ) );

		rpc = new ReportParameterConverter( "!", locDef );
		assertEquals( "123", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "", locDef );
		assertEquals( "123", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( ">", locDef );
		assertEquals( "123AAA", rpc.format( "123aaA" ) );

		rpc = new ReportParameterConverter( "^^^\"!", locDef );
		assertEquals( "^^^\"123", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "^^^&YY&&&!", locDef );
		assertEquals( "^^^1YY23", rpc.format( "123" ) );
		rpc = new ReportParameterConverter( "^^^&YY@@@!", locDef );
		assertEquals( "^^^1YY23 ", rpc.format( "123" ) );

		// test for SSN
		rpc = new ReportParameterConverter( "@@@-@@-@@@@!", locDef );
		assertEquals( "600-00-03274", rpc.format( "6000003274" ) );
		// test for zipcode+4
		rpc = new ReportParameterConverter( "@@@@@-@@@@!", locDef );
		assertEquals( "94305-0110", rpc.format( "943050110" ) );
		rpc = new ReportParameterConverter( "@@@@@-@@@@", locDef );
		assertEquals( "94305-0110", rpc.format( "943050110" ) );
		// test for zipcode
		rpc = new ReportParameterConverter( "@@@@@!", locDef );
		assertEquals( "94305", rpc.format( "94305" ) );
		rpc = new ReportParameterConverter( "@@@@@", locDef );
		assertEquals( "94305", rpc.format( "94305" ) );
		// test for phonenumber
		rpc = new ReportParameterConverter( "(@@@)-@@@-@@@@!", locDef );
		assertEquals( "(650)-837-2345,", rpc.format( "6508372345," ) );
		rpc = new ReportParameterConverter( "(@@@)-@@@-@@@@", locDef );
		assertEquals( "(650)-837-2345", rpc.format( "6508372345" ) );
	}

	/*
	 * test for formating a data
	 */
	public void testDateFormat( )
	{
		ULocale locDef = ULocale.getDefault( ), locale;
		ReportParameterConverter rpc;
		Calendar dateCal;
		Date date;

		locale = new ULocale( "en", "us" );
		dateCal = Calendar.getInstance( locDef.toLocale( ) );
		dateCal.set( 1998, 8, 13, 20, 1, 44 );
		date = dateCal.getTime( );
		locale = ULocale.ITALY;
		rpc = new ReportParameterConverter( "MM/dd/yy KK:mm aa", locale );
		assertEquals( "09/13/98 08:01 p.", rpc.format( date ) );
		rpc = new ReportParameterConverter( "Long Date", locale );
		assertEquals( "13 settembre 1998", rpc.format( date ) );

		locale = new ULocale( "en", "us" );
		dateCal = Calendar.getInstance( locDef.toLocale( ) );
		dateCal.set( 1998, 8, 13, 20, 1, 44 );
		date = dateCal.getTime( );
		rpc = new ReportParameterConverter( "MM/dd/yyyy hh:mm:ss a", locale );
		assertEquals( "09/13/1998 08:01:44 PM", rpc.format( date ) );

		// test the instance of locale
		dateCal = Calendar.getInstance( locDef.toLocale( ) );
		dateCal.set( 1998, 8, 13, 20, 1, 44 );
		date = dateCal.getTime( );

		locale = new ULocale( "en", "us" );
		rpc = new ReportParameterConverter( "Long Date", locale );
		assertEquals( "September 13, 1998", rpc.format( date ) );
		rpc = new ReportParameterConverter( "D", locale );
		assertEquals( "September 13, 1998", rpc.format( date ) );
		rpc = new ReportParameterConverter( "Medium Date", locale );
		assertEquals( "Sep 13, 1998", rpc.format( date ) );
		rpc = new ReportParameterConverter( "Short Date", locale );
		assertEquals( "9/13/98", rpc.format( date ) );
		rpc = new ReportParameterConverter( "d", locale );
		assertEquals( "9/13/98", rpc.format( date ) );
		rpc = new ReportParameterConverter( "Long Time", locale );
		assertEquals( true, rpc.format( date ).startsWith( "8:01:44 PM GMT" ) );
		rpc = new ReportParameterConverter( "T", locale );
		assertEquals( true, rpc.format( date ).startsWith( "8:01:44 PM GMT" ) );

		SimpleDateFormat javaSample = (SimpleDateFormat) java.text.DateFormat
				.getDateTimeInstance( java.text.DateFormat.LONG,
						java.text.DateFormat.SHORT, locale.toLocale( ) );
		rpc = new ReportParameterConverter( "f", locale );
		assertEquals( javaSample.format( date ), rpc.format( date ) );

		rpc = new ReportParameterConverter( "General Date", locale );
		assertEquals( true, rpc.format( date ).startsWith(
				"September 13, 1998 8:01:44 PM GMT" ) );
		rpc = new ReportParameterConverter( "Short Time", locale );
		assertEquals( "20:01", rpc.format( date ) );
		rpc = new ReportParameterConverter( "Medium Time", locale );
		assertEquals( "8:01:44 PM", rpc.format( date ) );
	}

	/*
	 * test for formating a number or other obj
	 */
	public void testOtherFormat( )
	{
		ULocale locDef = ULocale.getDefault( );
		ReportParameterConverter rpc;

		NumberFormat number = NumberFormat.getInstance( Locale.getDefault( ) );
		number.setGroupingUsed( false );
		rpc = new ReportParameterConverter( null, locDef.toLocale( ) );
		assertEquals( number.format( 1002.234 ),
				rpc.format( new Double( 1002.234 ) ) );

		// test format with different style.
{'fraction_non_alphanumeric': 0.19355234878722752, 'fraction_numerical': 0.05587964384402825, 'mean_word_length': 4.459604425075427, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3878958', 'n_tokens_mistral': 6243, 'n_tokens_neox': 5263, 'n_words': 1336}