You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jackrabbit.apache.org by Jukka Zitting <ju...@gmail.com> on 2006/03/22 11:24:34 UTC

Simple unit test template

Hi,

Quite a few of the bug reports we receive could quite easily be
expressed as unit tests. It would be great if we could provide a
simple unit test template class that users could fill in and attach to
the bug report. This would make verifying the bug reports easier and
also provide a ready-made regression test case to be included in the
Jackrabbit test suite.

The requirements for such a template class would be:

1) It should depend only on JUnit, JCR, and perhaps some helper class
available in the Jackrabbit jar so that you could compile and run it
by just adding JUnit to the Jackrabbit classpath without having to
checkout and compile the Jackrabbit sources

2) It should require minimal configuration. Something like the
TransientRepository default constructor could be used to automatically
set up a test repository.

3) It should be short and simple, preferably less than 50 lines
including documentation.

4) It should compile with "javac ExampleTest.java" and run with "java
junit.textui.TestRunner ExampleTest" with the classpath from
requirement 1

The RepositoryHelper and AbstractJCRTest classes in the TCK could be a
good starting point, but they are somewhat complex and don't meet
requirement 1 at least at the moment. Another alternative based on
TransientFactory and some default configuration (TODO) is shown below.
Comments?

    import javax.jcr.*; // Import all main JCR interfaces to simplify
test writing
    import junit.framework.Test;
    import junit.framework.TestCase;
    import junit.framework.TestSuite;

    /**
     * Template test case for Jackrabbit. Use this template to write test cases
     * to accompany Jackrabbit bug reports.
     */
    public class ExampleTest extends TestCase {

        public void testSomething() throws Exception {
            // TODO: Your test case here. (Hint: use the session variable.)
        }

        /**
         * Read-write session for accessing the test repository.
         */
        private Session session;

        /**
         * Starts a test fixture by acquiring a new read-write session
         * on the test repository.
         *
         * @throws Exception if an error occurs
         */
        protected void setUp() throws Exception {
            RepositoryConfig config = ...; // TODO: Test config with
defaultUserId
            session = new TransientRepository(config).login();
        }

        /**
         * Ends a test fixture by closing the test session.
         *
         * @throws Exception if an error occurs
         */
        protected void tearDown() throws Exception {
            session.logout();
        }

        /**
         * Returns a test suite that contains all the tests in this class.
         *
         * @return test suite
         */
        public static Test suite() {
            return new TestSuite(ExampleTest.class);
        }

    }


BR,

Jukka Zitting

--
Yukatan - http://yukatan.fi/ - info@yukatan.fi
Software craftsmanship, JCR consulting, and Java development

Re: Simple unit test template

Posted by Jukka Zitting <ju...@gmail.com>.
Hi,

Another shot at this same issue. I'd like to add a
org.apache.jackrabbit.core.TestRepository helper class that could be
used to solve the standalone vs. integrated test case problem without
exposing any details to the
test case writer. The TestRepository class would contain a static
repository instance with configuration that gives a session with full
read-write access from the default Repository.login() method. Unless
this instance is explicitly set by the main test suite then it a
default test repository is automatically created when it is first
requested.

Such a helper class could simplify the unit test template to:

    import javax.jcr.*;
    import junit.framework.Test;
    import junit.framework.TestCase;
    import junit.framework.TestSuite;
    import org.apache.jackrabbit.core.TestRepository;

    /**
     * Template test case for Jackrabbit. Use this template to write test cases
     * to accompany Jackrabbit bug reports.
     */
    public class ExampleTest extends TestCase {

        public void testSomething() throws Exception {
            Session session = TestRepository.getInstance().login();
            try {
                // TODO: Add test code here.
            } finally {
                session.logout();
            }
        }

         /**
          * Returns a test suite that contains all the tests in this class.
          *
          * @return test suite
          */
         public static Test suite() {
             return new TestSuite(ExampleTest.class);
         }

     }

I'll file an issue with a simple implementation of the TestRepository
class. Unless anyone objects I'd like to include also this class in
1.0-rc3 so that we can instruct people to use such a unit test
template with any Jackrabbit 1.x version.

BR,

Jukka Zitting

--
Yukatan - http://yukatan.fi/ - info@yukatan.fi
Software craftsmanship, JCR consulting, and Java development

Re: Simple unit test template

Posted by Jukka Zitting <ju...@gmail.com>.
Hi,

On 3/22/06, Jukka Zitting <ju...@gmail.com> wrote:
> TransientFactory and some default configuration (TODO) is shown below.

s/TransientFactory/TransientRepository/

The main problem I see with this initial approach is that it wouldn't
integrate with the main Jackrabbit test suite. Good ideas for
achieving both standalone and integrated functionality with minimal
setup? Perhaps a helper class to be included in the Jackrabbit jar?

BR,

Jukka Zitting

--
Yukatan - http://yukatan.fi/ - info@yukatan.fi
Software craftsmanship, JCR consulting, and Java development