001: /**
002: * Copyright 2003-2007 Luck Consulting Pty Ltd
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package net.sf.ehcache.distribution;
016:
017: import junit.framework.TestCase;
018: import net.sf.ehcache.AbstractCacheTest;
019: import net.sf.ehcache.CacheManager;
020: import net.sf.ehcache.Ehcache;
021: import net.sf.ehcache.Element;
022:
023: import java.rmi.Naming;
024: import java.util.Date;
025:
026: /**
027: *
028: * Note these tests need a live network interface running in multicast mode to work
029: *
030: * @author Greg Luck
031: * @version $Id: RMIDistributedCacheTest.java 571 2007-12-25 23:56:28Z gregluck $
032: */
033: public class RMIDistributedCacheTest extends TestCase {
034:
035: /**
036: * manager
037: */
038: protected CacheManager manager;
039: /**
040: * the cache name we wish to test
041: */
042: private String cacheName1 = "sampleCache1";
043: private String cacheName2 = "sampleCache2";
044: /**
045: * the cache we wish to test
046: */
047: private Ehcache sampleCache1;
048: private Ehcache sampleCache2;
049:
050: private String hostName = "localhost";
051:
052: private Integer port = new Integer(40000);
053: private Element element;
054: private CachePeer cache1Peer;
055: private CachePeer cache2Peer;
056:
057: /**
058: * {@inheritDoc}
059: *
060: * @throws Exception
061: */
062: protected void setUp() throws Exception {
063: if (JVMUtil.isSingleRMIRegistryPerVM()) {
064: return;
065: }
066:
067: manager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR
068: + "distribution/ehcache-distributed1.xml");
069: MulticastKeepaliveHeartbeatSender.setHeartBeatInterval(1000);
070: sampleCache1 = manager.getCache(cacheName1);
071: sampleCache2 = manager.getCache(cacheName2);
072: sampleCache1.removeAll();
073: element = new Element("key", new Date());
074: sampleCache1.put(element);
075: CacheManagerPeerListener cacheManagerPeerListener = new RMICacheManagerPeerListener(
076: hostName, port, manager, new Integer(2000));
077: cacheManagerPeerListener.init();
078: cache1Peer = (CachePeer) Naming.lookup(createNamingUrl()
079: + cacheName1);
080: cache2Peer = (CachePeer) Naming.lookup(createNamingUrl()
081: + cacheName2);
082: }
083:
084: /**
085: * Shutdown the cache
086: */
087: protected void tearDown() throws InterruptedException {
088: if (JVMUtil.isSingleRMIRegistryPerVM()) {
089: return;
090: }
091:
092: Thread.sleep(10);
093: manager.shutdown();
094: int i = 0;
095: }
096:
097: /**
098: * Getting an RMI Server going is a big deal
099: */
100: public void testCreation() throws Exception {
101: if (JVMUtil.isSingleRMIRegistryPerVM()) {
102: return;
103: }
104:
105: assertNotNull(cache1Peer);
106: assertNotNull(cache2Peer);
107: }
108:
109: /**
110: * The use of one-time registry creation and Naming.rebind should mean we can create as many listeneres as we like.
111: * They will simply replace the ones that were there.
112: */
113: public void testMultipleCreationOfRMIServers() throws Exception {
114: if (JVMUtil.isSingleRMIRegistryPerVM()) {
115: return;
116: }
117: RMICacheManagerPeerListener[] listeners = new RMICacheManagerPeerListener[100];
118: for (int i = 0; i < 100; i++) {
119: listeners[i] = new RMICacheManagerPeerListener(hostName,
120: port, manager, new Integer(2000));
121: }
122: cache1Peer = (CachePeer) Naming.lookup(createNamingUrl()
123: + cacheName1);
124: assertNotNull(cache1Peer);
125:
126: for (int i = 0; i < 100; i++) {
127: listeners[i].dispose();
128: }
129: }
130:
131: private String createNamingUrl() {
132: return "//" + hostName + ":" + port + "/";
133: }
134:
135: /**
136: * Attempts to get the cache name
137: *
138: * @throws java.net.MalformedURLException
139: * @throws java.rmi.NotBoundException
140: * @throws java.rmi.RemoteException
141: */
142: public void testGetName() throws Exception {
143: if (JVMUtil.isSingleRMIRegistryPerVM()) {
144: return;
145: }
146:
147: String lookupCacheName = cache1Peer.getName();
148: assertEquals(cacheName1, lookupCacheName);
149: lookupCacheName = cache2Peer.getName();
150: assertEquals(cacheName2, lookupCacheName);
151: }
152:
153: }
|