From dfc43107399326a3aa27cf17b20b0d7147be198a Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Mon, 22 Mar 2010 19:23:58 +0000 Subject: [PATCH] HHH-2350 : tests for 2nd level cache broken for non-inverse bidirectional one-to-many relation git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19080 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- ...ctRecursiveBidirectionalOneToManyTest.java | 159 ++++++++++++++++++ .../org/hibernate/test/onetomany/Node.hbm.xml | 26 +++ .../org/hibernate/test/onetomany/Node.java | 78 +++++++++ ...ursiveBidirectionalOneToManyCacheTest.java | 84 +++++++++ ...siveBidirectionalOneToManyNoCacheTest.java | 71 ++++++++ 5 files changed, 418 insertions(+) create mode 100644 testsuite/src/test/java/org/hibernate/test/onetomany/AbstractRecursiveBidirectionalOneToManyTest.java create mode 100644 testsuite/src/test/java/org/hibernate/test/onetomany/Node.hbm.xml create mode 100644 testsuite/src/test/java/org/hibernate/test/onetomany/Node.java create mode 100644 testsuite/src/test/java/org/hibernate/test/onetomany/RecursiveBidirectionalOneToManyCacheTest.java create mode 100644 testsuite/src/test/java/org/hibernate/test/onetomany/RecursiveBidirectionalOneToManyNoCacheTest.java diff --git a/testsuite/src/test/java/org/hibernate/test/onetomany/AbstractRecursiveBidirectionalOneToManyTest.java b/testsuite/src/test/java/org/hibernate/test/onetomany/AbstractRecursiveBidirectionalOneToManyTest.java new file mode 100644 index 0000000000..28aaee16d4 --- /dev/null +++ b/testsuite/src/test/java/org/hibernate/test/onetomany/AbstractRecursiveBidirectionalOneToManyTest.java @@ -0,0 +1,159 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program 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 distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + * + */ +package org.hibernate.test.onetomany; + +import java.util.ArrayList; + +import junit.framework.Test; + +import org.hibernate.CacheMode; +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.junit.functional.FunctionalTestCase; +import org.hibernate.junit.functional.FunctionalTestClassTestSuite; + +/** + * @author Burkhard Graves, Gail Badner + */ + +public abstract class AbstractRecursiveBidirectionalOneToManyTest extends FunctionalTestCase { + + /* + * What is done: + * ___ ___ + * | | | | + * -> 1 -> 1 + * | -transform-> / \ + * 2 2 3 + * | + * 3 + * + */ + + public AbstractRecursiveBidirectionalOneToManyTest(String str) { + super(str); + } + + public org.hibernate.classic.Session openSession() { + org.hibernate.classic.Session s = super.openSession(); + s.setCacheMode( getSessionCacheMode() ); + return s; + } + + protected abstract CacheMode getSessionCacheMode(); + + public String[] getMappings() { + return new String[] { "onetomany/Node.hbm.xml" }; + } + + public void testOneToManyMoveElement() { + init(); + transformMove(); + check(); + delete(); + } + + public void testOneToManyReplaceList() { + init(); + transformReplace(); + check(); + delete(); + } + + void init() { + + Session s = openSession(); + Transaction tx = s.beginTransaction(); + + Node node1 = new Node( 1 ); + Node node2 = new Node( 2 ); + Node node3 = new Node( 3 ); + + node1.addSubNode( node2 ); + node2.addSubNode( node3 ); + + s.save(node1); + + tx.commit(); + s.close(); + } + + void transformMove() { + + Session s = openSession(); + Transaction tx = s.beginTransaction(); + + Node node3 = (Node) s.load(Node.class, new Integer(3)); + Node node2 = node3.getParentNode(); + Node node1 = node2.getParentNode(); + + node2.removeSubNode( node3 ); + node1.addSubNode( node3 ); + + tx.commit(); + s.close(); + } + + void transformReplace() { + + Session s = openSession(); + Transaction tx = s.beginTransaction(); + + Node node3 = (Node) s.load(Node.class, new Integer(3)); + Node node2 = node3.getParentNode(); + Node node1 = node2.getParentNode(); + + node2.removeSubNode( node3 ); + node1.setSubNodes( new ArrayList() ); + node1.addSubNode( node2 ); + node1.addSubNode( node3 ); + + tx.commit(); + s.close(); + } + + void check() { + + Session s = openSession(); + Transaction tx = s.beginTransaction(); + Node node3 = (Node) s.get(Node.class, new Integer(3)); + + // fails with 2nd level cache enabled + assertEquals(1, node3.getParentNode().getId().intValue()); + + tx.commit(); + s.close(); + } + + void delete() { + + Session s = openSession(); + Transaction tx = s.beginTransaction(); + Node node1 = ( Node ) s.get( Node.class, new Integer( 1 ) ); + s.delete( node1 ); + tx.commit(); + s.close(); + } +} \ No newline at end of file diff --git a/testsuite/src/test/java/org/hibernate/test/onetomany/Node.hbm.xml b/testsuite/src/test/java/org/hibernate/test/onetomany/Node.hbm.xml new file mode 100644 index 0000000000..102006e76d --- /dev/null +++ b/testsuite/src/test/java/org/hibernate/test/onetomany/Node.hbm.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + diff --git a/testsuite/src/test/java/org/hibernate/test/onetomany/Node.java b/testsuite/src/test/java/org/hibernate/test/onetomany/Node.java new file mode 100644 index 0000000000..30c19c935d --- /dev/null +++ b/testsuite/src/test/java/org/hibernate/test/onetomany/Node.java @@ -0,0 +1,78 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program 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 distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + * + */ +package org.hibernate.test.onetomany; + +import java.io.Serializable; +import java.util.List; +import java.util.ArrayList; + +public class Node implements Serializable { + + private Integer id; + + private Node parentNode; + private List subNodes = new ArrayList(); + + public Node() { + } + + public Node(int id) { + setId( id ); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Node getParentNode() { + return parentNode; + } + + public void setParentNode(Node parentNode) { + this.parentNode = parentNode; + } + + public List getSubNodes() { + return subNodes; + } + + public void setSubNodes(List subNodes) { + this.subNodes = subNodes; + } + + public void addSubNode(Node subNode) { + subNodes.add( subNode ); + subNode.setParentNode( this ); + } + + public void removeSubNode(Node subNode) { + subNodes.remove( subNode ); + subNode.setParentNode( null ); + } +} diff --git a/testsuite/src/test/java/org/hibernate/test/onetomany/RecursiveBidirectionalOneToManyCacheTest.java b/testsuite/src/test/java/org/hibernate/test/onetomany/RecursiveBidirectionalOneToManyCacheTest.java new file mode 100644 index 0000000000..fc9ad1ebb1 --- /dev/null +++ b/testsuite/src/test/java/org/hibernate/test/onetomany/RecursiveBidirectionalOneToManyCacheTest.java @@ -0,0 +1,84 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program 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 distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + * + */ +package org.hibernate.test.onetomany; + +import junit.framework.Test; + +import org.hibernate.CacheMode; +import org.hibernate.junit.functional.FunctionalTestClassTestSuite; + +/** + * @author Burkhard Graves, Gail Badner + */ + +public class RecursiveBidirectionalOneToManyCacheTest extends AbstractRecursiveBidirectionalOneToManyTest { + + /* + * What is done: + * ___ ___ + * | | | | + * -> 1 -> 1 + * | -transform-> / \ + * 2 2 3 + * | + * 3 + * + * Commenting out + * @Cache(usage=CacheConcurrencyStrategy.READ_WRITE) + * in Node.java makes the assertion true (in check() below). + */ + + public RecursiveBidirectionalOneToManyCacheTest(String str) { + super(str); + } + + protected CacheMode getSessionCacheMode() { + return CacheMode.NORMAL; + } + + public static Test suite() { + return new FunctionalTestClassTestSuite( RecursiveBidirectionalOneToManyCacheTest.class ); + } + + public void testOneToManyMoveElement() { + reportSkip( "non-inverse one-to-many known to fail with 2nd-level cache", "cache support"); + } + + // HHH-2350 + public void testOneToManyMoveElementFailureExpected() { + super.testOneToManyMoveElement(); + } + + public void testOneToManyReplaceList() { + reportSkip( "non-inverse one-to-many known to fail with 2nd-level cache", "cache support"); + } + + // HHH-2350 + public void testOneToManyReplaceListFailureExpected() { + super.testOneToManyReplaceList(); + } + + +} diff --git a/testsuite/src/test/java/org/hibernate/test/onetomany/RecursiveBidirectionalOneToManyNoCacheTest.java b/testsuite/src/test/java/org/hibernate/test/onetomany/RecursiveBidirectionalOneToManyNoCacheTest.java new file mode 100644 index 0000000000..b9226a9e7b --- /dev/null +++ b/testsuite/src/test/java/org/hibernate/test/onetomany/RecursiveBidirectionalOneToManyNoCacheTest.java @@ -0,0 +1,71 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program 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 distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + * + */ +package org.hibernate.test.onetomany; + +import junit.framework.Test; + +import org.hibernate.CacheMode; +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.junit.functional.FunctionalTestCase; +import org.hibernate.junit.functional.FunctionalTestClassTestSuite; + +/** + * @author Burkhard Graves, Gail Badner + */ + +public class RecursiveBidirectionalOneToManyNoCacheTest extends AbstractRecursiveBidirectionalOneToManyTest { + + /* + * What is done: + * ___ ___ + * | | | | + * -> 1 -> 1 + * | -transform-> / \ + * 2 2 3 + * | + * 3 + * + * Commenting out + * @Cache(usage=CacheConcurrencyStrategy.READ_WRITE) + * in Node.java makes the assertion true (in check() below). + */ + + public RecursiveBidirectionalOneToManyNoCacheTest(String str) { + super(str); + } + + public String getCacheConcurrencyStrategy() { + return null; + } + + protected CacheMode getSessionCacheMode() { + return CacheMode.IGNORE; + } + + public static Test suite() { + return new FunctionalTestClassTestSuite( RecursiveBidirectionalOneToManyNoCacheTest.class ); + } +} \ No newline at end of file