Announcement Announcement Module
Collapse

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions (http://www.springsource.org/extensions) project, this forum has been decommissioned in favour of Spring Extensions one at:
http://forum.springsource.org/forumdisplay.php?f=44

Please see the Spring Extensions home page for a complete list of current projects in Java, .NET and ActionScript. You can also propose one if you want.

Cheers,
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
See more
See less
JCR: unit testing my application Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JCR: unit testing my application

    I've been working on using the springmodules-jsr module. I am looking at using it with an existing spring mvc web application and have a service layer that I want to use to access the JSR-170 repository. I essentially took the JcrService sample class in the download, created an interface for it, and wired it into the application with spring beans. I have everything wired up and, using a junit test can see that I can save an item in the repository. However, I have a simple service method as follows:

    public Node getRootNode() {
    return (Node) template.execute(new JcrCallback() {
    public Object doInJcr(Session session) throws RepositoryException {
    return session.getRootNode();
    }
    });
    }

    So I wrote a junit test for my JsrServiceImpl which test this method:

    I can get a Node from the method as follows:

    Node root = svc.getRootNode(); // svc is a JsrService object wired in as a bean

    The node is not null but when I try to do something as simple as root.getName() I get a RepositoryException with a message that the Session is closed. How can I test these service methods without getting the RepositoryException?

  • #2
    I've done some unit tests using JCR, but I believe the JcrTemplate automatically closes the sessions for you and that's your issue (see the quote below). I think if you're using the JcrTemplate you'll need to do all your work in the callback unless there is some way to tell it not to close the session.

    In the Spring Module docs it says: "JcrTemplate contains many of the operations defined in javax.jcr.Session and javax.jcr.query.Query classes plus some convenient ones; however there are cases when they are not enought. With JcrCallback, one can work directly with the Session, the callback begin thread-safe, opens/closes sessions and deals with exceptions:"

    I have some tests posted in the Spring Modules JCR Node Creation & Retrieval example. There's another longer test in JcrNodeCreationTest that verifies a content node exists too.

    Excerpt from JcrNodeCreationTest

    Code:
        @Test
        public void testAddNodeIfDoesntExist() {
            assertNotNull("JCR Template is null.", template);
    
            template.execute(new JcrCallback() {
                public Object doInJcr(Session session) throws RepositoryException {
                    Node root = session.getRootNode();
    
                    logger.debug("Starting from root node.  node={}", root);
    
                    Node node = null;
    
                    if (root.hasNode(nodeName)) {
                        node = root.getNode(nodeName);
    
                        logger.debug("Node exists.  node={}", node);
                    } else {
                        node = root.addNode(nodeName);
    
                        session.save();
    
                        logger.info("Saved node.  node={}", node);
                    }
    
                    assertNotNull("Node is null.", node);
    
                    return node;
                }
            });
        }
    Last edited by David Winterfeldt; Jun 9th, 2008, 02:38 PM.

    Comment

    Working...
    X