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.apache.myfaces.tobago.internal.config.AbstractTobagoTestBase;
25  import org.junit.jupiter.api.Assertions;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  import javax.faces.convert.Converter;
30  
31  public class DurationConverterUnitTest extends AbstractTobagoTestBase {
32  
33    private Converter converter;
34  
35    @Override
36    @BeforeEach
37    public void setUp() throws Exception {
38      super.setUp();
39      converter = new DurationConverter();
40    }
41  
42    @Test
43    public void testFormat() {
44  
45      format(null, 1000L, "0:01");
46      format("second", 1000L, "16:40");
47      format("minute", -100L, "-1:40:00");
48      format("hour", 1L, "1:00:00");
49      format("day", 1L, "24:00:00");
50      format("year", 1L, "8765:45:36");
51      format("milli", 75_000L, "1:15");
52      format("hour", 1L, "1:00:00");
53      format(null, 4_500_000L, "1:15:00");
54    }
55  
56    @Test
57    public void testParse() {
58  
59      parse(null, 1000L, "0:01");
60      parse(null, 1000L, "1");
61      parse("second", 1001L, "16:41");
62      parse("minute", -16L, "-16:41");
63      parse("hour", 1L, "1:00:00");
64      parse("day", 1L, "24:00:00");
65      parse("year", 1L, "8765:45:36");
66      parse("milli", 75_000L, "1:15");
67      parse("hour", 1L, "1:15:00");
68      parse(null, 4_500_000L, "1:15:00");
69    }
70  
71    private void format(final String unit, final Long aLong, final String string) {
72      final UIIn input = new UIIn();
73      final String info = "Formatting numbers:"
74          + " unit='" + unit + "'"
75          + " long='" + aLong + "'";
76      final String result;
77      if (unit != null) {
78        input.getAttributes().put(Attributes.unit.getName(), unit);
79      }
80      result = converter.getAsString(null, input, aLong);
81      Assertions.assertEquals(string, result, info);
82    }
83  
84    private void parse(final String unit, final Long aLong, final String string) {
85      final UIIn input = new UIIn();
86      final String info = "Parsing numbers:"
87          + " unit='" + unit + "'"
88          + " string='" + string + "'";
89      final Long result;
90      if (unit != null) {
91        input.getAttributes().put(Attributes.unit.getName(), unit);
92      }
93      result = (Long) converter.getAsObject(null, input, string);
94      Assertions.assertEquals(aLong, result, info);
95    }
96  
97  }