PendingActionCurator { void curateActionList(@NotNull ActionStore.PendingAction added, @NotNull List<ActionStore.PendingAction> actions) { final ListIterator<ActionStore.PendingAction> iter = actions.listIterator(actions.size()); while (iter.hasPrevious()) { final ActionStore.PendingAction existingAction = iter.previous(); PendingActionCurator.CurateResult result = curate(added, existingAction); added = result.replacedAdded(added); if (result.removeExisting) { iter.remove(); } else { iter.set(result.replacedExisting(existingAction)); } if (result.removeAdded) { return; } if (result.stopSearch) { break; } } actions.add(added); } PendingActionCurator(@NotNull PendingActionFactory actionFactory);   }
@Test void curateFirstAction() { PendingActionCurator.PendingActionFactory actionFactory = mock(PendingActionCurator.PendingActionFactory.class); PendingActionCurator curator = new PendingActionCurator(actionFactory); List<ActionStore.PendingAction> actions = new ArrayList<>(); CreateJobAction addedAction = new CreateJobAction(new P4JobImpl("j1", "j1", null)); ActionStore.PendingAction added = ActionStore.createPendingAction(REF_A, addedAction); curator.curateActionList(added, actions); assertContainsExactly(actions, added); }
@Test void curateFile_add_delete() { Map<String, MockVirtualFile> fs = MockVirtualFileSystem.createTree( "a.txt", "abc" ); MockVirtualFile f1 = fs.get("a.txt"); PendingActionCurator.PendingActionFactory actionFactory = mock(PendingActionCurator.PendingActionFactory.class); PendingActionCurator curator = new PendingActionCurator(actionFactory); List<ActionStore.PendingAction> actions = new ArrayList<>(); P4ChangelistIdImpl cl = new P4ChangelistIdImpl(100, REF_A1); ActionStore.PendingAction addFile = ActionStore.createPendingAction(REF_A1, new AddEditAction( f1.asFilePath(), null, cl, "UTF-8")); actions.add(addFile); ActionStore.PendingAction deleteFile = ActionStore.createPendingAction(REF_A1, new DeleteFileAction(f1.asFilePath(), cl)); curator.curateActionList(deleteFile, actions); assertContainsExactly(actions, addFile, deleteFile); }
@Test void curateFile_delete_delete_sameCl() { PendingActionCurator.PendingActionFactory actionFactory = mock(PendingActionCurator.PendingActionFactory.class); PendingActionCurator curator = new PendingActionCurator(actionFactory); List<ActionStore.PendingAction> actions = new ArrayList<>(); P4ChangelistIdImpl cl1 = new P4ChangelistIdImpl(100, REF_A1); Map<String, MockVirtualFile> fs = MockVirtualFileSystem.createTree( "a.txt", "abc" ); MockVirtualFile f1 = fs.get("a.txt"); ActionStore.PendingAction firstDelete = ActionStore.createPendingAction(REF_A1, new DeleteFileAction(f1.asFilePath(), cl1)); actions.add(firstDelete); ActionStore.PendingAction secondDelete = ActionStore.createPendingAction(REF_A1, new DeleteFileAction(f1.asFilePath(), cl1)); curator.curateActionList(secondDelete, actions); assertContainsExactly(actions, firstDelete); }
@Test void curateFile_delete_delete_diffCl() { PendingActionCurator.PendingActionFactory actionFactory = mock(PendingActionCurator.PendingActionFactory.class); PendingActionCurator curator = new PendingActionCurator(actionFactory); List<ActionStore.PendingAction> actions = new ArrayList<>(); P4ChangelistIdImpl cl1 = new P4ChangelistIdImpl(100, REF_A1); P4ChangelistIdImpl cl2 = new P4ChangelistIdImpl(101, REF_A1); Map<String, MockVirtualFile> fs = MockVirtualFileSystem.createTree( "a.txt", "abc" ); MockVirtualFile f1 = fs.get("a.txt"); ActionStore.PendingAction firstDelete = ActionStore.createPendingAction(REF_A1, new DeleteFileAction(f1.asFilePath(), cl1)); actions.add(firstDelete); ActionStore.PendingAction secondDelete = ActionStore.createPendingAction(REF_A1, new DeleteFileAction(f1.asFilePath(), cl2)); curator.curateActionList(secondDelete, actions); assertContainsExactly(actions, secondDelete); }
@Test void curateFile_move_deleteTgt() { SimplePendingActionFactory actionFactory = new SimplePendingActionFactory(REF_A1); PendingActionCurator curator = new PendingActionCurator(actionFactory); Map<String, MockVirtualFile> fs = MockVirtualFileSystem.createTree( "a.txt", "abc", "b.txt", "def" ); MockVirtualFile srcFile = fs.get("a.txt"); MockVirtualFile tgtFile = fs.get("b.txt"); List<ActionStore.PendingAction> actions = new ArrayList<>(); P4ChangelistIdImpl cl1 = new P4ChangelistIdImpl(100, REF_A1); P4ChangelistIdImpl cl2 = new P4ChangelistIdImpl(100, REF_A1); ActionStore.PendingAction moveFile = ActionStore.createPendingAction(REF_A1, new MoveFileAction(srcFile.asFilePath(), tgtFile.asFilePath(), cl1)); actions.add(moveFile); ActionStore.PendingAction deleteFile = ActionStore.createPendingAction(REF_A1, new DeleteFileAction(tgtFile.asFilePath(), cl2)); curator.curateActionList(deleteFile, actions); assertSize(1, actionFactory.created); ActionStore.PendingAction created = actionFactory.created.get(0); assertContainsExactly(actions, created); assertNull(created.serverAction); assertNotNull(created.clientAction); assertEquals(created.clientAction.getClass(), DeleteFileAction.class); DeleteFileAction createdDelete = (DeleteFileAction) created.clientAction; assertEquals(cl1, createdDelete.getChangelistId()); }
@Test void curateFile_delete_revert_add() { SimplePendingActionFactory actionFactory = new SimplePendingActionFactory(REF_A1); PendingActionCurator curator = new PendingActionCurator(actionFactory); Map<String, MockVirtualFile> fs = MockVirtualFileSystem.createTree( "a.txt", "abc" ); MockVirtualFile file = fs.get("a.txt"); List<ActionStore.PendingAction> actions = new ArrayList<>(); P4ChangelistIdImpl cl = new P4ChangelistIdImpl(100, REF_A1); ActionStore.PendingAction deleteFile = ActionStore.createPendingAction(REF_A1, new DeleteFileAction(file.asFilePath(), cl)); actions.add(deleteFile); ActionStore.PendingAction revertFile = ActionStore.createPendingAction(REF_A1, new RevertFileAction(file.asFilePath(), false)); actions.add(revertFile); ActionStore.PendingAction addFile = ActionStore.createPendingAction(REF_A1, new AddEditAction(file.asFilePath(), null, cl, "UTF-8")); curator.curateActionList(addFile, actions); assertContainsExactly(actions, deleteFile, revertFile, addFile); }
@Test void curateFile_delete_add_delete() { SimplePendingActionFactory actionFactory = new SimplePendingActionFactory(REF_A1); PendingActionCurator curator = new PendingActionCurator(actionFactory); Map<String, MockVirtualFile> fs = MockVirtualFileSystem.createTree( "a.txt", "abc" ); MockVirtualFile file = fs.get("a.txt"); List<ActionStore.PendingAction> actions = new ArrayList<>(); P4ChangelistIdImpl cl = new P4ChangelistIdImpl(100, REF_A1); ActionStore.PendingAction deleteFile = ActionStore.createPendingAction(REF_A1, new DeleteFileAction(file.asFilePath(), cl)); actions.add(deleteFile); ActionStore.PendingAction addFile = ActionStore.createPendingAction(REF_A1, new AddEditAction(file.asFilePath(), null, cl, "UTF-8")); actions.add(addFile); curator.curateActionList(deleteFile, actions); assertContainsExactly(actions, deleteFile, addFile, deleteFile); }
@Test void curateFile_addTgt_moveSrc_deleteSrc() { SimplePendingActionFactory actionFactory = new SimplePendingActionFactory(REF_A1); PendingActionCurator curator = new PendingActionCurator(actionFactory); Map<String, MockVirtualFile> fs = MockVirtualFileSystem.createTree( "a.txt", "abc", "b.txt", "def" ); MockVirtualFile src = fs.get("a.txt"); MockVirtualFile tgt = fs.get("b.txt"); List<ActionStore.PendingAction> actions = new ArrayList<>(); P4ChangelistIdImpl cl1 = new P4ChangelistIdImpl(100, REF_A1); P4ChangelistIdImpl cl2 = new P4ChangelistIdImpl(100, REF_A1); ActionStore.PendingAction addFile = ActionStore.createPendingAction(REF_A1, new AddEditAction(tgt.asFilePath(), null, cl1, "UTF-8")); actions.add(addFile); ActionStore.PendingAction moveFile = ActionStore.createPendingAction(REF_A1, new MoveFileAction(src.asFilePath(), tgt.asFilePath(), cl1)); actions.add(moveFile); ActionStore.PendingAction deleteFile = ActionStore.createPendingAction(REF_A1, new DeleteFileAction(src.asFilePath(), cl2)); curator.curateActionList(deleteFile, actions); assertSize(0, actionFactory.created); assertContainsExactly(actions, addFile, moveFile); }
@Test void curateFile_addTgt_moveSrc_deleteTgt() { SimplePendingActionFactory actionFactory = new SimplePendingActionFactory(REF_A1); PendingActionCurator curator = new PendingActionCurator(actionFactory); Map<String, MockVirtualFile> fs = MockVirtualFileSystem.createTree( "a.txt", "abc", "b.txt", "def" ); MockVirtualFile src = fs.get("a.txt"); MockVirtualFile tgt = fs.get("b.txt"); List<ActionStore.PendingAction> actions = new ArrayList<>(); P4ChangelistIdImpl cl1 = new P4ChangelistIdImpl(100, REF_A1); P4ChangelistIdImpl cl2 = new P4ChangelistIdImpl(100, REF_A1); ActionStore.PendingAction addFile = ActionStore.createPendingAction(REF_A1, new AddEditAction(tgt.asFilePath(), null, cl1, "UTF-8")); actions.add(addFile); ActionStore.PendingAction moveFile = ActionStore.createPendingAction(REF_A1, new MoveFileAction(src.asFilePath(), tgt.asFilePath(), cl1)); actions.add(moveFile); ActionStore.PendingAction deleteFile = ActionStore.createPendingAction(REF_A1, new DeleteFileAction(tgt.asFilePath(), cl2)); curator.curateActionList(deleteFile, actions); assertSize(1, actionFactory.created); ActionStore.PendingAction createdDelete = actionFactory.created.get(0); assertNull(createdDelete.serverAction); assertNotNull(createdDelete.clientAction); assertEquals(createdDelete.clientAction.getClass(), DeleteFileAction.class); DeleteFileAction deleteAction = (DeleteFileAction) createdDelete.clientAction; assertEquals(src.asFilePath(), deleteAction.getFile()); assertEquals(cl1, deleteAction.getChangelistId()); assertContainsExactly(actions, addFile, createdDelete); }