Skip to content

Commit

Permalink
Testing simplification and minor bug fix on tearing down.
Browse files Browse the repository at this point in the history
  • Loading branch information
oshoukry committed Apr 15, 2018
1 parent 87077ff commit 9470c3b
Showing 1 changed file with 9 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.atomic.AtomicBoolean;

import com.openpojo.dns.DnsControl;
import com.openpojo.dns.service.java.reflection.ReflectionHelper;
Expand All @@ -42,40 +41,24 @@
* @author oshoukry
*/
public class Java9NameServiceInterceptorTest {
private static final String NAME_SERVICE_INTERFACE_CLASS = "java.net.InetAddress$NameService";
private static final String NAME_SERVICE_FIELD = "nameservice";

private Java9NameServiceInterceptor java9NameServiceInterceptor;
private static Object nameServiceFieldValue;
private static AtomicBoolean testCanRun = new AtomicBoolean(false);

@Before
public void setup() {
try {
assumeThat(ClassUtil.loadClass(NAME_SERVICE_INTERFACE_CLASS), notNullValue());

//noinspection JavaReflectionMemberAccess
nameServiceFieldValue = ReflectionHelper.getFieldValue(InetAddress.class, NAME_SERVICE_FIELD, null);
public void setup() throws ClassNotFoundException {
assumeThat(ClassUtil.loadClass("java.net.InetAddress$NameService"), notNullValue());

Lookup.refreshDefault();
DnsControl.recreateInstance().registerRoutingResolver();
Lookup.refreshDefault();
DnsControl.recreateInstance().registerRoutingResolver();

java9NameServiceInterceptor = new Java9NameServiceInterceptor();
java9NameServiceInterceptor.changeInetAddressProxy(java9NameServiceInterceptor.createProxyForNameService());
testCanRun = new AtomicBoolean(true);
} catch (Exception ignored) {
assumeThat("Ignoring running tests as setup failed!", false, is(true));
}
java9NameServiceInterceptor = new Java9NameServiceInterceptor();
java9NameServiceInterceptor.changeInetAddressProxy(java9NameServiceInterceptor.createProxyForNameService());
}

@After
public void teardown() {
if (testCanRun.get()) {
ReflectionHelper.setFieldValue(InetAddress.class, NAME_SERVICE_FIELD, null, nameServiceFieldValue);

DnsControl.getInstance().unRegisterRoutingResolver();
Lookup.refreshDefault();
}
Object value = ReflectionHelper.invokeMethodOnClass(InetAddress.class, "createNameService", null);
ReflectionHelper.setFieldValue(InetAddress.class, "nameService", null, value);
Lookup.refreshDefault();
}

@Test
Expand Down

0 comments on commit 9470c3b

Please sign in to comment.