[LIB-10] Tests for SnookerDateUtils
[snooker-score-api.git] / src / test / java / org / hedgecode / snooker / SnookerDateUtilsTest.java
1 /*
2  * Copyright (c) 2017-2020. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.snooker;
18
19 import java.text.DateFormat;
20 import java.text.SimpleDateFormat;
21 import java.util.Date;
22 import java.util.TimeZone;
23
24 import org.junit.Assert;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28
29 /**
30  * Tests for {@link SnookerDateUtils}.
31  *
32  * @author Dmitry Samoshin aka gotty
33  */
34 @RunWith(JUnit4.class)
35 public class SnookerDateUtilsTest extends Assert {
36
37     private static final String DATE_PATTERN = SnookerScoreProperties.get("snooker.date.format");
38     private static final String TIME_PATTERN = SnookerScoreProperties.get("snooker.time.format");
39     private static final String DATETIME_PATTERN = String.format("%s %s", TIME_PATTERN , DATE_PATTERN);
40
41     private static final TimeZone SNOOKER_TIMEZONE = TimeZone.getTimeZone(
42             SnookerScoreProperties.get("snooker.timezone")
43     );
44     private static final TimeZone LOCAL_TIMEZONE = TimeZone.getDefault();
45
46     @Test
47     public void testFormatDate() throws Exception {
48         Date currentDate = new Date();
49         DateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN);
50         assertEquals(
51                 dateFormat.format(currentDate),
52                 SnookerDateUtils.formatDate(currentDate)
53         );
54     }
55
56     @Test
57     public void testFormatTime() throws Exception {
58         Date currentTime = new Date();
59         DateFormat timeFormat = new SimpleDateFormat(TIME_PATTERN);
60         timeFormat.setTimeZone(SNOOKER_TIMEZONE);
61         assertEquals(
62                 timeFormat.format(currentTime),
63                 SnookerDateUtils.formatTime(currentTime)
64         );
65         DateFormat datetimeFormat = new SimpleDateFormat(DATETIME_PATTERN);
66         datetimeFormat.setTimeZone(SNOOKER_TIMEZONE);
67         assertEquals(
68                 datetimeFormat.format(currentTime),
69                 SnookerDateUtils.formatTime(currentTime, true)
70         );
71     }
72
73     @Test
74     public void testFormatLocalTime() throws Exception {
75         Date currentTime = new Date();
76         DateFormat timeFormat = new SimpleDateFormat(TIME_PATTERN);
77         timeFormat.setTimeZone(LOCAL_TIMEZONE);
78         assertEquals(
79                 timeFormat.format(currentTime),
80                 SnookerDateUtils.formatLocalTime(currentTime)
81         );
82         DateFormat datetimeFormat = new SimpleDateFormat(DATETIME_PATTERN);
83         datetimeFormat.setTimeZone(LOCAL_TIMEZONE);
84         assertEquals(
85                 datetimeFormat.format(currentTime),
86                 SnookerDateUtils.formatLocalTime(currentTime, true)
87         );
88     }
89
90     @Test
91     public void testGetInstance() throws Exception {
92         assertNotNull(
93                 SnookerDateUtils.getInstance()
94         );
95     }
96
97 }