package jargs.test.gnu;

import jargs.gnu.CmdLineParser;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

import junit.framework.TestCase;

public class CustomOptionTestCase extends TestCase {

    public CustomOptionTestCase(String name) {
        super(name);
    }

    public void testCustomOption() throws Exception {
        CmdLineParser parser = new CmdLineParser();
        CmdLineParser.Option date =
            parser.addOption(new ShortDateOption('d', "date"));
        parser.parse(new String[]{"-d", "11/03/2003"}, Locale.UK);
        Date d = (Date)parser.getOptionValue(date);
        assertEquals(11, d.getDay());
        assertEquals(3, d.getMonth());
        assertEquals(2003, d.getYear());

        parser.parse(new String[]{"-d", "11/03/2003"}, Locale.US);
        d = (Date)parser.getOptionValue(date);
        assertEquals(3, d.getDay());
        assertEquals(11, d.getMonth());
        assertEquals(2003, d.getYear());
    }

    public void testIllegalCustomOption() throws Exception {
        CmdLineParser parser = new CmdLineParser();
        CmdLineParser.Option date =
            parser.addOption(new ShortDateOption('d', "date"));
        try {
            parser.parse(new String[]{"-d", "foobar"}, Locale.US);
            fail("Expected IllegalOptionValueException");
        }
        catch (CmdLineParser.IllegalOptionValueException e) {
            //pass
        }
    }

    public static class ShortDateOption extends CmdLineParser.Option {
        public ShortDateOption( char shortForm, String longForm ) {
            super(shortForm, longForm, true);
        }
        protected Object parseValue( String arg, Locale locale )
            throws CmdLineParser.IllegalOptionValueException {
            try {
                DateFormat dateFormat =
                    DateFormat.getDateInstance(DateFormat.SHORT, locale);
                return dateFormat.parse(arg);
            }
            catch (ParseException e) {
                throw new CmdLineParser.IllegalOptionValueException(this, arg);
            }
        }
    }


}

@@ -113,16 +113,43 @@
 eption;%0A
+import java.util.Calendar;%0A
 import j
@@ -398,32 +398,84 @@
 ows Exception %7B%0A
+        Calendar calendar = Calendar.getInstance();%0A
         CmdLineP
@@ -602,32 +602,33 @@
 ('d', %22date%22));%0A
+%0A
         parser.p
@@ -742,347 +742,598 @@
     
-assertEquals(11, d.getDay());%0A        assertEquals(3, d.getMonth());%0A        assertEquals(2003, d.getYear());%0A%0A        parser.parse(new String%5B%5D%7B%22-d%22, %2211/03/2003%22%7D, Locale.US);%0A        d = (Date)parser.getOptionValue(date);%0A        assertEquals(3, d.getDay());%0A        assertEquals(11, d.getMonth());%0A        assertEquals(2003, d.getYear(
+calendar.setTime(d);%0A        assertEquals(11,             calendar.get(Calendar.DAY_OF_MONTH));%0A        assertEquals(Calendar.MARCH, calendar.get(Calendar.MONTH));%0A        assertEquals(2003,           calendar.get(Calendar.YEAR));%0A%0A        parser.parse(new String%5B%5D%7B%22-d%22, %2211/03/2003%22%7D, Locale.US);%0A        d = (Date)parser.getOptionValue(date);%0A        calendar.setTime(d);%0A        assertEquals(3,                 calendar.get(Calendar.DAY_OF_MONTH));%0A        assertEquals(Calendar.NOVEMBER, calendar.get(Calendar.MONTH));%0A        assertEquals(2003,              calendar.get(Calendar.YEAR
 ));%0A

Replace calls to Date.get{Day,Month,Year} -- which are now deprecated -- with calls to Calendar.get instead.