You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/04/21 07:13:09 UTC

[groovy] branch master updated: Set `java.rmi.server.hostname` to make `JmxClientConnectorFactoryTest` robust

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 756870e  Set `java.rmi.server.hostname` to make `JmxClientConnectorFactoryTest` robust
756870e is described below

commit 756870e44e878c016f8ecd083951f43c1193a410
Author: Daniel Sun <su...@apache.org>
AuthorDate: Wed Apr 21 13:57:40 2021 +0800

    Set `java.rmi.server.hostname` to make `JmxClientConnectorFactoryTest` robust
---
 .../builder/JmxClientConnectorFactoryTest.groovy   | 26 ++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/subprojects/groovy-jmx/src/test/groovy/groovy/jmx/builder/JmxClientConnectorFactoryTest.groovy b/subprojects/groovy-jmx/src/test/groovy/groovy/jmx/builder/JmxClientConnectorFactoryTest.groovy
index c68511b..fe8ff46 100644
--- a/subprojects/groovy-jmx/src/test/groovy/groovy/jmx/builder/JmxClientConnectorFactoryTest.groovy
+++ b/subprojects/groovy-jmx/src/test/groovy/groovy/jmx/builder/JmxClientConnectorFactoryTest.groovy
@@ -30,6 +30,9 @@ class JmxClientConnectorFactoryTest extends GroovyTestCase {
 
     void setUp() {
         super.setUp()
+        def hostAddress = getHostAddress()
+        println "hostAddress: ${hostAddress}"
+        System.setProperty("java.rmi.server.hostname", hostAddress)
         builder = new JmxBuilder()
         rmi = JmxConnectorHelper.createRmiRegistry(defaultPort)
     }
@@ -71,4 +74,27 @@ class JmxClientConnectorFactoryTest extends GroovyTestCase {
             client.connect()
         }
     }
+
+    static String getHostAddress() {
+        try {
+            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
+            while (allNetInterfaces.hasMoreElements()) {
+                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
+                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
+                while (addresses.hasMoreElements()) {
+                    InetAddress ip = (InetAddress) addresses.nextElement();
+                    if (ip != null
+                            && ip instanceof Inet4Address
+                            && !ip.isLoopbackAddress()
+                            && ip.getHostAddress().indexOf(":") == -1) {
+                        return ip.getHostAddress()
+                    }
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace()
+        }
+        return null
+    }
+
 }
\ No newline at end of file