From c111adbbb01c233881dc1cb1dd8059ee3287f89b Mon Sep 17 00:00:00 2001 From: lsitu Date: Fri, 22 Jul 2016 16:50:17 -0700 Subject: [PATCH] Added test to catch the SN-Sibling issue with the nt:file node. Move SNS assertion into the test block. --- .../test/java/org/modeshape/jcr/JcrToolsTest.java | 77 ++++++++++++++++++++++ .../org/modeshape/jcr/SingleUseAbstractTest.java | 3 + 2 files changed, 80 insertions(+) diff --git a/modeshape-jcr/src/test/java/org/modeshape/jcr/JcrToolsTest.java b/modeshape-jcr/src/test/java/org/modeshape/jcr/JcrToolsTest.java index 80d2193..fb19a80 100644 --- a/modeshape-jcr/src/test/java/org/modeshape/jcr/JcrToolsTest.java +++ b/modeshape-jcr/src/test/java/org/modeshape/jcr/JcrToolsTest.java @@ -20,6 +20,15 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + import javax.jcr.Node; import javax.jcr.PathNotFoundException; import javax.jcr.RepositoryException; @@ -32,6 +41,8 @@ import org.junit.Before; import org.junit.Test; import org.modeshape.common.collection.Problems; import org.modeshape.common.collection.SimpleProblems; +import org.modeshape.jcr.api.Binary; +import org.modeshape.jcr.api.ValueFactory; import org.modeshape.jcr.api.JcrTools; import org.modeshape.jcr.security.SimplePrincipal; @@ -323,6 +334,72 @@ public class JcrToolsTest extends SingleUseAbstractTest { assertThat(childNode.getName(), is("County")); } + @Test + public void testConcurrentSameNameSiblingBinaryContent() + throws RepositoryException, IOException, InterruptedException, ExecutionException { + + File testFile = File.createTempFile("Content", ".txt"); + testFile.deleteOnExit(); + try (final FileWriter fw = new FileWriter(testFile)) { + for (int i = 0; i < 1000000; i++) { + fw.write("fooooooooooooooooooooooooooooooooooooooooooooooooooo"); + } + } catch (final IOException e) { + throw new RuntimeException(e); + } + + // create two concurrent threads for test + final ExecutorService executor = Executors.newFixedThreadPool(2); + + Future firstTask = createIngestTask(executor, testFile, session); + Future proceedingTask = createIngestTask(executor, testFile, session1); + + Node node = firstTask.get(); + + assertTrue("Same Name Sibling nt:file type node created!", + node.getPath().indexOf("[2]") < 0); + + node = proceedingTask.get(); + + assertTrue("Same Name Sibling nt:file type node created!", + node.getPath().indexOf("[2]") < 0); + + executor.shutdown(); + } + + /** + * upload datastreams + */ + private Future createIngestTask(final ExecutorService executor, File testFile, JcrSession sess) { + + return executor.submit(() -> { + + final Node fileNode = tools.findOrCreateNode(sess, "object", DEF_TYPE, "nt:folder"); + + final Node dsNode = fileNode.addNode("ds", "nt:file"); + + final Node contentNode = tools.findOrCreateChild(dsNode, "jcr:content", "nt:resource"); + ValueFactory modevf = + (ValueFactory) sess.getValueFactory(); + + try (final InputStream in = new java.io.FileInputStream(testFile)) { + Binary binary = modevf.createBinary(in, null); + contentNode.setProperty("jcr:data", binary); + } catch (final IOException e) { + throw new RuntimeException(e); + } + + assertTrue("Same Name Sibling nt:file type node created before session save!", + contentNode.getPath().indexOf("[2]") < 0); + sess.save(); + + //assertTrue("Same Name Sibling nt:file type node created after session save!", + // contentNode.getPath().indexOf("[2]") < 0); + + return contentNode; + }); + } + private void setPolicy( String path, String... privileges ) throws Exception { AccessControlManager acm = session.getAccessControlManager(); diff --git a/modeshape-jcr/src/test/java/org/modeshape/jcr/SingleUseAbstractTest.java b/modeshape-jcr/src/test/java/org/modeshape/jcr/SingleUseAbstractTest.java index fbe3fec..7f9444a 100644 --- a/modeshape-jcr/src/test/java/org/modeshape/jcr/SingleUseAbstractTest.java +++ b/modeshape-jcr/src/test/java/org/modeshape/jcr/SingleUseAbstractTest.java @@ -64,6 +64,7 @@ public abstract class SingleUseAbstractTest extends AbstractJcrRepositoryTest { protected JcrRepository repository; protected JcrSession session; + protected JcrSession session1; protected JcrTools tools; protected void startRepository() throws Exception { @@ -72,12 +73,14 @@ public abstract class SingleUseAbstractTest extends AbstractJcrRepositoryTest { repository = new JcrRepository(repositoryConfiguration); repository.start(); session = repository.login(); + session1 = repository.login(); } protected void stopRepository() throws Exception { try { try { if (session != null && session.isLive()) session.logout(); + if (session1 != null && session1.isLive()) session1.logout(); } finally { TestingUtil.killRepositories(repository); } -- 1.9.5 (Apple Git-50.3)