Search in sources :

Example 6 with GitControllerBase

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

the class BranchActionsTest method testCreateLocalBranchAction_dontCheckout.

/**
 * <p><b>Description:</b> create local branch but don't check it out.</p>
 * <p><b>Bug ID:</b> EXM-47204</p>
 *
 * @author sorin_carbunaru
 *
 * @throws Exception
 */
public void testCreateLocalBranchAction_dontCheckout() throws Exception {
    boolean initialIsCheckoutNewBranch = OptionsManager.getInstance().isCheckoutNewlyCreatedLocalBranch();
    try {
        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());
        // ------------- Create branch LOCAL_BRANCH_COPY_NAME from 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.getValue(AbstractAction.NAME).equals(translator.getTranslation(Tags.CREATE_BRANCH) + "...")) {
                SwingUtilities.invokeLater(() -> {
                    abstractAction.actionPerformed(null);
                });
                flushAWT();
                JDialog createBranchDialog = findDialog(translator.getTranslation(Tags.CREATE_BRANCH));
                JCheckBox checkoutBranchCheckBox = findCheckBox(createBranchDialog, Tags.CHECKOUT_BRANCH);
                assertNotNull(checkoutBranchCheckBox);
                checkoutBranchCheckBox.setSelected(false);
                flushAWT();
                JTextField branchNameTextField = findComponentNearJLabel(createBranchDialog, translator.getTranslation(Tags.BRANCH_NAME) + ": ", JTextField.class);
                branchNameTextField.setText(LOCAL_BRANCH_COPY_NAME);
                JButton okButton = findFirstButton(createBranchDialog, "Create");
                if (okButton != null) {
                    okButton.setEnabled(true);
                    okButton.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/LocalBranch\n" + "    refs/heads/LocalBranch2\n" + "    refs/heads/LocalBranchCopy\n" + "    refs/heads/" + GitAccess.DEFAULT_BRANCH_NAME + "\n", actualTree.toString());
        assertEquals(initialBranchName, gitAccess.getBranchInfo().getBranchName());
    } finally {
        OptionsManager.getInstance().setCheckoutNewlyCreatedLocalBranch(initialIsCheckoutNewBranch);
    }
}
Also used : FileStatus(com.oxygenxml.git.service.entities.FileStatus) JButton(javax.swing.JButton) GitController(com.oxygenxml.git.view.event.GitController) JTextField(javax.swing.JTextField) JCheckBox(javax.swing.JCheckBox) GitControllerBase(com.oxygenxml.git.service.GitControllerBase) GitTreeNode(com.oxygenxml.git.view.GitTreeNode) File(java.io.File) AbstractAction(javax.swing.AbstractAction) JDialog(javax.swing.JDialog)

Example 7 with GitControllerBase

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

the class BranchMergingTest method testFailingBranchMerging.

/**
 *<p><b>Description:</b>Tests the failing merging.</p>
 * <p><b>Bug ID:</b> EXM-43410</p>
 *
 * @author gabriel_nedianu
 *
 * @throws Exception
 */
public void testFailingBranchMerging() throws Exception {
    JDialog mergeFailDialog = null;
    try {
        File file1 = new File(LOCAL_TEST_REPOSITORY, "local1.txt");
        File file2 = new File(LOCAL_TEST_REPOSITORY, "local2.txt");
        file1.createNewFile();
        file2.createNewFile();
        setFileContent(file1, "local file 1 content");
        setFileContent(file2, "local file 2 content");
        // Make the first commit for the local repository, create a new branch and move on it
        gitAccess.add(new FileStatus(GitChangeType.ADD, "local1.txt"));
        gitAccess.add(new FileStatus(GitChangeType.ADD, "local2.txt"));
        gitAccess.commit("First local commit on main.");
        // Create new branch and commit some changes
        gitAccess.createBranch(LOCAL_BRANCH_NAME1);
        gitAccess.setBranch(LOCAL_BRANCH_NAME1);
        setFileContent(file1, "branch file1 modification ");
        setFileContent(file2, "branch file2 modification ");
        gitAccess.add(new FileStatus(GitChangeType.ADD, "local1.txt"));
        gitAccess.add(new FileStatus(GitChangeType.ADD, "local2.txt"));
        gitAccess.commit("Commit on secondary branch");
        // Move on main branch, make some uncommitted modifications
        gitAccess.setBranch(GitAccess.DEFAULT_BRANCH_NAME);
        setFileContent(file1, "file1 something xx...xx...");
        setFileContent(file2, "file2 something xx...xx...");
        gitAccess.add(new FileStatus(GitChangeType.ADD, "local1.txt"));
        gitAccess.add(new FileStatus(GitChangeType.ADD, "local2.txt"));
        // Try to merge the secondary branch into the default one - CHECKOUT CONFLICT
        GitControllerBase mock = new GitController();
        BranchManagementPanel branchManagementPanel = new BranchManagementPanel(mock);
        branchManagementPanel.refreshBranches();
        flushAWT();
        BranchTreeMenuActionsProvider branchTreeMenuActionsProvider = new BranchTreeMenuActionsProvider(mock);
        GitTreeNode root = (GitTreeNode) (branchManagementPanel.getTree().getModel().getRoot());
        GitTreeNode secondaryBranchNode = (GitTreeNode) root.getFirstLeaf();
        List<AbstractAction> actionsForNode = branchTreeMenuActionsProvider.getActionsForNode(secondaryBranchNode);
        for (AbstractAction action : actionsForNode) {
            if (action != null) {
                String actionName = action.getValue(AbstractAction.NAME).toString();
                if ("Merge_Branch1_Into_Branch2".equals(actionName)) {
                    SwingUtilities.invokeLater(() -> action.actionPerformed(null));
                    break;
                }
            }
        }
        flushAWT();
        sleep(300);
        // Confirm merge dialog
        JDialog mergeOkDialog = findDialog(translator.getTranslation(Tags.MERGE_BRANCHES));
        JButton mergeOkButton = findFirstButton(mergeOkDialog, translator.getTranslation(Tags.MERGE));
        mergeOkButton.doClick();
        sleep(200);
        mergeFailDialog = findDialog(translator.getTranslation(Tags.MERGE_FAILED_UNCOMMITTED_CHANGES_TITLE));
        assertNotNull(mergeFailDialog);
        mergeFailDialog.setVisible(false);
        mergeFailDialog.dispose();
        // Commit the changes on the main branch then make other uncommitted changes and try to merge again
        // MERGE FAILED
        gitAccess.commit("Commit on main branch");
        setFileContent(file1, "file1 something modif");
        setFileContent(file2, "file2 something modif");
        gitAccess.add(new FileStatus(GitChangeType.ADD, "local1.txt"));
        gitAccess.add(new FileStatus(GitChangeType.ADD, "local2.txt"));
        actionsForNode = branchTreeMenuActionsProvider.getActionsForNode(secondaryBranchNode);
        for (AbstractAction action : actionsForNode) {
            if (action != null) {
                String actionName = action.getValue(AbstractAction.NAME).toString();
                if ("Merge_Branch1_Into_Branch2".equals(actionName)) {
                    SwingUtilities.invokeLater(() -> action.actionPerformed(null));
                    break;
                }
            }
        }
        flushAWT();
        sleep(300);
        // Confirm merge dialog
        mergeOkDialog = findDialog(translator.getTranslation(Tags.MERGE_BRANCHES));
        mergeOkButton = findFirstButton(mergeOkDialog, translator.getTranslation(Tags.MERGE));
        mergeOkButton.doClick();
        sleep(200);
        mergeFailDialog = findDialog(translator.getTranslation(Tags.MERGE_FAILED_UNCOMMITTED_CHANGES_TITLE));
        assertNotNull(mergeFailDialog);
    } finally {
        if (mergeFailDialog != null) {
            mergeFailDialog.setVisible(false);
            mergeFailDialog.dispose();
        }
    }
}
Also used : GitControllerBase(com.oxygenxml.git.service.GitControllerBase) FileStatus(com.oxygenxml.git.service.entities.FileStatus) GitTreeNode(com.oxygenxml.git.view.GitTreeNode) JButton(javax.swing.JButton) GitController(com.oxygenxml.git.view.event.GitController) File(java.io.File) AbstractAction(javax.swing.AbstractAction) JDialog(javax.swing.JDialog)

Example 8 with GitControllerBase

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

the class BranchMergingTest method testBranchMergingWithConflict.

/**
 * <p><b>Description:</b>Tests the branch merging. Conflict happens.</p>
 * <p><b>Bug ID:</b> EXM-43410</p>
 *
 * @author gabriel_nedianu
 *
 * @throws Exception
 */
public void testBranchMergingWithConflict() throws Exception {
    JDialog conflictMergeDialog = null;
    try {
        File file1 = new File(LOCAL_TEST_REPOSITORY, "local1.txt");
        File file2 = new File(LOCAL_TEST_REPOSITORY, "local2.txt");
        file1.createNewFile();
        file2.createNewFile();
        setFileContent(file1, "local file 1 content");
        setFileContent(file2, "local file 2 content");
        // Make the first commit for the local repository and create a new branch
        gitAccess.add(new FileStatus(GitChangeType.ADD, "local1.txt"));
        gitAccess.add(new FileStatus(GitChangeType.ADD, "local2.txt"));
        refreshSupport.call();
        gitAccess.commit("First local commit on main.");
        gitAccess.createBranch(LOCAL_BRANCH_NAME1);
        GitControllerBase mock = new GitController();
        BranchManagementPanel branchManagementPanel = new BranchManagementPanel(mock);
        branchManagementPanel.refreshBranches();
        flushAWT();
        refreshSupport.call();
        // ------------- Checkout branch: LOCAL_BRANCH_NAME1  -------------
        gitAccess.setBranch(LOCAL_BRANCH_NAME1);
        // Commit on this branch
        setFileContent(file1, "local file 1 on new branch");
        setFileContent(file2, "local file 2 on new branch");
        gitAccess.add(new FileStatus(GitChangeType.ADD, "local1.txt"));
        gitAccess.add(new FileStatus(GitChangeType.ADD, "local2.txt"));
        refreshSupport.call();
        gitAccess.commit("Commit on secondary branch");
        refreshSupport.call();
        // ------------- Move to the main branch and commit something there ---------------
        gitAccess.setBranch(GitAccess.DEFAULT_BRANCH_NAME);
        setFileContent(file1, "local file 1 modifications");
        setFileContent(file2, "local file 2 modifications");
        gitAccess.add(new FileStatus(GitChangeType.ADD, "local1.txt"));
        gitAccess.add(new FileStatus(GitChangeType.ADD, "local2.txt"));
        refreshSupport.call();
        gitAccess.commit("2nd commit on main branch");
        refreshSupport.call();
        // Merge secondary branch into main
        BranchTreeMenuActionsProvider branchTreeMenuActionsProvider = new BranchTreeMenuActionsProvider(mock);
        GitTreeNode root = (GitTreeNode) (branchManagementPanel.getTree().getModel().getRoot());
        GitTreeNode secondaryBranchNode = (GitTreeNode) root.getFirstLeaf();
        String secondaryBranchPath = (String) secondaryBranchNode.getUserObject();
        assertTrue(secondaryBranchPath.contains(Constants.R_HEADS));
        refreshSupport.call();
        List<AbstractAction> actionsForSecondaryBranch = branchTreeMenuActionsProvider.getActionsForNode(secondaryBranchNode);
        for (AbstractAction action : actionsForSecondaryBranch) {
            if (action != null) {
                String actionName = action.getValue(AbstractAction.NAME).toString();
                if ("Merge_Branch1_Into_Branch2".equals(actionName)) {
                    SwingUtilities.invokeLater(() -> action.actionPerformed(null));
                    break;
                }
            }
        }
        flushAWT();
        sleep(300);
        // Confirm merge dialog
        JDialog mergeOkDialog = findDialog(translator.getTranslation(Tags.MERGE_BRANCHES));
        JButton mergeOkButton = findFirstButton(mergeOkDialog, translator.getTranslation(Tags.MERGE));
        mergeOkButton.doClick();
        flushAWT();
        sleep(200);
        waitForScheduler();
        conflictMergeDialog = findDialog(translator.getTranslation(Tags.MERGE_CONFLICTS_TITLE));
        System.out.println(conflictMergeDialog);
        assertNotNull(conflictMergeDialog);
        assertTrue(TestUtil.read(file1.toURI().toURL()).contains("<<<<<<< HEAD\n" + "local file 1 modifications\n" + "=======\n" + "local file 1 on new branch\n" + ">>>>>>>"));
        assertTrue(TestUtil.read(file2.toURI().toURL()).contains("<<<<<<< HEAD\n" + "local file 2 modifications\n" + "=======\n" + "local file 2 on new branch\n" + ">>>>>>>"));
    } finally {
        if (conflictMergeDialog != null) {
            conflictMergeDialog.setVisible(false);
            conflictMergeDialog.dispose();
        }
    }
}
Also used : GitControllerBase(com.oxygenxml.git.service.GitControllerBase) FileStatus(com.oxygenxml.git.service.entities.FileStatus) GitTreeNode(com.oxygenxml.git.view.GitTreeNode) JButton(javax.swing.JButton) GitController(com.oxygenxml.git.view.event.GitController) File(java.io.File) AbstractAction(javax.swing.AbstractAction) JDialog(javax.swing.JDialog)

Example 9 with GitControllerBase

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

the class BranchMergingTest method testBranchMerging.

/**
 * <p><b>Description:</b>Tests the branch merging.</p>
 * <p><b>Bug ID:</b> EXM-43410</p>
 *
 * @author gabriel_nedianu
 *
 * @throws Exception
 */
public void testBranchMerging() throws Exception {
    File file = new File(LOCAL_TEST_REPOSITORY, "local.txt");
    file.createNewFile();
    setFileContent(file, "local content");
    String initialBranchName = gitAccess.getBranchInfo().getBranchName();
    assertEquals(GitAccess.DEFAULT_BRANCH_NAME, initialBranchName);
    // Make the first commit for the local repository on the main branch
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local.txt"));
    gitAccess.commit("First local commit.");
    gitAccess.createBranch(LOCAL_BRANCH_NAME1);
    // ------------- Checkout the other branch in the tree: LOCAL_BRANCH_NAME1 -------------
    GitControllerBase mock = new GitController();
    BranchManagementPanel branchManagementPanel = new BranchManagementPanel(mock);
    branchManagementPanel.refreshBranches();
    flushAWT();
    refreshSupport.call();
    GitTreeNode root = (GitTreeNode) (branchManagementPanel.getTree().getModel().getRoot());
    GitTreeNode firstLeaf = (GitTreeNode) root.getFirstLeaf();
    String firstLeafPath = (String) firstLeaf.getUserObject();
    assertTrue(firstLeafPath.contains(Constants.R_HEADS));
    String[] branchPath = firstLeafPath.split("/");
    assertEquals(LOCAL_BRANCH_NAME1, branchPath[branchPath.length - 1]);
    refreshSupport.call();
    BranchTreeMenuActionsProvider branchTreeMenuActionsProvider = new BranchTreeMenuActionsProvider(mock);
    List<AbstractAction> actionsForNode = branchTreeMenuActionsProvider.getActionsForNode(firstLeaf);
    for (AbstractAction abstractAction : actionsForNode) {
        if (abstractAction.getValue(AbstractAction.NAME).equals(translator.getTranslation(Tags.CHECKOUT))) {
            abstractAction.actionPerformed(null);
            break;
        }
    }
    refreshSupport.call();
    flushAWT();
    sleep(300);
    assertEquals(LOCAL_BRANCH_NAME1, gitAccess.getRepository().getBranch());
    // Change file on the secondary branch
    setFileContent(file, "local content for merging");
    gitAccess.add(new FileStatus(GitChangeType.ADD, "local.txt"));
    gitAccess.commit("Branch commit 10");
    refreshSupport.call();
    // Move back to the main branch
    gitAccess.setBranch(GitAccess.DEFAULT_BRANCH_NAME);
    assertEquals(GitAccess.DEFAULT_BRANCH_NAME, gitAccess.getBranchInfo().getBranchName());
    refreshSupport.call();
    // Merge LocalBranch into main
    List<AbstractAction> actionsForSecondaryBranch = branchTreeMenuActionsProvider.getActionsForNode(firstLeaf);
    for (AbstractAction action : actionsForSecondaryBranch) {
        if (action != null) {
            String actionName = action.getValue(AbstractAction.NAME).toString();
            if ("Merge_Branch1_Into_Branch2".equals(actionName)) {
                action.actionPerformed(null);
                break;
            }
        }
    }
    flushAWT();
    sleep(300);
    // Confirm merge dialog
    JDialog mergeOkDialog = findDialog(translator.getTranslation(Tags.MERGE_BRANCHES));
    JButton mergeOkButton = findFirstButton(mergeOkDialog, translator.getTranslation(Tags.MERGE));
    mergeOkButton.doClick();
    sleep(200);
    assertEquals("local content for merging", TestUtil.read(file.toURI().toURL()));
}
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)

Example 10 with GitControllerBase

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

the class DiffPresenterTest method testExistingFileDiff.

/**
 * Scenario 2:
 * - an existing file modified. Added into the index.
 * - the new file modified again.
 * To test:
 * - Diff in not-staged: compares the modified version with the index version
 * - Diff in staged: compares the index version with the remote one.
 *
 * @throws Exception If it fails.
 */
@Test
public void testExistingFileDiff() throws Exception {
    /**
     * The local repository.
     */
    String localTestRepository = "target/test-resources/local";
    /**
     * The remote repository.
     */
    String remoteTestRepository = "target/test-resources/remote";
    GitAccess gitAccess = GitAccess.getInstance();
    Repository remoteRepo = createRepository(remoteTestRepository);
    // Create the local repository.
    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();
    // Modify the newly created file.
    setFileContent(file, "initial content");
    // Add it to the index.
    gitAccess.add(new FileStatus(GitChangeType.ADD, "test.txt"));
    gitAccess.commit("First version.");
    // Change the file.
    setFileContent(file, "index content");
    // Add it to the index.
    gitAccess.add(new FileStatus(GitChangeType.ADD, "test.txt"));
    // Change it again.
    setFileContent(file, "local 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, but was: " + 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.CHANGED, "test.txt");
    DiffPresenter.showDiff(fileStatus, gitCtrl);
    assertNotNull(leftDiff);
    assertNotNull(rightDiff);
    assertEquals("The index version should be on the left, but was: " + leftDiff.toString(), indexVersionURL, leftDiff.toString());
    String headVersionURL = "git://" + VersionIdentifier.LAST_COMMIT + "/test.txt";
    assertEquals("The head version should be on the right, but was: " + rightDiff.toString(), headVersionURL, rightDiff.toString());
    // Assert content.
    assertEquals("index content", TestUtil.read(new URL(indexVersionURL)));
}
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) Translator(com.oxygenxml.git.translator.Translator) InvocationOnMock(org.mockito.invocation.InvocationOnMock) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

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