/** * Method testTimeoutIsEmpty */ @Test public void testTimeoutIsEmpty() { TimeoutMap map = new TimeoutMap(50L); assertNull(map.put("key", "value-1")); assertEquals("value-1", map.get("key")); assertNull(map.put("another", "value-2")); assertEquals("value-2", map.get("another")); synchronized (this) { try { Thread.sleep(100L); } catch (InterruptedException e) { // Continue, but might break the timeout thing below... } } // This for loop should not print anything, if the tests succeed. Set set = map.keySet(); assertEquals(0, set.size()); for (Iterator iterator = set.iterator(); iterator.hasNext(); System.out.println(iterator.next())) { ; } assertEquals(0, map.size()); assertTrue(map.isEmpty()); }
@Test public void testIteratorPredictableRemovedEntry() { TimeoutMap map = (TimeoutMap) makeEmptyMap(); map.setExpiryTime(1000l); // No elements should expire during this test map.put("key-1", new Integer(1)); map.put("key-2", new Integer(2)); assertFalse(map.isEmpty()); Iterator iterator = map.entrySet().iterator(); assertTrue("Iterator was empty", iterator.hasNext()); try { assertTrue("Wrong entry removed, keySet().iterator() is broken.", !map.containsKey(removedKey)); assertTrue("Wrong entry removed, keySet().iterator() is broken.", map.containsKey(otherKey));
oldValue = null; entry = createEntry(pKey, pValue); removeExpiredEntries(); modCount++;
/** * Method testWrapMaxSize */ @Test public void testWrapMaxSize() { LRUMap lru = new LRUMap(); lru.setMaxSize(2); TimeoutMap map = new TimeoutMap(lru, null, 1000L); assertNull(map.put("key", "value-1")); assertEquals("value-1", map.get("key")); assertNull(map.put("another", "value-2")); assertEquals("value-2", map.get("another")); assertNull(map.put("third", "value-3")); assertEquals("value-3", map.get("third")); // This value should have expired assertNull(map.get("key")); // These should be left assertEquals("value-2", map.get("another")); assertEquals("value-3", map.get("third")); }
@Test public void testIteratorPredictableRemove() { TimeoutMap map = (TimeoutMap) makeFullMap(); map.setExpiryTime(50l); assertFalse(map.isEmpty()); int count = 0; for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) { if (count == 0) { // NOTE: Only wait fist time, to avoid slooow tests synchronized (this) { try { wait(60l); } catch (InterruptedException e) { } } } try { iterator.remove(); count++; } catch (NoSuchElementException nse) { fail("Elements expired between Interator.hasNext() and Iterator.remove()"); } } assertTrue("Elements expired too early, test did not run as expected.", count > 0); //assertEquals("Elements did not expire as expected.", 1, count); }
public Map makeEmptyMap() { return new TimeoutMap(60 * 60 * 1000); } /*
/** * Returns {@code true} if this map contains no key-value mappings. * * @return {@code true} if this map contains no key-value mappings. */ public boolean isEmpty() { return (size() <= 0); }
/** * Keeps track of timed objects */ private class TimedEntry<K, V> extends BasicEntry<K, V> { private long mTimestamp; TimedEntry(K pKey, V pValue) { super(pKey, pValue); mTimestamp = System.currentTimeMillis(); } public V setValue(V pValue) { mTimestamp = System.currentTimeMillis(); return super.setValue(pValue); } final boolean isExpired() { return isExpiredBy(System.currentTimeMillis()); } final boolean isExpiredBy(final long pTime) { return pTime > expires(); } final long expires() { return mTimestamp + expiryTime; } } }
/** * Removes all mappings from this map. */ public void clear() { entries.clear(); // Finally something straightforward.. :-) init(); }
@Test public void testIteratorPredictableNext() { TimeoutMap map = (TimeoutMap) makeFullMap(); map.setExpiryTime(50l); assertFalse(map.isEmpty()); int count = 0; for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) { if (count == 0) { // NOTE: Only wait fist time, to avoid slooow tests synchronized (this) { try { wait(60l); } catch (InterruptedException e) { } } } try { Map.Entry entry = (Map.Entry) iterator.next(); assertNotNull(entry); count++; } catch (NoSuchElementException nse) { fail("Elements expire between Interator.hasNext() and Iterator.next()"); } } assertTrue("Elements expired too early, test did not run as expected.", count > 0); //assertEquals("Elements did not expire as expected.", 1, count); }
/** * Method testWrapMapContainingValues */ @Test public void testWrapMapContainingValues() { Map backing = new TreeMap(); backing.put("key", "original"); TimeoutMap map = null; try { map = new TimeoutMap(backing, backing, 1000L); Object value = map.put("key", "value-1"); assertNotNull(value); // Should now have value! assertEquals("original", value); } catch (ClassCastException cce) { cce.printStackTrace(); fail("Content not converted to TimedEntries properly!"); } assertEquals("value-1", map.get("key")); assertNull(map.put("another", "value-2")); assertEquals("value-2", map.get("another")); assertNull(map.put("third", "value-3")); assertEquals("value-3", map.get("third")); }
/** * Method testBasicMap */ @Test public void testBasicMap() { Map map = new TimeoutMap(60000L); Object key = "key"; Object value = new Integer(3); map.put(key, value); assertEquals(value, map.get(key)); }
/** * Returns {@code true} if this map contains no key-value mappings. * * @return {@code true} if this map contains no key-value mappings. */ public boolean isEmpty() { return (size() <= 0); }
/** * Returns the value to which this map maps the specified pKey. Returns * {@code null} if the map contains no mapping for this pKey. A return * value of {@code null} does not <i>necessarily</i> indicate that the * map contains no mapping for the pKey; it's also possible that the map * explicitly maps the pKey to {@code null}. The {@code containsKey} * operation may be used to distinguish these two cases. * * @param pKey pKey whose associated value is to be returned. * @return the value to which this map maps the specified pKey, or * {@code null} if the map contains no mapping for this pKey. * @see #containsKey(java.lang.Object) */ public V get(Object pKey) { TimedEntry<K, V> entry = (TimedEntry<K, V>) entries.get(pKey); if (entry == null) { return null; } else if (entry.isExpired()) { //noinspection SuspiciousMethodCalls entries.remove(pKey); processRemoved(entry); return null; } return entry.getValue(); }
/** * Removes all mappings from this map. */ public void clear() { mEntries.clear(); // Finally something straightforward.. :-) init(); }
/** * Method testTimeoutIsEmpty */ public void testTimeoutIsEmpty() { TimeoutMap map = new TimeoutMap(50L); assertNull(map.put("key", "value-1")); assertEquals("value-1", map.get("key")); assertNull(map.put("another", "value-2")); assertEquals("value-2", map.get("another")); synchronized (this) { try { Thread.sleep(100L); } catch (InterruptedException e) { // Continue, but might break the timeout thing below... } } // This for loop should not print anything, if the tests succeed. Set set = map.keySet(); assertEquals(0, set.size()); for (Iterator iterator = set.iterator(); iterator.hasNext(); System.out.println(iterator.next())) { ; } assertEquals(0, map.size()); assertTrue(map.isEmpty()); }
@Test public void testIteratorPredictableNextRemove() { TimeoutMap map = (TimeoutMap) makeFullMap(); map.setExpiryTime(50l); assertFalse(map.isEmpty()); for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) { if (count == 0) {
public void testIteratorPredictableRemovedEntry() { TimeoutMap map = (TimeoutMap) makeEmptyMap(); map.setExpiryTime(1000l); // No elements should expire during this test map.put("key-1", new Integer(1)); map.put("key-2", new Integer(2)); assertFalse(map.isEmpty()); Iterator iterator = map.entrySet().iterator(); assertTrue("Iterator was empty", iterator.hasNext()); try { assertTrue("Wrong entry removed, keySet().iterator() is broken.", !map.containsKey(removedKey)); assertTrue("Wrong entry removed, keySet().iterator() is broken.", map.containsKey(otherKey));
/** * Method testWrapMaxSize */ public void testWrapMaxSize() { LRUMap lru = new LRUMap(); lru.setMaxSize(2); TimeoutMap map = new TimeoutMap(lru, null, 1000L); assertNull(map.put("key", "value-1")); assertEquals("value-1", map.get("key")); assertNull(map.put("another", "value-2")); assertEquals("value-2", map.get("another")); assertNull(map.put("third", "value-3")); assertEquals("value-3", map.get("third")); // This value should have expired assertNull(map.get("key")); // These should be left assertEquals("value-2", map.get("another")); assertEquals("value-3", map.get("third")); }
/** * Method testBasicMapAllowsNull */ @Test public void testBasicMapAllowsNull() { Map map = new TimeoutMap(60000L); Object key = "key"; Object value = null; map.put(key, value); assertNull(map.get(key)); }