/** * Builds the {@link CacheEventListenerConfiguration} this builder represents. * * @return the {@code CacheEventListenerConfiguration} */ public DefaultCacheEventListenerConfiguration build() { DefaultCacheEventListenerConfiguration defaultCacheEventListenerConfiguration; if (this.listenerClass != null) { defaultCacheEventListenerConfiguration = new DefaultCacheEventListenerConfiguration(this.eventsToFireOn, this.listenerClass, this.listenerArguments); } else { defaultCacheEventListenerConfiguration = new DefaultCacheEventListenerConfiguration(this.eventsToFireOn, this.listenerInstance); } if (eventOrdering != null) { defaultCacheEventListenerConfiguration.setEventOrderingMode(this.eventOrdering); } if (eventFiringMode != null) { defaultCacheEventListenerConfiguration.setEventFiringMode(this.eventFiringMode); } return defaultCacheEventListenerConfiguration; }
@Test public void parseServiceConfiguration() throws Exception { CacheConfiguration<?, ?> cacheConfiguration = new XmlConfiguration(getClass().getResource("/configs/ehcache-cacheEventListener.xml")).getCacheConfigurations().get("bar"); DefaultCacheEventListenerConfiguration listenerConfig = findSingletonAmongst(DefaultCacheEventListenerConfiguration.class, cacheConfiguration.getServiceConfigurations()); assertThat(listenerConfig).isNotNull(); assertThat(listenerConfig.getClazz()).isEqualTo(TestCacheEventListener.class); assertThat(listenerConfig.firingMode()).isEqualTo(SYNCHRONOUS); assertThat(listenerConfig.orderingMode()).isEqualTo(UNORDERED); assertThat(listenerConfig.fireOn()).containsExactlyInAnyOrder(EventType.values()); }
@Test(expected = IllegalArgumentException.class) public void testFailsToConstructWithEmptyEventSetAndInstance() { Set<EventType> fireOn = emptySet(); new DefaultCacheEventListenerConfiguration(fireOn, mock(CacheEventListener.class)); }
@Test(expected = IllegalArgumentException.class) public void testFailsToConstructWithEmptyEventSetAndClass() { Set<EventType> fireOn = emptySet(); Class<TestCacheEventListener> eventListenerClass = TestCacheEventListener.class; new DefaultCacheEventListenerConfiguration(fireOn, eventListenerClass); }
if(serviceConfig.getInstance() == null) { return listener.withClazz(serviceConfig.getClazz().getName()) .withEventFiringMode(EventFiringType.fromValue(serviceConfig.firingMode().name())) .withEventOrderingMode(EventOrderingType.fromValue(serviceConfig.orderingMode().name())) .withEventsToFireOn(serviceConfig.fireOn() .stream() .map(eventType -> EventType.fromValue(eventType.name()))
@Test public void unparseServiceConfiguration() { DefaultCacheEventListenerConfiguration listenerConfig = new DefaultCacheEventListenerConfiguration(EnumSet.of(CREATED, REMOVED), TestCacheEventListener.class); listenerConfig.setEventFiringMode(SYNCHRONOUS); listenerConfig.setEventOrderingMode(UNORDERED); CacheConfiguration<?, ?> cacheConfig = newCacheConfigurationBuilder(Object.class, Object.class, heap(10)).add(listenerConfig).build(); CacheType cacheType = new CacheType(); cacheType = new DefaultCacheEventListenerConfigurationParser().unparseServiceConfiguration(cacheConfig, cacheType); List<ListenersType.Listener> listeners = cacheType.getListeners().getListener(); assertThat(listeners).hasSize(1); ListenersType.Listener listener = listeners.get(0); assertThat(listener.getEventFiringMode()).isEqualTo(EventFiringType.SYNCHRONOUS); assertThat(listener.getEventOrderingMode()).isEqualTo(EventOrderingType.UNORDERED); assertThat(listener.getEventsToFireOn()).contains(org.ehcache.xml.model.EventType.CREATED, org.ehcache.xml.model.EventType.REMOVED); }
@Override public CacheType unparseServiceConfiguration(CacheConfiguration<?, ?> cacheConfiguration, CacheType cacheType) { Collection<DefaultCacheEventListenerConfiguration> serviceConfigs = findAmongst(DefaultCacheEventListenerConfiguration.class, cacheConfiguration.getServiceConfigurations()); if (!serviceConfigs.isEmpty()) { ListenersType listenersType = cacheType.getListeners(); if (listenersType == null) { listenersType = new ListenersType(); cacheType.setListeners(listenersType); } Set<ListenersType.Listener> listeners = serviceConfigs.stream().map(serviceConfig -> { ListenersType.Listener listener = new ListenersType.Listener(); checkNoConcreteInstance(serviceConfig); return listener.withClazz(serviceConfig.getClazz().getName()) .withEventFiringMode(EventFiringType.fromValue(serviceConfig.firingMode().name())) .withEventOrderingMode(EventOrderingType.fromValue(serviceConfig.orderingMode().name())) .withEventsToFireOn(serviceConfig.fireOn() .stream() .map(eventType -> EventType.fromValue(eventType.name())) .collect(toSet())); }).collect(toSet()); cacheType.withListeners(listenersType.withListener(listeners)); } return cacheType; } }
@Test public void unparseServiceConfigurationWithInstance() { TestCacheEventListener testCacheEventListener = new TestCacheEventListener(); DefaultCacheEventListenerConfiguration listenerConfig = new DefaultCacheEventListenerConfiguration(EnumSet.of(CREATED, REMOVED), testCacheEventListener); listenerConfig.setEventFiringMode(SYNCHRONOUS); listenerConfig.setEventOrderingMode(UNORDERED); CacheConfiguration<?, ?> cacheConfig = newCacheConfigurationBuilder(Object.class, Object.class, heap(10)).add(listenerConfig).build(); CacheType cacheType = new CacheType(); assertThatExceptionOfType(XmlConfigurationException.class).isThrownBy(() -> new DefaultCacheEventListenerConfigurationParser().unparseServiceConfiguration(cacheConfig, cacheType)) .withMessage("%s", "XML translation for instance based initialization for " + "DefaultCacheEventListenerConfiguration is not supported"); } }
/** * Builds the {@link CacheEventListenerConfiguration} this builder represents. * * @return the {@code CacheEventListenerConfiguration} */ public DefaultCacheEventListenerConfiguration build() { DefaultCacheEventListenerConfiguration defaultCacheEventListenerConfiguration; if (this.listenerClass != null) { defaultCacheEventListenerConfiguration = new DefaultCacheEventListenerConfiguration(this.eventsToFireOn, this.listenerClass, this.listenerArguments); } else { defaultCacheEventListenerConfiguration = new DefaultCacheEventListenerConfiguration(this.eventsToFireOn, this.listenerInstance); } if (eventOrdering != null) { defaultCacheEventListenerConfiguration.setEventOrderingMode(this.eventOrdering); } if (eventFiringMode != null) { defaultCacheEventListenerConfiguration.setEventFiringMode(this.eventFiringMode); } return defaultCacheEventListenerConfiguration; }