1   /**
2    *  Copyright 2003-2007 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.AbstractCacheTest;
21  import net.sf.ehcache.CacheManager;
22  import net.sf.ehcache.Ehcache;
23  import net.sf.ehcache.Element;
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 512 2007-07-10 09:18:45Z 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 Ehcache sampleCache1;
51      private Ehcache 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          MulticastKeepaliveHeartbeatSender.setHeartBeatInterval(1000);
73          sampleCache1 = manager.getCache(cacheName1);
74          sampleCache2 = manager.getCache(cacheName2);
75          sampleCache1.removeAll();
76          element = new Element("key", new Date());
77          sampleCache1.put(element);
78          CacheManagerPeerListener cacheManagerPeerListener =
79                  new RMICacheManagerPeerListener(hostName, port, manager, new Integer(2000));
80          cacheManagerPeerListener.init();
81          cache1Peer = (CachePeer) Naming.lookup(createNamingUrl() + cacheName1);
82          cache2Peer = (CachePeer) Naming.lookup(createNamingUrl() + cacheName2);
83      }
84  
85      /**
86       * Shutdown the cache
87       */
88      protected void tearDown() throws InterruptedException {
89          if (JVMUtil.isSingleRMIRegistryPerVM()) {
90              return;
91          }
92  
93          Thread.sleep(10);
94          manager.shutdown();
95      }
96  
97  
98      /**
99       * Getting an RMI Server going is a big deal
100      */
101     public void testCreation() throws Exception {
102         if (JVMUtil.isSingleRMIRegistryPerVM()) {
103             return;
104         }
105 
106         assertNotNull(cache1Peer);
107         assertNotNull(cache2Peer);
108     }
109 
110     /**
111      * The use of one-time registry creation and Naming.rebind should mean we can create as many listeneres as we like.
112      * They will simply replace the ones that were there.
113      */
114     public void testMultipleCreationOfRMIServers() throws Exception {
115         if (JVMUtil.isSingleRMIRegistryPerVM()) {
116             return;
117         }
118         RMICacheManagerPeerListener[] listeners = new RMICacheManagerPeerListener[100];
119         for (int i = 0; i < 100; i++) {
120             listeners[i] = new RMICacheManagerPeerListener(hostName, port, manager, new Integer(2000));
121         }
122         cache1Peer = (CachePeer) Naming.lookup(createNamingUrl() + cacheName1);
123         assertNotNull(cache1Peer);
124 
125         for (int i = 0; i < 100; i++) {
126             listeners[i].dispose();
127         }
128     }
129 
130     private String createNamingUrl() {
131         return "//" + hostName + ":" + port + "/";
132     }
133 
134     /**
135      * Attempts to get the cache name
136      *
137      * @throws java.net.MalformedURLException
138      * @throws java.rmi.NotBoundException
139      * @throws java.rmi.RemoteException
140      */
141     public void testGetName() throws Exception {
142         if (JVMUtil.isSingleRMIRegistryPerVM()) {
143             return;
144         }
145 
146         String lookupCacheName = cache1Peer.getName();
147         assertEquals(cacheName1, lookupCacheName);
148         lookupCacheName = cache2Peer.getName();
149         assertEquals(cacheName2, lookupCacheName);
150     }
151 
152 
153 }