ICU-4530 Initial version of tests.

X-SVN-Rev: 18661
This commit is contained in:
Eric Mader 2005-10-11 21:35:47 +00:00
parent 953d673966
commit 519b15e3c6
5 changed files with 1149 additions and 16 deletions

View File

@ -0,0 +1,197 @@
/*
*******************************************************************************
* Copyright (C) 1996-2005, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
*/
package com.ibm.icu.dev.test.serializable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Locale;
import com.ibm.icu.util.BuddhistCalendar;
import com.ibm.icu.util.Calendar;
import com.ibm.icu.util.ChineseCalendar;
import com.ibm.icu.util.CopticCalendar;
import com.ibm.icu.util.EthiopicCalendar;
import com.ibm.icu.util.GregorianCalendar;
import com.ibm.icu.util.HebrewCalendar;
import com.ibm.icu.util.IslamicCalendar;
import com.ibm.icu.util.JapaneseCalendar;
import com.ibm.icu.util.TimeZone;
/**
* @author emader
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class CalendarTests
{
static class CalendarTest implements SerializableTest.Handler
{
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
TimeZone pst = TimeZone.getTimeZone("America/Los_Angeles");
Calendar calendars[] = new Calendar[locales.length];
for (int i = 0; i < locales.length; i += 1) {
calendars[i] = Calendar.getInstance(pst, locales[i]);
}
return calendars;
}
public boolean hasSameBehavior(Object a, Object b)
{
Calendar cal_a = (Calendar) a;
Calendar cal_b = (Calendar) b;
long now = System.currentTimeMillis();
cal_a.setTimeInMillis(now);
cal_a.roll(Calendar.MONTH, 1);
cal_b.setTimeInMillis(now);
cal_b.roll(Calendar.MONTH, 1);
return cal_a.getTime().equals(cal_a.getTime());
}
}
static class BuddhistCalendarTest extends CalendarTest
{
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
TimeZone tst = TimeZone.getTimeZone("Asia/Bangkok");
BuddhistCalendar calendars[] = new BuddhistCalendar[locales.length];
for (int i = 0; i < locales.length; i += 1) {
calendars[i] = new BuddhistCalendar(tst, locales[i]);
}
return calendars;
}
}
static class ChineseCalendarTest extends CalendarTest
{
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
TimeZone cst = TimeZone.getTimeZone("Asia/Shanghai");
ChineseCalendar calendars[] = new ChineseCalendar[locales.length];
for (int i = 0; i < locales.length; i += 1) {
calendars[i] = new ChineseCalendar(cst, locales[i]);
}
return calendars;
}
}
static class CopticCalendarTest extends CalendarTest
{
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
TimeZone ast = TimeZone.getTimeZone("Europe/Athens");
CopticCalendar calendars[] = new CopticCalendar[locales.length];
for (int i = 0; i < locales.length; i += 1) {
calendars[i] = new CopticCalendar(ast, locales[i]);
}
return calendars;
}
}
static class EthiopicCalendarTest extends CalendarTest
{
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
TimeZone ast = TimeZone.getTimeZone("Africa/Addis_Ababa");
EthiopicCalendar calendars[] = new EthiopicCalendar[locales.length];
for (int i = 0; i < locales.length; i += 1) {
calendars[i] = new EthiopicCalendar(ast, locales[i]);
}
return calendars;
}
}
static class GregorianCalendarTest extends CalendarTest
{
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
TimeZone pst = TimeZone.getTimeZone("America/Los_Angeles");
GregorianCalendar calendars[] = new GregorianCalendar[locales.length];
for (int i = 0; i < locales.length; i += 1) {
calendars[i] = new GregorianCalendar(pst, locales[i]);
}
return calendars;
}
}
static class HebrewCalendarTest extends CalendarTest
{
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
TimeZone jst = TimeZone.getTimeZone("Asia/Jerusalem");
HebrewCalendar calendars[] = new HebrewCalendar[locales.length];
for (int i = 0; i < locales.length; i += 1) {
calendars[i] = new HebrewCalendar(jst, locales[i]);
}
return calendars;
}
}
static class IslamicCalendarTest extends CalendarTest
{
public Object[] getTestObjects() {
Locale locales[] = SerializableTest.getLocales();
TimeZone cst = TimeZone.getTimeZone("Africa/Cairo");
IslamicCalendar calendars[] = new IslamicCalendar[locales.length];
for (int i = 0; i < locales.length; i += 1) {
calendars[i] = new IslamicCalendar(cst, locales[i]);
}
return calendars;
}
}
static class JapaneseCalendarTest extends CalendarTest
{
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
TimeZone jst = TimeZone.getTimeZone("Asia/Tokyo");
JapaneseCalendar calendars[] = new JapaneseCalendar[locales.length];
for (int i = 0; i < locales.length; i += 1) {
calendars[i] = new JapaneseCalendar(jst, locales[i]);
}
return calendars;
}
}
public static void main(String[] args)
{
//nothing needed yet...
}
}

View File

@ -0,0 +1,87 @@
/*
*******************************************************************************
* Copyright (C) 1996-2005, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
*/
package com.ibm.icu.dev.test.serializable;
import java.util.Locale;
import com.ibm.icu.text.ArabicShapingException;
import com.ibm.icu.text.StringPrepParseException;
import com.ibm.icu.util.UResourceTypeMismatchException;
/**
* @author emader
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ExceptionTests
{
static abstract class ExceptionTest implements SerializableTest.Handler
{
abstract public Object[] getTestObjects();
public boolean hasSameBehavior(Object a, Object b)
{
Exception ea = (Exception) a;
Exception eb = (Exception) b;
return ea.toString().equals(eb.toString());
}
}
static class ArabicShapingExceptionTest extends ExceptionTest
{
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
ArabicShapingException exceptions[] = new ArabicShapingException[locales.length];
for (int i = 0; i < locales.length; i += 1) {
exceptions[i] = new ArabicShapingException(locales[i].toString());
}
return exceptions;
}
}
static class StringPrepParseExceptionTest extends ExceptionTest
{
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
String rules = "This is a very odd little set of rules, just for testing, you know...";
StringPrepParseException exceptions[] = new StringPrepParseException[locales.length];
for (int i = 0; i < locales.length; i += 1) {
exceptions[i] = new StringPrepParseException(locales[i].toString(), i, rules, i);
}
return exceptions;
}
}
static class UResourceTypeMismatchExceptionTest extends ExceptionTest
{
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
UResourceTypeMismatchException exceptions[] = new UResourceTypeMismatchException[locales.length];
for (int i = 0; i < locales.length; i += 1) {
exceptions[i] = new UResourceTypeMismatchException(locales[i].toString());
}
return exceptions;
}
}
public static void main(String[] args)
{
}
}

View File

@ -0,0 +1,309 @@
/*
*******************************************************************************
* Copyright (C) 1996-2005, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
*/
package com.ibm.icu.dev.test.serializable;
import java.util.Date;
import java.util.Locale;
import com.ibm.icu.text.ChineseDateFormat;
import com.ibm.icu.text.ChineseDateFormatSymbols;
import com.ibm.icu.text.DateFormat;
import com.ibm.icu.text.DateFormatSymbols;
import com.ibm.icu.text.DecimalFormat;
import com.ibm.icu.text.DecimalFormatSymbols;
import com.ibm.icu.text.MessageFormat;
import com.ibm.icu.text.NumberFormat;
import com.ibm.icu.text.RuleBasedNumberFormat;
import com.ibm.icu.text.SimpleDateFormat;
import com.ibm.icu.util.GregorianCalendar;
import com.ibm.icu.util.ULocale;
/**
* @author emader
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class FormatTests
{
public static class NumberFormatTest implements SerializableTest.Handler
{
public Object[] getTestObjects()
{
NumberFormat formats[] = {
NumberFormat.getInstance(Locale.US),
NumberFormat.getCurrencyInstance(Locale.US),
NumberFormat.getPercentInstance(Locale.US),
NumberFormat.getScientificInstance(Locale.US)
};
return formats;
}
public boolean hasSameBehavior(Object a, Object b)
{
NumberFormat format_a = (NumberFormat) a;
NumberFormat format_b = (NumberFormat) b;
double number = 1234.56;
return format_a.format(number).equals(format_b.format(number));
}
}
public static class DecimalFormatTest extends NumberFormatTest
{
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
DecimalFormat formats[] = new DecimalFormat[locales.length];
for (int i = 0; i < locales.length; i += 1) {
formats[i] = new DecimalFormat("#,##0.###", new DecimalFormatSymbols(locales[i]));
}
return formats;
}
}
public static class RuleBasedNumberFormatTest extends NumberFormatTest
{
int types[] = {RuleBasedNumberFormat.SPELLOUT, RuleBasedNumberFormat.ORDINAL, RuleBasedNumberFormat.DURATION};
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
RuleBasedNumberFormat formats[] = new RuleBasedNumberFormat[types.length * locales.length];
int i = 0;
for (int t = 0; t < types.length; t += 1) {
for (int l = 0; l < locales.length; l += 1) {
formats[i++] = new RuleBasedNumberFormat(locales[l], types[t]);
}
}
return formats;
}
}
public static class DecimalFormatSymbolsTest implements SerializableTest.Handler
{
private char[] getCharSymbols(DecimalFormatSymbols dfs)
{
char symbols[] = {
dfs.getDecimalSeparator(),
dfs.getDigit(),
dfs.getGroupingSeparator(),
dfs.getMinusSign(),
dfs.getMonetaryDecimalSeparator(),
dfs.getPadEscape(),
dfs.getPatternSeparator(),
dfs.getPercent(),
dfs.getPerMill(),
dfs.getPlusSign(),
dfs.getSignificantDigit(),
dfs.getZeroDigit()
};
return symbols;
}
private String[] getStringSymbols(DecimalFormatSymbols dfs)
{
String symbols[] = {
dfs.getCurrencySymbol(),
dfs.getExponentSeparator(),
dfs.getInfinity(),
dfs.getInternationalCurrencySymbol(),
dfs.getNaN()
};
return symbols;
}
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
DecimalFormatSymbols dfs[] = new DecimalFormatSymbols[locales.length];
for (int i = 0; i < locales.length; i += 1) {
dfs[i] = new DecimalFormatSymbols(locales[i]);
}
return dfs;
}
public boolean hasSameBehavior(Object a, Object b)
{
DecimalFormatSymbols dfs_a = (DecimalFormatSymbols) a;
DecimalFormatSymbols dfs_b = (DecimalFormatSymbols) b;
String strings_a[] = getStringSymbols(dfs_a);
String strings_b[] = getStringSymbols(dfs_b);
char chars_a[] = getCharSymbols(dfs_a);
char chars_b[] = getCharSymbols(dfs_b);
return SerializableTest.compareStrings(strings_a, strings_b) && SerializableTest.compareChars(chars_a, chars_b);
}
}
public static class MessageFormatTest implements SerializableTest.Handler
{
public Object[] getTestObjects()
{
MessageFormat formats[] = {new MessageFormat("pattern{0}")};
return formats;
}
public boolean hasSameBehavior(Object a, Object b)
{
MessageFormat mfa = (MessageFormat) a;
MessageFormat mfb = (MessageFormat) b;
Object arguments[] = {new Integer(123456)};
return mfa.format(arguments) != mfb.format(arguments);
}
}
public static class DateFormatTest implements SerializableTest.Handler
{
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
DateFormat formats[] = new DateFormat[locales.length];
for (int i = 0; i < locales.length; i += 1) {
formats[i] = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locales[i]);
}
return formats;
}
public boolean hasSameBehavior(Object a, Object b)
{
DateFormat dfa = (DateFormat) a;
DateFormat dfb = (DateFormat) b;
Date date = new Date(System.currentTimeMillis());
return dfa.format(date).equals(dfb.format(date));
}
}
public static class DateFormatSymbolsTest implements SerializableTest.Handler
{
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
DateFormatSymbols dfs[] = new DateFormatSymbols[locales.length];
for (int i = 0; i < locales.length; i += 1) {
dfs[i] = new DateFormatSymbols(GregorianCalendar.class, ULocale.forLocale(locales[i]));
}
return dfs;
}
public boolean hasSameBehavior(Object a, Object b)
{
DateFormatSymbols dfs_a = (DateFormatSymbols) a;
DateFormatSymbols dfs_b = (DateFormatSymbols) b;
String months_a[] = dfs_a.getMonths();
String months_b[] = dfs_b.getMonths();
return SerializableTest.compareStrings(months_a, months_b);
}
}
public static class SimpleDateFormatTest extends DateFormatTest
{
String patterns[] = {
"EEEE, yyyy MMMM dd",
"yyyy MMMM d",
"yyyy MMM d",
"yy/MM/dd"
};
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
SimpleDateFormat dateFormats[] = new SimpleDateFormat[patterns.length * locales.length];
int i = 0;
for (int p = 0; p < patterns.length; p += 1) {
for (int l = 0; l < locales.length; l += 1) {
dateFormats[i++] = new SimpleDateFormat(patterns[p], ULocale.forLocale(locales[l]));
}
}
return dateFormats;
}
}
public static class ChineseDateFormatTest extends DateFormatTest
{
String patterns[] = {
"EEEE y'x'G-Ml-d",
"y'x'G-Ml-d",
"y'x'G-Ml-d",
"y'x'G-Ml-d"
};
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
ChineseDateFormat dateFormats[] = new ChineseDateFormat[patterns.length * locales.length];
int i = 0;
for (int p = 0; p < patterns.length; p += 1) {
for (int l = 0; l < locales.length; l += 1) {
ULocale locale = new ULocale(locales[l].toString() + "@calendar=chinese");
dateFormats[i++] = new ChineseDateFormat(patterns[p], locale);
}
}
return dateFormats;
}
}
public static class ChineseDateFormatSymbolsTest extends DateFormatSymbolsTest
{
public Object[] getTestObjects()
{
Locale locales[] = SerializableTest.getLocales();
ChineseDateFormatSymbols cdfs[] = new ChineseDateFormatSymbols[locales.length];
for (int i = 0; i < locales.length; i += 1) {
cdfs[i] = new ChineseDateFormatSymbols(locales[i]);
}
return cdfs;
}
public boolean hasSameBehavior(Object a, Object b)
{
if (! super.hasSameBehavior(a, b)) {
return false;
}
ChineseDateFormatSymbols cdfs_a = (ChineseDateFormatSymbols) a;
ChineseDateFormatSymbols cdfs_b = (ChineseDateFormatSymbols) b;
return cdfs_a.getLeapMonth(0).equals(cdfs_b.getLeapMonth(0)) &&
cdfs_a.getLeapMonth(1).equals(cdfs_b.getLeapMonth(1));
}
}
public static void main(String[] args)
{
// nothing needed...
}
}

View File

@ -0,0 +1,421 @@
/*
*******************************************************************************
* Copyright (C) 1996-2005, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
*/
package com.ibm.icu.dev.test.serializable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.HashMap;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import com.ibm.icu.impl.OlsonTimeZone;
import com.ibm.icu.math.BigDecimal;
import com.ibm.icu.math.MathContext;
import com.ibm.icu.util.Currency;
import com.ibm.icu.util.SimpleTimeZone;
import com.ibm.icu.util.TimeZone;
import com.ibm.icu.util.ULocale;
/**
* @author emader
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SerializableTest
{
public interface Handler
{
public Object[] getTestObjects();
public boolean hasSameBehavior(Object a, Object b);
}
public static Handler getHandler(String className)
{
return (Handler) map.get(className);
}
private static class TimeZoneTest implements Handler
{
String[] ZONES = { "GMT", "MET", "IST" };
public Object[] getTestObjects()
{
TimeZone zones[] = new TimeZone[ZONES.length];
for(int z = 0; z < ZONES.length; z += 1) {
zones[z] = TimeZone.getTimeZone(ZONES[z]);
}
return zones;
}
public boolean hasSameBehavior(Object a, Object b)
{
TimeZone zone_a = (TimeZone) a;
TimeZone zone_b = (TimeZone) b;
return zone_a.getDisplayName().equals(zone_b.getDisplayName()) && zone_a.hasSameRules(zone_b);
}
}
private static Locale locales[] = {
Locale.CANADA, Locale.CANADA_FRENCH, Locale.CHINA,
Locale.CHINESE, Locale.ENGLISH, Locale.FRANCE, Locale.FRENCH,
Locale.GERMAN, Locale.GERMANY, Locale.ITALIAN, Locale.ITALY,
Locale.JAPAN, Locale.JAPANESE, Locale.KOREA, Locale.KOREAN,
Locale.PRC, Locale.SIMPLIFIED_CHINESE, Locale.TAIWAN,
Locale.TRADITIONAL_CHINESE, Locale.UK, Locale.US
};
private static Locale places[] = {
Locale.CANADA, Locale.CANADA_FRENCH, Locale.CHINA,
Locale.FRANCE, Locale.GERMANY, Locale.ITALY,
Locale.JAPAN, Locale.KOREA, Locale.PRC, Locale.TAIWAN,
Locale.UK, Locale.US
};
public static Locale[] getLocales()
{
return locales;
}
public static boolean compareStrings(String a[], String b[])
{
if (a.length != b.length) {
return false;
}
for (int i = 0; i < a.length; i += 1) {
if (! a[i].equals(b[i])) {
return false;
}
}
return true;
}
public static boolean compareChars(char a[], char b[])
{
if (a.length != b.length) {
return false;
}
for (int i = 0; i < a.length; i += 1) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
private static class SimpleTimeZoneTest extends TimeZoneTest
{
public Object[] getTestObjects()
{
SimpleTimeZone simpleTimeZones[] = new SimpleTimeZone[6];
simpleTimeZones[0] = new SimpleTimeZone(32400000, "MyTimeZone");
simpleTimeZones[1] = new SimpleTimeZone(32400000, "Asia/Tokyo");
simpleTimeZones[2] = new SimpleTimeZone(32400000, "Asia/Tokyo");
simpleTimeZones[2].setRawOffset(0);
simpleTimeZones[3] = new SimpleTimeZone(32400000, "Asia/Tokyo");
simpleTimeZones[3].setStartYear(100);
simpleTimeZones[4] = new SimpleTimeZone(32400000, "Asia/Tokyo");
simpleTimeZones[4].setStartYear(1000);
simpleTimeZones[4].setDSTSavings(1800000);
simpleTimeZones[4].setStartRule(3, 4, 180000);
simpleTimeZones[4].setEndRule(6, 3, 4, 360000);
simpleTimeZones[5] = new SimpleTimeZone(32400000, "Asia/Tokyo");
simpleTimeZones[5].setStartRule(2, 3, 4, 360000);
simpleTimeZones[5].setEndRule(6, 3, 4, 360000);
return simpleTimeZones;
}
}
private static class ULocaleTest implements Handler
{
public Object[] getTestObjects()
{
ULocale uLocales[] = new ULocale[locales.length];
for (int i = 0; i < locales.length; i += 1) {
uLocales[i] = ULocale.forLocale(locales[i]);
}
return uLocales;
}
public boolean hasSameBehavior(Object a, Object b)
{
ULocale uloc_a = (ULocale) a;
ULocale uloc_b = (ULocale) b;
return uloc_a.getName().equals(uloc_b.getName());
}
}
private static class CurrencyTest implements Handler
{
public Object[] getTestObjects()
{
Currency currencies[] = new Currency[places.length];
for (int i = 0; i < places.length; i += 1) {
currencies[i] = Currency.getInstance(places[i]);
}
return currencies;
}
public boolean hasSameBehavior(Object a, Object b)
{
Currency curr_a = (Currency) a;
Currency curr_b = (Currency) b;
return curr_a.getCurrencyCode().equals(curr_b.getCurrencyCode());
}
}
private static class OlsonTimeZoneTest implements Handler
{
String zoneIDs[] = {
"Pacific/Honolulu", "America/Anchorage", "America/Los_Angeles", "America/Denver",
"America/Chicago", "America/New_York", "Africa/Cairo", "Africa/Addis_Ababa", "Africa/Dar_es_Salaam",
"Africa/Freetown", "Africa/Johannesburg", "Africa/Nairobi", "Asia/Bangkok", "Asia/Baghdad",
"Asia/Calcutta", "Asia/Hong_Kong", "Asia/Jakarta", "Asia/Jerusalem", "Asia/Manila", "Asia/Tokyo",
"Europe/Amsterdam", "Europe/Athens", "Europe/Berlin", "Europe/London", "Europe/Malta", "Europe/Moscow",
"Europe/Paris", "Europe/Rome"
};
public Object[] getTestObjects()
{
OlsonTimeZone timeZones[] = new OlsonTimeZone[zoneIDs.length];
for (int i = 0; i < zoneIDs.length; i += 1) {
timeZones[i] = new OlsonTimeZone(zoneIDs[i]);
}
return timeZones;
}
public boolean hasSameBehavior(Object a, Object b)
{
OlsonTimeZone otz_a = (OlsonTimeZone) a;
OlsonTimeZone otz_b = (OlsonTimeZone) b;
long now = System.currentTimeMillis();
int a_offsets[] = {0, 0};
int b_offsets[] = {0, 0};
otz_a.getOffset(now, false, a_offsets);
otz_b.getOffset(now, false, b_offsets);
return a_offsets[0] == b_offsets[0] && a_offsets[1] == b_offsets[1];
}
}
private static class BigDecimalTest implements Handler
{
String values[] = {
"1234567890.",
"123456789.0",
"12345678.90",
"1234567.890",
"123456.7890",
"12345.67890",
"1234.567890",
"123.4567890",
"12.34567890",
"1.234567890",
".1234567890"};
public Object[] getTestObjects()
{
BigDecimal bds[] = new BigDecimal[values.length];
for (int i = 0; i < values.length; i += 1) {
bds[i] = new BigDecimal(values[i]);
}
return bds;
}
public boolean hasSameBehavior(Object a, Object b) {
BigDecimal bda = (BigDecimal) a;
BigDecimal bdb = (BigDecimal) b;
return bda.toString().equals(bdb.toString());
}
}
private static class MathContextTest implements Handler
{
int forms[] = {MathContext.PLAIN, MathContext.ENGINEERING, MathContext.SCIENTIFIC};
int rounds[] = {
MathContext.ROUND_CEILING, MathContext.ROUND_DOWN, MathContext.ROUND_FLOOR,
MathContext.ROUND_HALF_DOWN, MathContext.ROUND_HALF_EVEN, MathContext.ROUND_HALF_UP,
MathContext.ROUND_UNNECESSARY, MathContext.ROUND_UP};
public Object[] getTestObjects()
{
int objectCount = forms.length * rounds.length;
MathContext contexts[] = new MathContext[objectCount];
int i = 0;
for (int f = 0; f < forms.length; f += 1) {
for (int r = 0; r < rounds.length; r += 1) {
int digits = f * r;
boolean lostDigits = (r & 1) != 0;
contexts[i++] = new MathContext(digits, forms[f], lostDigits, rounds[r]);
}
}
return contexts;
}
public boolean hasSameBehavior(Object a, Object b)
{
MathContext mca = (MathContext) a;
MathContext mcb = (MathContext) b;
return mca.toString().equals(mcb.toString());
}
}
private static HashMap map = new HashMap();
static {
map.put("com.ibm.icu.util.TimeZone", new TimeZoneTest());
map.put("com.ibm.icu.util.SimpleTimeZone", new SimpleTimeZoneTest());
map.put("com.ibm.icu.util.ULocale", new ULocaleTest());
map.put("com.ibm.icu.util.Currency", new CurrencyTest());
map.put("com.ibm.icu.impl.OlsonTimeZone", new OlsonTimeZoneTest());
map.put("com.ibm.icu.math.BigDecimal", new BigDecimalTest());
map.put("com.ibm.icu.math.MathContext", new MathContextTest());
map.put("com.ibm.icu.text.NumberFormat", new FormatTests.NumberFormatTest());
map.put("com.ibm.icu.text.DecimalFormat", new FormatTests.DecimalFormatTest());
map.put("com.ibm.icu.text.RuleBasedNumberFormat", new FormatTests.RuleBasedNumberFormatTest());
map.put("com.ibm.icu.text.DecimalFormatSymbols", new FormatTests.DecimalFormatSymbolsTest());
map.put("com.ibm.icu.text.MessageFormat", new FormatTests.MessageFormatTest());
map.put("com.ibm.icu.text.DateFormat", new FormatTests.DateFormatTest());
map.put("com.ibm.icu.text.DateFormatSymbols", new FormatTests.DateFormatSymbolsTest());
map.put("com.ibm.icu.text.SimpleDateFormat", new FormatTests.SimpleDateFormatTest());
map.put("com.ibm.icu.text.ChineseDateFormat", new FormatTests.ChineseDateFormatTest());
map.put("com.ibm.icu.text.ChineseDateFormatSymbols", new FormatTests.ChineseDateFormatSymbolsTest());
map.put("com.ibm.icu.util.Calendar", new CalendarTests.CalendarTest());
map.put("com.ibm.icu.util.BuddhistCalendar", new CalendarTests.BuddhistCalendarTest());
map.put("com.ibm.icu.util.ChineseCalendar", new CalendarTests.ChineseCalendarTest());
map.put("com.ibm.icu.util.CopticCalendar", new CalendarTests.CopticCalendarTest());
map.put("com.ibm.icu.util.EthiopicCalendar", new CalendarTests.EthiopicCalendarTest());
map.put("com.ibm.icu.util.GregorianCalendar", new CalendarTests.GregorianCalendarTest());
map.put("com.ibm.icu.util.HebrewCalendar", new CalendarTests.HebrewCalendarTest());
map.put("com.ibm.icu.util.IslamicCalendar", new CalendarTests.IslamicCalendarTest());
map.put("com.ibm.icu.util.JapaneseCalendar", new CalendarTests.JapaneseCalendarTest());
map.put("com.ibm.icu.text.ArabicShapingException", new ExceptionTests.ArabicShapingExceptionTest());
map.put("com.ibm.icu.text.StringPrepParseException", new ExceptionTests.StringPrepParseExceptionTest());
map.put("com.ibm.icu.util.UResourceTypeMismatchException", new ExceptionTests.UResourceTypeMismatchExceptionTest());
}
public void testDirectory(File dir)
{
File files[] = dir.listFiles();
for (int i = 0; i < files.length; i += 1) {
check(files[i]);
}
}
public void check(File file)
{
String filename = file.getName();
int ix = filename.lastIndexOf(".dat");
if (ix < 0) {
return;
}
String className = filename.substring(0, ix);
Handler handler = getHandler(className);
System.out.print(className + " - ");
if (handler == null) {
System.out.println("no test.");
return;
}
try {
FileInputStream fs = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fs);
Object inputObjects[] = (Object[]) in.readObject();
Object testObjects[] = handler.getTestObjects();
boolean passed = true;
in.close();
fs.close();
// TODO: add equality test...
for (int i = 0; i < testObjects.length; i += 1) {
if (! handler.hasSameBehavior(inputObjects[i], testObjects[i])) {
passed = false;
System.out.println("Input object " + i + " failed behavior test.");
}
}
if (passed) {
System.out.println("test passed.");
}
} catch (Exception e) {
System.out.println("Error processing test object: " + e.toString());
}
}
public static void main(String[] args)
{
SerializableTest test = new SerializableTest();
List argList = Arrays.asList(args);
boolean write = false;
for (Iterator it = argList.iterator(); it.hasNext(); /*anything?*/) {
String arg = (String) it.next();
try {
File dir = new File(arg);
if (! dir.isDirectory()) {
System.out.println(dir + " is not a directory.");
continue;
}
System.out.println("Checking test data from " + arg + ":");
test.testDirectory(dir);
} catch (Exception e) {
System.out.println("Error processing " + arg + ": " + e.getMessage());
}
}
}
}

View File

@ -9,9 +9,23 @@
package com.ibm.icu.dev.tool.serializable;
import com.ibm.icu.impl.URLHandler;
import com.ibm.icu.dev.test.serializable.SerializableTest;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.Class;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* This class examines all the classes in a Jar file or a directory
@ -23,18 +37,50 @@ import java.net.URL;
public class SerializableChecker implements URLHandler.URLVisitor
{
private static Class serializable;
private static Class throwable;
//private static Class throwable;
private String path = null;
private boolean write;
public SerializableChecker(String path)
{
this.path = path;
if (path != null) {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
}
}
static {
try {
serializable = Class.forName("java.io.Serializable");
throwable = Class.forName("java.lang.Throwable");
//throwable = Class.forName("java.lang.Throwable");
} catch (Exception e) {
// we're in deep trouble...
System.out.println("Woops! Can't get class info for Serializable and Throwable.");
}
}
private void writeFile(String className, byte bytes[])
{
File file = new File(path + File.separator + className + ".dat");
FileOutputStream stream;
try {
stream = new FileOutputStream(file);
stream.write(bytes);
stream.close();
} catch (Exception e) {
System.out.print(" - can't write file!");
}
}
public void visit(String str)
{
int ix = str.lastIndexOf(".class");
@ -49,39 +95,112 @@ public class SerializableChecker implements URLHandler.URLVisitor
try {
Class c = Class.forName(className);
int m = c.getModifiers();
if (serializable.isAssignableFrom(c) && ! throwable.isAssignableFrom(c)) {
if (serializable.isAssignableFrom(c) /*&&
(! throwable.isAssignableFrom(c) || c.getDeclaredFields().length > 0)*/) {
Field uid;
System.out.print(className);
System.out.print(className + " (" + Modifier.toString(m) + ") - ");
try {
uid = c.getDeclaredField("serialVersionUID");
} catch (Exception e) {
System.out.print(" - no serialVersionUID!");
System.out.print("no serialVersionUID - ");
}
if (Modifier.isPublic(m)) {
SerializableTest.Handler handler = SerializableTest.getHandler(className);
if (handler != null) {
Object objectsOut[] = handler.getTestObjects();
Object objectsIn[];
boolean passed = true;
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
try {
out.writeObject(objectsOut);
out.close();
byteOut.close();
} catch (IOException e) {
System.out.println("Eror writing test objects:" + e.toString());
return;
}
if (path != null) {
writeFile(className, byteOut.toByteArray());
}
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
try {
objectsIn = (Object[]) in.readObject();
in.close();
byteIn.close();
} catch (Exception e) {
System.out.println("Error reading test objects:" + e.toString());
return;
}
for(int i = 0; i < objectsIn.length; i += 1) {
if (! handler.hasSameBehavior(objectsIn[i], objectsOut[i])) {
passed = false;
System.out.println("Object " + i + " failed behavior test.");
}
}
if (passed) {
System.out.print("test passed.");
}
} else {
// it's OK to not have tests for abstract classes...
if (! Modifier.isAbstract(m)) {
System.out.print("no test.");
}
}
}
System.out.println();
}
} catch (Exception e) {
System.out.println("Error processing " + className);
System.out.println("Error processing " + className + ": " + e.toString());
}
}
}
public static void main(String[] args) throws java.net.MalformedURLException
{
try {
//URL jarURL = new URL("jar:file:/dev/eclipse/workspace/icu4j/icu4j.jar!/com/ibm/icu");
//URL fileURL = new URL("file:/dev/eclipse/workspace/icu4j/classes/com/ibm/icu");
URL url = new URL(args[0]);
URLHandler handler = URLHandler.get(url);
SerializableChecker checker = new SerializableChecker();
List argList = Arrays.asList(args);
String path = null;
for (Iterator it = argList.iterator(); it.hasNext(); /*anything?*/) {
String arg = (String) it.next();
System.out.println("Checking classes from " + args[0] + ":");
handler.guide(checker, true, false);
} catch (Exception e) {
System.out.println("Error processing URL \"" + args[0] + "\" - " + e.getMessage());
if (arg.equals("-w")) {
if (it.hasNext()) {
path = (String) it.next();
} else {
System.out.println("Missing directory name on -w command.");
}
} else {
try {
//URL jarURL = new URL("jar:file:/dev/eclipse/workspace/icu4j/icu4j.jar!/com/ibm/icu");
//URL fileURL = new URL("file:/dev/eclipse/workspace/icu4j/classes/com/ibm/icu");
URL url = new URL(arg);
URLHandler handler = URLHandler.get(url);
SerializableChecker checker = new SerializableChecker(path);
System.out.println("Checking classes from " + arg + ":");
handler.guide(checker, true, false);
} catch (Exception e) {
System.out.println("Error processing URL \"" + arg + "\" - " + e.getMessage());
}
}
}
}
}