/*
 * This file is part of memoization.java. It is subject to the license terms in the LICENSE file found in the top-level
 * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of memoization.java,
 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
 * in the LICENSE file.
 */
package de.xn__ho_hia.memoization.jcache;

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;

import java.util.function.Consumer;
import java.util.function.Predicate;

import javax.cache.Cache;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;

import de.xn__ho_hia.memoization.shared.MemoizationException;
import de.xn__ho_hia.quality.suppression.CompilerWarnings;

/**
 *
 */
@SuppressWarnings({ CompilerWarnings.NLS, CompilerWarnings.STATIC_METHOD, CompilerWarnings.UNCHECKED })
public class JCacheBasedConsumerMemoizerTest {

    /** Captures expected exceptions. */
    @Rule
    public ExpectedException thrown = ExpectedException.none();

    /**
    *
    */
    @Test
    public void shouldMemoizeConsumer() {
        // given
        final Consumer<String> consumer = Mockito.mock(Consumer.class);
        try (final Cache<String, String> cache = JCacheMemoization.createCache(Predicate.class.getSimpleName())) {
            // when
            final JCacheBasedConsumerMemoizer<String> loader = new JCacheBasedConsumerMemoizer<>(cache,
                    consumer);

            // then
            loader.accept("test");
            Mockito.verify(consumer).accept("test");
        }
    }

    /**
    *
    */
    @Test
    public void shouldWrapRuntimeExceptionInMemoizationException() {
        // given
        try (final Cache<String, String> cache = Mockito.mock(Cache.class)) {
            final JCacheBasedConsumerMemoizer<String> loader = new JCacheBasedConsumerMemoizer<>(cache, null);
            given(cache.invoke(any(), any())).willThrow(RuntimeException.class);

            // when
            thrown.expect(MemoizationException.class);

            // then
            loader.accept("test");
        }
    }

}

@@ -1686,16 +1686,613 @@
 %0A    %7D%0A%0A
+    /**%0A    *%0A    */%0A    @Test%0A    public void shouldMemoizeConsumerOnce() %7B%0A        // given%0A        final Consumer%3CString%3E consumer = Mockito.mock(Consumer.class);%0A        try (final Cache%3CString, String%3E cache = JCacheMemoization.createCache(Predicate.class.getSimpleName())) %7B%0A            // when%0A            final JCacheBasedConsumerMemoizer%3CString%3E loader = new JCacheBasedConsumerMemoizer%3C%3E(cache,%0A                    consumer);%0A%0A            // then%0A            loader.accept(%22test%22);%0A            loader.accept(%22test%22);%0A            Mockito.verify(consumer).accept(%22test%22);%0A        %7D%0A    %7D%0A%0A
     /**%0A

verify that existing entries do not get overwritten