Very good!
I was hoping that if I plant a seed, a tree starts to grow ;)
need to check out and test the new functionality right away...
Great!
regards,
Martin
On 10/7/05, Arvid Hülsebus <[EMAIL PROTECTED]> wrote:
> Looks like I'm ending up spending as much time on date parsing as Martin...
>
> Parsing of MMM and MMMM starts to work now, too. Additionally I looked
> into parsing and formatting of yy vs. yyyy.
>
> Regards,
> Arvid
>
>
> import org.mozilla.javascript.JavaScriptException;
>
> import java.text.SimpleDateFormat;
> import java.text.DecimalFormat;
> import java.text.ParseException;
> import java.util.Date;
> import java.util.Locale;
> import java.util.Calendar;
> import java.io.IOException;
>
> public class DateTest extends JavaScriptTestCase {
>
> private static final int[] YEAR_MONTH_DAY
> = {Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH};
>
> protected void setUp() throws Exception {
> super.setUp();
> // loadScriptFile("debug.js");
> loadScriptFile("prototype-basics.js");
> loadScriptFile("date.js");
> }
>
> public void testNumberOnlyDateFormats() throws IOException,
> JavaScriptException {
> Date date = new Date();
>
> checkFormat("yyyyMMdd", date, YEAR_MONTH_DAY);
> checkFormat("ddMMyyyy", date, YEAR_MONTH_DAY);
> checkFormat("dd/MM/yyyy", date, YEAR_MONTH_DAY);
> checkFormat("dd/MM/yyy", date, YEAR_MONTH_DAY);
> checkFormat("dd/MM/yy", date, YEAR_MONTH_DAY);
> checkFormat("EddMMyyyy", date, YEAR_MONTH_DAY);
> }
>
> public void testEnglishMonths() throws IOException {
> for (int month = 0; month < 12; ++month) {
> Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
> calendar.set(Calendar.MONTH, month);
> Date date = calendar.getTime();
>
> StringBuffer format = new StringBuffer("M");
> for (int i = 0; i < 4; ++i) {
> format.append('M');
> checkFormat(format.toString(), date, new int[] {Calendar.MONTH});
> }
> }
> }
>
> public void testTwoDigitYears() throws IOException, ParseException {
> DecimalFormat decimalFormat = new DecimalFormat("00");
> SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy",
> Locale.ENGLISH);
> Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
> for (int year = 0; year < 100; ++year) {
> String yearString = decimalFormat.format(year);
> calendar.setTime(simpleDateFormat.parse(yearString));
> assertEquals(calendar.get(Calendar.YEAR), evalParseDate(yearString,
> "yy").get(Calendar.YEAR));
> }
> }
>
> public void testEnglishWeekDays() throws IOException {
> for (int day = 1; day <= 7; ++day) {
> Calendar calendar = Calendar.getInstance();
> calendar.set(Calendar.DAY_OF_WEEK, day);
> Date date = calendar.getTime();
>
> StringBuffer format = new StringBuffer();
> for (int i = 0; i < 4; ++i) {
> format.append('E');
> checkFormat(format.toString(), date, new int[0]); // XXX new int[]
> {Calendar.DAY_OF_WEEK}
> }
> }
> }
>
> private Object evalFormatDate(Date date, String format) {
> return eval("new SimpleDateFormat(\"" + format + "\")"
> + ".format(new Date(" + date.getTime() + "))");
> }
>
> private Calendar evalParseDate(String input, String format) {
> long time = evalLong("new SimpleDateFormat(\"" + format + "\")"
> + ".parse(\"" + input + "\").getTime()");
> Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
> calendar.setTime(new Date(time));
> return calendar;
> }
>
> private void checkFormat(String format, Date date, int[] fields) {
> SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format,
> Locale.ENGLISH);
> assertEquals(simpleDateFormat.format(date), evalFormatDate(date, format));
> Calendar calendar1 = Calendar.getInstance(Locale.ENGLISH);
> calendar1.setTime(date);
> Calendar calendar2 = evalParseDate(simpleDateFormat.format(date),
> format.toString());
> for (int i = 0; i < fields.length; i++) {
> int field = fields[i];
> checkField(calendar1, calendar2, field);
> }
> }
>
> private void checkField(Calendar calendar1, Calendar calendar2, int field) {
> assertEquals(calendar1.get(field), calendar2.get(field));
> }
>
> }
>
>
> import junit.framework.TestCase;
> import org.mozilla.javascript.Context;
> import org.mozilla.javascript.Scriptable;
> import org.mozilla.javascript.JavaScriptException;
>
> import java.io.IOException;
> import java.io.FileReader;
> import java.util.Date;
>
> public class JavaScriptTestCase extends TestCase {
>
> protected Context cx;
> protected Scriptable scope;
>
> protected void setUp() throws Exception {
> super.setUp();
> cx = Context.enter();
> scope = cx.initStandardObjects(null);
> }
>
> protected void tearDown() throws Exception {
> Context.exit();
> }
>
> protected Object eval(String script) throws JavaScriptException {
> return cx.evaluateString(scope, script, "test", 1, null);
> }
>
> protected int evalInt(String script) throws JavaScriptException {
> Object o = eval(script);
> if (o instanceof Number) {
> return ((Number) o).intValue();
> }
> throw new JavaScriptException(null, "invalid return type "
> + o.getClass().getName() + " with value " + o, 0);
> }
>
> protected long evalLong(String script) throws JavaScriptException {
> Object o = eval(script);
> if (o instanceof Number) {
> return ((Number) o).longValue();
> }
> throw new JavaScriptException(null, "invalid return type "
> + o.getClass().getName() + " with value " + o, 0);
> }
>
> protected boolean evalBoolean(String script) throws JavaScriptException {
> Object o = eval(script);
> if (o instanceof Boolean) {
> return ((Boolean) o).booleanValue();
> }
> throw new JavaScriptException(null, "invalid return type "
> + o.getClass().getName() + " with value " + o, 0);
> }
>
> // XXX directory handling
> protected void loadScriptFile(String jsFile)
> throws IOException, JavaScriptException {
> cx.evaluateReader(scope, new FileReader(jsFile), jsFile, 0, null);
> }
> }
>
>
>
>
--
http://www.irian.at
Your JSF powerhouse -
JSF Trainings in English and German