~raginggoblin/infolog/infolog

« back to all changes in this revision

Viewing changes to InfologServer/lib/hibernate-distribution-3.3.2.GA/project/cache-jbosscache2/src/test/java/org/hibernate/test/cache/jbc2/AbstractRegionImplTestCase.java

  • Committer: Raging Goblin
  • Date: 2013-11-16 16:51:32 UTC
  • Revision ID: raging_goblin-20131116165132-weujnptzc88uy4ah
Mavenized the project, now using shared project InfologSync

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Hibernate, Relational Persistence for Idiomatic Java
3
 
 *
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.
8
 
 *
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.
12
 
 *
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
16
 
 * for more details.
17
 
 *
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
23
 
 */
24
 
package org.hibernate.test.cache.jbc2;
25
 
 
26
 
import java.util.Map;
27
 
import java.util.Properties;
28
 
 
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;
44
 
 
45
 
/**
46
 
 * Base class for tests of Region implementations.
47
 
 * 
48
 
 * @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a>
49
 
 * @version $Revision: 1 $
50
 
 */
51
 
public abstract class AbstractRegionImplTestCase extends AbstractJBossCacheTestCase {
52
 
 
53
 
    /**
54
 
     * Create a new RegionImplTestCaseBase.
55
 
     * 
56
 
     * @param name
57
 
     */
58
 
    public AbstractRegionImplTestCase(String name) {
59
 
        super(name);
60
 
    }  
61
 
 
62
 
    /**
63
 
     * Tests proper handling of region initialization and destruction.
64
 
     * 
65
 
     * @throws Exception
66
 
     */
67
 
    public void testActivationDeactivation() throws Exception {
68
 
        
69
 
        // Set up a cache to monitor affects of starting the region
70
 
        Cache remoteCache = DefaultCacheFactory.getInstance().createCache(SharedCacheInstanceManager.DEFAULT_CACHE_RESOURCE, false);
71
 
        
72
 
        // This test assumes replication; verify that's correct
73
 
        assertEquals("Cache is REPL_SYNC", "REPL_SYNC", remoteCache.getConfiguration().getCacheModeString());
74
 
        
75
 
        JChannelFactory channelFactory = new JChannelFactory();
76
 
        channelFactory.setMultiplexerConfig(SharedCacheInstanceManager.DEF_JGROUPS_RESOURCE);
77
 
        remoteCache.getConfiguration().getRuntimeConfig().setMuxChannelFactory(channelFactory);
78
 
        remoteCache.start();
79
 
        
80
 
        // Make sure we stop the remoteCache
81
 
        registerCache(remoteCache);
82
 
        
83
 
        Fqn regionFqn = getRegionFqn("test/test", "test");
84
 
        
85
 
        assertNull("No region node", remoteCache.getRoot().getChild( regionFqn ));
86
 
        
87
 
        Configuration cfg = CacheTestUtil.buildConfiguration("test", SharedJBossCacheRegionFactory.class, true, true);
88
 
        JBossCacheRegionFactory regionFactory = CacheTestUtil.startRegionFactory(cfg, getCacheTestSupport());
89
 
        
90
 
        Region region = createRegion(regionFactory, "test/test", cfg.getProperties(), getCacheDataDescription());
91
 
        
92
 
        Cache localCache = getJBossCache( regionFactory );
93
 
        
94
 
        // This test assumes replication; verify that's correct
95
 
        assertEquals("Cache is REPL_SYNC", "REPL_SYNC", localCache.getConfiguration().getCacheModeString());
96
 
        
97
 
        // Region creation should not have affected remoteCache
98
 
 
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() );
103
 
        
104
 
        // Confirm region destroy does not affect remote cache
105
 
        
106
 
        Option option = new Option();
107
 
        option.setCacheModeLocal(true);
108
 
        remoteCache.getInvocationContext().setOptionOverrides(option);
109
 
        remoteCache.put(regionFqn, "test", "test");
110
 
        
111
 
        assertEquals("Put succeeded", "test", remoteCache.get(regionFqn, "test"));
112
 
        assertNull("Put was local", localCache.get(regionFqn, "test"));
113
 
        
114
 
        region.destroy();
115
 
        
116
 
        assertEquals("Remote cache unchanged", "test", remoteCache.get(regionFqn, "test"));
117
 
        assertNull("No region node", localCache.getRoot().getChild( regionFqn ));
118
 
    }
119
 
    
120
 
    public void testToMap() throws Exception {
121
 
        Configuration cfg = CacheTestUtil.buildConfiguration("test", SharedJBossCacheRegionFactory.class, true, true);
122
 
        JBossCacheRegionFactory regionFactory = CacheTestUtil.startRegionFactory(cfg, getCacheTestSupport());
123
 
        
124
 
        Region region = createRegion(regionFactory, "test/test", cfg.getProperties(), getCacheDataDescription());
125
 
        
126
 
        Map map = region.toMap();
127
 
        assertNotNull(map);
128
 
        assertEquals(0, map.size());
129
 
        
130
 
        putInRegion(region, "key1", "value1");
131
 
        putInRegion(region, "key2", "value2");
132
 
        
133
 
        map = region.toMap();
134
 
        assertNotNull(map);
135
 
        assertEquals(2, map.size());
136
 
        assertEquals("value1", map.get("key1"));
137
 
        assertEquals("value2", map.get("key2"));
138
 
        
139
 
        removeFromRegion(region, "key1");
140
 
        
141
 
        map = region.toMap();
142
 
        assertNotNull(map);
143
 
        assertEquals(1, map.size());
144
 
        assertEquals("value2", map.get("key2"));
145
 
    }
146
 
    
147
 
    protected abstract Cache getJBossCache(JBossCacheRegionFactory regionFactory);
148
 
    
149
 
    protected abstract Fqn getRegionFqn(String regionName, String regionPrefix);
150
 
    
151
 
    protected abstract Region createRegion(JBossCacheRegionFactory regionFactory, String regionName, Properties properties, CacheDataDescription cdd);
152
 
    
153
 
    protected abstract void putInRegion(Region region, Object key, Object value);
154
 
    protected abstract void removeFromRegion(Region region, Object key);
155
 
    
156
 
    protected CacheDataDescription getCacheDataDescription() {
157
 
       return new CacheDataDescriptionImpl(true, true, ComparableComparator.INSTANCE);
158
 
    }   
159
 
 
160
 
}