Question

Is there a way that I can configure custom node types for Apache Jackrabbit to be registered when a new repository is instantiated?

I am automating my build using Apache Maven and have some unit tests to run with JUnit and integration tests to run with Jetty and want to be able to easily set-up and tear-down a test repository.

Was it helpful?

Solution

If you are able to upgrade to the newly-released Jackrabbit 2.0.0, you can programmatically create and register nodetypes. The main hook is the JSR-283 NodeTypeManager which doubles as a factory for new NodeTypes, and a place for them to be registered. Just register them in the setup method of your JUnit tests, and you should be good to go.

OTHER TIPS

I suggest that you define your nodetypes using a CND file and configure your JUnit test cases to register them for you, as in this example. I would say the most appropriate way to do so is to define an abstract test case which performs this configuration.

Also notice that the node types are associated to a workspace, and not to the whole repository.

According to NodeTypeManager, this is a code sample to create node type:

    import javax.jcr.PropertyType;
    import javax.jcr.Session;
    import javax.jcr.nodetype.NodeType;
    import javax.jcr.nodetype.NodeTypeManager;
    import javax.jcr.nodetype.NodeTypeTemplate;
    import javax.jcr.nodetype.PropertyDefinitionTemplate;

    /**
     *
     * @author Aroniaina
     */
    public class FileType {
        public static void RegisterFileType(Session session) throws Exception {        
            NodeTypeManager nodeTypeManager = session.getWorkspace().getNodeTypeManager();

            NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
            nodeType.setName("FileType");
            String[] str = {"nt:resource"};        
            nodeType.setDeclaredSuperTypeNames(str);
            nodeType.setMixin(false);
            nodeType.setQueryable(true);


            PropertyDefinitionTemplate path = nodeTypeManager.createPropertyDefinitionTemplate();
            path.setName("jcr:path");
            path.setRequiredType(PropertyType.PATH);
            path.setQueryOrderable(false);
            path.setFullTextSearchable(false);
            nodeType.getPropertyDefinitionTemplates().add(path);

            PropertyDefinitionTemplate nom = nodeTypeManager.createPropertyDefinitionTemplate();
            nom.setName("jcr:nom");
            nom.setRequiredType(PropertyType.STRING);
            nom.setQueryOrderable(true);
            nom.setFullTextSearchable(true);
            nodeType.getPropertyDefinitionTemplates().add(nom);

            PropertyDefinitionTemplate description = nodeTypeManager.createPropertyDefinitionTemplate();
            description.setName("jcr:description");
            description.setRequiredType(PropertyType.STRING);
            description.setQueryOrderable(true);
            description.setFullTextSearchable(true);
            nodeType.getPropertyDefinitionTemplates().add(description);

            PropertyDefinitionTemplate motsCles = nodeTypeManager.createPropertyDefinitionTemplate();
            motsCles.setName("jcr:motsCles");
            motsCles.setRequiredType(PropertyType.STRING);
            motsCles.setQueryOrderable(true);
            motsCles.setFullTextSearchable(true);
            nodeType.getPropertyDefinitionTemplates().add(motsCles);

            PropertyDefinitionTemplate size = nodeTypeManager.createPropertyDefinitionTemplate();
            size.setName("jcr:size");
            size.setRequiredType(PropertyType.STRING);
            size.setQueryOrderable(true);
            size.setFullTextSearchable(false);
            nodeType.getPropertyDefinitionTemplates().add(size);

            PropertyDefinitionTemplate users = nodeTypeManager.createPropertyDefinitionTemplate();
            users.setName("jcr:users");
            users.setRequiredType(PropertyType.STRING);
            users.setQueryOrderable(true);
            users.setFullTextSearchable(false);
            nodeType.getPropertyDefinitionTemplates().add(users);

            PropertyDefinitionTemplate groupe = nodeTypeManager.createPropertyDefinitionTemplate();
            groupe.setName("jcr:groupe");
            groupe.setRequiredType(PropertyType.STRING);
            groupe.setQueryOrderable(true);
            groupe.setFullTextSearchable(false);
            nodeType.getPropertyDefinitionTemplates().add(groupe);

            NodeType newnodetype = nodeTypeManager.registerNodeType(nodeType, true);             
            session.save();        
        }

    }

You can take a look at our code in Silverpeas. We are using Apache Jackrabbit with some unit tests. Currently this is a work in progress on my dev branch : https://github.com/ehsavoie/Silverpeas-Core/tree/feature_82 using in memory repository, loading CND files and testing with spring. Look for example at DocumentRepositoryTest

I'm not sure about nodetypes, but I've read that apache sling has a way to specify initial content when a package (osgi bundle) is installed. Sounds at least similar to what you have in mind.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top