@SuppressWarnings({"rawtypes", "unchecked"}) @SPITest public void missingIterableEntriesAreIgnoredByTheStore() throws Exception { final Store<K, V> kvStore = factory.newStore( factory.newConfiguration( factory.getKeyType(), factory.getValueType(), null, null, null)); final K k1 = factory.createKey(1L); final V v1 = factory.createValue(1L); Set<K> set = new HashSet<K>(); set.add(k1); kvStore.put(k1, v1); try { kvStore.bulkComputeIfAbsent( Arrays.asList((K[]) set.toArray()), new Function< Iterable<? extends K>, Iterable<? extends Map.Entry<? extends K, ? extends V>>>() { @Override public Iterable<? extends Map.Entry<? extends K, ? extends V>> apply( Iterable<? extends K> entries) { return new HashMap<K, V>().entrySet(); } }); assertThat(kvStore.get(k1).value(), is(v1)); } catch (CacheAccessException e) { System.err.println("Warning, an exception is thrown due to the SPI test"); e.printStackTrace(); } }
@SuppressWarnings({"rawtypes", "unchecked"}) @SPITest public void testWrongKeyType() throws Exception { final Store<K, V> kvStore = factory.newStore( factory.newConfiguration( factory.getKeyType(), factory.getValueType(), null, null, null)); Set<K> set = new HashSet<K>(); if (factory.getKeyType() == String.class) { set.add((K) new Object()); } else { set.add((K) "WrongKeyType"); } try { kvStore.bulkComputeIfAbsent( Arrays.asList((K[]) set.toArray()), new Function< Iterable<? extends K>, Iterable<? extends Map.Entry<? extends K, ? extends V>>>() { @Override public Iterable<? extends Map.Entry<? extends K, ? extends V>> apply( Iterable<? extends K> entries) { throw new AssertionError( "Expected ClassCastException because the key is of the wrong type"); } }); throw new AssertionError("Expected ClassCastException because the key is of the wrong type"); } catch (ClassCastException e) { // expected } catch (CacheAccessException e) { System.err.println("Warning, an exception is thrown due to the SPI test"); e.printStackTrace(); } }
@SPITest public void removesAllOfTheMappings() throws IllegalAccessException, InstantiationException, CacheAccessException { final Store<K, V> kvStore = factory.newStore( new StoreConfigurationImpl<K, V>( factory.getKeyType(), factory.getValueType(), null, Predicates.<Cache.Entry<K, V>>all(), null, ClassLoader.getSystemClassLoader(), Expirations.noExpiration())); K key = factory.getKeyType().newInstance(); V value = factory.getValueType().newInstance(); kvStore.put(key, value); try { kvStore.clear(); } catch (CacheAccessException e) { System.err.println("Warning, an exception is thrown due to the SPI test"); e.printStackTrace(); } assertThat(kvStore.containsKey(key), is(false)); }
/** * Allows for deregistering of a previously registered {@link * org.ehcache.event.CacheEventListener} instance * * @param listener the listener to deregister * @throws java.lang.IllegalStateException if the listener isn't already registered */ @Override public void deregisterCacheEventListener(CacheEventListener<? super K, ? super V> listener) { if (!registeredListeners.remove( new EventListenerWrapper( listener, EventFiring.ASYNCHRONOUS, EventOrdering.UNORDERED, EnumSet.allOf(EventType.class)))) { throw new IllegalStateException("Unknown cache event listener: " + listener); } if (!hasListeners()) { store.disableStoreEventNotifications(); } }
/** * Allows for registering {@link org.ehcache.event.CacheEventListener} on the cache * * @param listener the listener instance to register * @param ordering the {@link org.ehcache.event.EventOrdering} to invoke this listener * @param firing the {@link org.ehcache.event.EventFiring} to invoke this listener * @param forEventTypes the {@link org.ehcache.event.EventType} to notify this listener of * @throws java.lang.IllegalStateException if the listener is already registered */ @Override public void registerCacheEventListener( CacheEventListener<? super K, ? super V> listener, EventOrdering ordering, EventFiring firing, EnumSet<EventType> forEventTypes) { boolean doRegister = forEventTypes.contains(EventType.EVICTED) || forEventTypes.contains(EventType.EXPIRED); if (!registeredListeners.add( new EventListenerWrapper(listener, firing, ordering, forEventTypes))) { throw new IllegalStateException("Cache Event Listener already registered: " + listener); } if (doRegister) { store.enableStoreEventNotifications(storeListener); } }