kwin commented on code in PR #328: URL: https://github.com/apache/jackrabbit-filevault/pull/328#discussion_r1463074893
########## vault-core-it/vault-core-integration-tests/src/main/java/org/apache/jackrabbit/vault/packaging/integration/ReferenceableIdentifiersImportIT.java: ########## @@ -401,6 +422,138 @@ public void testImportDupPolicyLegacy() throws RepositoryException, IOException, assertNull(duplicateNode); } + @Test + public void testInstallPackage_CREATE_NEW_ID() throws Exception { + test(IdConflictPolicy.CREATE_NEW_ID, null, null, true, true); + } + + @Test + public void testInstallPackage_FAIL() throws Exception { + test(IdConflictPolicy.FAIL, RepositoryException.class, null, false, false); + } + + @Test + public void testInstallPackage_FORCE_REMOVE_CONFLICTING_ID() throws Exception { + test(IdConflictPolicy.FORCE_REMOVE_CONFLICTING_ID, null, null, false, false); + } + + @Test + public void testInstallPackage_LEGACY() throws Exception { + test(IdConflictPolicy.LEGACY, RepositoryException.class, IllegalStateException.class, false, false); + } + + private void test(IdConflictPolicy policy, Class<?> expectedException, Class<?> expectedRootCause, boolean expectNewId, + boolean expectRenamedNodeKept) throws Exception { + + String TEST_ROOT = "testroot"; + + Node testRoot = getNodeOrNull("/" + TEST_ROOT); + if (testRoot == null) { + testRoot = admin.getRootNode().addNode(TEST_ROOT); + } + + String srcName = String.format("%s-%x.txt", policy, System.nanoTime()); + String srcPath = PathUtil.append(testRoot.getPath(), srcName); + + Node asset = testRoot.addNode(srcName, NodeType.NT_FOLDER); + addFileNode(asset, "binary.txt"); + + asset.addMixin(NodeType.MIX_REFERENCEABLE); + admin.save(); + + String id1 = asset.getIdentifier(); + + File pkgFile = exportContentPackage(srcPath); + + String dstPath = srcPath + "-renamed"; + admin.move(srcPath, dstPath); + assertNodeMissing(srcPath); + + try { + installContentPackage(pkgFile, policy); + } catch (Exception ex) { + if (expectedException == null) { + throw ex; + } else { + assertTrue("expected: " + expectedException + ", but got: " + ex.getClass(), expectedException.isInstance(ex)); + if (expectedRootCause != null) { + Throwable rc = ExceptionUtils.getRootCause(ex); + assertTrue("expected: " + expectedRootCause + ", but got: " + rc.getClass(), expectedRootCause.isInstance(rc)); + } + // expected exception -> test done + return; + } + } + + if (expectRenamedNodeKept) { + assertNodeExists(dstPath); + } else { + assertNodeMissing(dstPath); + } + + assertNodeExists(srcPath); + assertNodeExists(srcPath + "/binary.txt"); + + Node asset2 = testRoot.getNode(srcName); + String id2 = asset2.getIdentifier(); + if (expectNewId) { + assertNotEquals(id1, id2); + } else { + assertEquals(id1, id2); + } + } + + private Node addFileNode(Node parent, String name) throws Exception { Review Comment: why not using https://jackrabbit.apache.org/api/2.20/org/apache/jackrabbit/commons/JcrUtils.html#putFile-javax.jcr.Node-java.lang.String-java.lang.String-java.io.InputStream-? ########## vault-core-it/vault-core-integration-tests/src/main/java/org/apache/jackrabbit/vault/packaging/integration/ReferenceableIdentifiersImportIT.java: ########## @@ -401,6 +422,138 @@ public void testImportDupPolicyLegacy() throws RepositoryException, IOException, assertNull(duplicateNode); } + @Test + public void testInstallPackage_CREATE_NEW_ID() throws Exception { + test(IdConflictPolicy.CREATE_NEW_ID, null, null, true, true); + } + + @Test + public void testInstallPackage_FAIL() throws Exception { + test(IdConflictPolicy.FAIL, RepositoryException.class, null, false, false); + } + + @Test + public void testInstallPackage_FORCE_REMOVE_CONFLICTING_ID() throws Exception { + test(IdConflictPolicy.FORCE_REMOVE_CONFLICTING_ID, null, null, false, false); + } + + @Test + public void testInstallPackage_LEGACY() throws Exception { + test(IdConflictPolicy.LEGACY, RepositoryException.class, IllegalStateException.class, false, false); + } + + private void test(IdConflictPolicy policy, Class<?> expectedException, Class<?> expectedRootCause, boolean expectNewId, + boolean expectRenamedNodeKept) throws Exception { + + String TEST_ROOT = "testroot"; + + Node testRoot = getNodeOrNull("/" + TEST_ROOT); + if (testRoot == null) { + testRoot = admin.getRootNode().addNode(TEST_ROOT); + } + + String srcName = String.format("%s-%x.txt", policy, System.nanoTime()); + String srcPath = PathUtil.append(testRoot.getPath(), srcName); + + Node asset = testRoot.addNode(srcName, NodeType.NT_FOLDER); + addFileNode(asset, "binary.txt"); + + asset.addMixin(NodeType.MIX_REFERENCEABLE); + admin.save(); + + String id1 = asset.getIdentifier(); + + File pkgFile = exportContentPackage(srcPath); + + String dstPath = srcPath + "-renamed"; + admin.move(srcPath, dstPath); + assertNodeMissing(srcPath); + + try { + installContentPackage(pkgFile, policy); + } catch (Exception ex) { + if (expectedException == null) { + throw ex; + } else { + assertTrue("expected: " + expectedException + ", but got: " + ex.getClass(), expectedException.isInstance(ex)); + if (expectedRootCause != null) { + Throwable rc = ExceptionUtils.getRootCause(ex); + assertTrue("expected: " + expectedRootCause + ", but got: " + rc.getClass(), expectedRootCause.isInstance(rc)); + } + // expected exception -> test done + return; + } + } + + if (expectRenamedNodeKept) { + assertNodeExists(dstPath); + } else { + assertNodeMissing(dstPath); + } + + assertNodeExists(srcPath); + assertNodeExists(srcPath + "/binary.txt"); + + Node asset2 = testRoot.getNode(srcName); + String id2 = asset2.getIdentifier(); + if (expectNewId) { + assertNotEquals(id1, id2); + } else { + assertEquals(id1, id2); + } + } + + private Node addFileNode(Node parent, String name) throws Exception { + + ValueFactory valueFactory = parent.getSession().getValueFactory(); + Binary contentValue = valueFactory.createBinary(new ByteArrayInputStream("Hello, world!".getBytes())); + Node fileNode = parent.addNode(name, NodeType.NT_FILE); + Node resNode = fileNode.addNode("jcr:content", "nt:resource"); + resNode.setProperty("jcr:mimeType", "text/plain"); + resNode.setProperty("jcr:data", contentValue); + + return fileNode; + } + + private File exportContentPackage(String path) throws Exception { + ExportOptions opts = new ExportOptions(); + DefaultMetaInf inf = new DefaultMetaInf(); + DefaultWorkspaceFilter filter = new DefaultWorkspaceFilter(); + + PathFilterSet pfs = new PathFilterSet(path); + pfs.addInclude(new DefaultPathFilter(path + "/.*")); + filter.add(pfs); + + inf.setFilter(filter); + Properties props = new Properties(); + props.setProperty(PackageProperties.NAME_GROUP, "jackrabbit/test"); + props.setProperty(PackageProperties.NAME_NAME, "test-package"); + inf.setProperties(props); + + opts.setMetaInf(inf); + File pkgFile = File.createTempFile("testImportMovedResource", ".zip"); + try (VaultPackage pkg = packMgr.assemble(admin, opts, pkgFile)) { + return pkg.getFile(); + } + } + + private void installContentPackage(File pkgFile, IdConflictPolicy policy) Review Comment: This is contained in the parent class `IntegrationTestBase` already. ########## vault-core-it/vault-core-integration-tests/src/main/java/org/apache/jackrabbit/vault/packaging/integration/ReferenceableIdentifiersImportIT.java: ########## @@ -401,6 +422,138 @@ public void testImportDupPolicyLegacy() throws RepositoryException, IOException, assertNull(duplicateNode); } + @Test + public void testInstallPackage_CREATE_NEW_ID() throws Exception { + test(IdConflictPolicy.CREATE_NEW_ID, null, null, true, true); + } + + @Test + public void testInstallPackage_FAIL() throws Exception { + test(IdConflictPolicy.FAIL, RepositoryException.class, null, false, false); + } + + @Test + public void testInstallPackage_FORCE_REMOVE_CONFLICTING_ID() throws Exception { + test(IdConflictPolicy.FORCE_REMOVE_CONFLICTING_ID, null, null, false, false); + } + + @Test + public void testInstallPackage_LEGACY() throws Exception { + test(IdConflictPolicy.LEGACY, RepositoryException.class, IllegalStateException.class, false, false); + } + + private void test(IdConflictPolicy policy, Class<?> expectedException, Class<?> expectedRootCause, boolean expectNewId, Review Comment: can we rename to `assertSomething` and replace the `Something` by the thing which is actually asserted here? Only actual test methods being executed directly by JUnit should have `test` in the name. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@jackrabbit.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org