2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.logging.context;
19 import static org.junit.Assert.assertTrue;
21 import java.net.InetAddress;
22 import java.net.UnknownHostException;
23 import org.easymock.EasyMock;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.powermock.api.easymock.PowerMock;
27 import org.powermock.core.classloader.annotations.PrepareForTest;
28 import org.powermock.modules.junit4.PowerMockRunner;
31 * Retrieval and caching of host address.
36 @PrepareForTest(InetAddress.class)
37 @RunWith(PowerMockRunner.class)
38 public class HostAddressCacheTest {
41 public void hostAddressIsAlwaysPopulated() {
42 assertTrue(new HostAddressCache().get().isPresent());
46 public void cachedAddressRemainsTheSameWhenGotWithingRefreshInterval() throws UnknownHostException {
48 HostAddressCache addressCache = new HostAddressCache(1000);
54 public void cachedAddressReplacedWhenGotAfterRefreshInterval() throws UnknownHostException {
56 HostAddressCache addressCache = new HostAddressCache(-1);
61 private void mockInetAddress(int times) throws UnknownHostException {
62 InetAddress inetAddress = EasyMock.mock(InetAddress.class);
63 EasyMock.replay(inetAddress);
64 PowerMock.mockStatic(InetAddress.class);
65 //noinspection ResultOfMethodCallIgnored
66 InetAddress.getLocalHost();
67 PowerMock.expectLastCall().andReturn(inetAddress).times(times);
68 PowerMock.replay(InetAddress.class);