1 package junitparams; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 import java.text.ParseException; 8 import java.text.SimpleDateFormat; 9 import java.util.Arrays; 10 import java.util.Calendar; 11 import java.util.Date; 12 import java.util.List; 13 14 import org.junit.Test; 15 import org.junit.runner.RunWith; 16 17 import junitparams.converters.ConversionFailedException; 18 import junitparams.converters.ConvertParam; 19 import junitparams.converters.Converter; 20 import junitparams.converters.Param; 21 import junitparams.converters.ParamConverter; 22 23 import static org.assertj.core.api.Assertions.*; 24 25 @RunWith(JUnitParamsRunner.class) 26 public class ParamsConverterTest { 27 28 @Test 29 @Parameters({"01.12.2012"}) convertSingleParam( @onvertParamvalue = StringToDateConverter.class, options = "dd.MM.yyyy") Date date)30 public void convertSingleParam( 31 @ConvertParam(value = StringToDateConverter.class, options = "dd.MM.yyyy") Date date) { 32 Calendar calendar = createCalendarWithDate(date); 33 assertCalendarDate(calendar); 34 } 35 36 @Test 37 @Parameters({"01.12.2012,A"}) convertMultipleParams( @onvertParamvalue = StringToDateConverter.class, options = "dd.MM.yyyy") Date date, @ConvertParam(LetterToNumberConverter.class) int num)38 public void convertMultipleParams( 39 @ConvertParam(value = StringToDateConverter.class, options = "dd.MM.yyyy") Date date, 40 @ConvertParam(LetterToNumberConverter.class) int num) { 41 Calendar calendar = createCalendarWithDate(date); 42 assertCalendarDate(calendar); 43 assertThat(num).isEqualTo(1); 44 } 45 46 @Test 47 @Parameters(method = "params") convertParamsFromMethod( @onvertParamvalue = StringToDateConverter.class, options = "dd.MM.yyyy") Date date)48 public void convertParamsFromMethod( 49 @ConvertParam(value = StringToDateConverter.class, options = "dd.MM.yyyy") Date date) { 50 Calendar calendar = createCalendarWithDate(date); 51 assertCalendarDate(calendar); 52 } 53 54 @Test 55 @Parameters({"01.12.2012"}) convertParamsUsingParamAnnotation(@aramconverter = SimpleDateConverter.class) Date date)56 public void convertParamsUsingParamAnnotation(@Param(converter = SimpleDateConverter.class) Date date) { 57 Calendar calendar = createCalendarWithDate(date); 58 assertCalendarDate(calendar); 59 } 60 61 @Test 62 @Parameters({"01.12.2012"}) convertParamsUsingCustomParamAnnotation(@ateParam Date date)63 public void convertParamsUsingCustomParamAnnotation(@DateParam Date date) { 64 Calendar calendar = createCalendarWithDate(date); 65 assertCalendarDate(calendar); 66 } 67 68 @Test 69 @Parameters(method = "params") convertParamsFromMethodUsingCustomParamAnnotation(@ateParam Date date)70 public void convertParamsFromMethodUsingCustomParamAnnotation(@DateParam Date date) { 71 Calendar calendar = createCalendarWithDate(date); 72 assertCalendarDate(calendar); 73 } 74 params()75 private List<String> params() { 76 return Arrays.asList("01.12.2012"); 77 } 78 79 @Test 80 @Parameters({"2012-12-01"}) convertParamsUsingCustomParamAnnotationOverridingAttributes(@ateParamformat = "yyyy-MM-dd") Date date)81 public void convertParamsUsingCustomParamAnnotationOverridingAttributes(@DateParam(format = "yyyy-MM-dd") Date date) { 82 Calendar calendar = createCalendarWithDate(date); 83 assertCalendarDate(calendar); 84 } 85 86 @Test 87 @Parameters({"2012-12-01"}) passesParametersWithOtherAnnotations(@ther String parameter)88 public void passesParametersWithOtherAnnotations(@Other String parameter) { 89 assertThat(parameter) 90 .isExactlyInstanceOf(String.class) 91 .isEqualTo("2012-12-01"); 92 } 93 assertCalendarDate(Calendar calendar)94 private void assertCalendarDate(Calendar calendar) { 95 assertThat(calendar.get(Calendar.YEAR)).isEqualTo(2012); 96 assertThat(calendar.get(Calendar.MONTH)).isEqualTo(11); 97 assertThat(calendar.get(Calendar.DAY_OF_MONTH)).isEqualTo(1); 98 } 99 createCalendarWithDate(Date date)100 private Calendar createCalendarWithDate(Date date) { 101 Calendar calendar = Calendar.getInstance(); 102 calendar.setTime(date); 103 return calendar; 104 } 105 106 public static class LetterToNumberConverter implements ParamConverter<Integer> { convert(Object param, String options)107 public Integer convert(Object param, String options) { 108 return param.toString().charAt(0) - 64; 109 } 110 } 111 112 public static class StringToDateConverter implements ParamConverter<Date> { convert(Object param, String options)113 public Date convert(Object param, String options) { 114 try { 115 return new SimpleDateFormat(options).parse(param.toString()); 116 } catch (ParseException e) { 117 throw new RuntimeException(e); 118 } 119 } 120 } 121 122 @Retention(RetentionPolicy.RUNTIME) 123 @Target(ElementType.PARAMETER) 124 @Param(converter = FormattedDateConverter.class) 125 public @interface DateParam { 126 format()127 String format() default "dd.MM.yyyy"; 128 } 129 130 @Retention(RetentionPolicy.RUNTIME) 131 @Target(ElementType.PARAMETER) 132 public @interface Other { 133 } 134 135 public static class FormattedDateConverter implements Converter<DateParam, Date> { 136 137 private String format; 138 139 @Override initialize(DateParam annotation)140 public void initialize(DateParam annotation) { 141 this.format = annotation.format(); 142 } 143 144 @Override convert(Object param)145 public Date convert(Object param) throws ConversionFailedException { 146 try { 147 return new SimpleDateFormat(format).parse(param.toString()); 148 } catch (ParseException e) { 149 throw new ConversionFailedException("failed"); 150 } 151 } 152 } 153 154 public static class SimpleDateConverter implements Converter<Param, Date> { 155 @Override initialize(Param annotation)156 public void initialize(Param annotation) { 157 } 158 159 @Override convert(Object param)160 public Date convert(Object param) throws ConversionFailedException { 161 try { 162 return new SimpleDateFormat("dd.MM.yyyy").parse(param.toString()); 163 } catch (ParseException e) { 164 throw new ConversionFailedException("failed"); 165 } 166 } 167 } 168 } 169