View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.named.hazelcast;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.UUID;
24  import java.util.stream.IntStream;
25  
26  import com.hazelcast.client.HazelcastClient;
27  import com.hazelcast.client.config.ClientConfig;
28  import com.hazelcast.config.Config;
29  import com.hazelcast.core.Hazelcast;
30  import com.hazelcast.core.HazelcastInstance;
31  
32  /**
33   * Hazelcast test utilities.
34   */
35  public final class HazelcastClientUtils {
36      private final List<HazelcastInstance> servers = new ArrayList<>();
37  
38      /**
39       * Creates similar but still randomized name.
40       */
41      public String clusterName(Class<?> klazz) {
42          return String.format("%s-%s", klazz.getSimpleName(), UUID.randomUUID());
43      }
44  
45      /**
46       * Creates single Hazelcast client instance.
47       */
48      public synchronized HazelcastInstance createClient(String clusterName) {
49          ClientConfig config = ClientConfig.load();
50          config.setClusterName(clusterName);
51          return HazelcastClient.newHazelcastClient(config);
52      }
53  
54      /**
55       * Creates single Hazelcast member instance.
56       */
57      public synchronized HazelcastInstance createMember(String clusterName) {
58          return createMembers(1, clusterName).get(0);
59      }
60  
61      /**
62       * Creates given count of Hazelcast member instances.
63       */
64      public synchronized List<HazelcastInstance> createMembers(int memberCount, String clusterName) {
65          Config config = Config.load();
66          config.setClusterName(clusterName);
67          ArrayList<HazelcastInstance> result = new ArrayList<>(memberCount);
68          IntStream.range(0, memberCount).forEach(i -> {
69              config.setInstanceName("node-" + i);
70              HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
71              result.add(instance);
72              servers.add(instance);
73          });
74          return result;
75      }
76  
77      /**
78       * Shuts down the created instances.
79       */
80      public synchronized void cleanup() {
81          servers.forEach(HazelcastInstance::shutdown);
82          servers.clear();
83      }
84  }