Class CloneableTester


  • public final class CloneableTester
    extends AbstractTester
    This tester checks class which implements Clonable and has therefore the clone method implemented. According Object.clone() the following conditions should be true:
     x.clone() != x
     
    will be true, and that the expression:
     x.clone().getClass() == x.getClass()
     
    will be true, but these are not absolute requirements. While it is typically the case that:
     x.clone().equals(x)
     
    will be true, this is not an absolute requirement.

    So the equals method will be only checked if it is overwritten.

    NOTE: In the future this class will be perhaps part of the ObjectTester class.

    Before v1.1 the methods are named "checkCloning". Since 1.1 these methods will have now an "assert" prefix ("assertCloning").

    Since:
    1.0.2 (06.08.2010)
    Author:
    oliver
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertCloning​(java.lang.Class<? extends java.lang.Cloneable> clazz)
      Check cloning.
      static void assertCloning​(java.lang.Cloneable orig)
      We call the clone method of the given orig paramter.Because the clone method is normally "protected" we use reflection to call it.
      static void assertCloning​(java.lang.Package pkg)
      Check for each class in the given package if it can be cloned correct.
      static void assertCloning​(java.lang.Package pkg, java.util.regex.Pattern... excluded)
      Check for each class in the given package if it can be cloned correct.
      static void assertCloning​(java.util.Collection<java.lang.Class<? extends java.lang.Cloneable>> classes)
      Check for each class in the given collection if it can be cloned correct.
      static void assertCloningOfPackage​(java.lang.String packageName)
      Check for each class in the given package if it can be cloned correct.
      static void assertCloningOfPackage​(java.lang.String packageName, java.lang.Class<? extends java.lang.Cloneable>... excluded)
      Check for each class in the given package if the clone method is implemented correct.
      static void assertCloningOfPackage​(java.lang.String packageName, java.util.regex.Pattern... excluded)
      Check for each class in the given package if the clone method is implemented correct.
      static java.lang.Cloneable getCloneOf​(java.lang.Cloneable orig)
      Gets the clone of the given Cloneable object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • assertCloning

        public static void assertCloning​(java.lang.Class<? extends java.lang.Cloneable> clazz)
        Check cloning.
        Parameters:
        clazz - the clazz
        Since:
        1.1
      • assertCloning

        public static void assertCloning​(java.lang.Cloneable orig)
        We call the clone method of the given orig paramter.Because the clone method is normally "protected" we use reflection to call it. Then we compare the orig and cloned object which should be equals.
        Parameters:
        orig - the original object
        Since:
        1.1
      • getCloneOf

        public static java.lang.Cloneable getCloneOf​(java.lang.Cloneable orig)
                                              throws java.lang.AssertionError
        Gets the clone of the given Cloneable object.
        Parameters:
        orig - the orig
        Returns:
        the clone of
        Throws:
        java.lang.AssertionError - the assertion error
      • assertCloning

        public static void assertCloning​(java.util.Collection<java.lang.Class<? extends java.lang.Cloneable>> classes)
        Check for each class in the given collection if it can be cloned correct.
        Parameters:
        classes - a collection of classes to be checked
        Since:
        1.1
      • assertCloning

        public static void assertCloning​(java.lang.Package pkg)
        Check for each class in the given package if it can be cloned correct.

        To get a name of a package call Package.getPackage(String). But be sure that you can't get null as result. In this case use assertCloningOfPackage(String).

        Parameters:
        pkg - the package e.g. "patterntesting.runtime"
        Since:
        1.1
        See Also:
        assertCloningOfPackage(String)
      • assertCloning

        public static void assertCloning​(java.lang.Package pkg,
                                         java.util.regex.Pattern... excluded)
        Check for each class in the given package if it can be cloned correct.

        To get a name of a package call Package.getPackage(String). But be sure that you can't get null as result. In this case use assertCloningOfPackage(String, Pattern...).

        Parameters:
        pkg - the package
        excluded - class pattern which should be excluded from the check
        Since:
        1.6
      • assertCloningOfPackage

        public static void assertCloningOfPackage​(java.lang.String packageName)
        Check for each class in the given package if it can be cloned correct.
        Parameters:
        packageName - the package name e.g. "patterntesting.runtime"
        Since:
        1.1
      • assertCloningOfPackage

        public static void assertCloningOfPackage​(java.lang.String packageName,
                                                  java.lang.Class<? extends java.lang.Cloneable>... excluded)
        Check for each class in the given package if the clone method is implemented correct.
        Parameters:
        packageName - the package name e.g. "patterntesting.runtime"
        excluded - classes which should be excluded from the check
        Since:
        1.1
        See Also:
        assertCloningOfPackage(String)
      • assertCloningOfPackage

        public static void assertCloningOfPackage​(java.lang.String packageName,
                                                  java.util.regex.Pattern... excluded)
        Check for each class in the given package if the clone method is implemented correct.
        Parameters:
        packageName - the package name e.g. "patterntesting.runtime"
        excluded - pattern for classes which should be excluded from the check
        Since:
        1.6
        See Also:
        assertCloningOfPackage(String)