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 LongTranslatorTest extends InternalBaseTestCase {
24: @Test
25: public void parse_invalid_format() {
26: Messages messages = validationMessages();
27:
28: Translator<Long> translator = new LongTranslator();
29:
30: try {
31: translator.parseClient("xyz", messages);
32: unreachable();
33: } catch (ValidationException ex) {
34: // It's okay that it says 'integer' (not 'long') here because users don't
35: // care about the distinction.
36:
37: assertEquals(ex.getMessage(),
38: "The input value 'xyz' is not parseable as an integer value.");
39: }
40: }
41:
42: @Test
43: public void blank_parses_to_null() throws Exception {
44: Messages messages = validationMessages();
45:
46: Translator<Long> translator = new LongTranslator();
47:
48: assertNull(translator.parseClient("", messages));
49: }
50:
51: @Test
52: public void null_converts_to_client_as_blank() {
53:
54: Translator<Long> translator = new LongTranslator();
55:
56: assertEquals(translator.toClient(null), "");
57: }
58:
59: @Test
60: public void convert_non_null() {
61:
62: Translator<Long> translator = new LongTranslator();
63:
64: assertEquals(translator.toClient(37l), "37");
65: }
66:
67: @Test
68: public void successful_parse_from_client() throws Exception {
69: Messages messages = validationMessages();
70:
71: Translator<Long> translator = new LongTranslator();
72:
73: assertEquals(translator.parseClient("-23823", messages),
74: new Long(-23823));
75: }
76:
77: @Test
78: public void parse_ignores_trimmed_whitespace() throws Exception {
79: Messages messages = validationMessages();
80:
81: Translator<Long> translator = new LongTranslator();
82:
83: assertEquals(translator.parseClient(" -123 ", messages),
84: new Long(-123));
85: }
86: }
|