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.testng.Assert.assertNotNull;
21 import java.net.InetAddress;
22 import java.net.UnknownHostException;
23 import org.easymock.EasyMock;
24 import org.powermock.api.easymock.PowerMock;
25 import org.powermock.core.classloader.annotations.PrepareForTest;
26 import org.powermock.modules.testng.PowerMockTestCase;
27 import org.testng.annotations.Test;
30 * Retrieval and caching of host address.
35 @PrepareForTest(InetAddress.class)
36 public class HostAddressTest extends PowerMockTestCase {
39 public void hostAddressIsAlwaysPopulated() {
40 assertNotNull(new HostAddress().get());
44 public void cachedAddressRemainsTheSameWhenGotWithingRefreshInterval() throws UnknownHostException {
46 HostAddress addressCache = new HostAddress(1000);
52 public void cachedAddressReplacedWhenGotAfterRefreshInterval() throws UnknownHostException {
54 HostAddress addressCache = new HostAddress(-1);
59 private void mockInetAddress(int times) throws UnknownHostException {
60 InetAddress inetAddress = EasyMock.mock(InetAddress.class);
61 EasyMock.replay(inetAddress);
62 PowerMock.mockStatic(InetAddress.class);
63 //noinspection ResultOfMethodCallIgnored
64 InetAddress.getLocalHost();
65 PowerMock.expectLastCall().andReturn(inetAddress).times(times);
66 PowerMock.replay(InetAddress.class);