Index: src/test/java/org/apache/jackrabbit/core/InconsistencyTest.java
===================================================================
--- src/test/java/org/apache/jackrabbit/core/InconsistencyTest.java	(revision 0)
+++ src/test/java/org/apache/jackrabbit/core/InconsistencyTest.java	(revision 0)
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.jcr.Node;
+import javax.jcr.ReferentialIntegrityException;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.SimpleCredentials;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.io.FileUtils;
+
+/**
+ * Test cases for JCR-2129. It expects the basedir system property to be set by the Surefire plugin. It
+ * creates a new repository for each test in the {@link #setUp()} method. The repository descriptor resource
+ * is repository-inconsistencytest.xml.
+ */
+public final class InconsistencyTest extends TestCase {
+
+    private static int counter = 0;
+
+    private String repoHome;
+
+    private String repoConfig;
+
+    private TransientRepository repository;
+
+    public InconsistencyTest() {
+        Logger.getLogger("org.apache.jackrabbit").setLevel(Level.WARNING);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setUp() throws Exception {
+        String targetDir = System.getProperty("basedir") + File.separator + "target";
+        String jackrabbitDir =
+            targetDir + File.separator + "inconsistencytest" + File.separator + "repository-" + counter;
+        counter++;
+        repoHome = jackrabbitDir + File.separator + "repository-home";
+        repoConfig = jackrabbitDir + File.separator + "repository.xml";
+        FileUtils.copyURLToFile(getClass().getClassLoader().getResource("repository-inconsistencytest.xml"),
+            new File(repoConfig));
+        startRepository();
+
+        Session session = createSession();
+        Node b = session.getRootNode().addNode("A").addNode("B");
+        b.addMixin("mix:referenceable");
+        session.getRootNode().addNode("C");
+        session.save();
+        session.logout();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void tearDown() {
+        stopRepository();
+    }
+
+    /**
+     * Parent-child inconsistency.
+     * 
+     * @throws Exception on test error
+     */
+    public void testConcurrentAddMove() throws Exception {
+        Session session2 = createSession();
+        session2.getRootNode().getNode("A").addNode("D");
+
+        Session session3 = createSession();
+        session3.move("/A/B", "/C/B");
+
+        session2.save();
+        session2.logout();
+        session3.save();
+        session3.logout();
+
+        Session session4 = createSession();
+        Node b = session4.getRootNode().getNode("C").getNode("B");
+        b.remove();
+        session4.save();
+        session4.logout();
+
+        // Restart the repository with a deleted search index.
+        stopRepository();
+        FileUtils.deleteDirectory(new File(repoHome + File.separator + "workspaces" + File.separator
+                + "default" + File.separator + "index"));
+        startRepository();
+        Session session = null;
+        try {
+            session = createSession();
+        } catch (RepositoryException e) {
+            fail("Failed to login to repository");
+        } finally {
+            if (session != null) {
+                session.logout();
+            }
+        }
+    }
+
+    /**
+     * References inconsistency.
+     * 
+     * @throws Exception on test error
+     */
+    public void testGhostReference() throws Exception {
+        Session session2 = createSession();
+        Node bses2 = session2.getRootNode().getNode("A").getNode("B");
+        session2.getRootNode().getNode("C").setProperty("ref to B", bses2);
+
+        Session session3 = createSession();
+        Node bses3 = session3.getRootNode().getNode("A").getNode("B");
+        session3.getRootNode().getNode("C").setProperty("ref to B", bses3);
+
+        session2.save();
+        session2.logout();
+        session3.save();
+        session3.logout();
+
+        Session session4 = createSession();
+        session4.getRootNode().getNode("C").remove();
+        session4.getRootNode().getNode("A").getNode("B").remove();
+        try {
+            session4.save();
+        } catch (ReferentialIntegrityException e) {
+            fail("Referential integrity exception due to ghost reference");
+        } finally {
+            session4.logout();
+        }
+    }
+
+    /**
+     * Starts the repository.
+     * 
+     * @throws IOException on error
+     */
+    private void startRepository() throws IOException {
+        repository = new TransientRepository(repoConfig, repoHome);
+    }
+
+    /**
+     * Stops the repository.
+     */
+    private void stopRepository() {
+        if (repository != null) {
+            repository.shutdown();
+        }
+    }
+
+    /**
+     * @return a super user session
+     * @throws RepositoryException on error
+     */
+    private Session createSession() throws RepositoryException {
+        return repository.login(new SimpleCredentials("test", "pwd".toCharArray()));
+    }
+}

Property changes on: src\test\java\org\apache\jackrabbit\core\InconsistencyTest.java
___________________________________________________________________
Name: svn:keywords
   + LastChangedBy LastChangedDate LastChangedRevision HeadURL
Name: svn:eol-style
   + native

Index: src/test/resources/repository-inconsistencytest.xml
===================================================================
--- src/test/resources/repository-inconsistencytest.xml	(revision 0)
+++ src/test/resources/repository-inconsistencytest.xml	(revision 0)
@@ -0,0 +1,53 @@
+<?xml version="1.0"?>
+<!DOCTYPE Repository PUBLIC "-//The Apache Software Foundation//DTD Jackrabbit 1.6//EN"
+                            "http://jackrabbit.apache.org/dtd/repository-1.6.dtd">
+<Repository>
+
+    <FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem">
+        <param name="path" value="${rep.home}/repository"/>
+    </FileSystem>
+
+    <Security appName="Jackrabbit">
+        <AccessManager class="org.apache.jackrabbit.core.security.SimpleAccessManager" />
+        <LoginModule class="org.apache.jackrabbit.core.security.SimpleLoginModule">
+            <param name="anonymousId" value="anonymous" />
+        </LoginModule>
+    </Security>
+
+    <Workspaces rootPath="${rep.home}/workspaces" defaultWorkspace="default" />
+
+    <Workspace name="wm9">
+
+        <FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem">
+            <param name="path" value="${wsp.home}"/>
+        </FileSystem>
+
+        <PersistenceManager class="org.apache.jackrabbit.core.persistence.bundle.DerbyPersistenceManager">
+          <param name="url" value="jdbc:derby:${wsp.home}/db;create=true"/>
+          <param name="schemaObjectPrefix" value="${wsp.name}_"/>
+        </PersistenceManager>
+
+        <SearchIndex class="org.apache.jackrabbit.core.query.lucene.SearchIndex">
+            <param name="path" value="${wsp.home}/index"/>
+        </SearchIndex>
+
+    </Workspace>
+
+    <Versioning rootPath="${rep.home}/version">
+
+        <FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem">
+            <param name="path" value="${rep.home}/version" />
+        </FileSystem>
+
+        <PersistenceManager class="org.apache.jackrabbit.core.persistence.bundle.DerbyPersistenceManager">
+          <param name="url" value="jdbc:derby:${rep.home}/version/db;create=true"/>
+          <param name="schemaObjectPrefix" value="version_"/>
+        </PersistenceManager>
+
+    </Versioning>
+
+    <SearchIndex class="org.apache.jackrabbit.core.query.lucene.SearchIndex">
+        <param name="path" value="${rep.home}/repository/index"/>
+    </SearchIndex>
+
+</Repository>
\ No newline at end of file

Property changes on: src\test\resources\repository-inconsistencytest.xml
___________________________________________________________________
Name: svn:eol-style
   + native

