/
CoherenceClusterResource.java
233 lines (194 loc) · 8.42 KB
/
CoherenceClusterResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* File: CoherenceClusterResource.java
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* The contents of this file are subject to the terms and conditions of
* the Common Development and Distribution License 1.0 (the "License").
*
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the License by consulting the LICENSE.txt file
* distributed with this file, or by consulting https://oss.oracle.com/licenses/CDDL
*
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file LICENSE.txt.
*
* MODIFICATIONS:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*/
package com.oracle.bedrock.junit;
import com.oracle.bedrock.Option;
import com.oracle.bedrock.OptionsByType;
import com.oracle.bedrock.runtime.Application;
import com.oracle.bedrock.runtime.AssemblyBuilder;
import com.oracle.bedrock.runtime.LocalPlatform;
import com.oracle.bedrock.runtime.Platform;
import com.oracle.bedrock.runtime.coherence.CoherenceCluster;
import com.oracle.bedrock.runtime.coherence.CoherenceClusterBuilder;
import com.oracle.bedrock.runtime.coherence.CoherenceClusterMember;
import com.oracle.bedrock.runtime.coherence.options.ClusterName;
import com.oracle.bedrock.runtime.coherence.options.LocalStorage;
import com.oracle.bedrock.runtime.coherence.options.RoleName;
import com.oracle.bedrock.runtime.java.options.Headless;
import com.oracle.bedrock.runtime.java.options.HeapSize;
import com.oracle.bedrock.runtime.java.options.HotSpot;
import com.oracle.bedrock.runtime.options.Console;
import com.oracle.bedrock.runtime.options.PlatformPredicate;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.ConfigurableCacheFactory;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import java.util.HashMap;
import java.util.Properties;
/**
* A Coherence-based {@link AbstractAssemblyResource} that creates {@link CoherenceCluster}s for use with JUnit tests.
* <p>
* Copyright (c) 2017. All Rights Reserved. Oracle Corporation.<br>
* Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
*
* @author Brian Oliver
*/
public class CoherenceClusterResource
extends AbstractAssemblyResource<CoherenceClusterMember, CoherenceCluster, CoherenceClusterResource>
{
/**
* The system properties prior to the creation of the {@link CoherenceClusterResource <R>}.
*/
private Properties systemProperties;
/**
* The {@link ConfigurableCacheFactory} sessions that have been locally created against the
* {@link CoherenceCluster} using this {@link CoherenceClusterResource}.
*/
private HashMap<SessionBuilder, ConfigurableCacheFactory> sessions;
/**
* Constructs a {@link CoherenceClusterResource}.
*/
public CoherenceClusterResource()
{
super();
// by default we have no sessions
this.sessions = new HashMap<>();
// establish default java process options
this.commonOptionsByType.add(Headless.enabled());
this.commonOptionsByType.add(HotSpot.Mode.SERVER);
this.commonOptionsByType.add(HeapSize.of(256, HeapSize.Units.MB, 1024, HeapSize.Units.MB));
// establish default bedrock options
this.commonOptionsByType.add(Console.system());
}
@Override
protected AssemblyBuilder<CoherenceClusterMember, CoherenceCluster> createBuilder()
{
return new CoherenceClusterBuilder();
}
/**
* Obtains the {@link CoherenceCluster} created by the {@link CoherenceClusterResource}.
*
* @return the {@link CoherenceCluster}
*/
public CoherenceCluster getCluster()
{
return assembly;
}
@Override
protected void before() throws Throwable
{
if (launchDefinitions.isEmpty())
{
throw new IllegalStateException("CoherenceClusterResource fails to define members to include when launching");
}
// take a snapshot of the current system properties so we can restore them when cleaning up the resource
this.systemProperties = com.oracle.bedrock.util.SystemProperties.createSnapshot();
// let's ensure that we don't have a local cluster member
CacheFactory.setCacheFactoryBuilder(null);
CacheFactory.shutdown();
// let the super-class perform it's initialization
super.before();
}
@Override
protected void after()
{
// clean up the sessions
synchronized (sessions)
{
for (ConfigurableCacheFactory session : sessions.values())
{
CacheFactory.getCacheFactoryBuilder().release(session);
}
}
// let's ensure that we don't have a local cluster member
CacheFactory.setCacheFactoryBuilder(null);
CacheFactory.shutdown();
// let the super-class perform it's clean-up
super.after();
// restore the system properties
com.oracle.bedrock.util.SystemProperties.replaceWith(systemProperties);
}
@Override
public Statement apply(Statement base,
Description description)
{
// automatically set the cluster name to the test class name
// if the cluster name isn't configured
commonOptionsByType.addIfAbsent(ClusterName.of(description.getClassName()));
return super.apply(base, description);
}
/**
* Defines the necessary information for launching one or more {@link CoherenceClusterMember}s
* as part of the {@link CoherenceCluster} when the {@link CoherenceClusterResource} is established.
* <p>
* The {@link Platform} on which the {@link CoherenceClusterMember}s are launched is based on the
* {@link PlatformPredicate} specified as an {@link Option}. By default this is {@link PlatformPredicate#any()}.
* <p>
* Multiple calls to this method are permitted, allowing a {@link CoherenceCluster} to be created containing
* multiple different types of {@link CoherenceCluster}s.
* <p>
* This is equivalent to calling {@link #include(int, Class, Option...)} using a {@link CoherenceClusterMember}
* class as the {@link Application} class.
*
* @param count the number of instances of the {@link CoherenceCluster} that should be launched for
* the {@link CoherenceCluster}
* @param options the {@link Option}s to use for launching the {@link CoherenceCluster}s
*
* @return the {@link CoherenceClusterResource} to permit fluent-style method calls
*/
public CoherenceClusterResource include(int count,
Option... options)
{
return include(count, CoherenceClusterMember.class, options);
}
/**
* Obtains a session, represented as a {@link ConfigurableCacheFactory}, against the {@link CoherenceCluster}.
* <p>
* Only a single session may be created by a {@link CoherenceClusterResource} against the {@link CoherenceCluster}.
* <p>
* Attempts to request a session multiple times with the same {@link SessionBuilder} will return the same session.
*
* @param builder the builder for the specific type of session
*
* @return a {@link ConfigurableCacheFactory} representing the Coherence Session.
*
* @throws IllegalStateException when an attempt to request sessions for
* different {@link SessionBuilder}s is made
*/
public synchronized ConfigurableCacheFactory createSession(SessionBuilder builder)
{
// restore the system properties (as the session needs to start cleanly)
com.oracle.bedrock.util.SystemProperties.replaceWith(systemProperties);
ConfigurableCacheFactory session = sessions.get(builder);
if (session == null)
{
OptionsByType optionsByType = OptionsByType.of(commonOptionsByType);
optionsByType.add(RoleName.of("client"));
optionsByType.add(LocalStorage.disabled());
session = builder.build(LocalPlatform.get(), getCluster(), optionsByType);
sessions.put(builder, session);
}
return session;
}
}