/* * Copyright 2012 Red Hat, Inc. and/or its affiliates. * * 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 library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA */ package org.infinispan.api.tree; import org.infinispan.Cache; import org.infinispan.atomic.AtomicHashMap; import org.infinispan.configuration.cache.ConfigurationBuilder; import org.infinispan.manager.EmbeddedCacheManager; import org.infinispan.notifications.Listener; import org.infinispan.notifications.cachelistener.annotation.CacheEntryModified; import org.infinispan.notifications.cachelistener.annotation.TransactionCompleted; import org.infinispan.notifications.cachelistener.event.CacheEntryModifiedEvent; import org.infinispan.notifications.cachelistener.event.TransactionCompletedEvent; import org.infinispan.test.SingleCacheManagerTest; import org.infinispan.test.fwk.TestCacheManagerFactory; import org.infinispan.tree.Fqn; import org.infinispan.tree.NodeKey; import org.infinispan.tree.TreeCache; import org.infinispan.tree.TreeCacheImpl; import org.testng.annotations.Test; import java.util.ArrayList; import java.util.List; import static org.testng.AssertJUnit.assertEquals; /** * // TODO: Document this * * @author Galder ZamarreƱo * @since // TODO */ @Test(groups = "functional", testName = "api.tree.TreeListenerTest") public class TreeListenerTest extends SingleCacheManagerTest { private TreeCache cache; @Override protected EmbeddedCacheManager createCacheManager() throws Exception { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.invocationBatching().enable(); EmbeddedCacheManager cm = TestCacheManagerFactory .createCacheManager(builder); Cache flatcache = cm.getCache(); cache = new TreeCacheImpl(flatcache); return cm; } public void testEntryModifiedListener() throws InterruptedException { TreeListener listener = new TreeListener(); cache.getCache().addListener(listener); cache.put(Fqn.fromString("/root"), "k", "v"); // cache.put(Fqn.fromRelativeFqn(Fqn.fromString("STATUS"), // Fqn.fromString("TRADE")),"key1","TRADE1"); List structure = listener.structure; assertEquals(2, structure.size()); assertEquals(Fqn.fromString("/"), structure.get(0).getFqn()); assertEquals(Fqn.fromString("root"), structure.get(1).getFqn()); List> data = listener.data; assertEquals(2, data.size()); assertEquals(1, data.get(0).size()); assertEquals(1, data.get(1).size()); } @Listener public static class TreeListener { List> data = new ArrayList>(); List structure = new ArrayList(); @CacheEntryModified public void modified(CacheEntryModifiedEvent> e) { if (!e.isPre()) { NodeKey k = e.getKey(); NodeKey.Type type = k.getContents(); if (type == NodeKey.Type.STRUCTURE) structure.add(k); else if (type == NodeKey.Type.DATA) data.add(e.getValue()); } } // @TransactionCompleted // public void completed(TransactionCompletedEvent e) { // // } } }