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  package org.apache.syncope.core.provisioning.api.utils;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  
23  import java.security.SecureRandom;
24  import java.text.DecimalFormat;
25  import java.text.DecimalFormatSymbols;
26  import java.text.ParseException;
27  import java.time.LocalDate;
28  import java.time.OffsetDateTime;
29  import java.time.ZoneOffset;
30  import java.time.format.DateTimeFormatter;
31  import java.time.temporal.ChronoUnit;
32  import java.util.Locale;
33  import org.apache.syncope.core.provisioning.api.AbstractTest;
34  import org.junit.jupiter.api.Test;
35  
36  public class FormatUtilsTest extends AbstractTest {
37  
38      private static final SecureRandom RANDOM = new SecureRandom();
39  
40      private static final OffsetDateTime DATE = OffsetDateTime.now();
41  
42      @Test
43      public void formatDate() {
44          assertEquals(
45                  DATE.truncatedTo(ChronoUnit.SECONDS).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME),
46                  FormatUtils.format(DATE));
47  
48          String conversionPattern = "dd/MM/yyyy";
49          assertEquals(
50                  DATE.format(DateTimeFormatter.ofPattern(conversionPattern)),
51                  FormatUtils.format(DATE, conversionPattern));
52      }
53  
54      @Test
55      public void formatLongNumber() {
56          long number = RANDOM.nextLong();
57          DecimalFormat df = new DecimalFormat();
58          df.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ENGLISH));
59          assertEquals(df.format(number), FormatUtils.format(number));
60  
61          String conversionPattern = "###,###";
62          df = new DecimalFormat(conversionPattern);
63          df.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ENGLISH));
64          assertEquals(df.format(number), FormatUtils.format(number, conversionPattern));
65      }
66  
67      @Test
68      public void formatDoubleNumber() {
69          double number = RANDOM.nextDouble();
70          DecimalFormat df = new DecimalFormat();
71          df.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ENGLISH));
72          assertEquals(df.format(number), FormatUtils.format(number));
73  
74          String conversionPattern = "###,###";
75          df = new DecimalFormat(conversionPattern);
76          df.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ENGLISH));
77          assertEquals(df.format(number), FormatUtils.format(number, conversionPattern));
78      }
79  
80      @Test
81      public void parseDate() throws ParseException {
82          String source = DATE.truncatedTo(ChronoUnit.SECONDS).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
83          assertEquals(
84                  OffsetDateTime.parse(source, DateTimeFormatter.ISO_OFFSET_DATE_TIME),
85                  FormatUtils.parseDate(source));
86  
87          String conversionPattern = "dd-MM-yyyy";
88          source = DATE.format(DateTimeFormatter.ofPattern(conversionPattern));
89          assertEquals(
90                  LocalDate.parse(source, DateTimeFormatter.ofPattern(conversionPattern)).
91                          atStartOfDay(ZoneOffset.UTC).toOffsetDateTime(),
92                  FormatUtils.parseDate(source, conversionPattern));
93      }
94  
95      @Test
96      public void parseNumber() throws ParseException {
97          String source = String.valueOf(RANDOM.nextLong());
98          String conversionPattern = "###,###";
99          assertEquals(Long.valueOf(source), FormatUtils.parseNumber(source, conversionPattern));
100     }
101 }