package junitparams.paramconverters;

import static org.junit.Assert.*;

import java.text.*;
import java.util.*;

import junitparams.*;
import junitparams.converters.*;

import org.junit.*;
import org.junit.runner.*;

@RunWith(JUnitParamsRunner.class)
public class ParamsConverterTest {

    @Test
    @Parameters({ "01.12.2012" })
    public void convertSingleParam(
        @ConvertParam(value = StringToDateConverter.class, options = "dd.MM.yyyy") Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        assertEquals(2012, calendar.get(Calendar.YEAR));
        assertEquals(11, calendar.get(Calendar.MONTH));
        assertEquals(1, calendar.get(Calendar.DAY_OF_MONTH));
    }

    @Test
    @Parameters({ "01.12.2012,A" })
    public void convertMultipleParams(
        @ConvertParam(value = StringToDateConverter.class, options = "dd.MM.yyyy") Date date,
        @ConvertParam(LetterToNumberConverter.class) int num) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        assertEquals(2012, calendar.get(Calendar.YEAR));
        assertEquals(11, calendar.get(Calendar.MONTH));
        assertEquals(1, calendar.get(Calendar.DAY_OF_MONTH));

        assertEquals(1, num);
    }

    public static class LetterToNumberConverter implements ParamConverter<Integer> {
        public Integer convert(Object param, String options) {
            return param.toString().charAt(0) - 64;
        }
    }

    public static class StringToDateConverter implements ParamConverter<Date> {
        public Date convert(Object param, String options) {
            try {
                return new SimpleDateFormat(options).parse(param.toString());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

    }

}

@@ -114,27 +114,25 @@
 %0Aimport 
+org.
 junit
-params
 .*;%0Aimpo
@@ -138,30 +138,24 @@
 ort 
+org.
 junit
-params.converters
+.runner
 .*;%0A
@@ -158,33 +158,35 @@
 .*;%0A%0Aimport 
-org.
 junit
+params
 .*;%0Aimport o
@@ -184,32 +184,38 @@
 %0Aimport 
-org.
 junit
-.runn
+params.convert
 er
+s
 .*;%0A%0A@Ru
@@ -481,32 +481,38 @@
  calendar = 
+create
 Calendar
 .getInstance
@@ -503,167 +503,367 @@
 ndar
-.getInstance();%0A        calendar.setTime(date);%0A%0A        assertEquals(2012, calendar.get(Calendar.YEAR));%0A        assertEquals(11, calendar.get(Calendar.MONTH)
+WithDate(date);%0A        assertCalendarDate(calendar);%0A    %7D%0A%0A    @Test%0A    @Parameters(%7B %2201.12.2012,A%22 %7D)%0A    public void convertMultipleParams(%0A        @ConvertParam(value = StringToDateConverter.class, options = %22dd.MM.yyyy%22) Date date,%0A        @ConvertParam(LetterToNumberConverter.class) int num) %7B%0A        Calendar calendar = createCalendarWithDate(date
 );%0A 
@@ -879,53 +879,59 @@
 sert
-Equals(1, c
+C
 alendar
-.get(C
+Date(c
 alendar
-.DAY_OF_MONTH)
+);%0A        assertEquals(1, num
 );%0A 
@@ -962,34 +962,33 @@
 ameters(
-%7B %2201.12.2012,A%22 %7D
+method = %22params%22
 )%0A    pu
@@ -1004,32 +1004,38 @@
  convert
-MultipleParams(%0A
+ParamsFromMethod(%0A    
         
@@ -1110,33 +1110,35 @@
 yyyy%22) Date date
-,
+) %7B
 %0A        @Conver
@@ -1134,64 +1134,57 @@
     
-@ConvertParam(LetterToNumberConverter.class) int num) %7B%0A
+Calendar calendar = createCalendarWithDate(date);
 %0A   
@@ -1184,32 +1184,38 @@
 e);%0A        
+assert
 Calendar
  calendar = 
@@ -1202,25 +1202,29 @@
 Calendar
- 
+Date(
 calendar
  = Calen
@@ -1219,67 +1219,161 @@
 ndar
- = Calendar.getInstance(
+);%0A    %7D%0A%0A    private List%3CString%3E params() %7B%0A        return Arrays.asList(%2201.12.2012%22
 );%0A    
+%7D%0A%0A
     
-calendar.setTime(date);%0A
+private void assertCalendarDate(Calendar calendar) %7B
 %0A   
@@ -1548,37 +1548,179 @@
 ));%0A
-%0A
     
+%7D%0A%0A
     
-assertEquals(1, num)
+private Calendar createCalendarWithDate(Date date) %7B%0A        Calendar calendar = Calendar.getInstance();%0A        calendar.setTime(date);%0A        return calendar
 ;%0A  

test for converting params given in a method