View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  class DaytimeTCPClientTest {
42  
43      private static MockDaytimeTCPServer mockDaytimeTCPServer; //NOPMD
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(0, getLocalHostInetAddress());
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      /**
65       * Gets the InetAddress to use as the localhost.
66       * <p>
67       * In order for this test to pass inside some VPNs, you cannot use the plain old {@link InetAddress#getLocalHost()}.
68       * </p>
69       *
70       * @return the InetAddress to use as the localhost.
71       */
72      private static InetAddress getLocalHostInetAddress() {
73          return InetAddress.getLoopbackAddress();
74      }
75  
76      private DaytimeTCPClient daytimeTCPClient;
77  
78      private InetAddress localHost;
79  
80      @Test
81      public void constructDaytimeTcpClient() {
82          final DaytimeTCPClient daytimeTCPClient = new DaytimeTCPClient();
83          assertEquals(13, daytimeTCPClient.getDefaultPort());
84      }
85  
86      @ParameterizedTest(name = "getTime() should return <{0}> for date <{2}> and zone <{1}>")
87      @Timeout(5)
88      @MethodSource("daytimeMockData")
89      public void getTime(final String expectedDaytimeString, final ZoneId zoneId, final LocalDateTime localDateTime) throws IOException {
90          final Clock mockClock = Clock.fixed(localDateTime.atZone(zoneId).toInstant(), zoneId);
91          mockDaytimeTCPServer.enqueue(mockClock);
92  
93          daytimeTCPClient = new DaytimeTCPClient();
94          daytimeTCPClient.setDefaultTimeout(60000);
95          daytimeTCPClient.connect(localHost, mockDaytimeTCPServer.getPort());
96  
97          final String time = daytimeTCPClient.getTime();
98          assertEquals(expectedDaytimeString, time);
99      }
100 
101     @BeforeEach
102     public void setUp() throws UnknownHostException {
103         localHost = getLocalHostInetAddress();
104     }
105 
106     @AfterEach
107     public void tearDown() throws IOException {
108         if (daytimeTCPClient != null && daytimeTCPClient.isConnected()) {
109             daytimeTCPClient.disconnect();
110         }
111     }
112 
113 }
114