PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/gemfire-core/src/test/java/com/gemstone/gemfire/cache/client/ClientCacheFactoryJUnitTest.java

https://gitlab.com/kidaa/incubator-geode
Java | 284 lines | 234 code | 25 blank | 25 comment | 12 complexity | 8f1a4bf77d9fd4a068ae0e933f011905 MD5 | raw file
  1. /*=========================================================================
  2. * Copyright (c) 2002-2014 Pivotal Software, Inc. All Rights Reserved.
  3. * This product is protected by U.S. and international copyright
  4. * and intellectual property laws. Pivotal products are covered by
  5. * more patents listed at http://www.pivotal.io/patents.
  6. *=========================================================================
  7. */
  8. package com.gemstone.gemfire.cache.client;
  9. import static org.junit.Assert.*;
  10. import static org.junit.runners.MethodSorters.*;
  11. import java.io.File;
  12. import java.net.InetAddress;
  13. import java.net.InetSocketAddress;
  14. import java.net.URL;
  15. import java.util.ArrayList;
  16. import java.util.Collections;
  17. import java.util.Properties;
  18. import org.junit.After;
  19. import org.junit.AfterClass;
  20. import org.junit.FixMethodOrder;
  21. import org.junit.Test;
  22. import org.junit.experimental.categories.Category;
  23. import com.gemstone.gemfire.cache.RegionService;
  24. import com.gemstone.gemfire.cache.client.internal.ProxyCache;
  25. import com.gemstone.gemfire.cache.client.internal.UserAttributes;
  26. import com.gemstone.gemfire.cache.server.CacheServer;
  27. import com.gemstone.gemfire.distributed.DistributedSystem;
  28. import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
  29. import com.gemstone.gemfire.internal.FileUtil;
  30. import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
  31. import com.gemstone.gemfire.pdx.ReflectionBasedAutoSerializer;
  32. import com.gemstone.gemfire.test.junit.categories.IntegrationTest;
  33. /**
  34. * Unit test for the ClientCacheFactory class
  35. * @author darrel
  36. * @since 6.5
  37. */
  38. @FixMethodOrder(NAME_ASCENDING)
  39. @Category(IntegrationTest.class)
  40. public class ClientCacheFactoryJUnitTest {
  41. private ClientCache cc;
  42. private File tmpFile;
  43. @After
  44. public void tearDown() throws Exception {
  45. if (this.cc != null && !this.cc.isClosed()) {
  46. cc.close();
  47. }
  48. if (tmpFile != null && tmpFile.exists()) {
  49. tmpFile.delete();
  50. }
  51. }
  52. @AfterClass
  53. public static void afterClass() {
  54. InternalDistributedSystem ids = InternalDistributedSystem.getAnyInstance();
  55. if (ids != null) {
  56. ids.disconnect();
  57. }
  58. }
  59. @Test
  60. public void test000Defaults() throws Exception {
  61. this.cc = new ClientCacheFactory().create();
  62. GemFireCacheImpl gfc = (GemFireCacheImpl)this.cc;
  63. assertEquals(true, gfc.isClient());
  64. Properties dsProps = this.cc.getDistributedSystem().getProperties();
  65. assertEquals("0", dsProps.getProperty("mcast-port"));
  66. assertEquals("", dsProps.getProperty("locators"));
  67. Pool defPool = gfc.getDefaultPool();
  68. assertEquals("DEFAULT", defPool.getName());
  69. assertEquals(new ArrayList(), defPool.getLocators());
  70. assertEquals(Collections.singletonList(new InetSocketAddress(InetAddress.getLocalHost(),CacheServer.DEFAULT_PORT)), defPool.getServers());
  71. ClientCache cc2 = new ClientCacheFactory().create();
  72. if (cc2 != this.cc) {
  73. fail("expected cc2 and cc to be == " + cc2 + this.cc);
  74. }
  75. try {
  76. new ClientCacheFactory().set("log-level", "severe").create();
  77. fail("expected create to fail");
  78. } catch (IllegalStateException expected) {
  79. }
  80. try {
  81. new ClientCacheFactory().addPoolLocator("127.0.0.1", 36666).create();
  82. fail("expected create to fail");
  83. } catch (IllegalStateException expected) {
  84. }
  85. }
  86. @Test
  87. public void test001FindDefaultFromXML() throws Exception {
  88. this.tmpFile = File.createTempFile("ClientCacheFactoryJUnitTest", ".xml");
  89. this.tmpFile.deleteOnExit();
  90. URL url = ClientCacheFactoryJUnitTest.class.getResource("ClientCacheFactoryJUnitTest_single_pool.xml");;
  91. FileUtil.copy(url, this.tmpFile);
  92. this.cc = new ClientCacheFactory()
  93. .set("cache-xml-file", this.tmpFile.getAbsolutePath())
  94. .create();
  95. GemFireCacheImpl gfc = (GemFireCacheImpl)this.cc;
  96. assertEquals(true, gfc.isClient());
  97. Properties dsProps = this.cc.getDistributedSystem().getProperties();
  98. assertEquals("0", dsProps.getProperty("mcast-port"));
  99. assertEquals("", dsProps.getProperty("locators"));
  100. Pool defPool = gfc.getDefaultPool();
  101. assertEquals("my_pool_name", defPool.getName());
  102. assertEquals(new ArrayList(), defPool.getLocators());
  103. assertEquals(Collections.singletonList(new InetSocketAddress("localhost",CacheServer.DEFAULT_PORT)), defPool.getServers());
  104. }
  105. /**
  106. * Make sure if we have a single pool that it will be used as the default
  107. */
  108. @Test
  109. public void test002DPsinglePool() throws Exception {
  110. Properties dsProps = new Properties();
  111. dsProps.setProperty("mcast-port", "0");
  112. DistributedSystem ds = DistributedSystem.connect(dsProps);
  113. Pool p = PoolManager.createFactory().addServer(InetAddress.getLocalHost().getHostName(), 7777).create("singlePool");
  114. this.cc = new ClientCacheFactory().create();
  115. GemFireCacheImpl gfc = (GemFireCacheImpl)this.cc;
  116. assertEquals(true, gfc.isClient());
  117. Pool defPool = gfc.getDefaultPool();
  118. assertEquals(p, defPool);
  119. // make sure if we can not create a secure user cache when one pool
  120. // exists that is not multiuser enabled
  121. try {
  122. Properties suProps = new Properties();
  123. suProps.setProperty("user", "foo");
  124. RegionService cc = this.cc.createAuthenticatedView(suProps);
  125. fail("expected IllegalStateException");
  126. } catch (IllegalStateException ignore) {
  127. }
  128. // however we should be to to create it by configuring a pool
  129. {
  130. Properties suProps = new Properties();
  131. suProps.setProperty("user", "foo");
  132. Pool pool = PoolManager.createFactory().addServer(InetAddress.getLocalHost().getHostName(),CacheServer.DEFAULT_PORT).setMultiuserAuthentication(true).create("pool1");
  133. RegionService cc = this.cc.createAuthenticatedView(suProps, pool.getName());
  134. ProxyCache pc = (ProxyCache)cc;
  135. UserAttributes ua = pc.getUserAttributes();
  136. Pool proxyDefPool = ua.getPool();
  137. assertEquals(Collections.singletonList(new InetSocketAddress(InetAddress.getLocalHost(),CacheServer.DEFAULT_PORT)), proxyDefPool.getServers());
  138. assertEquals(true, proxyDefPool.getMultiuserAuthentication());
  139. }
  140. }
  141. /**
  142. * Make sure if we have more than one pool that we do not have a default
  143. */
  144. @Test
  145. public void test003DPmultiplePool() throws Exception {
  146. Properties dsProps = new Properties();
  147. dsProps.setProperty("mcast-port", "0");
  148. DistributedSystem ds = DistributedSystem.connect(dsProps);
  149. PoolManager.createFactory().addServer(InetAddress.getLocalHost().getHostName(), 7777).create("p7");
  150. PoolManager.createFactory().addServer(InetAddress.getLocalHost().getHostName(), 6666).create("p6");
  151. this.cc = new ClientCacheFactory().create();
  152. GemFireCacheImpl gfc = (GemFireCacheImpl)this.cc;
  153. assertEquals(true, gfc.isClient());
  154. Pool defPool = gfc.getDefaultPool();
  155. assertEquals(null, defPool);
  156. // make sure if we can not create a secure user cache when more than one pool
  157. // exists that is not multiuser enabled
  158. try {
  159. Properties suProps = new Properties();
  160. suProps.setProperty("user", "foo");
  161. RegionService cc = this.cc.createAuthenticatedView(suProps);
  162. fail("expected IllegalStateException");
  163. } catch (IllegalStateException ignore) {
  164. }
  165. // however we should be to to create it by configuring a pool
  166. {
  167. Properties suProps = new Properties();
  168. suProps.setProperty("user", "foo");
  169. Pool pool = PoolManager.createFactory().addServer(InetAddress.getLocalHost().getHostName(),CacheServer.DEFAULT_PORT).setMultiuserAuthentication(true).create("pool1");
  170. RegionService cc = this.cc.createAuthenticatedView(suProps, pool.getName());
  171. ProxyCache pc = (ProxyCache)cc;
  172. UserAttributes ua = pc.getUserAttributes();
  173. Pool proxyDefPool = ua.getPool();
  174. assertEquals(Collections.singletonList(new InetSocketAddress(InetAddress.getLocalHost(),CacheServer.DEFAULT_PORT)), proxyDefPool.getServers());
  175. assertEquals(true, proxyDefPool.getMultiuserAuthentication());
  176. }
  177. }
  178. @Test
  179. public void test004SetMethod() throws Exception {
  180. this.cc = new ClientCacheFactory().set("log-level", "severe").create();
  181. GemFireCacheImpl gfc = (GemFireCacheImpl)this.cc;
  182. assertEquals(true, gfc.isClient());
  183. Properties dsProps = this.cc.getDistributedSystem().getProperties();
  184. assertEquals("0", dsProps.getProperty("mcast-port"));
  185. assertEquals("", dsProps.getProperty("locators"));
  186. assertEquals("severe", dsProps.getProperty("log-level"));
  187. }
  188. @Test
  189. public void test005SecureUserDefaults() throws Exception {
  190. Properties suProps = new Properties();
  191. suProps.setProperty("user", "foo");
  192. GemFireCacheImpl gfc = (GemFireCacheImpl) new ClientCacheFactory().setPoolMultiuserAuthentication(true).create();
  193. this.cc = gfc;
  194. RegionService cc1 = this.cc.createAuthenticatedView(suProps);
  195. assertEquals(true, gfc.isClient());
  196. Properties dsProps = this.cc.getDistributedSystem().getProperties();
  197. assertEquals("0", dsProps.getProperty("mcast-port"));
  198. assertEquals("", dsProps.getProperty("locators"));
  199. Pool defPool = gfc.getDefaultPool();
  200. assertEquals("DEFAULT", defPool.getName());
  201. assertEquals(new ArrayList(), defPool.getLocators());
  202. assertEquals(Collections.singletonList(new InetSocketAddress(InetAddress.getLocalHost(),CacheServer.DEFAULT_PORT)), defPool.getServers());
  203. assertEquals(true, defPool.getMultiuserAuthentication());
  204. // make sure we can create another secure user cache
  205. RegionService cc2 = this.cc.createAuthenticatedView(suProps);
  206. assertEquals(true, gfc.isClient());
  207. assertEquals("0", dsProps.getProperty("mcast-port"));
  208. assertEquals("", dsProps.getProperty("locators"));
  209. defPool = gfc.getDefaultPool();
  210. assertEquals("DEFAULT", defPool.getName());
  211. assertEquals(new ArrayList(), defPool.getLocators());
  212. assertEquals(Collections.singletonList(new InetSocketAddress(InetAddress.getLocalHost(),CacheServer.DEFAULT_PORT)), defPool.getServers());
  213. assertEquals(true, defPool.getMultiuserAuthentication());
  214. if (cc1 == cc2) {
  215. fail("expected two different secure user caches");
  216. }
  217. }
  218. @Test
  219. public void test006NonDefaultPool() throws Exception {
  220. this.cc = new ClientCacheFactory()
  221. .addPoolServer(InetAddress.getLocalHost().getHostName(), 55555)
  222. .create();
  223. GemFireCacheImpl gfc = (GemFireCacheImpl)this.cc;
  224. assertEquals(true, gfc.isClient());
  225. Properties dsProps = this.cc.getDistributedSystem().getProperties();
  226. assertEquals("0", dsProps.getProperty("mcast-port"));
  227. assertEquals("", dsProps.getProperty("locators"));
  228. Pool defPool = gfc.getDefaultPool();
  229. assertEquals("DEFAULT", defPool.getName());
  230. assertEquals(new ArrayList(), defPool.getLocators());
  231. assertEquals(Collections.singletonList(new InetSocketAddress(InetAddress.getLocalHost(),55555)), defPool.getServers());
  232. ClientCache cc2 = new ClientCacheFactory().create();
  233. gfc = (GemFireCacheImpl)this.cc;
  234. assertEquals(true, gfc.isClient());
  235. dsProps = this.cc.getDistributedSystem().getProperties();
  236. assertEquals("0", dsProps.getProperty("mcast-port"));
  237. assertEquals("", dsProps.getProperty("locators"));
  238. defPool = gfc.getDefaultPool();
  239. assertEquals("DEFAULT", defPool.getName());
  240. assertEquals(new ArrayList(), defPool.getLocators());
  241. assertEquals(Collections.singletonList(new InetSocketAddress(InetAddress.getLocalHost(),55555)), defPool.getServers());
  242. try {
  243. new ClientCacheFactory()
  244. .addPoolServer(InetAddress.getLocalHost().getHostName(), 44444)
  245. .create();
  246. fail("expected create to fail");
  247. } catch (IllegalStateException expected) {
  248. }
  249. }
  250. @Test
  251. public void test007Bug44907() {
  252. new ClientCacheFactory()
  253. .setPdxSerializer(new ReflectionBasedAutoSerializer())
  254. .create();
  255. new ClientCacheFactory()
  256. .setPdxSerializer(new ReflectionBasedAutoSerializer())
  257. .create();
  258. }
  259. }