<commit_before>/*
 * Copyright (c) 2014, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.instance;

import com.hazelcast.config.Config;
import com.hazelcast.config.MulticastConfig;
import com.hazelcast.core.Cluster;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;


/** Test the multicast loopback mode when there is no other
 * network interface than 127.0.0.1.
 *  
 * @author St&amp;eacute;<NAME> <<EMAIL>>
 */
@RunWith(HazelcastSerialClassRunner.class)
@Category(QuickTest.class)
public class MulticastLoopbackModeTest extends HazelcastTestSupport {

	/** Replies if a network interface was properly configured.
	 * 
	 * @return <code>true</code> if there is at least one configured interface;
	 * <code>false</code> otherwise.
	 */
	protected static boolean hasConfiguredNetworkInterface() {
		try {
			Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
			while (e.hasMoreElements()) {
				NetworkInterface i = e.nextElement();
				Enumeration<InetAddress> as = i.getInetAddresses();
				while (as.hasMoreElements()) {
					InetAddress a = as.nextElement();
					if (a instanceof Inet4Address && !a.isLoopbackAddress()
							&& !a.isMulticastAddress()) {
						return true;
					}
				}
			}
		}
		catch (Exception _) {
			// Silently cast the exceptions
		}
		return false;
	}
	
	private String multicastGroup;
	private HazelcastInstance hz1;
	private HazelcastInstance hz2;

	@Before
	public void setUpTests() {
		assumeFalse(
				"This test can be processed only if your host has no configured network interface.",
				hasConfiguredNetworkInterface());
		multicastGroup = System.clearProperty("hazelcast.multicast.group");
	}

	@After
	public void tearDownTests() {
		HazelcastInstanceManager.terminateAll();
		if (multicastGroup == null) {
			System.clearProperty("hazelcast.multicast.group");
		} else {
			System.setProperty("hazelcast.multicast.group", multicastGroup);
		}
	}	

	private void createTestEnvironment(boolean loopbackMode) throws Exception {
		Config config = new Config();
		config.setProperty("hazelcast.local.localAddress", "127.0.0.1");
		MulticastConfig multicastConfig = config.getNetworkConfig().getJoin().getMulticastConfig();
		multicastConfig.setEnabled(true);
		multicastConfig.setLoopbackModeEnabled(loopbackMode);

		hz1 = HazelcastInstanceManager.newHazelcastInstance(config);
		assertNotNull("Cannot create the first HazelcastInstance", hz1);

		hz2 = HazelcastInstanceManager.newHazelcastInstance(config);
		assertNotNull("Cannot create the second HazelcastInstance", hz2);
	}

	@Test
	public void testEnabledMode() throws Exception {
		createTestEnvironment(true);

		assertClusterSize(2, hz1);
		assertClusterSize(2, hz2);

		Cluster cluster1 = hz1.getCluster();
		Cluster cluster2 = hz2.getCluster();
		assertTrue("Members list " + cluster1.getMembers() + " should contain " + cluster2.getLocalMember(),
				cluster1.getMembers().contains(cluster2.getLocalMember()));
		assertTrue("Members list " + cluster2.getMembers() + " should contain " + cluster1.getLocalMember(),
				cluster2.getMembers().contains(cluster1.getLocalMember()));
	}

	@Test
	public void testDisabledMode() throws Exception {
		createTestEnvironment(false);

		assertClusterSize(1, hz1);
		assertClusterSize(1, hz2);
	}

}<commit_msg>Small cleanup of MulticastLoopbackModeTest (tabs vs. spaces, variable name which might be incompatible with Java 9+).<commit_after>/*
 * Copyright (c) 2014, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.instance;

import com.hazelcast.config.Config;
import com.hazelcast.config.MulticastConfig;
import com.hazelcast.core.Cluster;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;

/**
 * Test the multicast loopback mode when there is no other
 * network interface than 127.0.0.1.
 *
 * @author St&amp;eacute;<NAME> <<EMAIL>>
 */
@RunWith(HazelcastSerialClassRunner.class)
@Category(QuickTest.class)
public class MulticastLoopbackModeTest extends HazelcastTestSupport {

    /**
     * Replies if a network interface was properly configured.
     *
     * @return <code>true</code> if there is at least one configured interface;
     * <code>false</code> otherwise.
     */
    protected static boolean hasConfiguredNetworkInterface() {
        try {
            Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
            while (e.hasMoreElements()) {
                NetworkInterface i = e.nextElement();
                Enumeration<InetAddress> as = i.getInetAddresses();
                while (as.hasMoreElements()) {
                    InetAddress a = as.nextElement();
                    if (a instanceof Inet4Address && !a.isLoopbackAddress() && !a.isMulticastAddress()) {
                        return true;
                    }
                }
            }
        } catch (Exception ignored) {
            // silently cast the exceptions
        }
        return false;
    }

    private String multicastGroup;
    private HazelcastInstance hz1;
    private HazelcastInstance hz2;

    @Before
    public void setUpTests() {
        assumeFalse(
                "This test can be processed only if your host has no configured network interface.",
                hasConfiguredNetworkInterface());
        multicastGroup = System.clearProperty("hazelcast.multicast.group");
    }

    @After
    public void tearDownTests() {
        HazelcastInstanceManager.terminateAll();
        if (multicastGroup == null) {
            System.clearProperty("hazelcast.multicast.group");
        } else {
            System.setProperty("hazelcast.multicast.group", multicastGroup);
        }
    }

    private void createTestEnvironment(boolean loopbackMode) throws Exception {
        Config config = new Config();
        config.setProperty("hazelcast.local.localAddress", "127.0.0.1");
        MulticastConfig multicastConfig = config.getNetworkConfig().getJoin().getMulticastConfig();
        multicastConfig.setEnabled(true);
        multicastConfig.setLoopbackModeEnabled(loopbackMode);

        hz1 = HazelcastInstanceManager.newHazelcastInstance(config);
        assertNotNull("Cannot create the first HazelcastInstance", hz1);

        hz2 = HazelcastInstanceManager.newHazelcastInstance(config);
        assertNotNull("Cannot create the second HazelcastInstance", hz2);
    }

    @Test
    public void testEnabledMode() throws Exception {
        createTestEnvironment(true);

        assertClusterSize(2, hz1);
        assertClusterSize(2, hz2);

        Cluster cluster1 = hz1.getCluster();
        Cluster cluster2 = hz2.getCluster();
        assertTrue("Members list " + cluster1.getMembers() + " should contain " + cluster2.getLocalMember(),
                cluster1.getMembers().contains(cluster2.getLocalMember()));
        assertTrue("Members list " + cluster2.getMembers() + " should contain " + cluster1.getLocalMember(),
                cluster2.getMembers().contains(cluster1.getLocalMember()));
    }

    @Test
    public void testDisabledMode() throws Exception {
        createTestEnvironment(false);

        assertClusterSize(1, hz1);
        assertClusterSize(1, hz2);
    }
}
{'fraction_non_alphanumeric': 0.09960541751093101, 'fraction_numerical': 0.01034445984856564, 'mean_word_length': 3.7822539520652727, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3525830', 'n_tokens_mistral': 2828, 'n_tokens_neox': 2662, 'n_words': 674}