Search in sources :

Example 11 with GitControllerBase

use of com.oxygenxml.git.service.GitControllerBase in project oxygen-git-client-addon by oxygenxml.

the class DiffPresenterTest method testNewFileDiff.

/**
 * Scenario 1:
 * - a new file. Added into the index.
 * - modify the new.
 *
 * To test:
 * - Diff in not-staged: compares the modified version with the index version
 * - Diff in staged: compares the index version with nothing (no remote)
 *
 * @throws Exception If it fails.
 */
@Test
public void testNewFileDiff() throws Exception {
    String localTestRepository = "target/test-resources/local";
    String remoteTestRepository = "target/test-resources/remote";
    Repository remoteRepo = createRepository(remoteTestRepository);
    Repository localRepo = createRepository(localTestRepository);
    // Bind the local repository to the remote one.
    bindLocalToRemote(localRepo, remoteRepo);
    // Create a new file.
    File file = new File(localTestRepository + "/test.txt");
    file.createNewFile();
    // Add it to the index / Stage it.
    GitAccess.getInstance().add(new FileStatus(GitChangeType.ADD, "test.txt"));
    // Modify the newly created file.
    setFileContent(file, "content");
    FileStatus fileStatus = new FileStatus(GitChangeType.MODIFIED, "test.txt");
    GitControllerBase gitCtrl = Mockito.mock(GitControllerBase.class);
    // Mock the translator.
    Translator translator = Mockito.mock(Translator.class);
    Mockito.when(translator.getTranslation(Mockito.anyString())).then(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return (String) invocation.getArguments()[0];
        }
    });
    // Diff the first WC local file.
    DiffPresenter.showDiff(fileStatus, gitCtrl);
    assertNotNull(leftDiff);
    assertNotNull(rightDiff);
    String localVersionURL = file.toURI().toURL().toString();
    assertEquals("The local file should be on the left side: " + localVersionURL, localVersionURL, leftDiff.toString());
    String indexVersionURL = "git://" + VersionIdentifier.INDEX_OR_LAST_COMMIT + "/test.txt";
    assertEquals("The index version should be on the right, but was: " + rightDiff.toString(), indexVersionURL, rightDiff.toString());
    leftDiff = null;
    rightDiff = null;
    // Diff the index file.
    fileStatus = new FileStatus(GitChangeType.ADD, "test.txt");
    DiffPresenter.showDiff(fileStatus, gitCtrl);
    // On the left we present the Index version.
    assertEquals("git://IndexOrLastCommit/test.txt", leftDiff.toString());
    // On the right we present the HEAD version.
    assertNull(rightDiff);
    assertNull(toOpen);
    // Assert content.
    assertEquals("", TestUtil.read(new URL(indexVersionURL)));
}
Also used : GitControllerBase(com.oxygenxml.git.service.GitControllerBase) Repository(org.eclipse.jgit.lib.Repository) FileStatus(com.oxygenxml.git.service.entities.FileStatus) Translator(com.oxygenxml.git.translator.Translator) InvocationOnMock(org.mockito.invocation.InvocationOnMock) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 12 with GitControllerBase

use of com.oxygenxml.git.service.GitControllerBase in project oxygen-git-client-addon by oxygenxml.

the class DiffPresenterTest method testRebasingFileDiff.

@Test
public void testRebasingFileDiff() throws Exception {
    // The local repositories.
    String localTestRepository1 = "target/test-resources/local1";
    String localTestRepository2 = "target/test-resources/local2";
    // The remote repository.
    String remoteTestRepository = "target/test-resources/remote";
    GitAccess gitAccess = GitAccess.getInstance();
    Repository remoteRepo = createRepository(remoteTestRepository);
    Repository localRepo1 = createRepository(localTestRepository1);
    Repository localRepo2 = createRepository(localTestRepository2);
    // -------------
    // Set up the repositories for a rebase conflict
    // -------------
    // ----------------
    // LOCAL 1
    // ----------------
    // Bind the local repository 1 to the remote one.
    bindLocalToRemote(localRepo1, remoteRepo);
    gitAccess.setRepositorySynchronously(localTestRepository1);
    // Create a new file for the first repository.
    File localFile1 = new File(localTestRepository1 + "/test.txt");
    localFile1.createNewFile();
    // Modify the newly created file.
    setFileContent(localFile1, "initial content");
    // Add it to the index.
    gitAccess.add(new FileStatus(GitChangeType.ADD, "test.txt"));
    gitAccess.commit("First commit.");
    // Send it to remote/upstream.
    push("", "");
    // ----------------
    // LOCAL 2
    // ----------------
    // Bind the local repository 2 to the remote one.
    bindLocalToRemote(localRepo2, remoteRepo);
    gitAccess.setRepositorySynchronously(localTestRepository2);
    // Receive changes from remote/upstream.
    PullResponse pull = pull("", "", PullType.MERGE_FF, false);
    assertEquals(PullStatus.OK.toString(), pull.getStatus().toString());
    // Create new file for second repository.
    File local2File = new File(localTestRepository2, "test.txt");
    assertEquals("initial content", TestUtil.read(local2File.toURI().toURL()));
    // Modify the file.
    setFileContent(local2File, "changed in local 2, resolved");
    // Add it to the index.
    gitAccess.add(new FileStatus(GitChangeType.MODIFIED, "test.txt"));
    gitAccess.commit("Second commit");
    // Send it to remote/upstream.
    push("", "");
    // ----------------
    // LOCAL 1
    // ----------------
    gitAccess.setRepositorySynchronously(localTestRepository1);
    // Modify the file.
    setFileContent(localFile1, "changed in local 1, conflict content, original");
    // Add it to the index.
    gitAccess.add(new FileStatus(GitChangeType.MODIFIED, "test.txt"));
    // Commit the file.
    gitAccess.commit("Third commit, with conflict");
    // ------------
    // Rebase conflict prepared and will happen after the pull.
    // ------------
    final StringBuilder pullWithConflictsSB = new StringBuilder();
    boolean[] wasRebaseInterrupted = new boolean[1];
    final String[] pullFailedMessage = new String[1];
    GitController pc = new GitController(gitAccess) {

        @Override
        protected void showPullFailedBecauseOfCertainChanges(List<String> changes, String message) {
            pullFailedMessage[0] = message;
        }

        @Override
        protected void showPullSuccessfulWithConflicts(PullResponse response) {
            pullWithConflictsSB.append(response);
        }

        @Override
        protected void showRebaseInProgressDialog() {
            wasRebaseInterrupted[0] = true;
        }
    };
    final StringBuilder b = new StringBuilder();
    TestUtil.collectPushPullEvents(pc, b);
    // Get conflict
    pc.pull(PullType.REBASE).get();
    assertNull(pullFailedMessage[0]);
    assertFalse(wasRebaseInterrupted[0]);
    assertEquals("Status: CONFLICTS Conflicting files: [test.txt]", pullWithConflictsSB.toString());
    assertTrue(TestUtil.read(localFile1.toURI().toURL()).startsWith("<<<<<<< Upstream, based on branch '" + GitAccess.DEFAULT_BRANCH_NAME + "' of file:"));
    leftDiff = null;
    rightDiff = null;
    // Mock the GitController
    GitControllerBase gitCtrl = Mockito.mock(GitControllerBase.class);
    FileStatus fileStatus = new FileStatus(GitChangeType.CONFLICT, "test.txt");
    // Invoke DIFF over the changed file.
    DiffPresenter.showDiff(fileStatus, gitCtrl);
    assertNotNull(leftDiff);
    assertNotNull(rightDiff);
    // Verify that each side has the proper tag and content.
    assertTrue(leftDiff.toString().contains("MineResolved"));
    assertTrue(rightDiff.toString().contains("MineOriginal"));
    assertEquals("changed in local 2, resolved", TestUtil.read(leftDiff));
    assertEquals("changed in local 1, conflict content, original", TestUtil.read(rightDiff));
}
Also used : GitControllerBase(com.oxygenxml.git.service.GitControllerBase) Repository(org.eclipse.jgit.lib.Repository) GitAccess(com.oxygenxml.git.service.GitAccess) FileStatus(com.oxygenxml.git.service.entities.FileStatus) PullResponse(com.oxygenxml.git.service.PullResponse) GitController(com.oxygenxml.git.view.event.GitController) List(java.util.List) File(java.io.File) Test(org.junit.Test)

Example 13 with GitControllerBase

use of com.oxygenxml.git.service.GitControllerBase in project oxygen-git-client-addon by oxygenxml.

the class FlatView10Test method testAbortMergeButton.

/**
 * <p><b>Description:</b> show/hide/click "Abort merge" button.</p>
 * <p><b>Bug ID:</b> EXM-46222</p>
 *
 * @author sorin_carbunaru
 *
 * @throws Exception If it fails.
 */
@Test
public void testAbortMergeButton() throws Exception {
    String localTestRepository_1 = "target/test-resources/testShowHideAbortMergeButton-local-1";
    String localTestRepository_2 = "target/test-resources/testShowHideAbortMergeButton-local-2";
    String remoteTestRepository = "target/test-resources/testShowHideAbortMergeButton-remote";
    // Create and repositories
    Repository remoteRepo = createRepository(remoteTestRepository);
    Repository localRepo_1 = createRepository(localTestRepository_1);
    Repository localRepo_2 = createRepository(localTestRepository_2);
    bindLocalToRemote(localRepo_1, remoteRepo);
    bindLocalToRemote(localRepo_2, remoteRepo);
    new File(localTestRepository_1).mkdirs();
    new File(localTestRepository_2).mkdirs();
    // --------------  REPO 1
    GitAccess.getInstance().setRepositorySynchronously(localTestRepository_1);
    File firstRepoFile = new File(localTestRepository_1 + "/test.txt");
    firstRepoFile.createNewFile();
    setFileContent(firstRepoFile, "First version");
    GitAccess.getInstance().add(new FileStatus(GitChangeType.UNKNOWN, "test.txt"));
    GitAccess.getInstance().commit("First commit.");
    push("", "");
    // ----------------- REPO 2
    GitAccess.getInstance().setRepositorySynchronously(localTestRepository_2);
    File secondRepoFile = new File(localTestRepository_2 + "/test.txt");
    refreshSupport.call();
    flushAWT();
    sleep(400);
    assertFalse(secondRepoFile.exists());
    pull("", "", PullType.MERGE_FF, false);
    assertTrue(secondRepoFile.exists());
    // Modify file and commit and push
    setFileContent(secondRepoFile, "Second versions");
    GitAccess.getInstance().add(new FileStatus(GitChangeType.MODIFIED, "test.txt"));
    GitAccess.getInstance().commit("Second commit.");
    push("", "");
    // --------------  REPO 1
    GitAccess.getInstance().setRepositorySynchronously(localTestRepository_1);
    setFileContent(firstRepoFile, "Third version");
    GitAccess.getInstance().add(new FileStatus(GitChangeType.MODIFIED, "test.txt"));
    GitAccess.getInstance().commit("Third commit.");
    // Now pull to generate conflict
    ConflictButtonsPanel abortMergeButtonPanel = stagingPanel.getConflictButtonsPanel();
    assertFalse(abortMergeButtonPanel.isShowing());
    flushAWT();
    PullResponse pullResponse = pull("", "", PullType.MERGE_FF, false);
    refreshSupport.call();
    waitForScheduler();
    assertEquals(PullStatus.CONFLICTS, pullResponse.getStatus());
    RepositoryState repositoryState = GitAccess.getInstance().getRepository().getRepositoryState();
    assertEquals(RepositoryState.MERGING, repositoryState);
    assertTrue(abortMergeButtonPanel.isShowing());
    // --------------- REPO 2
    GitAccess.getInstance().setRepositorySynchronously(localTestRepository_2);
    sleep(300);
    assertFalse(abortMergeButtonPanel.isShowing());
    // --------------- REPO 1
    GitAccess.getInstance().setRepositorySynchronously(localTestRepository_1);
    sleep(300);
    assertTrue(abortMergeButtonPanel.isShowing());
    JButton abortMergeBtn = findFirstButton(abortMergeButtonPanel, Tags.ABORT_MERGE);
    assertNotNull(abortMergeBtn);
    // Resolve using mine
    GitControllerBase gitCtrl = stagingPanel.getGitController();
    PluginWorkspace spy = Mockito.spy(PluginWorkspaceProvider.getPluginWorkspace());
    Mockito.when(spy.showConfirmDialog(Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.any())).thenReturn(0);
    PluginWorkspaceProvider.setPluginWorkspace(spy);
    FileStatus testFileStatus = new FileStatus(GitChangeType.CONFLICT, "test.txt");
    gitCtrl.asyncResolveUsingMine(Arrays.asList(testFileStatus));
    refreshSupport.call();
    waitForScheduler();
    flushAWT();
    abortMergeButtonPanel = stagingPanel.getConflictButtonsPanel();
    assertFalse(abortMergeButtonPanel.isShowing());
    // Restart merge
    GitAccess.getInstance().restartMerge();
    flushAWT();
    sleep(200);
    abortMergeButtonPanel = stagingPanel.getConflictButtonsPanel();
    assertTrue(abortMergeButtonPanel.isShowing());
    abortMergeBtn = findFirstButton(abortMergeButtonPanel, Tags.ABORT_MERGE);
    assertNotNull(abortMergeBtn);
    // Resolve using theirs
    gitCtrl.asyncResolveUsingTheirs(Arrays.asList(testFileStatus));
    waitForScheduler();
    flushAWT();
    abortMergeButtonPanel = stagingPanel.getConflictButtonsPanel();
    assertFalse(abortMergeButtonPanel.isShowing());
    // Restart merge
    GitAccess.getInstance().restartMerge();
    flushAWT();
    abortMergeButtonPanel = stagingPanel.getConflictButtonsPanel();
    assertTrue(abortMergeButtonPanel.isShowing());
    abortMergeBtn = findFirstButton(abortMergeButtonPanel, Tags.ABORT_MERGE);
    assertNotNull(abortMergeBtn);
    repositoryState = GitAccess.getInstance().getRepository().getRepositoryState();
    assertEquals(RepositoryState.MERGING, repositoryState);
    assertEquals(1, GitAccess.getInstance().getPullsBehind());
    assertEquals(1, GitAccess.getInstance().getPushesAhead());
    // Abort merge
    abortMergeBtn.doClick();
    flushAWT();
    abortMergeButtonPanel = stagingPanel.getConflictButtonsPanel();
    assertFalse(abortMergeButtonPanel.isShowing());
    repositoryState = GitAccess.getInstance().getRepository().getRepositoryState();
    assertEquals(RepositoryState.SAFE, repositoryState);
    assertEquals(1, GitAccess.getInstance().getPullsBehind());
    assertEquals(1, GitAccess.getInstance().getPushesAhead());
}
Also used : GitControllerBase(com.oxygenxml.git.service.GitControllerBase) PluginWorkspace(ro.sync.exml.workspace.api.PluginWorkspace) Repository(org.eclipse.jgit.lib.Repository) FileStatus(com.oxygenxml.git.service.entities.FileStatus) PullResponse(com.oxygenxml.git.service.PullResponse) ConflictButtonsPanel(com.oxygenxml.git.view.staging.ConflictButtonsPanel) JButton(javax.swing.JButton) RepositoryState(org.eclipse.jgit.lib.RepositoryState) File(java.io.File) Test(org.junit.Test)

Example 14 with GitControllerBase

use of com.oxygenxml.git.service.GitControllerBase in project oxygen-git-client-addon by oxygenxml.

the class FlatView2Test method testShowRebasePanel_thenContinue.

/**
 * <p><b>Description:</b> Show and hide (by continuing the rebase) the rebase panel.</p>
 * <p><b>Bug ID:</b> EXM-42025</p>
 *
 * @author sorin_carbunaru
 *
 * @throws Exception If it fails.
 */
@Test
public void testShowRebasePanel_thenContinue() throws Exception {
    String localTestRepository_1 = "target/test-resources/testShowRebasePanel_thenContinue-local-1";
    String localTestRepository_2 = "target/test-resources/testShowRebasePanel_thenContinue-local-2";
    String remoteTestRepository = "target/test-resources/testShowRebasePanel_thenContinue-remote";
    // Create and repositories
    Repository remoteRepo = createRepository(remoteTestRepository);
    Repository localRepo_1 = createRepository(localTestRepository_1);
    Repository localRepo_2 = createRepository(localTestRepository_2);
    bindLocalToRemote(localRepo_1, remoteRepo);
    bindLocalToRemote(localRepo_2, remoteRepo);
    new File(localTestRepository_1).mkdirs();
    new File(localTestRepository_2).mkdirs();
    // --------------  REPO 1
    GitAccess.getInstance().setRepositorySynchronously(localTestRepository_1);
    File firstRepoFile = new File(localTestRepository_1 + "/test.txt");
    firstRepoFile.createNewFile();
    setFileContent(firstRepoFile, "First version");
    GitAccess.getInstance().add(new FileStatus(GitChangeType.UNKNOWN, "test.txt"));
    GitAccess.getInstance().commit("First commit.");
    push("", "");
    // ----------------- REPO 2
    GitAccess.getInstance().setRepositorySynchronously(localTestRepository_2);
    File secondRepoFile = new File(localTestRepository_2 + "/test.txt");
    refreshSupport.call();
    flushAWT();
    sleep(400);
    assertFalse(secondRepoFile.exists());
    pull("", "", PullType.REBASE, false);
    assertTrue(secondRepoFile.exists());
    // Modify file and commit and push
    setFileContent(secondRepoFile, "Second versions");
    GitAccess.getInstance().add(new FileStatus(GitChangeType.MODIFIED, "test.txt"));
    GitAccess.getInstance().commit("Second commit.");
    push("", "");
    // --------------  REPO 1
    GitAccess.getInstance().setRepositorySynchronously(localTestRepository_1);
    setFileContent(firstRepoFile, "Third version");
    GitAccess.getInstance().add(new FileStatus(GitChangeType.MODIFIED, "test.txt"));
    GitAccess.getInstance().commit("Third commit.");
    // Now pull to generate conflict
    ConflictButtonsPanel rebasePanel = stagingPanel.getConflictButtonsPanel();
    assertFalse(rebasePanel.isShowing());
    flushAWT();
    PullResponse pullResponse = pull("", "", PullType.REBASE, false);
    refreshSupport.call();
    waitForScheduler();
    assertEquals(PullStatus.CONFLICTS, pullResponse.getStatus());
    assertTrue(rebasePanel.isShowing());
    GitControllerBase sc = new GitControllerBase(GitAccess.getInstance()) {

        @Override
        protected boolean isUserOKWithResolvingRebaseConflictUsingMineOrTheirs(ConflictResolution cmd) {
            return cmd == ConflictResolution.RESOLVE_USING_MINE;
        }
    };
    sc.asyncResolveUsingMine(Arrays.asList(new FileStatus(GitChangeType.CONFLICT, "test.txt")));
    waitForScheduler();
    flushAWT();
    JButton continueBtn = findFirstButton(rebasePanel, Tags.CONTINUE_REBASE);
    assertNotNull(continueBtn);
    continueBtn.doClick();
    waitForScheduler();
    flushAWT();
    assertFalse(rebasePanel.isShowing());
}
Also used : GitControllerBase(com.oxygenxml.git.service.GitControllerBase) Repository(org.eclipse.jgit.lib.Repository) FileStatus(com.oxygenxml.git.service.entities.FileStatus) PullResponse(com.oxygenxml.git.service.PullResponse) ConflictButtonsPanel(com.oxygenxml.git.view.staging.ConflictButtonsPanel) JButton(javax.swing.JButton) ConflictResolution(com.oxygenxml.git.service.ConflictResolution) File(java.io.File) Test(org.junit.Test)

Example 15 with GitControllerBase

use of com.oxygenxml.git.service.GitControllerBase in project oxygen-git-client-addon by oxygenxml.

the class BranchActionsTest method testDeleteLocalBranchAction.

/**
 * Tests the action of deleting a local branch.
 *
 * @throws Exception
 */
public void testDeleteLocalBranchAction() throws Exception {
    File file = new File(LOCAL_TEST_REPOSITORY + "local.txt");
    file.createNewFile();
    setFileContent(file, "local content");
    // Make the first commit for the local repository and create a branch for it.
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local.txt"));
    gitAccess.commit("First local commit.");
    gitAccess.createBranch(LOCAL_BRANCH_NAME1);
    gitAccess.createBranch(LOCAL_BRANCH_NAME2);
    gitAccess.fetch();
    String initialBranchName = gitAccess.getBranchInfo().getBranchName();
    assertEquals(GitAccess.DEFAULT_BRANCH_NAME, initialBranchName);
    GitControllerBase mock = new GitController();
    BranchManagementPanel branchManagementPanel = new BranchManagementPanel(mock);
    branchManagementPanel.refreshBranches();
    flushAWT();
    BranchTreeMenuActionsProvider branchTreeMenuActionsProvider = new BranchTreeMenuActionsProvider(mock);
    GitTreeNode root = (GitTreeNode) (branchManagementPanel.getTree().getModel().getRoot());
    // ------------- Delete first branch in the tree: LOCAL_BRANCH_NAME1 -------------
    GitTreeNode firstLeaf = (GitTreeNode) root.getFirstLeaf();
    String firstLeafPath = (String) firstLeaf.getUserObject();
    assertTrue(firstLeafPath.contains(Constants.R_HEADS));
    String[] split = firstLeafPath.split("/");
    assertEquals(LOCAL_BRANCH_NAME1, split[split.length - 1]);
    List<AbstractAction> actionsForNode = branchTreeMenuActionsProvider.getActionsForNode(firstLeaf);
    for (AbstractAction abstractAction : actionsForNode) {
        if (abstractAction == null) {
            // Probably separator. Continue.
            continue;
        }
        if (abstractAction.getValue(AbstractAction.NAME).equals(translator.getTranslation(Tags.DELETE) + "...")) {
            SwingUtilities.invokeLater(() -> {
                abstractAction.actionPerformed(null);
            });
            JDialog deleteBranchDialog = findDialog(translator.getTranslation(Tags.DELETE_BRANCH));
            JButton yesButton = findFirstButton(deleteBranchDialog, translator.getTranslation(Tags.YES));
            yesButton.doClick();
            break;
        }
    }
    sleep(500);
    gitAccess.fetch();
    branchManagementPanel.refreshBranches();
    flushAWT();
    root = (GitTreeNode) (branchManagementPanel.getTree().getModel().getRoot());
    StringBuilder actualTree = new StringBuilder();
    BranchManagementTest.serializeTree(actualTree, root);
    assertEquals("localRepository\n" + "  refs/heads/\n" + "    refs/heads/LocalBranch2\n" + "    refs/heads/" + GitAccess.DEFAULT_BRANCH_NAME + "\n", actualTree.toString());
}
Also used : FileStatus(com.oxygenxml.git.service.entities.FileStatus) JButton(javax.swing.JButton) GitController(com.oxygenxml.git.view.event.GitController) GitControllerBase(com.oxygenxml.git.service.GitControllerBase) GitTreeNode(com.oxygenxml.git.view.GitTreeNode) File(java.io.File) AbstractAction(javax.swing.AbstractAction) JDialog(javax.swing.JDialog)

Aggregations

GitControllerBase (com.oxygenxml.git.service.GitControllerBase)18 FileStatus (com.oxygenxml.git.service.entities.FileStatus)17 File (java.io.File)17 GitController (com.oxygenxml.git.view.event.GitController)13 JButton (javax.swing.JButton)12 GitTreeNode (com.oxygenxml.git.view.GitTreeNode)11 AbstractAction (javax.swing.AbstractAction)11 JDialog (javax.swing.JDialog)9 Repository (org.eclipse.jgit.lib.Repository)6 Test (org.junit.Test)6 PullResponse (com.oxygenxml.git.service.PullResponse)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 GitAccess (com.oxygenxml.git.service.GitAccess)3 ConflictButtonsPanel (com.oxygenxml.git.view.staging.ConflictButtonsPanel)3 JTextField (javax.swing.JTextField)3 ConflictResolution (com.oxygenxml.git.service.ConflictResolution)2 Translator (com.oxygenxml.git.translator.Translator)2 URL (java.net.URL)2 JCheckBox (javax.swing.JCheckBox)2 StandalonePluginWorkspace (ro.sync.exml.workspace.api.standalone.StandalonePluginWorkspace)2