01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.translator;
16:
17: import org.apache.tapestry.Translator;
18: import org.apache.tapestry.ValidationException;
19: import org.apache.tapestry.internal.test.InternalBaseTestCase;
20: import org.apache.tapestry.ioc.Messages;
21: import org.testng.annotations.Test;
22:
23: public class IntegerTranslatorTest extends InternalBaseTestCase {
24: @Test
25: public void parse_invalid_format() {
26: Messages messages = validationMessages();
27:
28: Translator<Integer> translator = new IntegerTranslator();
29:
30: try {
31: translator.parseClient("xyz", messages);
32: unreachable();
33: } catch (ValidationException ex) {
34: assertEquals(ex.getMessage(),
35: "The input value 'xyz' is not parseable as an integer value.");
36: }
37: }
38:
39: @Test
40: public void blank_parses_to_null() throws Exception {
41: Messages messages = validationMessages();
42:
43: Translator<Integer> translator = new IntegerTranslator();
44:
45: assertNull(translator.parseClient("", messages));
46: }
47:
48: @Test
49: public void null_converts_to_client_as_blank() {
50:
51: Translator<Integer> translator = new IntegerTranslator();
52:
53: assertEquals(translator.toClient(null), "");
54: }
55:
56: @Test
57: public void convert_non_null() {
58:
59: Translator<Integer> translator = new IntegerTranslator();
60:
61: assertEquals(translator.toClient(37), "37");
62: }
63:
64: @Test
65: public void successful_parse_from_client() throws Exception {
66: Messages messages = validationMessages();
67:
68: Translator<Integer> translator = new IntegerTranslator();
69:
70: assertEquals(translator.parseClient("-23823", messages),
71: new Integer(-23823));
72: }
73:
74: @Test
75: public void parse_ignores_trimmed_whitespace() throws Exception {
76: Messages messages = validationMessages();
77:
78: Translator<Integer> translator = new IntegerTranslator();
79:
80: assertEquals(translator.parseClient(" -123 ", messages),
81: new Integer(-123));
82: }
83: }
|