/* * JBoss, Home of Professional Open Source * Copyright 2011 Red Hat Inc. and/or its affiliates and other * contributors as indicated by the @author tags. All rights reserved. * See the copyright.txt in the distribution for a full listing of * individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.infinispan.notifications.cachelistener; import org.apache.avalon.framework.activity.Suspendable; import org.infinispan.Cache; import org.infinispan.CacheException; import org.infinispan.config.Configuration; import org.infinispan.marshall.jboss.JBossMarshaller; import org.infinispan.notifications.Listener; import org.infinispan.notifications.cachelistener.annotation.CacheEntryCreated; import org.infinispan.notifications.cachelistener.event.CacheEntryEvent; import org.infinispan.remoting.transport.jgroups.SuspectException; import org.infinispan.test.MultipleCacheManagersTest; import org.infinispan.util.ByteArrayKey; import org.testng.annotations.Test; import java.lang.reflect.Method; import static org.infinispan.test.TestingUtil.k; import static org.infinispan.test.TestingUtil.v; import static org.testng.AssertJUnit.fail; /** * // TODO: Document this * * @author Galder ZamarreƱo * @since // TODO */ @Test(groups = "functional", testName = "notifications.cachelistener.ListenerThrowsExceptionTest") public class ListenerThrowsExceptionTest extends MultipleCacheManagersTest { // public static final String CACHE_NAME = ListenerThrowsExceptionTest.class.getName() + "-Cache"; @Override protected void createCacheManagers() throws Throwable { Configuration cfg = getDefaultClusteredConfig( Configuration.CacheMode.REPL_SYNC, true); createClusteredCaches(2, cfg); } public void testExceptionThrowingListener(Method m) { Cache cache = manager(0).getCache(); PreOpExceptionListener listener = new PreOpExceptionListener(); cache.addListener(listener); try { cache.put(k(m), v(m)); } catch (CacheException e) { assert e.getCause() instanceof SuspectException; // Expected, now try to simulate a failover listener.injectFailure = false; manager(1).getCache().put(k(m), v(m, 2)); return; } fail("Should have failed"); } @Listener public static class PreOpExceptionListener { boolean injectFailure = true; @CacheEntryCreated public void entryCreated(CacheEntryEvent event) throws Exception { if (event.isPre() && injectFailure) { throw new SuspectException("Simulated suspicion"); } } } }