PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-tests-parent/jira-tests-unit/src/test/java/com/atlassian/jira/propertyset/TestJiraCachingPropertySetLocks.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 387 lines | 277 code | 81 blank | 29 comment | 0 complexity | 031998947ffafd53a167183bdf5d9e18 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.propertyset;
  2. import com.opensymphony.module.propertyset.PropertySet;
  3. import com.opensymphony.module.propertyset.PropertySetSchema;
  4. import org.dom4j.dom.DOMDocument;
  5. import org.junit.Test;
  6. import javax.annotation.Nonnull;
  7. import java.util.Arrays;
  8. import java.util.Date;
  9. import java.util.Map;
  10. import java.util.Properties;
  11. import java.util.concurrent.TimeUnit;
  12. import java.util.concurrent.locks.Condition;
  13. import java.util.concurrent.locks.Lock;
  14. import static com.google.common.collect.Lists.newArrayList;
  15. import static com.google.common.collect.Maps.newHashMap;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertNull;
  19. import static org.junit.Assert.assertSame;
  20. import static org.junit.Assert.assertTrue;
  21. public class TestJiraCachingPropertySetLocks {
  22. @Test
  23. public void testSetBooleanLocks() throws Exception {
  24. final MockLock lock = new MockLock();
  25. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  26. init(propertySet);
  27. lock.assertLockedCount(1);
  28. // The first get will miss the cache and should lock
  29. assertFalse(propertySet.getBoolean("test"));
  30. lock.assertLockedCount(2);
  31. propertySet.setBoolean("test", true);
  32. lock.assertLockedCount(3);
  33. // The value is now cached, so no lock is needed.
  34. assertTrue(propertySet.getBoolean("test"));
  35. lock.assertLockedCount(3);
  36. }
  37. @Test
  38. public void testGetSetDataLocks() throws Exception {
  39. final MockLock lock = new MockLock();
  40. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  41. init(propertySet);
  42. // The first get will miss the cache and should lock
  43. assertNull(propertySet.getData("test"));
  44. lock.assertLockedCount(2);
  45. propertySet.setData("test", "roger".getBytes());
  46. lock.assertLockedCount(3);
  47. // The value is now cached, so no lock is needed.
  48. assertTrue(Arrays.equals("roger".getBytes(), propertySet.getData("test")));
  49. lock.assertLockedCount(3);
  50. }
  51. @Test
  52. public void testGetSetDateLocks() throws Exception {
  53. final MockLock lock = new MockLock();
  54. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  55. init(propertySet);
  56. // The first get will miss the cache and should lock
  57. assertNull(propertySet.getDate("test"));
  58. lock.assertLockedCount(2);
  59. propertySet.setDate("test", new Date(100000));
  60. lock.assertLockedCount(3);
  61. // The value is now cached, so no lock is needed.
  62. assertEquals(new Date(100000), propertySet.getDate("test"));
  63. lock.assertLockedCount(3);
  64. }
  65. @Test
  66. public void testGetSetDoubleLocks() throws Exception {
  67. final MockLock lock = new MockLock();
  68. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  69. init(propertySet);
  70. // The first get will miss the cache and should lock
  71. assertEquals(0.0, propertySet.getDouble("test"), 0.0);
  72. lock.assertLockedCount(2);
  73. propertySet.setDouble("test", new Double(123.456).doubleValue());
  74. lock.assertLockedCount(3);
  75. assertEquals(123.456, propertySet.getDouble("test"), 0.0);
  76. // The value is now cached, so no lock is needed.
  77. lock.assertLockedCount(3);
  78. }
  79. @Test
  80. public void testGetSetIntLocks() throws Exception {
  81. final MockLock lock = new MockLock();
  82. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  83. init(propertySet);
  84. // The first get will miss the cache and should lock
  85. assertEquals(0, propertySet.getInt("test"));
  86. lock.assertLockedCount(2);
  87. propertySet.setInt("test", new Integer(890).intValue());
  88. lock.assertLockedCount(3);
  89. assertEquals(new Integer(890).intValue(), propertySet.getInt("test"));
  90. // The value is now cached, so no lock is needed.
  91. lock.assertLockedCount(3);
  92. }
  93. @Test
  94. public void testGetSetLongLocks() throws Exception {
  95. final MockLock lock = new MockLock();
  96. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  97. init(propertySet);
  98. // The first get will miss the cache and should lock
  99. assertEquals(0, propertySet.getLong("test"));
  100. lock.assertLockedCount(2);
  101. propertySet.setLong("test", new Long(890).longValue());
  102. lock.assertLockedCount(3);
  103. assertEquals(new Long(890).longValue(), propertySet.getLong("test"));
  104. // The value is now cached, so no lock is needed.
  105. lock.assertLockedCount(3);
  106. }
  107. @Test
  108. public void testGetSetObjectLocks() throws Exception {
  109. final MockLock lock = new MockLock();
  110. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  111. init(propertySet);
  112. // The first get will miss the cache and should lock
  113. assertNull(propertySet.getObject("test"));
  114. lock.assertLockedCount(2);
  115. propertySet.setObject("test", new Integer(891));
  116. lock.assertLockedCount(3);
  117. assertEquals(891, propertySet.getObject("test"));
  118. // The value is now cached, so no lock is needed.
  119. lock.assertLockedCount(3);
  120. }
  121. @Test
  122. public void testGetSetPropertiesLocks() throws Exception {
  123. final MockLock lock = new MockLock();
  124. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  125. init(propertySet);
  126. // The first get will miss the cache and should lock
  127. assertNull(propertySet.getProperties("test"));
  128. lock.assertLockedCount(2);
  129. propertySet.setProperties("test", new Properties());
  130. lock.assertLockedCount(3);
  131. assertEquals(new Properties(), propertySet.getProperties("test"));
  132. // The value is now cached, so no lock is needed.
  133. lock.assertLockedCount(3);
  134. }
  135. @Test
  136. public void testGetSetXmlLocks() throws Exception {
  137. final MockLock lock = new MockLock();
  138. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  139. init(propertySet);
  140. // The first get will miss the cache and should lock
  141. assertNull(propertySet.getXML("test"));
  142. lock.assertLockedCount(2);
  143. propertySet.setXML("test", new DOMDocument());
  144. lock.assertLockedCount(3);
  145. // we don't really care about the fidelity of the document (they don't support equals())
  146. // just that the lock was called
  147. assertEquals(new DOMDocument().getAttributes(), propertySet.getXML("test").getAttributes());
  148. // The value is now cached, so no lock is needed.
  149. lock.assertLockedCount(3);
  150. }
  151. @Test
  152. public void testGetSetSchemaDoesNotLock() throws Exception {
  153. final MockLock lock = new MockLock();
  154. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  155. init(propertySet);
  156. lock.assertLockedCount(1);
  157. final PropertySetSchema propertySetSchema = new PropertySetSchema();
  158. propertySet.setSchema(propertySetSchema);
  159. lock.assertLockedCount(1);
  160. assertSame(propertySetSchema, propertySet.getSchema());
  161. lock.assertLockedCount(1);
  162. }
  163. @Test
  164. public void testGetSetStringLocks() throws Exception {
  165. final MockLock lock = new MockLock();
  166. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  167. init(propertySet);
  168. // The first get will miss the cache and should lock
  169. assertNull(propertySet.getString("test"));
  170. lock.assertLockedCount(2);
  171. propertySet.setString("test", "fred");
  172. lock.assertLockedCount(3);
  173. assertEquals("fred", propertySet.getString("test"));
  174. // The value is now cached, so no lock is needed.
  175. lock.assertLockedCount(3);
  176. }
  177. @Test
  178. public void testGetSetTextLocks() throws Exception {
  179. final MockLock lock = new MockLock();
  180. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  181. init(propertySet);
  182. // The first get will miss the cache and should lock
  183. assertNull(propertySet.getText("test"));
  184. lock.assertLockedCount(2);
  185. propertySet.setText("test", "some text");
  186. lock.assertLockedCount(3);
  187. assertEquals("some text", propertySet.getText("test"));
  188. // The value is now cached, so no lock is needed.
  189. lock.assertLockedCount(3);
  190. }
  191. @Test
  192. public void testGetTypeLocks() throws Exception {
  193. final MockLock lock = new MockLock();
  194. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  195. init(propertySet);
  196. propertySet.setText("test", "some text");
  197. lock.assertLockedCount(2);
  198. assertEquals(PropertySet.TEXT, propertySet.getType("test"));
  199. lock.assertLockedCount(3);
  200. }
  201. @Test
  202. public void testGetKeysDoesNotLock() throws Exception {
  203. final MockLock lock = new MockLock();
  204. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  205. init(propertySet);
  206. propertySet.setText("test", "some text");
  207. lock.assertLockedCount(2);
  208. assertEquals(newArrayList("test"), propertySet.getKeys());
  209. lock.assertLockedCount(2);
  210. assertEquals(newArrayList("test"), propertySet.getKeys(PropertySet.TEXT));
  211. lock.assertLockedCount(2);
  212. assertEquals(newArrayList("test"), propertySet.getKeys("tes"));
  213. lock.assertLockedCount(2);
  214. assertEquals(newArrayList("test"), propertySet.getKeys("tes", PropertySet.TEXT));
  215. lock.assertLockedCount(2);
  216. }
  217. @Test
  218. public void testIsSettableDoesNotLock() throws Exception {
  219. final MockLock lock = new MockLock();
  220. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  221. init(propertySet);
  222. lock.assertLockedCount(1);
  223. assertTrue(propertySet.isSettable("test"));
  224. lock.assertLockedCount(1);
  225. }
  226. @Test
  227. public void testSupportsTypeDoesNotLock() throws Exception {
  228. final MockLock lock = new MockLock();
  229. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  230. init(propertySet);
  231. lock.assertLockedCount(1);
  232. assertTrue(propertySet.supportsType(PropertySet.STRING));
  233. lock.assertLockedCount(1);
  234. }
  235. @Test
  236. public void testSupportsTypesDoesNotLock() throws Exception {
  237. final MockLock lock = new MockLock();
  238. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  239. init(propertySet);
  240. lock.assertLockedCount(1);
  241. assertTrue(propertySet.supportsTypes());
  242. lock.assertLockedCount(1);
  243. }
  244. @Test
  245. public void testInitLocks() throws Exception {
  246. final MockLock lock = new MockLock();
  247. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  248. init(propertySet);
  249. lock.assertLockedCount(1);
  250. }
  251. @Test
  252. public void testGetAsTypeDoesLessLocks() throws Exception {
  253. final MockLock lock = new MockLock();
  254. final JiraCachingPropertySet propertySet = new JiraCachingPropertySet(lock);
  255. init(propertySet);
  256. lock.assertLockedCount(1);
  257. // The first time we call, getType() will do a lock, but nothing else.
  258. assertNull(propertySet.getAsActualType("test"));
  259. lock.assertLockedCount(2);
  260. propertySet.setAsActualType("test", Boolean.TRUE);
  261. lock.assertLockedCount(3);
  262. assertEquals(Boolean.TRUE, propertySet.getAsActualType("test"));
  263. // Now we can get from cache, and should get no locks for getBoolean, but we get a lock from getType() the first time
  264. lock.assertLockedCount(4);
  265. assertEquals(Boolean.TRUE, propertySet.getAsActualType("test"));
  266. // but getType is cached the second time
  267. lock.assertLockedCount(4);
  268. assertEquals(Boolean.TRUE, propertySet.getAsActualType("test"));
  269. // and 3rd time etc
  270. lock.assertLockedCount(4);
  271. }
  272. private static void init(final JiraCachingPropertySet propertySet) {
  273. final Map<?, ?> config = newHashMap();
  274. final Map<String, Object> args = newHashMap();
  275. // now test with explicit false
  276. final PropertySet underlyingPropertySet = TestJiraCachingPropertySet.createUnderlyingPropertySet();
  277. args.put("PropertySet", underlyingPropertySet);
  278. propertySet.init(config, args);
  279. }
  280. static class MockLock implements Lock {
  281. int locked, unlocked;
  282. public void lock() {
  283. locked++;
  284. }
  285. public void lockInterruptibly() throws InterruptedException {
  286. }
  287. @Nonnull
  288. @Override
  289. public Condition newCondition() {
  290. throw new UnsupportedOperationException("Not implemented");
  291. }
  292. public boolean tryLock() {
  293. return false;
  294. }
  295. public boolean tryLock(final long time, @Nonnull final TimeUnit unit) throws InterruptedException {
  296. return false;
  297. }
  298. public void unlock() {
  299. unlocked++;
  300. }
  301. public void assertLockedCount(final int count) {
  302. assertEquals(count, locked);
  303. assertEquals(count, unlocked);
  304. }
  305. }
  306. }