View Javadoc
1   package com.atlassian.cache.hazelcast;
2   
3   import com.atlassian.hazelcast.serialization.OsgiClassLoaderRegistry;
4   import com.atlassian.hazelcast.serialization.OsgiSafe;
5   import com.atlassian.hazelcast.serialization.OsgiSafeStreamSerializer;
6   import com.atlassian.hazelcast.test.DefaultNodeConfigurer;
7   import com.atlassian.hazelcast.test.HazelcastCluster;
8   import com.hazelcast.config.Config;
9   import com.hazelcast.config.SerializerConfig;
10  import com.hazelcast.core.HazelcastInstance;
11  
12  import java.util.concurrent.atomic.AtomicInteger;
13  
14  /**
15   * JUnit rule to set up a Hazelcast cluster for testing.
16   */
17  public class InitOnceHazelcastCluster extends HazelcastCluster
18  {
19      private static final InitOnceHazelcastCluster INSTANCE = new Builder().size(2).nodeConfigurer(new DefaultNodeConfigurer()
20      {
21          @Override
22          public Config createConfig(int nodeIndex)
23          {
24              Config config = super.createConfig(nodeIndex);
25              config.getSerializationConfig().addSerializerConfig(
26                      new SerializerConfig()
27                              .setTypeClass(OsgiSafe.class)
28                              .setImplementation(new OsgiSafeStreamSerializer(new OsgiClassLoaderRegistry()))
29              );
30              return config;
31          }
32  
33          @Override
34          public void onReset(HazelcastInstance instance)
35          {
36              //Make sure we clear any mapConfigs too
37              super.onReset(instance);
38          }
39      }).build();
40  
41      // counter to keep track of the 'active users' of the HazelcastCluster. This is used to be able to set up the
42      // cluster only once for a set of tests (see HazelcastTestSuite)
43      private final AtomicInteger activeCount = new AtomicInteger();
44  
45      protected InitOnceHazelcastCluster(Builder builder)
46      {
47          super(builder);
48      }
49  
50      public int size()
51      {
52          return getNodes().size();
53      }
54  
55      @Override
56      protected void after()
57      {
58          if (activeCount.decrementAndGet() == 0)
59          {
60              // only truly shut down when the active count reaches zero
61              super.after();
62          } else {
63              super.reset();
64          }
65      }
66  
67      @Override
68      protected void before() throws Throwable
69      {
70          if (activeCount.getAndIncrement() == 0)
71          {
72              // only initialize when the active count is at zero
73              super.before();
74          } else {
75              super.reset();
76          }
77      }
78  
79      public static InitOnceHazelcastCluster getInstance()
80      {
81          return INSTANCE;
82      }
83  
84      public static class Builder extends AbstractBuilder<Builder, InitOnceHazelcastCluster>
85      {
86          @Override
87          public InitOnceHazelcastCluster build()
88          {
89              return new InitOnceHazelcastCluster(this);
90          }
91  
92          @Override
93          protected Builder self()
94          {
95              return this;
96          }
97      }
98  }