diff --git a/oak-upgrade/src/test/java/org/apache/jackrabbit/oak/upgrade/UniquenessConstraintViolatedTest.java b/oak-upgrade/src/test/java/org/apache/jackrabbit/oak/upgrade/UniquenessConstraintViolatedTest.java new file mode 100644 index 0000000..5ea8bf5 --- /dev/null +++ b/oak-upgrade/src/test/java/org/apache/jackrabbit/oak/upgrade/UniquenessConstraintViolatedTest.java @@ -0,0 +1,173 @@ +/* + * 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.oak.upgrade; + +import org.apache.jackrabbit.JcrConstants; +import org.apache.jackrabbit.commons.JcrUtils; +import org.apache.jackrabbit.core.RepositoryContext; +import org.apache.jackrabbit.core.RepositoryImpl; +import org.apache.jackrabbit.core.config.RepositoryConfig; +import org.apache.jackrabbit.oak.segment.SegmentNodeStoreBuilders; +import org.apache.jackrabbit.oak.segment.file.FileStore; +import org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException; +import org.apache.jackrabbit.oak.spi.lifecycle.RepositoryInitializer; +import org.apache.jackrabbit.oak.spi.state.NodeBuilder; +import org.apache.jackrabbit.oak.spi.state.NodeStore; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import javax.annotation.Nonnull; +import javax.jcr.Node; +import javax.jcr.RepositoryException; +import javax.jcr.Session; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; + +import static org.apache.jackrabbit.oak.segment.file.FileStoreBuilder.fileStoreBuilder; + +public class UniquenessConstraintViolatedTest extends AbstractRepositoryUpgradeTest { + + protected static boolean upgradeComplete; + private static FileStore fileStore; + + @Override + protected NodeStore createTargetNodeStore() { + return SegmentNodeStoreBuilders.builder(fileStore).build(); + } + + @BeforeClass + public static void initialize() { + final File dir = new File(getTestDirectory(), "segments"); + dir.mkdirs(); + try { + fileStore = fileStoreBuilder(dir).withMaxFileSize(128).build(); + upgradeComplete = false; + } catch (IOException e) { + throw new RuntimeException(e); + } catch (InvalidFileStoreVersionException e) { + throw new IllegalStateException(e); + } + } + + @AfterClass + public static void cleanup() { + fileStore.close(); + fileStore = null; + } + + @Override + protected void doUpgradeRepository(File source, NodeStore target) throws RepositoryException, IOException { + RepositoryUpgrade.copy(source, target); + } + + @Before + public synchronized void upgradeRepository() throws Exception { + if (!upgradeComplete) { + final File sourceDir = new File(getTestDirectory(), "jackrabbit2"); + + sourceDir.mkdirs(); + + RepositoryImpl source = createSourceRepository(sourceDir); + Session session = source.login(CREDENTIALS); + try { + createSourceContent(session); + } finally { + session.save(); + session.logout(); + source.shutdown(); + } + + final NodeStore target = getTargetNodeStore(); + doUpgradeRepository(sourceDir, target, false, null); + fileStore.flush(); + + // re-create source repo + source = createSourceRepository(sourceDir); + session = source.login(CREDENTIALS); + try { + modifySourceContent(session); + } finally { + session.save(); + session.logout(); + source.shutdown(); + } + + doUpgradeRepository(sourceDir, target, true, new String[]{"/content"}); + fileStore.flush(); + + upgradeComplete = true; + } + } + + protected void doUpgradeRepository(File source, NodeStore target, boolean skipInit, String[] mergePaths) throws RepositoryException, IOException { + final RepositoryConfig config = RepositoryConfig.create(source); + final RepositoryContext context = RepositoryContext.create(config); + try { + final RepositoryUpgrade repositoryUpgrade = new RepositoryUpgrade(context, target); + repositoryUpgrade.setSkipInitialization(skipInit); + if (mergePaths != null) repositoryUpgrade.setMerges(mergePaths); + repositoryUpgrade.copy(new RepositoryInitializer() { + @Override + public void initialize(@Nonnull NodeBuilder builder) { + + } + }); + } finally { + context.getRepository().shutdown(); + } + } + + @Override + protected void createSourceContent(Session session) throws RepositoryException { + + JcrUtils.getOrCreateByPath("/content/page1", "nt:unstructured", session); + JcrUtils.getOrCreateByPath("/content/page1/image.png", "nt:file", session); + Node data = JcrUtils.getOrCreateByPath("/content/page1/image.png/jcr:content", "nt:resource", session); + data.setProperty( + JcrConstants.JCR_DATA, + session.getValueFactory().createBinary(new ByteArrayInputStream("hello".getBytes()))); + + JcrUtils.getOrCreateByPath("/content/page2", "nt:unstructured", session); + + session.save(); + } + + protected void modifySourceContent(Session session) throws RepositoryException { + + session.move("/content/page1/image.png", "/content/page2/image.png"); + + session.save(); + } + + @Test + public void shouldReflectSourceAfterModifications() throws Exception { + + assertExisting( + "/", + "/content", + "/content/page1/image.png", + "/content/page2/image.png" + ); + + } + +}