1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.net.daytime;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21
22 import java.io.IOException;
23 import java.net.InetAddress;
24 import java.net.UnknownHostException;
25 import java.time.Clock;
26 import java.time.LocalDate;
27 import java.time.LocalDateTime;
28 import java.time.ZoneId;
29 import java.util.stream.Stream;
30
31 import org.junit.jupiter.api.AfterAll;
32 import org.junit.jupiter.api.AfterEach;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.Timeout;
37 import org.junit.jupiter.params.ParameterizedTest;
38 import org.junit.jupiter.params.provider.Arguments;
39 import org.junit.jupiter.params.provider.MethodSource;
40
41 public class DaytimeTCPClientTest {
42
43 private static MockDaytimeTCPServer mockDaytimeTCPServer;
44
45 @AfterAll
46 public static void afterAll() throws IOException {
47 mockDaytimeTCPServer.stop();
48 }
49
50 @BeforeAll
51 public static void beforeAll() throws IOException {
52 mockDaytimeTCPServer = new MockDaytimeTCPServer();
53 mockDaytimeTCPServer.start();
54 }
55
56 private static Stream<Arguments> daytimeMockData() {
57 return Stream.of(
58 Arguments.of("Thursday, February 2, 2006, 13:45:51-PST", ZoneId.of("PST", ZoneId.SHORT_IDS), LocalDateTime.of(2006, 2, 2, 13, 45, 51)),
59 Arguments.of("Thursday, January 1, 2004, 00:00:00-UTC", ZoneId.of("UTC"), LocalDate.of(2004, 1, 1).atStartOfDay()),
60 Arguments.of("Friday, July 28, 2023, 06:06:50-JST", ZoneId.of("JST", ZoneId.SHORT_IDS), LocalDateTime.of(2023, 7, 28, 6, 6, 50, 999))
61 );
62 }
63
64 private DaytimeTCPClient daytimeTCPClient;
65 private InetAddress localHost;
66
67 @Test
68 public void constructDaytimeTcpClient() {
69 final DaytimeTCPClient daytimeTCPClient = new DaytimeTCPClient();
70 assertEquals(13, daytimeTCPClient.getDefaultPort());
71 }
72
73 @ParameterizedTest(name = "getTime() should return <{0}> for date <{2}> and zone <{1}>")
74 @Timeout(5)
75 @MethodSource("daytimeMockData")
76 public void getTime(final String expectedDaytimeString, final ZoneId zoneId, final LocalDateTime localDateTime) throws IOException {
77 final Clock mockClock = Clock.fixed(localDateTime.atZone(zoneId).toInstant(), zoneId);
78 mockDaytimeTCPServer.enqueue(mockClock);
79
80 daytimeTCPClient = new DaytimeTCPClient();
81 daytimeTCPClient.setDefaultTimeout(60000);
82 daytimeTCPClient.connect(localHost, mockDaytimeTCPServer.getPort());
83
84 final String time = daytimeTCPClient.getTime();
85 assertEquals(expectedDaytimeString, time);
86 }
87
88 @BeforeEach
89 public void setUp() throws UnknownHostException {
90 localHost = InetAddress.getLocalHost();
91 }
92
93 @AfterEach
94 public void tearDown() throws IOException {
95 if (daytimeTCPClient != null && daytimeTCPClient.isConnected()) {
96 daytimeTCPClient.disconnect();
97 }
98 }
99
100 }
101