Skip to content

Commit

Permalink
Remove TestCollectionUtils.setOf and use Java 11 Set.of() instead [HZ…
Browse files Browse the repository at this point in the history
…-3080] (hazelcast#25402)
  • Loading branch information
orcunc authored Sep 8, 2023
1 parent 75e0d06 commit ef65f17
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.hazelcast.internal.diagnostics.StoreLatencyPlugin;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.TestCollectionUtils;
import com.hazelcast.test.annotation.ParallelJVMTest;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.Before;
Expand Down Expand Up @@ -57,7 +56,7 @@ public void setup() {
HazelcastInstance hz = createHazelcastInstance();
plugin = new StoreLatencyPlugin(getNodeEngineImpl(hz));
delegate = mock(QueueStore.class);
queueStore = new LatencyTrackingQueueStore<String>(delegate, plugin, NAME);
queueStore = new LatencyTrackingQueueStore<>(delegate, plugin, NAME);
}

@Test
Expand All @@ -76,7 +75,7 @@ public void load() {
@Test
public void loadAll() {
Collection<Long> keys = Arrays.asList(1L, 2L);
Map<Long, String> values = new HashMap<Long, String>();
Map<Long, String> values = new HashMap<>();
values.put(1L, "value1");
values.put(2L, "value2");

Expand All @@ -90,7 +89,7 @@ public void loadAll() {

@Test
public void loadAllKeys() {
Set<Long> keys = TestCollectionUtils.setOf(1L, 2L);
Set<Long> keys = Set.of(1L, 2L);

when(delegate.loadAllKeys()).thenReturn(keys);

Expand Down Expand Up @@ -133,7 +132,7 @@ public void store() {

@Test
public void storeAll() {
Map<Long, String> values = new HashMap<Long, String>();
Map<Long, String> values = new HashMap<>();
values.put(1L, "value1");
values.put(2L, "value2");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,21 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.io.InputStream;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

import static com.hazelcast.internal.util.XmlUtil.getNsAwareDocumentBuilderFactory;
import static com.hazelcast.test.TestCollectionUtils.setOf;
import static org.junit.Assert.assertEquals;

@RunWith(HazelcastSerialClassRunner.class)
@Category(SlowTest.class)
public class XmlConfigSchemaLocationTest extends HazelcastTestSupport {

// list of schema location URLs which we do not want to check
private static final Set<String> WHITELIST = setOf();
private static final Set<String> WHITELIST = Set.of();

private static final String XML_SCHEMA_NAMESPACE = "http://www.w3.org/2001/XMLSchema-instance";
private static final String XML_SCHEMA_LOCATION_ATTRIBUTE = "schemaLocation";
Expand All @@ -72,7 +70,7 @@ public class XmlConfigSchemaLocationTest extends HazelcastTestSupport {
public void setUp() throws ParserConfigurationException {
httpClient = HttpClients.createDefault();
documentBuilderFactory = getNsAwareDocumentBuilderFactory();
validUrlsCache = new HashSet<String>();
validUrlsCache = new HashSet<>();
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.Map;
import java.util.Set;

import static com.hazelcast.test.TestCollectionUtils.setOf;
import static java.util.Collections.singleton;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand All @@ -51,30 +50,30 @@ public class LoadMigrationStrategyTest extends HazelcastTestSupport {

@Before
public void setUp() {
ownerToPipelines = new HashMap<NioThread, Set<MigratablePipeline>>();
loadCounter = new ItemCounter<MigratablePipeline>();
ownerToPipelines = new HashMap<>();
loadCounter = new ItemCounter<>();
imbalance = new LoadImbalance(ownerToPipelines, loadCounter);
strategy = new LoadMigrationStrategy();
}

@Test
public void testImbalanceDetected_shouldReturnFalseWhenNoKnownMinimum() throws Exception {
public void testImbalanceDetected_shouldReturnFalseWhenNoKnownMinimum() {
imbalance.minimumLoad = Long.MIN_VALUE;

boolean imbalanceDetected = strategy.imbalanceDetected(imbalance);
assertFalse(imbalanceDetected);
}

@Test
public void testImbalanceDetected_shouldReturnFalseWhenNoKnownMaximum() throws Exception {
public void testImbalanceDetected_shouldReturnFalseWhenNoKnownMaximum() {
imbalance.maximumLoad = Long.MAX_VALUE;

boolean imbalanceDetected = strategy.imbalanceDetected(imbalance);
assertFalse(imbalanceDetected);
}

@Test
public void testImbalanceDetected_shouldReturnFalseWhenBalanced() throws Exception {
public void testImbalanceDetected_shouldReturnFalseWhenBalanced() {
imbalance.maximumLoad = 1000;
imbalance.minimumLoad = (long) (1000 * 0.8);

Expand All @@ -83,7 +82,7 @@ public void testImbalanceDetected_shouldReturnFalseWhenBalanced() throws Excepti
}

@Test
public void testImbalanceDetected_shouldReturnTrueWhenNotBalanced() throws Exception {
public void testImbalanceDetected_shouldReturnTrueWhenNotBalanced() {
imbalance.maximumLoad = 1000;
imbalance.minimumLoad = (long) (1000 * 0.8) - 1;

Expand All @@ -92,7 +91,7 @@ public void testImbalanceDetected_shouldReturnTrueWhenNotBalanced() throws Excep
}

@Test
public void testFindPipelineToMigrate() throws Exception {
public void testFindPipelineToMigrate() {
NioThread srcOwner = mock(NioThread.class);
NioThread dstOwner = mock(NioThread.class);
imbalance.srcOwner = srcOwner;
Expand All @@ -108,7 +107,7 @@ public void testFindPipelineToMigrate() throws Exception {
MigratablePipeline pipeline3 = mock(MigratablePipeline.class);
loadCounter.set(pipeline2, 200L);
loadCounter.set(pipeline3, 100L);
ownerToPipelines.put(srcOwner, setOf(pipeline2, pipeline3));
ownerToPipelines.put(srcOwner, Set.of(pipeline2, pipeline3));

MigratablePipeline pipelineToMigrate = strategy.findPipelineToMigrate(imbalance);
assertEquals(pipeline3, pipelineToMigrate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.Map;
import java.util.Set;

import static com.hazelcast.test.TestCollectionUtils.setOf;
import static java.lang.Math.abs;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand All @@ -52,16 +51,16 @@ public class MonkeyMigrationStrategyTest extends HazelcastTestSupport {

@Test
public void imbalanceDetected_shouldReturnFalseWhenNoPipelineExist() {
ownerPipelines.put(imbalance.srcOwner, Collections.<MigratablePipeline>emptySet());
ownerPipelines.put(imbalance.srcOwner, Collections.emptySet());

boolean imbalanceDetected = strategy.imbalanceDetected(imbalance);
assertFalse(imbalanceDetected);
}

@Before
public void setUp() {
ownerPipelines = new HashMap<NioThread, Set<MigratablePipeline>>();
pipelineLoadCounter = new ItemCounter<MigratablePipeline>();
ownerPipelines = new HashMap<>();
pipelineLoadCounter = new ItemCounter<>();
imbalance = new LoadImbalance(ownerPipelines, pipelineLoadCounter);
imbalance.srcOwner = mock(NioThread.class);

Expand All @@ -72,7 +71,7 @@ public void setUp() {
public void imbalanceDetected_shouldReturnTrueWhenPipelineExist() {
MigratablePipeline pipeline = mock(MigratablePipeline.class);

ownerPipelines.put(imbalance.srcOwner, setOf(pipeline));
ownerPipelines.put(imbalance.srcOwner, Set.of(pipeline));
boolean imbalanceDetected = strategy.imbalanceDetected(imbalance);
assertTrue(imbalanceDetected);
}
Expand All @@ -81,7 +80,7 @@ public void imbalanceDetected_shouldReturnTrueWhenPipelineExist() {
public void findPipelineToMigrate_shouldWorkEvenWithASinglePipelineAvailable() {
MigratablePipeline pipeline = mock(MigratablePipeline.class);

ownerPipelines.put(imbalance.srcOwner, setOf(pipeline));
ownerPipelines.put(imbalance.srcOwner, Set.of(pipeline));
MigratablePipeline pipelineToMigrate = strategy.findPipelineToMigrate(imbalance);
assertEquals(pipeline, pipelineToMigrate);
}
Expand All @@ -93,7 +92,7 @@ public void findPipelineToMigrate_shouldBeFair() {

MigratablePipeline pipeline1 = mock(MigratablePipeline.class);
MigratablePipeline pipeline2 = mock(MigratablePipeline.class);
ownerPipelines.put(imbalance.srcOwner, setOf(pipeline1, pipeline2));
ownerPipelines.put(imbalance.srcOwner, Set.of(pipeline1, pipeline2));

assertFairSelection(iterationCount, toleranceFactor, pipeline1, pipeline2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
import java.util.concurrent.atomic.AtomicReference;

import static com.hazelcast.internal.nio.IOUtil.toByteArray;
import static com.hazelcast.test.TestCollectionUtils.setOf;
import static java.util.Collections.singleton;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -168,9 +167,9 @@ public void testHookDeduplication() {
//the definition loaded by the child classloader -> it only delegates to the parent -> it's a duplicated
ServiceLoader.ServiceDefinition definition2 = new ServiceLoader.ServiceDefinition(hook.getName(), childClassloader);

Set<ServiceLoader.ServiceDefinition> definitions = setOf(definition1, definition2);
Set<ServiceLoader.ServiceDefinition> definitions = Set.of(definition1, definition2);
ServiceLoader.ClassIterator<PortableHook> iterator
= new ServiceLoader.ClassIterator<PortableHook>(definitions, PortableHook.class);
= new ServiceLoader.ClassIterator<>(definitions, PortableHook.class);

assertTrue(iterator.hasNext());
Class<PortableHook> hookFromIterator = iterator.next();
Expand All @@ -189,7 +188,7 @@ public void testSkipHooksWithImplementingTheExpectedInterfaceButLoadedByDifferen
ServiceLoader.ServiceDefinition definition = new ServiceLoader.ServiceDefinition(otherHook.getName(), otherClassloader);
Set<ServiceLoader.ServiceDefinition> definitions = singleton(definition);
ServiceLoader.ClassIterator<PortableHook> iterator
= new ServiceLoader.ClassIterator<PortableHook>(definitions, PortableHook.class);
= new ServiceLoader.ClassIterator<>(definitions, PortableHook.class);

assertFalse(iterator.hasNext());
}
Expand All @@ -205,7 +204,7 @@ public void testFailFastWhenHookDoesNotImplementExpectedInteface() {
ServiceLoader.ServiceDefinition definition = new ServiceLoader.ServiceDefinition(otherHook.getName(), otherClassloader);
Set<ServiceLoader.ServiceDefinition> definitions = singleton(definition);
ServiceLoader.ClassIterator<PortableHook> iterator
= new ServiceLoader.ClassIterator<PortableHook>(definitions, PortableHook.class);
= new ServiceLoader.ClassIterator<>(definitions, PortableHook.class);

assertFalse(iterator.hasNext());
}
Expand All @@ -216,7 +215,7 @@ public void testSkipUnknownClassesStartingFromHazelcastPackage() {
= new ServiceLoader.ServiceDefinition("com.hazelcast.DoesNotExist", getClass().getClassLoader());
Set<ServiceLoader.ServiceDefinition> definitions = singleton(definition);
ServiceLoader.ClassIterator<PortableHook> iterator
= new ServiceLoader.ClassIterator<PortableHook>(definitions, PortableHook.class);
= new ServiceLoader.ClassIterator<>(definitions, PortableHook.class);

assertFalse(iterator.hasNext());
}
Expand All @@ -227,7 +226,7 @@ public void testFailFastOnUnknownClassesFromNonHazelcastPackage() {
= new ServiceLoader.ServiceDefinition("non.a.hazelcast.DoesNotExist", getClass().getClassLoader());
Set<ServiceLoader.ServiceDefinition> definitions = singleton(definition);
ServiceLoader.ClassIterator<PortableHook> iterator
= new ServiceLoader.ClassIterator<PortableHook>(definitions, PortableHook.class);
= new ServiceLoader.ClassIterator<>(definitions, PortableHook.class);
assertFalse(iterator.hasNext());
}

Expand All @@ -245,7 +244,7 @@ public void testSkipHookLoadedByDifferentClassloader() {
ServiceLoader.ServiceDefinition definition2
= new ServiceLoader.ServiceDefinition(SpiDataSerializerHook.class.getName(), SpiDataSerializerHook.class.getClassLoader());

Set<ServiceLoader.ServiceDefinition> definitions = setOf(definition1, definition2);
Set<ServiceLoader.ServiceDefinition> definitions = Set.of(definition1, definition2);
ServiceLoader.ClassIterator<DataSerializerHook> iterator
= new ServiceLoader.ClassIterator<>(definitions, DataSerializerHook.class);

Expand All @@ -262,9 +261,9 @@ public void testPrivatePortableHook() {
ServiceLoader.ServiceDefinition definition = new ServiceLoader.ServiceDefinition(hookName, classLoader);

ServiceLoader.ClassIterator<PortableHook> classIterator
= new ServiceLoader.ClassIterator<PortableHook>(singleton(definition), PortableHook.class);
= new ServiceLoader.ClassIterator<>(singleton(definition), PortableHook.class);
ServiceLoader.NewInstanceIterator<PortableHook> instanceIterator
= new ServiceLoader.NewInstanceIterator<PortableHook>(classIterator);
= new ServiceLoader.NewInstanceIterator<>(classIterator);

assertTrue(instanceIterator.hasNext());
DummyPrivatePortableHook hook = (DummyPrivatePortableHook) instanceIterator.next();
Expand All @@ -278,9 +277,9 @@ public void testPrivateSerializerHook() {
ServiceLoader.ServiceDefinition definition = new ServiceLoader.ServiceDefinition(hookName, classLoader);

ServiceLoader.ClassIterator<SerializerHook> classIterator
= new ServiceLoader.ClassIterator<SerializerHook>(singleton(definition), SerializerHook.class);
= new ServiceLoader.ClassIterator<>(singleton(definition), SerializerHook.class);
ServiceLoader.NewInstanceIterator<SerializerHook> instanceIterator
= new ServiceLoader.NewInstanceIterator<SerializerHook>(classIterator);
= new ServiceLoader.NewInstanceIterator<>(classIterator);

assertTrue(instanceIterator.hasNext());
DummyPrivateSerializerHook hook = (DummyPrivateSerializerHook) instanceIterator.next();
Expand Down Expand Up @@ -357,7 +356,7 @@ public void loadServicesSingleClassLoader() throws Exception {
Class<ServiceLoaderTestInterface> type = ServiceLoaderTestInterface.class;
String factoryId = "com.hazelcast.ServiceLoaderTestInterface";

Set<ServiceLoaderTestInterface> implementations = new HashSet<ServiceLoaderTestInterface>();
Set<ServiceLoaderTestInterface> implementations = new HashSet<>();
Iterator<ServiceLoaderTestInterface> iterator = ServiceLoader.iterator(type, factoryId, null);
while (iterator.hasNext()) {
implementations.add(iterator.next());
Expand All @@ -373,7 +372,7 @@ public void loadServicesSimpleGivenClassLoader() throws Exception {

ClassLoader given = new URLClassLoader(new URL[0]);

Set<ServiceLoaderTestInterface> implementations = new HashSet<ServiceLoaderTestInterface>();
Set<ServiceLoaderTestInterface> implementations = new HashSet<>();
Iterator<ServiceLoaderTestInterface> iterator = ServiceLoader.iterator(type, factoryId, given);
while (iterator.hasNext()) {
implementations.add(iterator.next());
Expand All @@ -391,7 +390,7 @@ public void loadServicesSimpleDifferentThreadContextClassLoader() throws Excepti
ClassLoader tccl = current.getContextClassLoader();
current.setContextClassLoader(new URLClassLoader(new URL[0]));

Set<ServiceLoaderTestInterface> implementations = new HashSet<ServiceLoaderTestInterface>();
Set<ServiceLoaderTestInterface> implementations = new HashSet<>();
Iterator<ServiceLoaderTestInterface> iterator = ServiceLoader.iterator(type, factoryId, null);
while (iterator.hasNext()) {
implementations.add(iterator.next());
Expand All @@ -412,7 +411,7 @@ public void loadServicesTcclAndGivenClassLoader() throws Exception {
ClassLoader tccl = current.getContextClassLoader();
current.setContextClassLoader(new URLClassLoader(new URL[0]));

Set<ServiceLoaderTestInterface> implementations = new HashSet<ServiceLoaderTestInterface>();
Set<ServiceLoaderTestInterface> implementations = new HashSet<>();
Iterator<ServiceLoaderTestInterface> iterator = ServiceLoader.iterator(type, factoryId, given);
while (iterator.hasNext()) {
implementations.add(iterator.next());
Expand All @@ -433,7 +432,7 @@ public void loadServicesSameTcclAndGivenClassLoader() throws Exception {
ClassLoader tccl = current.getContextClassLoader();
current.setContextClassLoader(same);

Set<ServiceLoaderTestInterface> implementations = new HashSet<ServiceLoaderTestInterface>();
Set<ServiceLoaderTestInterface> implementations = new HashSet<>();
Iterator<ServiceLoaderTestInterface> iterator = ServiceLoader.iterator(type, factoryId, same);
while (iterator.hasNext()) {
implementations.add(iterator.next());
Expand All @@ -451,7 +450,7 @@ public void loadServicesWithSpaceInURL() throws Exception {
URL url = ClassLoader.getSystemResource("test with special chars^/");
ClassLoader given = new URLClassLoader(new URL[]{url});

Set<ServiceLoaderSpecialCharsTestInterface> implementations = new HashSet<ServiceLoaderSpecialCharsTestInterface>();
Set<ServiceLoaderSpecialCharsTestInterface> implementations = new HashSet<>();
Iterator<ServiceLoaderSpecialCharsTestInterface> iterator = ServiceLoader.iterator(type, factoryId, given);
while (iterator.hasNext()) {
implementations.add(iterator.next());
Expand All @@ -469,7 +468,7 @@ public void loadServicesFromInMemoryClassLoader() throws Exception {
// Handles META-INF/services/com.hazelcast.CustomServiceLoaderTestInterface
ClassLoader given = new CustomUrlStreamHandlerClassloader(parent);

Set<ServiceLoaderTestInterface> implementations = new HashSet<ServiceLoaderTestInterface>();
Set<ServiceLoaderTestInterface> implementations = new HashSet<>();
Iterator<ServiceLoaderTestInterface> iterator = ServiceLoader.iterator(type, factoryId, given);
while (iterator.hasNext()) {
implementations.add(iterator.next());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.Set;

import static com.hazelcast.test.Accessors.getSerializationService;
import static com.hazelcast.test.TestCollectionUtils.setOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -95,7 +94,7 @@ public void whenSelecting_withoutPredicate() {

Set<String> result = map.localKeySet();

assertEquals(setOf(localKey1, localKey2), result);
assertEquals(Set.of(localKey1, localKey2), result);
}

@Test
Expand All @@ -106,7 +105,7 @@ public void whenSelectingAllEntries() {

Set<String> result = map.localKeySet(Predicates.alwaysTrue());

assertEquals(setOf(localKey1, localKey2), result);
assertEquals(Set.of(localKey1, localKey2), result);
}

@Test
Expand All @@ -120,7 +119,7 @@ public void whenSelectingSomeEntries() {

Set<String> result = map.localKeySet(new GoodPredicate());

assertEquals(setOf(localKey1, localKey3), result);
assertEquals(Set.of(localKey1, localKey3), result);
}

@Test
Expand Down
Loading

0 comments on commit ef65f17

Please sign in to comment.