1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.controller.impl;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.io.UnsupportedEncodingException;
25 import java.lang.reflect.Method;
26 import java.math.BigDecimal;
27 import java.math.BigInteger;
28 import java.sql.Time;
29 import java.sql.Timestamp;
30 import java.text.SimpleDateFormat;
31 import java.util.Calendar;
32 import java.util.Collections;
33 import java.util.Date;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Set;
39
40 import org.apache.commons.fileupload.FileItem;
41 import org.seasar.cubby.action.Action;
42 import org.seasar.cubby.action.ActionResult;
43 import org.seasar.cubby.action.Form;
44 import org.seasar.cubby.action.RequestParameter;
45 import org.seasar.cubby.action.RequestParameterBindingType;
46 import org.seasar.cubby.controller.RequestParameterBinder;
47 import org.seasar.extension.unit.S2TestCase;
48
49
50
51
52
53 public class RequestParameterBinderImplTest extends S2TestCase {
54
55 public RequestParameterBinder requestParameterBinder;
56
57 @Override
58 protected void setUp() throws Exception {
59 super.setUp();
60 include(getClass().getName().replace('.', '/') + ".dicon");
61 }
62
63 public void testMapToBeanNullSource() {
64 FormDto dto = new FormDto();
65 requestParameterBinder.bind(null, dto, MockAction.class, actionMethod(
66 MockAction.class, "all"));
67 }
68
69 public void testMapToBean() {
70 Map<String, Object[]> map = new HashMap<String, Object[]>();
71 map.put("date", new Object[] { "2006-01-01" });
72
73 FormDto dto = new FormDto();
74
75 requestParameterBinder.bind(map, dto, MockAction.class, actionMethod(
76 MockAction.class, "all"));
77 Calendar cal = Calendar.getInstance();
78 cal.set(2006, 0, 1);
79 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
80 assertEquals(format.format(cal.getTime()), format.format(dto.date));
81 }
82
83 public void testMapToBean_OneValue() {
84 Map<String, Object[]> map = new HashMap<String, Object[]>();
85 map.put("num1", new Object[] { "1" });
86 map.put("num2", new Object[] { "2" });
87 map.put("num3", new Object[] { "def" });
88
89 FormDto dto = new FormDto();
90
91 requestParameterBinder.bind(map, dto, MockAction.class, actionMethod(
92 MockAction.class, "all"));
93 assertNotNull(dto.num1);
94 assertEquals(Integer.valueOf(1), dto.num1);
95 assertNotNull(dto.num2);
96 assertEquals(1, dto.num2.length);
97 assertEquals(Integer.valueOf(2), dto.num2[0]);
98 assertNotNull(dto.num3);
99 assertEquals(1, dto.num3.size());
100 assertEquals("def", dto.num3.get(0));
101 }
102
103 public void testMapToBean_MultiValue() {
104 Map<String, Object[]> map = new HashMap<String, Object[]>();
105 map.put("num2", new Object[] { "1", "2" });
106 map.put("num3", new Object[] { "abc", "def" });
107
108 FormDto dto = new FormDto();
109
110 requestParameterBinder.bind(map, dto, MockAction.class, actionMethod(
111 MockAction.class, "all"));
112 assertNotNull(dto.num2);
113 assertEquals(2, dto.num2.length);
114 assertEquals(Integer.valueOf(1), dto.num2[0]);
115 assertEquals(Integer.valueOf(2), dto.num2[1]);
116 assertNotNull(dto.num3);
117 assertEquals(2, dto.num3.size());
118 assertEquals("abc", dto.num3.get(0));
119 assertEquals("def", dto.num3.get(1));
120 }
121
122 public void testMapToBean_MultiValueIncludesEmptyValue() {
123 Map<String, Object[]> map = new HashMap<String, Object[]>();
124 map.put("num2", new String[] { "1", "", "2" });
125
126 FormDto dto = new FormDto();
127
128 requestParameterBinder.bind(map, dto, MockAction.class, actionMethod(
129 MockAction.class, "all"));
130 assertEquals(3, dto.num2.length);
131 assertEquals(Integer.valueOf(1), dto.num2[0]);
132 assertEquals(null, dto.num2[1]);
133 assertEquals(Integer.valueOf(2), dto.num2[2]);
134 }
135
136 public void testMapToBean_MultiValueIncludesNullValue() {
137 Map<String, Object[]> map = new HashMap<String, Object[]>();
138 map.put("num3", new String[] { "zzz", null, "xxx" });
139
140 FormDto dto = new FormDto();
141
142 requestParameterBinder.bind(map, dto, MockAction.class, actionMethod(
143 MockAction.class, "all"));
144 assertEquals(3, dto.num3.size());
145 assertEquals("zzz", dto.num3.get(0));
146 assertNull(dto.num3.get(1));
147 assertEquals("xxx", dto.num3.get(2));
148 }
149
150 public void testConverters() {
151 Map<String, Object[]> map = new HashMap<String, Object[]>();
152 map.put("decimal", new Object[] { "12.3" });
153 map.put("decimals", new Object[] { "45.6", "78.9" });
154 map.put("bigint", new Object[] { "9876" });
155 map.put("bigints", new Object[] { "5432", "10" });
156 map.put("bool1", new Object[] { "true" });
157 map.put("bools1", new Object[] { "true", "false" });
158 map.put("bool2", new Object[] { "false" });
159 map.put("bools2", new Object[] { "false", "true", "false" });
160 map.put("byte1", new Object[] { "12" });
161 map.put("bytes1", new Object[] { "34", "56" });
162 map.put("byte2", new Object[] { "98" });
163 map.put("bytes2", new Object[] { "76", "54" });
164 map.put("char1", new Object[] { "a" });
165 map.put("chars1", new Object[] { "b", "c" });
166 map.put("char2", new Object[] { "d" });
167 map.put("chars2", new Object[] { "e", "f" });
168 map.put("date", new Object[] { "2008-7-28" });
169 map.put("dates", new Object[] { "2008-8-14", "2008-10-30" });
170 map.put("double1", new Object[] { "1.2" });
171 map.put("doubles1", new Object[] { "3.4", "5.6" });
172 map.put("double2", new Object[] { "9.8" });
173 map.put("doubles2", new Object[] { "7.6", "5.4" });
174 map.put("en", new Object[] { "VALUE1" });
175 map.put("ens", new Object[] { "VALUE2", "VALUE3" });
176 map.put("float1", new Object[] { "1.2" });
177 map.put("floats1", new Object[] { "3.4", "5.6" });
178 map.put("float2", new Object[] { "9.8" });
179 map.put("floats2", new Object[] { "7.6", "5.4" });
180 map.put("int1", new Object[] { "12" });
181 map.put("ints1", new Object[] { "34", "56" });
182 map.put("int2", new Object[] { "98" });
183 map.put("ints2", new Object[] { "76", "54" });
184 map.put("long1", new Object[] { "12" });
185 map.put("longs1", new Object[] { "34", "56" });
186 map.put("long2", new Object[] { "98" });
187 map.put("longs2", new Object[] { "76", "54" });
188 map.put("short1", new Object[] { "12" });
189 map.put("shorts1", new Object[] { "34", "56" });
190 map.put("short2", new Object[] { "98" });
191 map.put("shorts2", new Object[] { "76", "54" });
192 map.put("sqldate", new Object[] { "2008-7-28" });
193 map.put("sqldates", new Object[] { "2008-8-14", "2008-10-30" });
194 map.put("sqltime", new Object[] { "12:34:56" });
195 map.put("sqltimes", new Object[] { "13:45:24", "23:44:00" });
196 map.put("sqltimestamp", new Object[] { "2008-7-28 12:34:56" });
197 map.put("sqltimestamps", new Object[] { "2008-8-14 13:45:24",
198 "2008-10-30 23:44:00" });
199
200 ConvertersDto dto = new ConvertersDto();
201
202 requestParameterBinder.bind(map, dto, MockAction.class, actionMethod(
203 MockAction.class, "all"));
204
205 assertNotNull(dto.decimal);
206 assertTrue(new BigDecimal("12.3").compareTo(dto.decimal) == 0);
207
208 assertNotNull(dto.decimals);
209 assertEquals(2, dto.decimals.length);
210 assertTrue(new BigDecimal("45.6").compareTo(dto.decimals[0]) == 0);
211 assertTrue(new BigDecimal("78.9").compareTo(dto.decimals[1]) == 0);
212
213 assertNotNull(dto.bigint);
214 assertTrue(new BigInteger("9876").compareTo(dto.bigint) == 0);
215
216 assertNotNull(dto.bigints);
217 assertEquals(2, dto.bigints.length);
218 assertTrue(new BigInteger("5432").compareTo(dto.bigints[0]) == 0);
219 assertTrue(new BigInteger("10").compareTo(dto.bigints[1]) == 0);
220
221 assertNotNull(dto.bool1);
222 assertTrue(dto.bool1);
223
224 assertNotNull(dto.bools1);
225 assertEquals(2, dto.bools1.length);
226 assertTrue(dto.bools1[0]);
227 assertFalse(dto.bools1[1]);
228
229 assertFalse(dto.bool2);
230
231 assertNotNull(dto.bools2);
232 assertEquals(3, dto.bools2.length);
233 assertFalse(dto.bools2[0]);
234 assertTrue(dto.bools2[1]);
235 assertFalse(dto.bools2[2]);
236
237 assertNotNull(dto.byte1);
238 assertEquals(new Byte((byte) 12), dto.byte1);
239
240 assertNotNull(dto.bytes1);
241 assertEquals(2, dto.bytes1.length);
242 assertEquals(new Byte((byte) 34), dto.bytes1[0]);
243 assertEquals(new Byte((byte) 56), dto.bytes1[1]);
244
245 assertEquals((byte) 98, dto.byte2);
246
247 assertNotNull(dto.bytes2);
248 assertEquals(2, dto.bytes2.length);
249 assertEquals((byte) 76, dto.bytes2[0]);
250 assertEquals((byte) 54, dto.bytes2[1]);
251
252 assertNotNull(dto.char1);
253 assertEquals(new Character('a'), dto.char1);
254
255 assertNotNull(dto.chars1);
256 assertEquals(2, dto.chars1.length);
257 assertEquals(new Character('b'), dto.chars1[0]);
258 assertEquals(new Character('c'), dto.chars1[1]);
259
260 assertNotNull(dto.char2);
261 assertEquals('d', dto.char2);
262
263 assertNotNull(dto.chars2);
264 assertEquals(2, dto.chars2.length);
265 assertEquals('e', dto.chars2[0]);
266 assertEquals('f', dto.chars2[1]);
267
268 assertNotNull(dto.date);
269 assertEquals(new Date(fromDateToMillis(2008, 7, 28)), dto.date);
270
271 assertNotNull(dto.dates);
272 assertEquals(2, dto.dates.length);
273 assertEquals(new Date(fromDateToMillis(2008, 8, 14)), dto.dates[0]);
274 assertEquals(new Date(fromDateToMillis(2008, 10, 30)), dto.dates[1]);
275
276 assertNotNull(dto.double1);
277 assertEquals(new Double(1.2d), dto.double1);
278
279 assertNotNull(dto.doubles1);
280 assertEquals(2, dto.doubles1.length);
281 assertEquals(new Double(3.4d), dto.doubles1[0]);
282 assertEquals(new Double(5.6d), dto.doubles1[1]);
283
284 assertEquals(9.8d, dto.double2);
285
286 assertNotNull(dto.doubles2);
287 assertEquals(2, dto.doubles2.length);
288 assertEquals(7.6d, dto.doubles2[0]);
289 assertEquals(5.4d, dto.doubles2[1]);
290
291 assertNotNull(dto.en);
292 assertSame(ExEnum.VALUE1, dto.en);
293
294 assertNotNull(dto.ens);
295 assertEquals(2, dto.ens.length);
296 assertSame(ExEnum.VALUE2, dto.ens[0]);
297 assertSame(ExEnum.VALUE3, dto.ens[1]);
298
299 assertNotNull(dto.float1);
300 assertEquals(new Float(1.2f), dto.float1);
301
302 assertNotNull(dto.floats1);
303 assertEquals(2, dto.floats1.length);
304 assertEquals(new Float(3.4f), dto.floats1[0]);
305 assertEquals(new Float(5.6f), dto.floats1[1]);
306
307 assertEquals(9.8f, dto.float2);
308
309 assertNotNull(dto.floats2);
310 assertEquals(2, dto.floats2.length);
311 assertEquals(7.6f, dto.floats2[0]);
312 assertEquals(5.4f, dto.floats2[1]);
313
314 assertNotNull(dto.int1);
315 assertEquals(new Integer(12), dto.int1);
316
317 assertNotNull(dto.ints1);
318 assertEquals(2, dto.ints1.length);
319 assertEquals(new Integer(34), dto.ints1[0]);
320 assertEquals(new Integer(56), dto.ints1[1]);
321
322 assertEquals(98, dto.int2);
323
324 assertNotNull(dto.ints2);
325 assertEquals(2, dto.ints2.length);
326 assertEquals(76, dto.ints2[0]);
327 assertEquals(54, dto.ints2[1]);
328
329 assertNotNull(dto.long1);
330 assertEquals(new Long(12l), dto.long1);
331
332 assertNotNull(dto.longs1);
333 assertEquals(2, dto.longs1.length);
334 assertEquals(new Long(34l), dto.longs1[0]);
335 assertEquals(new Long(56l), dto.longs1[1]);
336
337 assertEquals(98l, dto.long2);
338
339 assertNotNull(dto.longs2);
340 assertEquals(2, dto.longs2.length);
341 assertEquals(76l, dto.longs2[0]);
342 assertEquals(54l, dto.longs2[1]);
343
344 assertNotNull(dto.short1);
345 assertEquals(new Short((short) 12), dto.short1);
346
347 assertNotNull(dto.shorts1);
348 assertEquals(2, dto.shorts1.length);
349 assertEquals(new Short((short) 34), dto.shorts1[0]);
350 assertEquals(new Short((short) 56), dto.shorts1[1]);
351
352 assertEquals((short) 98, dto.short2);
353
354 assertNotNull(dto.shorts2);
355 assertEquals(2, dto.shorts2.length);
356 assertEquals((short) 76, dto.shorts2[0]);
357 assertEquals((short) 54, dto.shorts2[1]);
358
359 assertNotNull(dto.sqldate);
360 assertEquals(new java.sql.Date(fromDateToMillis(2008, 7, 28)),
361 dto.sqldate);
362
363 assertNotNull(dto.sqldates);
364 assertEquals(2, dto.sqldates.length);
365 assertEquals(new java.sql.Date(fromDateToMillis(2008, 8, 14)),
366 dto.sqldates[0]);
367 assertEquals(new java.sql.Date(fromDateToMillis(2008, 10, 30)),
368 dto.sqldates[1]);
369
370 assertNotNull(dto.sqltime);
371 assertEquals(new Time(fromTimeToMillis(12, 34, 56)), dto.sqltime);
372
373 assertNotNull(dto.sqltimes);
374 assertEquals(2, dto.sqltimes.length);
375 assertEquals(new Time(fromTimeToMillis(13, 45, 24)), dto.sqltimes[0]);
376 assertEquals(new Time(fromTimeToMillis(23, 44, 00)), dto.sqltimes[1]);
377
378 assertNotNull(dto.sqltimestamp);
379 assertEquals(new Timestamp(fromTimestampToMillis(2008, 7, 28, 12, 34,
380 56)), dto.sqltimestamp);
381
382 assertNotNull(dto.sqltimestamps);
383 assertEquals(2, dto.sqltimestamps.length);
384 assertEquals(new Timestamp(fromTimestampToMillis(2008, 8, 14, 13, 45,
385 24)), dto.sqltimestamps[0]);
386 assertEquals(new Timestamp(fromTimestampToMillis(2008, 10, 30, 23, 44,
387 00)), dto.sqltimestamps[1]);
388
389 System.out.println(dto);
390 }
391
392 public void testConvertFileItem() throws UnsupportedEncodingException {
393 Map<String, Object[]> map = new HashMap<String, Object[]>();
394 map.put("file", new Object[] { new MockFileItem("123") });
395 map.put("bytefile", new Object[] { new MockFileItem("456") });
396 map.put("bytefiles", new Object[] { new MockFileItem("abc"),
397 new MockFileItem("def") });
398 map.put("bytefilelist", new Object[] { new MockFileItem("GHI"),
399 new MockFileItem("JKL") });
400 map.put("input", new Object[] { new MockFileItem("QQ") });
401
402 FileItemDto dto = new FileItemDto();
403
404 requestParameterBinder.bind(map, dto, MockAction.class, actionMethod(
405 MockAction.class, "all"));
406 String encoding = "UTF-8";
407 assertNotNull(dto.file);
408 assertEquals("123", new String(dto.file.get(), encoding));
409 assertNotNull(dto.bytefile);
410 assertEquals("456", new String(dto.bytefile, encoding));
411 assertNotNull(dto.bytefiles);
412 assertEquals(2, dto.bytefiles.length);
413 assertEquals("abc", new String(dto.bytefiles[0], encoding));
414 assertEquals("def", new String(dto.bytefiles[1], encoding));
415 assertNotNull(dto.bytefilelist);
416 assertEquals(2, dto.bytefilelist.size());
417 Iterator<byte[]> it = dto.bytefilelist.iterator();
418 assertEquals("GHI", new String(it.next(), encoding));
419 assertEquals("JKL", new String(it.next(), encoding));
420 assertNotNull(dto.input);
421 assertEquals("QQ", new String(getBytes(dto.input), encoding));
422 }
423
424 public void testBindTypeNoAnnotated() {
425 Map<String, Object[]> map = new HashMap<String, Object[]>();
426 map.put("hasRequestParameter", new Object[] { "abc" });
427 map.put("noRequestParameter", new Object[] { "def" });
428 FormDto2 dto = new FormDto2();
429 requestParameterBinder.bind(map, dto, MockAction.class, actionMethod(
430 MockAction.class, "noAnnotated"));
431 assertNotNull(dto.hasRequestParameter);
432 assertEquals("abc", dto.hasRequestParameter);
433 assertNull(dto.noRequestParameter);
434 }
435
436 public void testBindTypeNoBindingType() {
437 Map<String, Object[]> map = new HashMap<String, Object[]>();
438 map.put("hasRequestParameter", new Object[] { "abc" });
439 map.put("noRequestParameter", new Object[] { "def" });
440 FormDto2 dto = new FormDto2();
441 requestParameterBinder.bind(map, dto, MockAction.class, actionMethod(
442 MockAction.class, "noBindingType"));
443 assertNotNull(dto.hasRequestParameter);
444 assertEquals("abc", dto.hasRequestParameter);
445 assertNotNull(dto.noRequestParameter);
446 assertEquals("def", dto.noRequestParameter);
447 }
448
449 public void testBindTypeAllProperties() {
450 Map<String, Object[]> map = new HashMap<String, Object[]>();
451 map.put("hasRequestParameter", new Object[] { "abc" });
452 map.put("noRequestParameter", new Object[] { "def" });
453 FormDto2 dto = new FormDto2();
454 requestParameterBinder.bind(map, dto, MockAction.class, actionMethod(
455 MockAction.class, "all"));
456 assertNotNull(dto.hasRequestParameter);
457 assertEquals("abc", dto.hasRequestParameter);
458 assertNotNull(dto.noRequestParameter);
459 assertEquals("def", dto.noRequestParameter);
460 }
461
462 public void testBindTypeOnlySpecifiedProperties() {
463 Map<String, Object[]> map = new HashMap<String, Object[]>();
464 map.put("hasRequestParameter", new Object[] { "abc" });
465 map.put("noRequestParameter", new Object[] { "def" });
466 FormDto2 dto = new FormDto2();
467 requestParameterBinder.bind(map, dto, MockAction.class, actionMethod(
468 MockAction.class, "specified"));
469 assertNotNull(dto.hasRequestParameter);
470 assertEquals("abc", dto.hasRequestParameter);
471 assertNull(dto.noRequestParameter);
472 }
473
474 public void testBindTypeNoAnnotatedOnClass() {
475 Map<String, Object[]> map = new HashMap<String, Object[]>();
476 map.put("hasRequestParameter", new Object[] { "abc" });
477 map.put("noRequestParameter", new Object[] { "def" });
478 FormDto2 dto = new FormDto2();
479 requestParameterBinder.bind(map, dto, MockAction2.class, actionMethod(
480 MockAction2.class, "noAnnotated"));
481 assertNotNull(dto.hasRequestParameter);
482 assertEquals("abc", dto.hasRequestParameter);
483 assertNotNull(dto.noRequestParameter);
484 assertEquals("def", dto.noRequestParameter);
485 }
486
487 public void testBindTypeOnlySpecifiedPropertiesOnClass() {
488 Map<String, Object[]> map = new HashMap<String, Object[]>();
489 map.put("hasRequestParameter", new Object[] { "abc" });
490 map.put("noRequestParameter", new Object[] { "def" });
491 FormDto2 dto = new FormDto2();
492 requestParameterBinder.bind(map, dto, MockAction2.class, actionMethod(
493 MockAction2.class, "specified"));
494 assertNotNull(dto.hasRequestParameter);
495 assertEquals("abc", dto.hasRequestParameter);
496 assertNull(dto.noRequestParameter);
497 }
498
499 private Method actionMethod(Class<? extends Action> actionClass,
500 String methodName) {
501 try {
502 return actionClass.getMethod(methodName);
503 } catch (NoSuchMethodException ex) {
504 throw new RuntimeException();
505 }
506 }
507
508 private class MockAction extends Action {
509 @Form(bindingType = RequestParameterBindingType.ALL_PROPERTIES)
510 public ActionResult all() {
511 return null;
512 }
513
514 @Form(bindingType = RequestParameterBindingType.ONLY_SPECIFIED_PROPERTIES)
515 public ActionResult specified() {
516 return null;
517 }
518
519 @Form
520 public ActionResult noBindingType() {
521 return null;
522 }
523
524 public ActionResult noAnnotated() {
525 return null;
526 }
527 }
528
529 @Form(bindingType = RequestParameterBindingType.ALL_PROPERTIES)
530 private class MockAction2 extends Action {
531 @Form(bindingType = RequestParameterBindingType.ONLY_SPECIFIED_PROPERTIES)
532 public ActionResult specified() {
533 return null;
534 }
535
536 public ActionResult noAnnotated() {
537 return null;
538 }
539 }
540
541 public static class FormDto2 {
542 @RequestParameter
543 public String hasRequestParameter;
544
545 public String noRequestParameter;
546 }
547
548 public static class FormDto {
549 public Date date;
550 public Integer num1;
551 public Integer[] num2;
552 public List<String> num3;
553 }
554
555 public static class ConvertersDto {
556 public BigDecimal decimal;
557 public BigDecimal[] decimals;
558 public BigInteger bigint;
559 public BigInteger[] bigints;
560 public Boolean bool1;
561 public Boolean[] bools1;
562 public boolean bool2;
563 public boolean[] bools2;
564 public Byte byte1;
565 public Byte[] bytes1;
566 public byte byte2;
567 public byte[] bytes2;
568 public Character char1;
569 public Character[] chars1;
570 public char char2;
571 public char[] chars2;
572 public Date date;
573 public Date[] dates;
574 public Double double1;
575 public Double[] doubles1;
576 public double double2;
577 public double[] doubles2;
578 public ExEnum en;
579 public ExEnum[] ens;
580 public Float float1;
581 public Float[] floats1;
582 public float float2;
583 public float[] floats2;
584 public Integer int1;
585 public Integer[] ints1;
586 public int int2;
587 public int[] ints2;
588 public Long long1;
589 public Long[] longs1;
590 public long long2;
591 public long[] longs2;
592 public Short short1;
593 public Short[] shorts1;
594 public short short2;
595 public short[] shorts2;
596 public java.sql.Date sqldate;
597 public java.sql.Date[] sqldates;
598 public Time sqltime;
599 public Time[] sqltimes;
600 public Timestamp sqltimestamp;
601 public Timestamp[] sqltimestamps;
602 }
603
604 public enum ExEnum {
605 VALUE1, VALUE2, VALUE3;
606 }
607
608 public static class FileItemDto {
609 public FileItem file;
610 public byte[] bytefile;
611 public byte[][] bytefiles;
612 public Set<byte[]> bytefilelist;
613 public InputStream input;
614 }
615
616 private static class MockFileItem implements FileItem {
617
618 private static final long serialVersionUID = 1L;
619
620 private String name;
621
622 public MockFileItem(String name) {
623 this.name = name;
624 }
625
626 public void delete() {
627 }
628
629 public byte[] get() {
630 try {
631 return name.getBytes("UTF-8");
632 } catch (UnsupportedEncodingException e) {
633 throw new RuntimeException();
634 }
635 }
636
637 public String getContentType() {
638 return null;
639 }
640
641 public String getFieldName() {
642 return null;
643 }
644
645 public InputStream getInputStream() throws IOException {
646 return new ByteArrayInputStream(get());
647 }
648
649 public String getName() {
650 return this.name;
651 }
652
653 public OutputStream getOutputStream() throws IOException {
654 return null;
655 }
656
657 public long getSize() {
658 return 0;
659 }
660
661 public String getString() {
662 return null;
663 }
664
665 public String getString(String encoding)
666 throws UnsupportedEncodingException {
667 return null;
668 }
669
670 public boolean isFormField() {
671 return false;
672 }
673
674 public boolean isInMemory() {
675 return false;
676 }
677
678 public void setFieldName(String name) {
679 }
680
681 public void setFormField(boolean state) {
682 }
683
684 public void write(File file) throws Exception {
685 }
686 }
687
688 private static final Map<Integer, Integer> MONTHS;
689 static {
690 Map<Integer, Integer> map = new HashMap<Integer, Integer>();
691 map.put(1, Calendar.JANUARY);
692 map.put(2, Calendar.FEBRUARY);
693 map.put(3, Calendar.MARCH);
694 map.put(4, Calendar.APRIL);
695 map.put(5, Calendar.MAY);
696 map.put(6, Calendar.JUNE);
697 map.put(7, Calendar.JULY);
698 map.put(8, Calendar.AUGUST);
699 map.put(9, Calendar.SEPTEMBER);
700 map.put(10, Calendar.OCTOBER);
701 map.put(11, Calendar.NOVEMBER);
702 map.put(12, Calendar.DECEMBER);
703 MONTHS = Collections.unmodifiableMap(map);
704 }
705
706 private static long fromDateToMillis(int year, int month, int date) {
707 Calendar c = Calendar.getInstance();
708 c.clear();
709 c.set(Calendar.YEAR, year);
710 c.set(Calendar.MONTH, MONTHS.get(month));
711 c.set(Calendar.DATE, date);
712 return c.getTimeInMillis();
713 }
714
715 private static long fromTimeToMillis(int hour, int minute, int second) {
716 Calendar c = Calendar.getInstance();
717 c.clear();
718 c.set(Calendar.HOUR, hour);
719 c.set(Calendar.MINUTE, minute);
720 c.set(Calendar.SECOND, second);
721 return c.getTimeInMillis();
722 }
723
724 private static long fromTimestampToMillis(int year, int month, int date,
725 int hour, int minute, int second) {
726 Calendar c = Calendar.getInstance();
727 c.clear();
728 c.set(Calendar.YEAR, year);
729 c.set(Calendar.MONTH, MONTHS.get(month));
730 c.set(Calendar.DATE, date);
731 c.set(Calendar.HOUR, hour);
732 c.set(Calendar.MINUTE, minute);
733 c.set(Calendar.SECOND, second);
734 return c.getTimeInMillis();
735 }
736
737 private static final byte[] getBytes(InputStream is) {
738 byte[] bytes = null;
739 byte[] buf = new byte[8192];
740 try {
741 ByteArrayOutputStream baos = new ByteArrayOutputStream();
742 int n = 0;
743 while ((n = is.read(buf, 0, buf.length)) != -1) {
744 baos.write(buf, 0, n);
745 }
746 bytes = baos.toByteArray();
747 } catch (IOException e) {
748 throw new RuntimeException(e);
749 } finally {
750 if (is != null) {
751 try {
752 is.close();
753 } catch (IOException e) {
754 }
755 }
756 }
757 return bytes;
758 }
759
760 }