1 /** 2 * Copyright 2003-2006 Greg Luck 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package net.sf.ehcache.distribution; 18 19 import junit.framework.TestCase; 20 import net.sf.ehcache.Cache; 21 import net.sf.ehcache.CacheManager; 22 import net.sf.ehcache.Element; 23 import net.sf.ehcache.AbstractCacheTest; 24 25 import java.rmi.Naming; 26 import java.util.Date; 27 28 /** 29 * 30 * Note these tests need a live network interface running in multicast mode to work 31 * 32 * @author Greg Luck 33 * @version $Id: RMIDistributedCacheTest.java 51 2006-04-24 09:21:10Z gregluck $ 34 */ 35 public class RMIDistributedCacheTest extends TestCase { 36 37 38 /** 39 * manager 40 */ 41 protected CacheManager manager; 42 /** 43 * the cache name we wish to test 44 */ 45 private String cacheName1 = "sampleCache1"; 46 private String cacheName2 = "sampleCache2"; 47 /** 48 * the cache we wish to test 49 */ 50 private Cache sampleCache1; 51 private Cache sampleCache2; 52 53 54 private String hostName = "localhost"; 55 56 private Integer port = new Integer(40000); 57 private Element element; 58 private CachePeer cache1Peer; 59 private CachePeer cache2Peer; 60 61 /** 62 * {@inheritDoc} 63 * 64 * @throws Exception 65 */ 66 protected void setUp() throws Exception { 67 if (JVMUtil.isSingleRMIRegistryPerVM()) { 68 return; 69 } 70 71 manager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed1.xml"); 72 sampleCache1 = manager.getCache(cacheName1); 73 sampleCache2 = manager.getCache(cacheName2); 74 sampleCache1.removeAll(); 75 element = new Element("key", new Date()); 76 sampleCache1.put(element); 77 CacheManagerPeerListener cacheManagerPeerListener = 78 new RMICacheManagerPeerListener(hostName, port, manager, new Integer(2000)); 79 cacheManagerPeerListener.init(); 80 cache1Peer = (CachePeer) Naming.lookup(createNamingUrl() + cacheName1); 81 cache2Peer = (CachePeer) Naming.lookup(createNamingUrl() + cacheName2); 82 } 83 84 /** 85 * Shutdown the cache 86 */ 87 protected void tearDown() throws InterruptedException { 88 if (JVMUtil.isSingleRMIRegistryPerVM()) { 89 return; 90 } 91 92 Thread.sleep(10); 93 manager.shutdown(); 94 } 95 96 97 /** 98 * Getting an RMI Server going is a big deal 99 */ 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 118 for (int i = 0; i < 100; i++) { 119 new RMICacheManagerPeerListener(hostName, port, manager, new Integer(2000)); 120 } 121 cache1Peer = (CachePeer) Naming.lookup(createNamingUrl() + cacheName1); 122 assertNotNull(cache1Peer); 123 } 124 125 private String createNamingUrl() { 126 return "//" + hostName + ":" + port + "/"; 127 } 128 129 /** 130 * Attempts to get the cache name 131 * 132 * @throws java.net.MalformedURLException 133 * @throws java.rmi.NotBoundException 134 * @throws java.rmi.RemoteException 135 */ 136 public void testGetName() throws Exception { 137 if (JVMUtil.isSingleRMIRegistryPerVM()) { 138 return; 139 } 140 141 String lookupCacheName = cache1Peer.getName(); 142 assertEquals(cacheName1, lookupCacheName); 143 lookupCacheName = cache2Peer.getName(); 144 assertEquals(cacheName2, lookupCacheName); 145 } 146 147 148 }