View Javadoc
1   package org.apache.commons.jcs.auxiliary.remote.server;
2   
3   import java.rmi.server.RMISocketFactory;
4   import java.util.Properties;
5   
6   import org.apache.commons.jcs.auxiliary.remote.behavior.ICommonRemoteCacheAttributes;
7   import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheConstants;
8   
9   /*
10   * Licensed to the Apache Software Foundation (ASF) under one
11   * or more contributor license agreements.  See the NOTICE file
12   * distributed with this work for additional information
13   * regarding copyright ownership.  The ASF licenses this file
14   * to you under the Apache License, Version 2.0 (the
15   * "License"); you may not use this file except in compliance
16   * with the License.  You may obtain a copy of the License at
17   *
18   *   http://www.apache.org/licenses/LICENSE-2.0
19   *
20   * Unless required by applicable law or agreed to in writing,
21   * software distributed under the License is distributed on an
22   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23   * KIND, either express or implied.  See the License for the
24   * specific language governing permissions and limitations
25   * under the License.
26   */
27  
28  import junit.framework.TestCase;
29  
30  /** Unit tests for the factory */
31  public class RemoteCacheServerFactoryUnitTest
32      extends TestCase
33  {
34      /** verify that we get the timeout value */
35      public void testConfigureRemoteCacheServerAttributes_eventQueuePoolName()
36      {
37          // SETUP
38          String eventQueuePoolName = "specialName";
39          Properties props = new Properties();
40          props.put( IRemoteCacheConstants.CACHE_SERVER_ATTRIBUTES_PROPERTY_PREFIX + ".EventQueuePoolName", eventQueuePoolName );
41  
42          // DO WORK
43          RemoteCacheServerAttributes result = RemoteCacheServerFactory.configureRemoteCacheServerAttributes( props );
44  
45          // VERIFY
46          assertEquals( "Wrong eventQueuePoolName", eventQueuePoolName, result.getEventQueuePoolName() );
47      }
48  
49      /** verify that we get the timeout value */
50      public void testConfigureRemoteCacheServerAttributes_timeoutPresent()
51      {
52          // SETUP
53          int timeout = 123245;
54          Properties props = new Properties();
55          props.put( IRemoteCacheConstants.SOCKET_TIMEOUT_MILLIS, String.valueOf( timeout ) );
56  
57          // DO WORK
58          RemoteCacheServerAttributes result = RemoteCacheServerFactory.configureRemoteCacheServerAttributes( props );
59  
60          // VERIFY
61          assertEquals( "Wrong timeout", timeout, result.getRmiSocketFactoryTimeoutMillis() );
62      }
63  
64      /** verify that we get the timeout value */
65      public void testConfigureRemoteCacheServerAttributes_timeoutNotPresent()
66      {
67          // SETUP
68          Properties props = new Properties();
69  
70          // DO WORK
71          RemoteCacheServerAttributes result = RemoteCacheServerFactory.configureRemoteCacheServerAttributes( props );
72  
73          // VERIFY
74          assertEquals( "Wrong timeout", ICommonRemoteCacheAttributes.DEFAULT_RMI_SOCKET_FACTORY_TIMEOUT_MILLIS, result.getRmiSocketFactoryTimeoutMillis() );
75      }
76  
77      /** verify that we get the registryKeepAliveDelayMillis value */
78      public void testConfigureRemoteCacheServerAttributes_registryKeepAliveDelayMillisPresent()
79      {
80          // SETUP
81          int registryKeepAliveDelayMillis = 123245;
82          Properties props = new Properties();
83          props.put( IRemoteCacheConstants.CACHE_SERVER_ATTRIBUTES_PROPERTY_PREFIX + ".registryKeepAliveDelayMillis", String.valueOf( registryKeepAliveDelayMillis ) );
84  
85          // DO WORK
86          RemoteCacheServerAttributes result = RemoteCacheServerFactory.configureRemoteCacheServerAttributes( props );
87  
88          // VERIFY
89          assertEquals( "Wrong registryKeepAliveDelayMillis", registryKeepAliveDelayMillis, result.getRegistryKeepAliveDelayMillis() );
90      }
91  
92      /** verify that we get the useRegistryKeepAlive value */
93      public void testConfigureRemoteCacheServerAttributes_useRegistryKeepAlivePresent()
94      {
95          // SETUP
96          boolean useRegistryKeepAlive = false;
97          Properties props = new Properties();
98          props.put( IRemoteCacheConstants.CACHE_SERVER_ATTRIBUTES_PROPERTY_PREFIX + ".useRegistryKeepAlive", String.valueOf( useRegistryKeepAlive ) );
99  
100         // DO WORK
101         RemoteCacheServerAttributes result = RemoteCacheServerFactory.configureRemoteCacheServerAttributes( props );
102 
103         // VERIFY
104         assertEquals( "Wrong useRegistryKeepAlive", useRegistryKeepAlive, result.isUseRegistryKeepAlive() );
105     }
106 
107     /** verify that we get the registryKeepAliveDelayMillis value */
108     public void testConfigureRemoteCacheServerAttributes_rmiSocketFactoryTimeoutMillisPresent()
109     {
110         // SETUP
111         int rmiSocketFactoryTimeoutMillis = 123245;
112         Properties props = new Properties();
113         props.put( IRemoteCacheConstants.CACHE_SERVER_ATTRIBUTES_PROPERTY_PREFIX + ".rmiSocketFactoryTimeoutMillis", String.valueOf( rmiSocketFactoryTimeoutMillis ) );
114 
115         // DO WORK
116         RemoteCacheServerAttributes result = RemoteCacheServerFactory.configureRemoteCacheServerAttributes( props );
117 
118         // VERIFY
119         assertEquals( "Wrong rmiSocketFactoryTimeoutMillis", rmiSocketFactoryTimeoutMillis, result.getRmiSocketFactoryTimeoutMillis() );
120     }
121 
122     /** verify that we get the allowClusterGet value */
123     public void testConfigureRemoteCacheServerAttributes_allowClusterGetPresent()
124     {
125         // SETUP
126         boolean allowClusterGet = false;
127         Properties props = new Properties();
128         props.put( IRemoteCacheConstants.CACHE_SERVER_ATTRIBUTES_PROPERTY_PREFIX + ".allowClusterGet", String.valueOf( allowClusterGet ) );
129 
130         // DO WORK
131         RemoteCacheServerAttributes result = RemoteCacheServerFactory.configureRemoteCacheServerAttributes( props );
132 
133         // VERIFY
134         assertEquals( "Wrong allowClusterGet", allowClusterGet, result.isAllowClusterGet() );
135     }
136 
137     /** verify that we get the localClusterConsistency value */
138     public void testConfigureRemoteCacheServerAttributes_localClusterConsistencyPresent()
139     {
140         // SETUP
141         boolean localClusterConsistency = false;
142         Properties props = new Properties();
143         props.put( IRemoteCacheConstants.CACHE_SERVER_ATTRIBUTES_PROPERTY_PREFIX + ".localClusterConsistency", String.valueOf( localClusterConsistency ) );
144 
145         // DO WORK
146         RemoteCacheServerAttributes result = RemoteCacheServerFactory.configureRemoteCacheServerAttributes( props );
147 
148         // VERIFY
149         assertEquals( "Wrong localClusterConsistency", localClusterConsistency, result.isLocalClusterConsistency() );
150     }
151 
152     /** verify that we get the testStringProperty value */
153     public void testConfigureObjectSpecificCustomFactory_withProperty()
154     {
155         // SETUP
156         String testValue = "123245";
157         Properties props = new Properties();
158         props.put( IRemoteCacheConstants.CUSTOM_RMI_SOCKET_FACTORY_PROPERTY_PREFIX, MockRMISocketFactory.class.getName() );
159         props.put( IRemoteCacheConstants.CUSTOM_RMI_SOCKET_FACTORY_PROPERTY_PREFIX + ".testStringProperty", testValue );
160 
161         // DO WORK
162         RMISocketFactory result = RemoteCacheServerFactory.configureObjectSpecificCustomFactory( props );
163 
164         // VERIFY
165         assertNotNull( "Should have a custom socket factory.", result );
166         assertEquals( "Wrong testValue", testValue, ((MockRMISocketFactory)result).getTestStringProperty() );
167     }
168 
169     /** verify that we get the timeout value */
170     public void testConfigureObjectSpecificCustomFactory_withProperty_TimeoutConfigurableRMIScoketFactory()
171     {
172         // SETUP
173         int readTimeout = 1234;
174         int openTimeout = 1234;
175         Properties props = new Properties();
176         props.put( IRemoteCacheConstants.CUSTOM_RMI_SOCKET_FACTORY_PROPERTY_PREFIX, TimeoutConfigurableRMISocketFactory.class.getName() );
177         props.put( IRemoteCacheConstants.CUSTOM_RMI_SOCKET_FACTORY_PROPERTY_PREFIX + ".readTimeout", String.valueOf( readTimeout ) );
178         props.put( IRemoteCacheConstants.CUSTOM_RMI_SOCKET_FACTORY_PROPERTY_PREFIX + ".openTimeout", String.valueOf( openTimeout ) );
179 
180         // DO WORK
181         RMISocketFactory result = RemoteCacheServerFactory.configureObjectSpecificCustomFactory( props );
182 
183         // VERIFY
184         assertNotNull( "Should have a custom socket factory.", result );
185         assertEquals( "Wrong readTimeout", readTimeout, ((TimeoutConfigurableRMISocketFactory)result).getReadTimeout() );
186         assertEquals( "Wrong readTimeout", openTimeout, ((TimeoutConfigurableRMISocketFactory)result).getOpenTimeout() );
187     }
188 }