fri.util.database.jpa.tree
Class AbstractTreeTest<D extends TreeDao<N>,N extends TreeNode>

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by fri.util.database.jpa.commons.AbstractJpaTest
              extended by fri.util.database.jpa.tree.AbstractTreeTest<D,N>
All Implemented Interfaces:
junit.framework.Test
Direct Known Subclasses:
AbstractTemporalTreeTest, ClosureTableTreeTest, NestedSetsTreeTest

public abstract class AbstractTreeTest<D extends TreeDao<N>,N extends TreeNode>
extends AbstractJpaTest

Tests all public methods of TreeDao with following sample tree:

    ROOT
      - A
        - A1
      - B
        - B1
        - B2
      - C
        - C1
          - C11
 
TODO: add a concurrent test, e.g. reading nodes while adding, moving and copying.

Author:
Fritz Ritzberger, 08.10.2011

Field Summary
protected static java.lang.String COPIED_NAME_PREFIX
          Constant prefix for testing node renames.
protected static java.lang.String[][] UNIQUE_PROPERTY_NAMES
          Unique constraint property names to test.
 
Fields inherited from class fri.util.database.jpa.commons.AbstractJpaTest
ECLIPSELINK_PERSISTENCE_UNIT_NAME, HIBERNATE_PERSISTENCE_UNIT_NAME
 
Constructor Summary
AbstractTreeTest()
           
 
Method Summary
protected  void assertTreeCacheable(java.util.List<N> treeList)
          Check depth-first order.
protected  DbSession beginDbTransaction(java.lang.String message)
           
protected abstract  void checkTreeIntegrity(DbSession session, N root)
           
protected  void commitDbTransaction(java.lang.String message)
           
protected  java.io.Serializable createTree()
           
protected  java.io.Serializable createTree(java.lang.String rootName)
           
protected  java.io.Serializable createTree(java.lang.String rootName, boolean useLowerCaseNames)
           
protected  N findByName(N root, java.lang.String name)
           
protected  D getDao()
           
protected  java.util.List<N> getFullTreeForIntegrityCheck(N root)
           
protected abstract  java.lang.String getName(N node)
          For a concrete POJO we must return properties from it.
protected  boolean isTestCopy()
           
protected abstract  D newDao(DbSession session)
           
protected  DbSession newDbSession(java.lang.String message)
           
protected abstract  N newTreePojo(java.lang.String name)
          Factory method for new NestedSetsTreePojos.
protected abstract  UniqueTreeConstraint<N> newUniqueChildrenTreeConstraintImpl()
           
protected abstract  UniqueTreeConstraint<N> newUniqueChildrenTreeConstraintImplWithoutRoots()
           
protected abstract  UniqueTreeConstraint<N> newUniqueWholeTreeConstraintImpl()
           
protected abstract  UniqueTreeConstraint<N> newUniqueWholeTreeConstraintImplWithoutRoots()
           
static void outputTree(java.util.List<? extends TreeNode> nodes)
           
static void outputTree(TreeNode root, TreeDao dao)
          Utility method.
protected abstract  void renameBeforeCopy(N node)
           
protected  void rollbackDbTransaction(java.lang.String message)
           
protected  void setName(N entity, N root, java.lang.String name)
          Name is unique, so a check is done here explicitly before calling entity.setName().
protected abstract  void setNameNotConstraintChecking(N entity, java.lang.String name)
           
protected  void setValid(N entity)
          To be overridden by temporal test.
protected  void tearDown()
          Overridden to clean-up test table(s).
 void testAddToTree()
          Tests adding tree nodes.
 void testBigTree()
          Create a big tree.
 void testCopiedNodeRenamer()
          Test renaming nodes while copying.
 void testCopyRootToBeRoot()
          Test copying root to be another root.
 void testCopyToOtherRoot()
          Test copy a sub-tree to another root.
 void testCopyToSelf()
          Test copy a sub-tree to itself, which is cloning a tree.
 void testCopyTree()
          Test copy a sub-tree without unique constraint on database level.
 void testCopyTreeBelowItselfFails()
          Test copy a sub-tree below itself.
 void testCopyTreeFailsWithUniqueConstraint()
          Test copy a sub-tree.
 void testCopyTreeToAlteredName()
          Test copy a sub-tree to another name.
 void testCopyTreeToAlteredNameWithUniqueConstraint()
          Test copy a sub-tree to another name with unique constraint turned on.
 void testCopyTreeToBeRoot()
          Test copying trees to be roots.
 void testCopyTreeWithPrecedingDelete()
          Test copy a sub-tree without unique constraint on database level.
 void testCreateAndRemoveRoot()
           
 void testCreateTree()
          Tests creation of a tree, including addChild().
 void testDatabaseToBeCaseSensitive()
          Especially for MySql, check that string-compares are case-sensitive!
 void testFindChildrenInCachedTree()
          Demonstrates how to retrieve the tree structure from a list of all nodes under a root.
 void testGetRoots()
          Tests reading all roots.
 void testIsBelow()
          Tests reading parent of all tree nodes.
 void testIsRoot()
           
 void testLevel()
          Tests level of all tree nodes.
 void testMoveRootToOtherTree()
          Test moving sub-trees to other roots.
 void testMoveToOtherTree()
          Test moving sub-trees to other roots.
 void testMoveTree()
          Tests moving subtrees in a tree.
 void testMoveTreeBelowItselfFails()
          Test move a sub-tree below itself.
 void testMoveTreeToBeRoot()
          Test moving tree to be roots.
 void testNonUniqueRootsButUniqueChildren()
           
 void testNonUniqueRootsButWholeTreeUniqueNodes()
           
 void testParent()
          Tests reading parent of all tree nodes.
 void testPath()
          Tests reading parents of tree nodes.
 void testRemoveAllTrees()
          Tests removing all tree roots.
 void testRemoveFromTree()
          Tests removing tree nodes.
 void testRemoveTree()
          Tests removing tree roots.
 void testUniqueChildrenConstraint()
          Test unique constraint for children.
 void testUniqueness()
          Test unique constraint for root and sub-node.
 void testUniquenessWithDeletion()
          Test unique constraint together with deletion, special for temporal extension.
 void testUniqueWholeTreeConstraintOnMove()
          Test unique constraint for children.
 void testUpdateNode()
           
 void testUpdateRootWithWholeTreeUniqueConstraint()
           
 void testUpdateToSameNameWithUniqueConstraint()
           
 
Methods inherited from class fri.util.database.jpa.commons.AbstractJpaTest
beginTransaction, commitTransaction, getPersistenceUnitName, log, logAfterEnd, logBeforeEnd, logStart, rollbackTransaction, setUp
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame, format
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

UNIQUE_PROPERTY_NAMES

protected static final java.lang.String[][] UNIQUE_PROPERTY_NAMES
Unique constraint property names to test.


COPIED_NAME_PREFIX

protected static final java.lang.String COPIED_NAME_PREFIX
Constant prefix for testing node renames.

See Also:
Constant Field Values
Constructor Detail

AbstractTreeTest

public AbstractTreeTest()
Method Detail

outputTree

public static final void outputTree(TreeNode root,
                                    TreeDao dao)
Utility method.


outputTree

public static final void outputTree(java.util.List<? extends TreeNode> nodes)

testDatabaseToBeCaseSensitive

public void testDatabaseToBeCaseSensitive()
                                   throws java.lang.Exception
Especially for MySql, check that string-compares are case-sensitive!

Throws:
java.lang.Exception

testCreateTree

public void testCreateTree()
                    throws java.lang.Exception
Tests creation of a tree, including addChild().

Throws:
java.lang.Exception

assertTreeCacheable

protected void assertTreeCacheable(java.util.List<N> treeList)
Check depth-first order. To be overridden for other capabilities.


testIsRoot

public void testIsRoot()
                throws java.lang.Exception
Throws:
java.lang.Exception

testCreateAndRemoveRoot

public void testCreateAndRemoveRoot()
                             throws java.lang.Exception
Throws:
java.lang.Exception

testUpdateNode

public void testUpdateNode()
                    throws java.lang.Exception
Throws:
java.lang.Exception

testUpdateToSameNameWithUniqueConstraint

public void testUpdateToSameNameWithUniqueConstraint()
                                              throws java.lang.Exception
Throws:
java.lang.Exception

testUpdateRootWithWholeTreeUniqueConstraint

public void testUpdateRootWithWholeTreeUniqueConstraint()
                                                 throws java.lang.Exception
Throws:
java.lang.Exception

testNonUniqueRootsButWholeTreeUniqueNodes

public void testNonUniqueRootsButWholeTreeUniqueNodes()
                                               throws java.lang.Exception
Throws:
java.lang.Exception

testNonUniqueRootsButUniqueChildren

public void testNonUniqueRootsButUniqueChildren()
                                         throws java.lang.Exception
Throws:
java.lang.Exception

testGetRoots

public void testGetRoots()
                  throws java.lang.Exception
Tests reading all roots.

Throws:
java.lang.Exception

testLevel

public void testLevel()
               throws java.lang.Exception
Tests level of all tree nodes.

Throws:
java.lang.Exception

testParent

public void testParent()
                throws java.lang.Exception
Tests reading parent of all tree nodes.

Throws:
java.lang.Exception

testPath

public void testPath()
              throws java.lang.Exception
Tests reading parents of tree nodes.

Throws:
java.lang.Exception

testIsBelow

public void testIsBelow()
                 throws java.lang.Exception
Tests reading parent of all tree nodes.

Throws:
java.lang.Exception

testAddToTree

public void testAddToTree()
                   throws java.lang.Exception
Tests adding tree nodes.

Throws:
java.lang.Exception

testRemoveFromTree

public void testRemoveFromTree()
                        throws java.lang.Exception
Tests removing tree nodes.

Throws:
java.lang.Exception

testRemoveTree

public void testRemoveTree()
                    throws java.lang.Exception
Tests removing tree roots.

Throws:
java.lang.Exception

testRemoveAllTrees

public void testRemoveAllTrees()
                        throws java.lang.Exception
Tests removing all tree roots.

Throws:
java.lang.Exception

testMoveTree

public void testMoveTree()
                  throws java.lang.Exception
Tests moving subtrees in a tree.

Throws:
java.lang.Exception

testUniqueness

public void testUniqueness()
                    throws java.lang.Exception
Test unique constraint for root and sub-node.

Throws:
java.lang.Exception

testUniquenessWithDeletion

public void testUniquenessWithDeletion()
                                throws java.lang.Exception
Test unique constraint together with deletion, special for temporal extension.

Throws:
java.lang.Exception

testMoveTreeBelowItselfFails

public void testMoveTreeBelowItselfFails()
                                  throws java.lang.Exception
Test move a sub-tree below itself.

Throws:
java.lang.Exception

testMoveToOtherTree

public void testMoveToOtherTree()
                         throws java.lang.Exception
Test moving sub-trees to other roots.

Throws:
java.lang.Exception

testMoveRootToOtherTree

public void testMoveRootToOtherTree()
                             throws java.lang.Exception
Test moving sub-trees to other roots.

Throws:
java.lang.Exception

testMoveTreeToBeRoot

public void testMoveTreeToBeRoot()
                          throws java.lang.Exception
Test moving tree to be roots.

Throws:
java.lang.Exception

testCopyTreeFailsWithUniqueConstraint

public void testCopyTreeFailsWithUniqueConstraint()
                                           throws java.lang.Exception
Test copy a sub-tree. Unique constraint on database level will prevent this.

Throws:
java.lang.Exception

testCopyTree

public void testCopyTree()
                  throws java.lang.Exception
Test copy a sub-tree without unique constraint on database level.

Throws:
java.lang.Exception

testCopyTreeWithPrecedingDelete

public void testCopyTreeWithPrecedingDelete()
                                     throws java.lang.Exception
Test copy a sub-tree without unique constraint on database level.

Throws:
java.lang.Exception

testCopyTreeToAlteredName

public void testCopyTreeToAlteredName()
                               throws java.lang.Exception
Test copy a sub-tree to another name.

Throws:
java.lang.Exception

testCopyTreeToAlteredNameWithUniqueConstraint

public void testCopyTreeToAlteredNameWithUniqueConstraint()
                                                   throws java.lang.Exception
Test copy a sub-tree to another name with unique constraint turned on.

Throws:
java.lang.Exception

testCopyTreeBelowItselfFails

public void testCopyTreeBelowItselfFails()
                                  throws java.lang.Exception
Test copy a sub-tree below itself.

Throws:
java.lang.Exception

testCopyToSelf

public void testCopyToSelf()
                    throws java.lang.Exception
Test copy a sub-tree to itself, which is cloning a tree.

Throws:
java.lang.Exception

testCopyToOtherRoot

public void testCopyToOtherRoot()
                         throws java.lang.Exception
Test copy a sub-tree to another root.

Throws:
java.lang.Exception

testCopyTreeToBeRoot

public void testCopyTreeToBeRoot()
                          throws java.lang.Exception
Test copying trees to be roots.

Throws:
java.lang.Exception

testCopyRootToBeRoot

public void testCopyRootToBeRoot()
                          throws java.lang.Exception
Test copying root to be another root.

Throws:
java.lang.Exception

testCopiedNodeRenamer

public void testCopiedNodeRenamer()
                           throws java.lang.Exception
Test renaming nodes while copying.

Throws:
java.lang.Exception

testFindChildrenInCachedTree

public void testFindChildrenInCachedTree()
                                  throws java.lang.Exception
Demonstrates how to retrieve the tree structure from a list of all nodes under a root.

Throws:
java.lang.Exception

testUniqueWholeTreeConstraintOnMove

public void testUniqueWholeTreeConstraintOnMove()
                                         throws java.lang.Exception
Test unique constraint for children.

Throws:
java.lang.Exception

testUniqueChildrenConstraint

public void testUniqueChildrenConstraint()
                                  throws java.lang.Exception
Test unique constraint for children.

Throws:
java.lang.Exception

testBigTree

public void testBigTree()
                 throws UniqueConstraintViolationException
Create a big tree.

Throws:
UniqueConstraintViolationException

newUniqueWholeTreeConstraintImpl

protected abstract UniqueTreeConstraint<N> newUniqueWholeTreeConstraintImpl()
Returns:
an UniqueTreeConstraint implementation that checks for uniqueness per tree.

newUniqueWholeTreeConstraintImplWithoutRoots

protected abstract UniqueTreeConstraint<N> newUniqueWholeTreeConstraintImplWithoutRoots()
Returns:
an UniqueTreeConstraint implementation that checks for uniqueness per tree.

newUniqueChildrenTreeConstraintImpl

protected abstract UniqueTreeConstraint<N> newUniqueChildrenTreeConstraintImpl()
Returns:
an UniqueTreeConstraint implementation that checks for uniqueness on child level only.

newUniqueChildrenTreeConstraintImplWithoutRoots

protected abstract UniqueTreeConstraint<N> newUniqueChildrenTreeConstraintImplWithoutRoots()
Returns:
an UniqueTreeConstraint implementation that checks for uniqueness on child level only, ignoring uniqueness among roots.

tearDown

protected void tearDown()
                 throws java.lang.Exception
Overridden to clean-up test table(s).

Overrides:
tearDown in class AbstractJpaTest
Throws:
java.lang.Exception

newDao

protected abstract D newDao(DbSession session)
Returns:
a new DAO for passed session. To be overridden by subclasses.

getDao

protected final D getDao()
Returns:
the current session DAO.

newTreePojo

protected abstract N newTreePojo(java.lang.String name)
Factory method for new NestedSetsTreePojos. To be overridden by subclasses.


getName

protected abstract java.lang.String getName(N node)
For a concrete POJO we must return properties from it. Here the cast to NestedSetsTreePojo is done.


setName

protected final void setName(N entity,
                             N root,
                             java.lang.String name)
                      throws UniqueConstraintViolationException
Name is unique, so a check is done here explicitly before calling entity.setName().

Throws:
UniqueConstraintViolationException

setNameNotConstraintChecking

protected abstract void setNameNotConstraintChecking(N entity,
                                                     java.lang.String name)

checkTreeIntegrity

protected abstract void checkTreeIntegrity(DbSession session,
                                           N root)

getFullTreeForIntegrityCheck

protected java.util.List<N> getFullTreeForIntegrityCheck(N root)

isTestCopy

protected final boolean isTestCopy()

renameBeforeCopy

protected abstract void renameBeforeCopy(N node)

beginDbTransaction

protected final DbSession beginDbTransaction(java.lang.String message)

newDbSession

protected DbSession newDbSession(java.lang.String message)

commitDbTransaction

protected void commitDbTransaction(java.lang.String message)

rollbackDbTransaction

protected void rollbackDbTransaction(java.lang.String message)

findByName

protected final N findByName(N root,
                             java.lang.String name)

createTree

protected final java.io.Serializable createTree()
                                         throws UniqueConstraintViolationException
Throws:
UniqueConstraintViolationException

createTree

protected final java.io.Serializable createTree(java.lang.String rootName)
                                         throws UniqueConstraintViolationException
Throws:
UniqueConstraintViolationException

createTree

protected final java.io.Serializable createTree(java.lang.String rootName,
                                                boolean useLowerCaseNames)
                                         throws UniqueConstraintViolationException
Throws:
UniqueConstraintViolationException

setValid

protected void setValid(N entity)
To be overridden by temporal test.