/* * JBoss, Home of Professional Open Source * Copyright 2009 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. */ /* * JBoss, Home of Professional Open Source * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.infinispan.api.tree; import org.infinispan.Cache; import org.infinispan.config.Configuration; import org.infinispan.test.MultipleCacheManagersTest; import org.infinispan.test.TestingUtil; import org.infinispan.tree.Fqn; import org.infinispan.tree.Node; import org.infinispan.tree.TreeCache; import org.infinispan.tree.TreeCacheImpl; import org.infinispan.tree.TreeStructureSupport; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertNull; import org.testng.annotations.Test; import javax.transaction.TransactionManager; @Test(groups = "functional", testName = "api.tree.NodeReplicatedMoveTest") public class NodeReplicatedMoveTest extends MultipleCacheManagersTest { static final Fqn A = Fqn.fromString("/a"), B = Fqn.fromString("/b"), C = Fqn.fromString("/c"), D = Fqn.fromString("/d"), E = Fqn.fromString("/e"); static final Object k = "key", vA = "valueA", vB = "valueB", vC = "valueC", vD = "valueD", vE = "valueE"; TreeCache<Object, Object> cache1, cache2; TransactionManager tm1; protected void createCacheManagers() throws Throwable { Configuration c = getDefaultClusteredConfig(Configuration.CacheMode.REPL_SYNC, true); c.setInvocationBatchingEnabled(true); createClusteredCaches(2, "replSync", c); Cache c1 = cache(0, "replSync"); Cache c2 = cache(1, "replSync"); tm1 = TestingUtil.getTransactionManager(c1); cache1 = new TreeCacheImpl<Object, Object>(c1); cache2 = new TreeCacheImpl<Object, Object>(c2); } public void testReplicatability() { Node<Object, Object> rootNode = cache1.getRoot(); Node<Object, Object> nodeA = rootNode.addChild(A); Node<Object, Object> nodeB = nodeA.addChild(B); nodeA.put(k, vA); nodeB.put(k, vB); assertEquals(vA, cache1.getRoot().getChild(A).get(k)); assertEquals(vB, cache1.getRoot().getChild(A).getChild(B).get(k)); assertEquals(vA, cache2.getRoot().getChild(A).get(k)); assertEquals(vB, cache2.getRoot().getChild(A).getChild(B).get(k)); // now move... cache1.move(nodeB.getFqn(), Fqn.ROOT); assertEquals(vA, cache1.getRoot().getChild(A).get(k)); assertEquals(vB, cache1.getRoot().getChild(B).get(k)); assertEquals(vA, cache2.getRoot().getChild(A).get(k)); assertEquals(vB, cache2.getRoot().getChild(B).get(k)); } public void testReplTxCommit() throws Exception { Node<Object, Object> rootNode = cache1.getRoot(); Fqn A_B = Fqn.fromRelativeFqn(A, B); Node<Object, Object> nodeA = rootNode.addChild(A); Node<Object, Object> nodeB = nodeA.addChild(B); nodeA.put(k, vA); nodeB.put(k, vB); assertEquals(vA, cache1.getRoot().getChild(A).get(k)); assertEquals(vB, cache1.getRoot().getChild(A).getChild(B).get(k)); assertEquals(vA, cache2.getRoot().getChild(A).get(k)); assertEquals(vB, cache2.getRoot().getChild(A).getChild(B).get(k)); // now move... tm1.begin(); cache1.move(nodeB.getFqn(), Fqn.ROOT); assertEquals(vA, cache1.get(A, k)); assertNull(cache1.get(A_B, k)); assertEquals(vB, cache1.get(B, k)); tm1.commit(); assertEquals(vA, cache1.getRoot().getChild(A).get(k)); assertEquals(vB, cache1.getRoot().getChild(B).get(k)); assertEquals(vA, cache2.getRoot().getChild(A).get(k)); assertEquals(vB, cache2.getRoot().getChild(B).get(k)); } public void testReplTxRollback() throws Exception { System.out.println(TreeStructureSupport.printTree(cache1, true)); Node<Object, Object> rootNode = cache1.getRoot(); Node<Object, Object> nodeA = rootNode.addChild(A); Node<Object, Object> nodeB = nodeA.addChild(B); nodeA.put(k, vA); nodeB.put(k, vB); assertEquals(vA, cache1.getRoot().getChild(A).get(k)); assertEquals(vB, cache1.getRoot().getChild(A).getChild(B).get(k)); assertEquals(vA, cache2.getRoot().getChild(A).get(k)); assertEquals(vB, cache2.getRoot().getChild(A).getChild(B).get(k)); System.out.println(TreeStructureSupport.printTree(cache1, true)); // now move... tm1.begin(); cache1.move(nodeB.getFqn(), Fqn.ROOT); assertEquals(vA, cache1.get(A, k)); assertEquals(vB, cache1.get(B, k)); tm1.rollback(); System.out.println(TreeStructureSupport.printTree(cache1, true)); assertEquals(vA, cache1.getRoot().getChild(A).get(k)); assertEquals(vB, cache1.getRoot().getChild(A).getChild(B).get(k)); assertEquals(vA, cache2.getRoot().getChild(A).get(k)); assertEquals(vB, cache2.getRoot().getChild(A).getChild(B).get(k)); } }