jackrabbit-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Kennedy <da...@us.ibm.com>
Subject JCR 423 scenario still broken
Date Mon, 08 May 2006 20:07:37 GMT
I finally got a chance to try out the fix for issue JCR 423 (
Node.restore() fails for existing non-versioned OPV=Version child nodes) 
and unfortunately it still does not work.  I now get an 
ItemExistsException:

Here is a testcase illustrating the problem.  Feel free to adapt it for a 
JUnit test (or I can depending on the nodetypes you'd like to use).  It 
shouldn't need anything, but you can provide a configuration and home 
definition in a test.properties file. 

David

/*
 *
 */
package test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.jcr.Credentials;
import javax.jcr.NamespaceException;
import javax.jcr.NamespaceRegistry;
import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.Workspace;
import javax.jcr.nodetype.NoSuchNodeTypeException;
import javax.jcr.nodetype.NodeType;
import javax.jcr.version.OnParentVersionAction;
import javax.jcr.version.Version;

import org.apache.jackrabbit.JcrConstants;
import org.apache.jackrabbit.core.TransientRepository;
import org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException;
import org.apache.jackrabbit.core.nodetype.NodeDefImpl;
import org.apache.jackrabbit.core.nodetype.NodeTypeDef;
import org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl;
import org.apache.jackrabbit.core.nodetype.NodeTypeRegistry;
import org.apache.jackrabbit.name.QName;


/**
 *
 */
public class VersionRestoreTest
{

  private static final String DEFAULT_REPOSITORY_CONFIG_PATH = 
"repository.xml";
 
  private static final String DEFAULT_REPOSITORY_HOME = ".";
 
  private static final String TEST_PROPERTIES = "test.properties";
 
  public static final String REPOSITORY_CONFIG_PATH_KEY = 
"repository.config.path";
 
  public static final String REPOSITORY_HOME_KEY = "repository.home";
 
  public static final String NS_PREFIX_SEPARATOR = ":";
  public static final String NS_JRTEST_PREFIX = "tst";
  public static final String NS_JRTEST_URI = "http://test/jr/1.0";
 
  public static final String FOO = "foo";
  public static final String BAR = "bar";
  public static final String CHILD = "child";
  public static final String TEST_TYPE = "testType";
  public static final String PREFIXED_CHILD = NS_JRTEST_PREFIX + 
NS_PREFIX_SEPARATOR + "child";
  public static final String PREFIXED_TEST_TYPE = NS_JRTEST_PREFIX + 
NS_PREFIX_SEPARATOR + "testType";
 

  private Properties properties;
 
  public static void main(String[] args){
    try {
        new VersionRestoreTest().run();
    } catch (IOException e){
      e.printStackTrace();
    }
 
  }
 
  protected VersionRestoreTest()throws IOException {
      properties = loadProperties();
  }
 
  protected void run(){
    Session session = null;
    try {
            Repository repository = getRepository();
            session = login(repository);
            executeTest(session);
    } catch (Exception e){
      e.printStackTrace();
    } finally {
            logout(session);
    }
 

  }
  protected Properties loadProperties() throws IOException {
    Properties props = null;
    InputStream stream = 
this.getClass().getResourceAsStream(TEST_PROPERTIES);
    if (stream != null){
      props = new Properties();
      props.load(stream);
    }
    return props;
  }
 
  protected Repository getRepository() throws RepositoryException {
    try {
      return new TransientRepository(getConfigPath(), getHome());
    } catch (IOException e){
      throw new RepositoryException(e.getMessage(), e);
    }
  }
  protected Session login(Repository repository) throws 
RepositoryException {
    Credentials credentials = new SimpleCredentials(getUserID(), 
getPassword().toCharArray());
    return repository.login(credentials);
  }
 
  protected void logout(Session session){
    if (session != null)
      session.logout();
  }
 
  protected String getConfigPath(){
    String cPath = DEFAULT_REPOSITORY_CONFIG_PATH;
    if (properties != null)
      cPath = properties.getProperty(REPOSITORY_CONFIG_PATH_KEY, 
DEFAULT_REPOSITORY_CONFIG_PATH);
 
    return cPath;
  }
 
  protected String getHome(){
    String home = DEFAULT_REPOSITORY_HOME;
    if (properties != null)
      home = properties.getProperty(REPOSITORY_HOME_KEY, 
DEFAULT_REPOSITORY_HOME);
 
    return home;
  }
 
  protected String getUserID(){
    return "userid";
  }
 
  protected String getPassword(){
    return "password";
  }
 
  protected void executeTest(Session session) throws RepositoryException {
    testCheckinRestore(session);
  }
 
  protected void testCheckinRestore(Session session) throws 
RepositoryException
  {
    registerNodeTypes(session);
    Node root = session.getRootNode();
    Node n = root.addNode(FOO, PREFIXED_TEST_TYPE);
    n.addNode(PREFIXED_CHILD);
    root.save();
    Version v = n.checkin();
    n.restore(v, true);
 
  }
 
  protected void registerNodeTypes(Session session) throws 
RepositoryException
  {
      Workspace workspace = session.getWorkspace();
      NodeTypeManagerImpl ntm = 
(NodeTypeManagerImpl)workspace.getNodeTypeManager();
      NodeTypeRegistry ntRegistry = ntm.getNodeTypeRegistry();
 
      NamespaceRegistry nsRegistry = workspace.getNamespaceRegistry();
      String testPrefix = null;
      try {
         testPrefix = nsRegistry.getPrefix(NS_JRTEST_URI);
      } catch (NamespaceException nse) {
        //namespace does not exist....continue
      }
      if (testPrefix == null)
          nsRegistry.registerNamespace(NS_JRTEST_PREFIX, NS_JRTEST_URI);
      else if (!testPrefix.equals(NS_JRTEST_PREFIX))
        throw new RepositoryException(NS_JRTEST_PREFIX + " is not 
registered with the expected URI");


      String nodeTypeName = FOO; 
      try
      {
        ntm.getNodeType(nodeTypeName);
      }
      catch (NoSuchNodeTypeException nsnte)
      {
        NodeType baseNodeType = ntm.getNodeType(JcrConstants.NT_BASE);
        NodeType referenceableNodeType = 
ntm.getNodeType(JcrConstants.MIX_REFERENCEABLE);
        NodeType versionableNodeType = 
ntm.getNodeType(JcrConstants.MIX_VERSIONABLE);
 
        NodeTypeDef nodeTypeDef = new NodeTypeDef();
        QName qualifiedNodeTypeName = new QName(NS_JRTEST_URI, TEST_TYPE);
        nodeTypeDef.setName(qualifiedNodeTypeName);
        nodeTypeDef.setSupertypes(new QName[] {QName.NT_BASE, 
QName.MIX_REFERENCEABLE, QName.MIX_VERSIONABLE});

        NodeDefImpl nodeDef = new NodeDefImpl();
        nodeDef.setName(new QName(NS_JRTEST_URI, CHILD));
        nodeDef.setDeclaringNodeType(qualifiedNodeTypeName);
        nodeDef.setAutoCreated(false);
        nodeDef.setDefaultPrimaryType(QName.NT_BASE);
        nodeDef.setAllowsSameNameSiblings(false);
        nodeDef.setOnParentVersion(OnParentVersionAction.VERSION);
        nodeDef.setProtected(false);
        nodeDef.setRequiredPrimaryTypes(new QName[]{QName.NT_BASE});

        nodeTypeDef.setChildNodeDefs(new NodeDefImpl[]{nodeDef});
 
        try {
          ntRegistry.registerNodeType(nodeTypeDef);
        } catch (InvalidNodeTypeDefException e){
          //Eating this for now, hoping the only exception is that it 
already exists....would be nice to be able to catch that specifically
          e.printStackTrace();
        }

      }
    }
}


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message