@Test @SuppressWarnings("unchecked") public void testSimplePutIfAbsentWithLoaderAndWriter_existsInStore() throws Exception { testCache.put(1, "un"); reset(cacheLoaderWriter); assertThat(testCache.putIfAbsent(1, "one"), Matchers.<CharSequence>equalTo("un")); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("un")); verifyZeroInteractions(cacheLoaderWriter); }
@Override public void run() { for (int i = number; i < number + 10; i++) { cache.put(i , i); logger.info(Thread.currentThread().getName() + " putting " + i); } } }
@Test public void testPutWithWriterException() throws Exception { doThrow(new Exception("Mock Exception: cannot write 1")).when(cacheLoaderWriter).write(eq(1), eq("one")); try { testCache.put(1, "one"); fail("expected CacheWritingException"); } catch (CacheWritingException ex) { // expected } }
private void put(Cache<Long, String> cache, String value, boolean addToCacheRecords) { cache.put(KEY, value); if (addToCacheRecords) { cacheRecords.add(new Record(KEY, cache.get(KEY))); } }
@Test public void testRemove2ArgsWithNotMatchingCacheEntry_should_not_call_writer() throws Exception { doThrow(new Exception("Mock Exception: cannot write 1")).when(cacheLoaderWriter).delete(eq(1)); testCache.put(1, "un"); testCache.remove(1, "one"); }
@Test public void getEvictions() { for (long i = 0; i < 11; i++) { cache.put(i, "a"); } assertThat(onHeap.getEvictions()).isEqualTo(1L); assertStat("OnHeap:EvictionCount").isEqualTo(1L); }
@Test @SuppressWarnings("unchecked") public void testSimpleRemove2ArgsWithLoaderAndWriter_existsInStore_notEquals() throws Exception { testCache.put(1, "un"); reset(cacheLoaderWriter); assertThat(testCache.remove(1, "one"), is(false)); verifyZeroInteractions(cacheLoaderWriter); }
@Test public void getMappings() throws Exception { cache.put(1L, "a"); assertThat(onHeap.getMappings()).isEqualTo(1L); assertStat("OnHeap:MappingCount").isEqualTo(1L); }
@Test public void getHits() throws Exception { cache.put(1L, "a"); cache.get(1L); assertThat(onHeap.getHits()).isEqualTo(1L); assertStat("OnHeap:HitCount").isEqualTo(1L); }
@Test public void getHits() { cache.put(1L, "a"); cache.get(1L); assertThat(onHeap.getHits()).isEqualTo(0L); assertNoStat("OnHeap:HitCount"); }
@Test public void getExpirations() throws Exception { cache.put(1L, "a"); timeSource.advanceTime(TIME_TO_EXPIRATION); cache.get(1L); assertThat(onHeap.getExpirations()).isEqualTo(1L); assertStat("OnHeap:ExpirationCount").isEqualTo(1L); }
@Test public void replaceKON() { expect(cache.replace(1, "a", "b")).isFalse(); changesOf(0, 1, 0, 0); cache.put(1, "a"); changesOf(0, 0, 1, 0); expect(cache.replace(1, "xxx", "b")).isFalse(); changesOf(1, 0, 0, 0); expect(cache.replace(1, "a", "b")).isTrue(); changesOf(1, 0, 1, 0); }
@Test public void get() { expect(cache.get(1)).isNull(); changesOf(0, 1, 0, 0); cache.put(1, "a"); changesOf(0, 0, 1, 0); expect(cache.get(1)).isEqualTo("a"); changesOf(1, 0, 0, 0); }
@Test public void replaceKV() { expect(cache.replace(1, "a")).isNull(); changesOf(0, 1, 0, 0); cache.put(1, "a"); changesOf(0, 0, 1, 0); expect(cache.replace(1, "b")).isEqualTo("a"); changesOf(1, 0, 1, 0); }
public static void testEhcache3WithSerializationAndClientClass() { CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("myCache", newCacheConfigurationBuilder(Long.class, Person.class, heap(10)) .add(new DefaultCopierConfiguration<>(SerializingCopier.<Person>asCopierClass(), DefaultCopierConfiguration.Type.VALUE)) .withClassLoader(TestMethods.class.getClassLoader()) .build()) .build(true); Cache<Long, Person> myCache = cacheManager.getCache("myCache", Long.class, Person.class); myCache.put(42L, new Person("Arthur")); assertTrue(myCache.get(42L) instanceof Person); }