01: package org.vraptor.converter;
02:
03: import java.util.Arrays;
04: import java.util.List;
05: import java.util.Locale;
06:
07: import org.vraptor.AbstractTest;
08: import org.vraptor.LogicRequest;
09:
10: public class LocalePrimitiveDoubleConverterTest extends AbstractTest {
11:
12: public void testCanConvert() {
13: List<Class<?>> list = Arrays
14: .asList(new LocalePrimitiveDoubleConverter()
15: .getSupportedTypes());
16: assertTrue(list.contains(double.class));
17: }
18:
19: private LogicRequest getSetContext() {
20: LogicRequest context = createLogicRequest();
21: context.getSessionContext().setAttribute(
22: "javax.servlet.jsp.jstl.fmt.locale.session",
23: new Locale("pt", "BR"));
24: return context;
25: }
26:
27: public void testConvertsValidString() throws ConversionException {
28: LogicRequest context = getSetContext();
29: assertEquals(10.33, new LocalePrimitiveDoubleConverter()
30: .convert("10,33", double.class, context));
31: }
32:
33: public void testConvertionErrorWithNaN() {
34: LogicRequest context = getSetContext();
35: try {
36: new LocalePrimitiveDoubleConverter().convert("xx",
37: double.class, context);
38: fail();
39: } catch (ConversionException e) {
40: // ok
41: }
42: }
43:
44: public void testConvertsNegative() throws ConversionException {
45: LogicRequest context = createLogicRequest();
46: assertEquals(-500.2, new LocalePrimitiveDoubleConverter()
47: .convert("-500,2", double.class, context));
48: }
49:
50: public void testConvertionErrorWithEmptyString()
51: throws ConversionException {
52: LogicRequest context = createLogicRequest();
53: try {
54: new LocalePrimitiveDoubleConverter().convert("",
55: double.class, context);
56: fail();
57: } catch (ConversionException e) {
58: // ok
59: }
60: }
61:
62: public void testConvertionErrorWithNullValue()
63: throws ConversionException {
64: LogicRequest context = createLogicRequest();
65: try {
66: new LocalePrimitiveDoubleConverter().convert(null,
67: double.class, context);
68: fail();
69: } catch (ConversionException e) {
70: // ok
71: }
72: }
73:
74: public void testConvertsZero() throws ConversionException {
75: LogicRequest context = createLogicRequest();
76: assertEquals(0d, new LocalePrimitiveDoubleConverter().convert(
77: "0", double.class, context));
78: }
79:
80: }
|