View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.convert;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.component.UIIn;
24  import org.junit.Assert;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  import javax.faces.convert.Converter;
29  
30  public class DurationConverterUnitTest {
31  
32    private Converter converter;
33  
34    @Before
35    public void setUp() throws Exception {
36      converter = new DurationConverter();
37    }
38  
39    @Test
40    public void testFormat() {
41  
42      format(null, 1000L, "0:01");
43      format("second", 1000L, "16:40");
44      format("minute", -100L, "-1:40:00");
45      format("hour", 1L, "1:00:00");
46      format("day", 1L, "24:00:00");
47      format("year", 1L, "8765:45:36");
48  
49    }
50  
51    @Test
52    public void testParse() {
53  
54      parse(null, 1000L, "0:01");
55      parse("second", 1001L, "16:41");
56      parse("minute", -16L, "-16:41");
57      parse("hour", 1L, "1:00:00");
58      parse("day", 1L, "24:00:00");
59      parse("year", 1L, "8765:45:36");
60    }
61  
62    private void format(String unit, Long aLong, String string) {
63      UIIn input = new UIIn();
64      String info = "Formatting numbers:"
65          + " unit='" + unit + "'"
66          + " long='" + aLong + "'";
67      String result;
68      if (unit != null) {
69        input.getAttributes().put(Attributes.UNIT, unit);
70      }
71      result = converter.getAsString(null, input, aLong);
72      Assert.assertEquals(info, string, result);
73    }
74  
75    private void parse(String unit, Long aLong, String string) {
76      UIIn input = new UIIn();
77      String info = "Parsing numbers:"
78          + " unit='" + unit + "'"
79          + " string='" + string + "'";
80      Long result;
81      if (unit != null) {
82        input.getAttributes().put(Attributes.UNIT, unit);
83      }
84      result = (Long) converter.getAsObject(null, input, string);
85      Assert.assertEquals(info, aLong, result);
86    }
87  
88  }