2
* Hibernate, Relational Persistence for Idiomatic Java
4
* Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as
5
* indicated by the @author tags or express copyright attribution
6
* statements applied by the authors. All third-party contributions are
7
* distributed under license by Red Hat Middleware LLC.
9
* This copyrighted material is made available to anyone wishing to use, modify,
10
* copy, or redistribute it subject to the terms and conditions of the GNU
11
* Lesser General Public License, as published by the Free Software Foundation.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
18
* You should have received a copy of the GNU Lesser General Public License
19
* along with this distribution; if not, write to:
20
* Free Software Foundation, Inc.
21
* 51 Franklin Street, Fifth Floor
22
* Boston, MA 02110-1301 USA
24
package org.hibernate.test.cache.jbc2;
27
import java.util.Properties;
29
import org.hibernate.cache.CacheDataDescription;
30
import org.hibernate.cache.Region;
31
import org.hibernate.cache.impl.CacheDataDescriptionImpl;
32
import org.hibernate.cache.jbc2.JBossCacheRegionFactory;
33
import org.hibernate.cache.jbc2.SharedJBossCacheRegionFactory;
34
import org.hibernate.cache.jbc2.builder.SharedCacheInstanceManager;
35
import org.hibernate.cfg.Configuration;
36
import org.hibernate.test.util.CacheTestUtil;
37
import org.hibernate.util.ComparableComparator;
38
import org.jboss.cache.Cache;
39
import org.jboss.cache.DefaultCacheFactory;
40
import org.jboss.cache.Fqn;
41
import org.jboss.cache.Node;
42
import org.jboss.cache.config.Option;
43
import org.jgroups.JChannelFactory;
46
* Base class for tests of Region implementations.
48
* @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a>
49
* @version $Revision: 1 $
51
public abstract class AbstractRegionImplTestCase extends AbstractJBossCacheTestCase {
54
* Create a new RegionImplTestCaseBase.
58
public AbstractRegionImplTestCase(String name) {
63
* Tests proper handling of region initialization and destruction.
67
public void testActivationDeactivation() throws Exception {
69
// Set up a cache to monitor affects of starting the region
70
Cache remoteCache = DefaultCacheFactory.getInstance().createCache(SharedCacheInstanceManager.DEFAULT_CACHE_RESOURCE, false);
72
// This test assumes replication; verify that's correct
73
assertEquals("Cache is REPL_SYNC", "REPL_SYNC", remoteCache.getConfiguration().getCacheModeString());
75
JChannelFactory channelFactory = new JChannelFactory();
76
channelFactory.setMultiplexerConfig(SharedCacheInstanceManager.DEF_JGROUPS_RESOURCE);
77
remoteCache.getConfiguration().getRuntimeConfig().setMuxChannelFactory(channelFactory);
80
// Make sure we stop the remoteCache
81
registerCache(remoteCache);
83
Fqn regionFqn = getRegionFqn("test/test", "test");
85
assertNull("No region node", remoteCache.getRoot().getChild( regionFqn ));
87
Configuration cfg = CacheTestUtil.buildConfiguration("test", SharedJBossCacheRegionFactory.class, true, true);
88
JBossCacheRegionFactory regionFactory = CacheTestUtil.startRegionFactory(cfg, getCacheTestSupport());
90
Region region = createRegion(regionFactory, "test/test", cfg.getProperties(), getCacheDataDescription());
92
Cache localCache = getJBossCache( regionFactory );
94
// This test assumes replication; verify that's correct
95
assertEquals("Cache is REPL_SYNC", "REPL_SYNC", localCache.getConfiguration().getCacheModeString());
97
// Region creation should not have affected remoteCache
99
assertNull("No region node", remoteCache.getRoot().getChild( regionFqn ));
100
Node regionRoot = localCache.getRoot().getChild( regionFqn );
101
assertTrue("Has a node at " + regionFqn, regionRoot != null );
102
assertTrue(regionFqn + " is resident", regionRoot.isResident() );
104
// Confirm region destroy does not affect remote cache
106
Option option = new Option();
107
option.setCacheModeLocal(true);
108
remoteCache.getInvocationContext().setOptionOverrides(option);
109
remoteCache.put(regionFqn, "test", "test");
111
assertEquals("Put succeeded", "test", remoteCache.get(regionFqn, "test"));
112
assertNull("Put was local", localCache.get(regionFqn, "test"));
116
assertEquals("Remote cache unchanged", "test", remoteCache.get(regionFqn, "test"));
117
assertNull("No region node", localCache.getRoot().getChild( regionFqn ));
120
public void testToMap() throws Exception {
121
Configuration cfg = CacheTestUtil.buildConfiguration("test", SharedJBossCacheRegionFactory.class, true, true);
122
JBossCacheRegionFactory regionFactory = CacheTestUtil.startRegionFactory(cfg, getCacheTestSupport());
124
Region region = createRegion(regionFactory, "test/test", cfg.getProperties(), getCacheDataDescription());
126
Map map = region.toMap();
128
assertEquals(0, map.size());
130
putInRegion(region, "key1", "value1");
131
putInRegion(region, "key2", "value2");
133
map = region.toMap();
135
assertEquals(2, map.size());
136
assertEquals("value1", map.get("key1"));
137
assertEquals("value2", map.get("key2"));
139
removeFromRegion(region, "key1");
141
map = region.toMap();
143
assertEquals(1, map.size());
144
assertEquals("value2", map.get("key2"));
147
protected abstract Cache getJBossCache(JBossCacheRegionFactory regionFactory);
149
protected abstract Fqn getRegionFqn(String regionName, String regionPrefix);
151
protected abstract Region createRegion(JBossCacheRegionFactory regionFactory, String regionName, Properties properties, CacheDataDescription cdd);
153
protected abstract void putInRegion(Region region, Object key, Object value);
154
protected abstract void removeFromRegion(Region region, Object key);
156
protected CacheDataDescription getCacheDataDescription() {
157
return new CacheDataDescriptionImpl(true, true, ComparableComparator.INSTANCE);