PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/org/quickcached/protocol/ProtocolTest.java

http://quickcached.googlecode.com/
Java | 525 lines | 380 code | 101 blank | 44 comment | 33 complexity | bddd08003ee3a33a11a3de4c869920c3 MD5 | raw file
Possible License(s): LGPL-2.1
  1. package org.quickcached.protocol;
  2. import java.net.InetSocketAddress;
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.Map;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. import junit.framework.TestCase;
  10. import net.rubyeye.xmemcached.exception.MemcachedException;
  11. import org.quickcached.client.*;
  12. /**
  13. *
  14. * @author akshath
  15. */
  16. public class ProtocolTest extends TestCase {
  17. protected MemcachedClient c = null;
  18. public ProtocolTest(String name) {
  19. super(name);
  20. }
  21. public void testGet() throws TimeoutException {
  22. String readObject = null;
  23. String key = null;
  24. //1
  25. key = "testget1";
  26. c.set(key, 3600, "World");
  27. readObject = (String) c.get(key);
  28. assertNotNull(readObject);
  29. assertEquals("World", readObject);
  30. //2
  31. Date value = new Date();
  32. key = "testget2";
  33. c.set(key, 3600, value);
  34. Date readObjectDate = (Date) c.get(key);
  35. assertNotNull(readObjectDate);
  36. assertEquals(value.getTime(), readObjectDate.getTime());
  37. //3
  38. Object client = c.getBaseClient();
  39. key = "testget3";
  40. if(client instanceof net.rubyeye.xmemcached.MemcachedClient) {
  41. net.rubyeye.xmemcached.MemcachedClient xmc = (net.rubyeye.xmemcached.MemcachedClient) client;
  42. try {
  43. xmc.setWithNoReply(key, 3600, "World");
  44. } catch (InterruptedException ex) {
  45. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  46. } catch (MemcachedException ex) {
  47. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  48. }
  49. readObject = (String) c.get(key);
  50. assertNotNull(readObject);
  51. assertEquals("World", readObject);
  52. } else if (client instanceof net.spy.memcached.MemcachedClient) {
  53. net.spy.memcached.MemcachedClient smc = (net.spy.memcached.MemcachedClient) client;
  54. //does not support noreply
  55. }
  56. }
  57. public void testAppend() throws TimeoutException {
  58. String readObject = null;
  59. String key = null;
  60. String value = null;
  61. //1
  62. key = "testapp1";
  63. c.set(key, 3600, "ABCD");
  64. c.append(key, "EFGH");
  65. readObject = (String) c.get(key);
  66. assertNotNull(readObject);
  67. assertEquals("ABCDEFGH", readObject);
  68. //2
  69. Object client = c.getBaseClient();
  70. if(client instanceof net.rubyeye.xmemcached.MemcachedClient) {
  71. net.rubyeye.xmemcached.MemcachedClient xmc = (net.rubyeye.xmemcached.MemcachedClient) client;
  72. key = "testapp1";
  73. try {
  74. xmc.appendWithNoReply(key,"XYZ");
  75. } catch (InterruptedException ex) {
  76. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  77. } catch (MemcachedException ex) {
  78. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  79. }
  80. readObject = (String) c.get(key);
  81. assertNotNull(readObject);
  82. assertEquals("ABCDEFGHXYZ", readObject);
  83. } else if (client instanceof net.spy.memcached.MemcachedClient) {
  84. //does not support noreply
  85. }
  86. }
  87. public void testPrepend() throws TimeoutException {
  88. String readObject = null;
  89. String key = null;
  90. String value = null;
  91. key = "testpre1";
  92. //1
  93. c.set(key, 3600, "ABCD");
  94. c.prepend(key, "EFGH");
  95. readObject = (String) c.get(key);
  96. assertNotNull(readObject);
  97. assertEquals("EFGHABCD", readObject);
  98. //2
  99. Object client = c.getBaseClient();
  100. if(client instanceof net.rubyeye.xmemcached.MemcachedClient) {
  101. net.rubyeye.xmemcached.MemcachedClient xmc = (net.rubyeye.xmemcached.MemcachedClient) client;
  102. key = "testpre1";
  103. try {
  104. xmc.prependWithNoReply(key,"XYZ");
  105. } catch (InterruptedException ex) {
  106. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  107. } catch (MemcachedException ex) {
  108. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  109. }
  110. readObject = (String) c.get(key);
  111. assertNotNull(readObject);
  112. assertEquals("XYZEFGHABCD", readObject);
  113. } else if (client instanceof net.spy.memcached.MemcachedClient) {
  114. //does not support noreply
  115. }
  116. }
  117. public void testAdd() throws TimeoutException {
  118. String readObject = null;
  119. String key = null;
  120. String value = null;
  121. //1
  122. value = "ABCD";
  123. key = "testadd1";
  124. c.delete(key);
  125. boolean flag = c.add(key, 3600, value);
  126. assertTrue(flag);
  127. readObject = (String) c.get(key);
  128. assertNotNull(readObject);
  129. assertEquals("ABCD", readObject);
  130. flag = c.add(key, 3600, "XYZ");
  131. assertFalse(flag);
  132. //read old value
  133. readObject = (String) c.get(key);
  134. assertNotNull(readObject);
  135. assertEquals("ABCD", readObject);
  136. //2
  137. key = "testadd2";
  138. c.delete(key);
  139. Object client = c.getBaseClient();
  140. if(client instanceof net.rubyeye.xmemcached.MemcachedClient) {
  141. net.rubyeye.xmemcached.MemcachedClient xmc = (net.rubyeye.xmemcached.MemcachedClient) client;
  142. try {
  143. xmc.addWithNoReply(key, 3600, value);
  144. } catch (InterruptedException ex) {
  145. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  146. } catch (MemcachedException ex) {
  147. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  148. }
  149. readObject = (String) c.get(key);
  150. assertNotNull(readObject);
  151. assertEquals("ABCD", readObject);
  152. flag = c.add(key, 3600, "XYZ");
  153. assertFalse(flag);
  154. //read old value
  155. readObject = (String) c.get(key);
  156. assertNotNull(readObject);
  157. assertEquals("ABCD", readObject);
  158. } else if (client instanceof net.spy.memcached.MemcachedClient) {
  159. //does not support noreply
  160. }
  161. }
  162. public void testReplace() throws TimeoutException {
  163. String readObject = null;
  164. String key = null;
  165. String value = null;
  166. //1
  167. value = "ABCD";
  168. key = "testrep1";
  169. c.set(key, 3600, "World");
  170. boolean flag = c.replace(key, 3600, value);
  171. assertTrue(flag);
  172. readObject = (String) c.get(key);
  173. assertNotNull(readObject);
  174. assertEquals("ABCD", readObject);
  175. c.delete(key);
  176. flag = c.replace(key, 3600, "XYZ");
  177. assertFalse(flag);
  178. //read old value i.e. no value
  179. readObject = (String) c.get(key);
  180. assertNull(readObject);
  181. //2
  182. key = "testrep2";
  183. c.set(key, 3600, "World");
  184. Object client = c.getBaseClient();
  185. if(client instanceof net.rubyeye.xmemcached.MemcachedClient) {
  186. net.rubyeye.xmemcached.MemcachedClient xmc = (net.rubyeye.xmemcached.MemcachedClient) client;
  187. try {
  188. xmc.replaceWithNoReply(key, 3600, value);
  189. } catch (InterruptedException ex) {
  190. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  191. } catch (MemcachedException ex) {
  192. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  193. }
  194. readObject = (String) c.get(key);
  195. assertNotNull(readObject);
  196. assertEquals("ABCD", readObject);
  197. } else if (client instanceof net.spy.memcached.MemcachedClient) {
  198. //does not support noreply
  199. }
  200. c.delete(key);
  201. if(client instanceof net.rubyeye.xmemcached.MemcachedClient) {
  202. net.rubyeye.xmemcached.MemcachedClient xmc = (net.rubyeye.xmemcached.MemcachedClient) client;
  203. try {
  204. xmc.replaceWithNoReply(key, 3600, "XYZ");
  205. } catch (InterruptedException ex) {
  206. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  207. } catch (MemcachedException ex) {
  208. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  209. }
  210. //read old value i.e. no value
  211. readObject = (String) c.get(key);
  212. assertNull(readObject);
  213. } else if (client instanceof net.spy.memcached.MemcachedClient) {
  214. //does not support noreply
  215. }
  216. }
  217. public void testIncrement() throws TimeoutException {
  218. String readObject = null;
  219. String key = null;
  220. String value = null;
  221. //1
  222. key = "testinc1";
  223. value = "10";
  224. c.set(key, 3600, value);
  225. c.increment(key, 10);
  226. readObject = (String) c.get(key);
  227. assertNotNull(readObject);
  228. assertEquals("20", readObject);
  229. c.increment(key, 1);
  230. readObject = (String) c.get(key);
  231. assertNotNull(readObject);
  232. assertEquals("21", readObject);
  233. //2
  234. Object client = c.getBaseClient();
  235. if(client instanceof net.rubyeye.xmemcached.MemcachedClient) {
  236. net.rubyeye.xmemcached.MemcachedClient xmc = (net.rubyeye.xmemcached.MemcachedClient) client;
  237. try {
  238. xmc.incrWithNoReply(key, 4);
  239. } catch (InterruptedException ex) {
  240. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  241. } catch (MemcachedException ex) {
  242. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  243. }
  244. readObject = (String) c.get(key);
  245. assertNotNull(readObject);
  246. assertEquals("25", readObject);
  247. } else if (client instanceof net.spy.memcached.MemcachedClient) {
  248. //does not support noreply
  249. }
  250. }
  251. public void testDecrement() throws TimeoutException {
  252. String readObject = null;
  253. String key = null;
  254. String value = null;
  255. //1
  256. key = "testdec1";
  257. value = "25";
  258. c.set(key, 3600, value);
  259. c.decrement(key, 10);
  260. readObject = (String) c.get(key);
  261. assertNotNull(readObject);
  262. assertEquals("15", readObject);
  263. c.decrement(key, 1);
  264. readObject = (String) c.get(key);
  265. assertNotNull(readObject);
  266. assertEquals("14", readObject);
  267. //2
  268. Object client = c.getBaseClient();
  269. if(client instanceof net.rubyeye.xmemcached.MemcachedClient) {
  270. net.rubyeye.xmemcached.MemcachedClient xmc = (net.rubyeye.xmemcached.MemcachedClient) client;
  271. try {
  272. xmc.decrWithNoReply(key, 4);
  273. } catch (InterruptedException ex) {
  274. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  275. } catch (MemcachedException ex) {
  276. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  277. }
  278. readObject = (String) c.get(key);
  279. assertNotNull(readObject);
  280. assertEquals("10", readObject);
  281. } else if (client instanceof net.spy.memcached.MemcachedClient) {
  282. //does not support noreply
  283. }
  284. }
  285. public void testDelete() throws TimeoutException {
  286. String readObject = null;
  287. String key = null;
  288. String value = null;
  289. //1
  290. key = "testdel1";
  291. c.set(key, 3600, "World");
  292. readObject = (String) c.get(key);
  293. assertNotNull(readObject);
  294. assertEquals("World", readObject);
  295. c.delete(key);
  296. readObject = (String) c.get(key);
  297. assertNull(readObject);
  298. //2
  299. c.set(key, 3600, "World");
  300. readObject = (String) c.get(key);
  301. assertNotNull(readObject);
  302. assertEquals("World", readObject);
  303. Object client = c.getBaseClient();
  304. if(client instanceof net.rubyeye.xmemcached.MemcachedClient) {
  305. net.rubyeye.xmemcached.MemcachedClient xmc = (net.rubyeye.xmemcached.MemcachedClient) client;
  306. try {
  307. xmc.deleteWithNoReply(key);
  308. } catch (InterruptedException ex) {
  309. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  310. } catch (MemcachedException ex) {
  311. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  312. }
  313. readObject = (String) c.get(key);
  314. assertNull(readObject);
  315. } else if (client instanceof net.spy.memcached.MemcachedClient) {
  316. //does not support noreply
  317. }
  318. }
  319. public void testVersion() throws TimeoutException {
  320. Map ver = c.getVersions();
  321. assertNotNull(ver);
  322. //System.out.println("ver: "+ver);
  323. Iterator iterator = ver.keySet().iterator();
  324. InetSocketAddress key = null;
  325. while(iterator.hasNext()) {
  326. key = (InetSocketAddress) iterator.next();
  327. assertNotNull(key);
  328. //assertEquals("1.4.5", (String) ver.get(key));
  329. }
  330. }
  331. public void testStats() throws Exception {
  332. Map stats = c.getStats();
  333. assertNotNull(stats);
  334. Iterator iterator = stats.keySet().iterator();
  335. InetSocketAddress key = null;
  336. while(iterator.hasNext()) {
  337. key = (InetSocketAddress) iterator.next();
  338. assertNotNull(key);
  339. //System.out.println("Stat for "+key+" " +stats.get(key));
  340. }
  341. }
  342. public void testFlush() throws TimeoutException {
  343. String readObject = null;
  344. String key = null;
  345. String value = null;
  346. //1
  347. key = "testflush1";
  348. c.set(key, 3600, "World");
  349. readObject = (String) c.get(key);
  350. assertNotNull(readObject);
  351. assertEquals("World", readObject);
  352. c.flushAll();
  353. readObject = (String) c.get(key);
  354. assertNull(readObject);
  355. //2
  356. c.set(key, 3600, "World");
  357. readObject = (String) c.get(key);
  358. assertNotNull(readObject);
  359. assertEquals("World", readObject);
  360. Object client = c.getBaseClient();
  361. if(client instanceof net.rubyeye.xmemcached.MemcachedClient) {
  362. net.rubyeye.xmemcached.MemcachedClient xmc = (net.rubyeye.xmemcached.MemcachedClient) client;
  363. try {
  364. xmc.flushAllWithNoReply();
  365. } catch (InterruptedException ex) {
  366. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  367. } catch (MemcachedException ex) {
  368. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  369. }
  370. readObject = (String) c.get(key);
  371. assertNull(readObject);
  372. } else if (client instanceof net.spy.memcached.MemcachedClient) {
  373. //does not support noreply
  374. }
  375. }
  376. public void testDoubleSet1() throws TimeoutException {
  377. String readObject = null;
  378. String key = null;
  379. String value = null;
  380. //1
  381. key = "testdset1";
  382. value = "v1";
  383. c.set(key, 3600, value);
  384. readObject = (String) c.get(key);
  385. assertNotNull(readObject);
  386. assertEquals("v1", readObject);
  387. value = "v2";
  388. c.set(key, 3600, value);
  389. readObject = (String) c.get(key);
  390. assertNotNull(readObject);
  391. assertEquals("v2", readObject);
  392. //2
  393. key = "testdset2";
  394. Map valuemap = new HashMap();
  395. valuemap.put("key1", "v1");
  396. c.set(key, 3600, valuemap);
  397. Map readObjectMap = (Map) c.get(key);
  398. assertNotNull(readObjectMap);
  399. assertEquals(valuemap, readObjectMap);
  400. valuemap.put("key2", "v2");
  401. c.set(key, 3600, valuemap);
  402. readObjectMap = (Map) c.get(key);
  403. assertNotNull(readObjectMap);
  404. assertEquals(valuemap, readObjectMap);
  405. //3
  406. valuemap.put("key2", "v3");
  407. Object client = c.getBaseClient();
  408. if(client instanceof net.rubyeye.xmemcached.MemcachedClient) {
  409. net.rubyeye.xmemcached.MemcachedClient xmc = (net.rubyeye.xmemcached.MemcachedClient) client;
  410. try {
  411. xmc.setWithNoReply(key, 3600, valuemap);
  412. } catch (InterruptedException ex) {
  413. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  414. } catch (MemcachedException ex) {
  415. Logger.getLogger(ProtocolTest.class.getName()).log(Level.SEVERE, null, ex);
  416. }
  417. readObjectMap = (Map) c.get(key);
  418. assertNotNull(readObjectMap);
  419. assertEquals(valuemap, readObjectMap);
  420. } else if (client instanceof net.spy.memcached.MemcachedClient) {
  421. //does not support noreply
  422. }
  423. }
  424. }