Search in sources :

Example 26 with GitController

use of com.oxygenxml.git.view.event.GitController in project oxygen-git-client-addon by oxygenxml.

the class GitTestBase method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // Create the unstaged resources panel
    refreshSupport = new PanelRefresh(null) {

        @Override
        protected int getScheduleDelay() {
            // Execute refresh events immediately from tests.
            return 1;
        }
    };
    gitInit();
    ColorTheme colorTheme = Mockito.mock(ColorTheme.class);
    Mockito.when(colorTheme.isDarkTheme()).thenReturn(false);
    StandalonePluginWorkspace pluginWSMock = Mockito.mock(StandalonePluginWorkspace.class);
    Mockito.when(pluginWSMock.getColorTheme()).thenReturn(colorTheme);
    PluginWorkspaceProvider.setPluginWorkspace(pluginWSMock);
    Mockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            if (arguments.length == 2) {
                urls2compare.add((URL) arguments[0]);
                urls2compare.add((URL) arguments[1]);
            }
            return null;
        }
    }).when(pluginWSMock).openDiffFilesApplication(Mockito.any(), Mockito.any());
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            if (arguments != null && arguments.length > 0) {
                toOpen.add((URL) invocation.getArguments()[0]);
            }
            return null;
        }
    }).when(pluginWSMock).open(Mockito.any());
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            if (invocation.getArguments().length > 0) {
                WSEditorChangeListener listener = (WSEditorChangeListener) invocation.getArguments()[0];
                editorChangeListeners.add(listener);
            }
            return null;
        }
    }).when(pluginWSMock).addEditorChangeListener((WSEditorChangeListener) Mockito.any(), Mockito.anyInt());
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WSEditorChangeListener listener = (WSEditorChangeListener) invocation.getArguments()[0];
            editorChangeListeners.remove(listener);
            return null;
        }
    }).when(pluginWSMock).removeEditorChangeListener((WSEditorChangeListener) Mockito.any(), Mockito.anyInt());
    Mockito.when(pluginWSMock.getEditorAccess((URL) Mockito.any(), Mockito.anyInt())).then(new Answer<WSEditor>() {

        @Override
        public WSEditor answer(InvocationOnMock invocation) throws Throwable {
            WSEditor wsEditorMock = createWSEditorMock((URL) invocation.getArguments()[0]);
            return wsEditorMock;
        }
    });
    XMLUtilAccess xmlUtilAccess = Mockito.mock(XMLUtilAccess.class);
    Mockito.when(xmlUtilAccess.escapeTextValue(Mockito.anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            Object object = invocation.getArguments()[0];
            return object != null ? (String) object : "";
        }
    });
    Mockito.when(xmlUtilAccess.unescapeAttributeValue(Mockito.anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            Object object = invocation.getArguments()[0];
            return object != null ? (String) object : "";
        }
    });
    Mockito.doReturn(xmlUtilAccess).when(pluginWSMock).getXMLUtilAccess();
    UtilAccess utilAccessMock = Mockito.mock(UtilAccess.class);
    Mockito.when(pluginWSMock.getUtilAccess()).thenReturn(utilAccessMock);
    Mockito.when(utilAccessMock.locateFile((URL) Mockito.any())).then(new Answer<File>() {

        @Override
        public File answer(InvocationOnMock invocation) throws Throwable {
            URL url = (URL) invocation.getArguments()[0];
            String path = url.getPath();
            if (PlatformDetectionUtil.isWin() && path.startsWith("/")) {
                path = path.substring(1, path.length());
            }
            return new File(url.getPath());
        }
    });
    // PluginWorkspaceProvider.getPluginWorkspace().getUtilAccess().getFileName()
    Mockito.when(utilAccessMock.getFileName(Mockito.anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            String file = (String) invocation.getArguments()[0];
            file = file.replace('\\', '/');
            int index = file.lastIndexOf("/");
            return index != -1 ? file.substring(index + 1) : file;
        }
    });
    Mockito.when(utilAccessMock.uncorrectURL(Mockito.anyString())).then(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArguments()[0].toString().replace("%20", " ");
        }
    });
    ImageUtilities imgUtils = Mockito.mock(ImageUtilities.class);
    Mockito.when(pluginWSMock.getImageUtilities()).thenReturn(imgUtils);
    Mockito.when(imgUtils.loadIcon((URL) Mockito.any())).thenAnswer(new Answer<ImageIcon>() {

        @Override
        public ImageIcon answer(InvocationOnMock invocation) throws Throwable {
            URL url = (URL) invocation.getArguments()[0];
            return new ImageIcon(url);
        }
    });
    ProjectController projectCtrlMock = Mockito.mock(ProjectController.class);
    Mockito.when(pluginWSMock.getProjectManager()).thenReturn(projectCtrlMock);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            return null;
        }
    }).when(projectCtrlMock).refreshFolders(Mockito.any());
    WSOptionsStorage wsOptions = new TestWsOptionsStorage();
    Mockito.when(pluginWSMock.getOptionsStorage()).thenReturn(wsOptions);
    installGitProtocol();
    GitAccess gitAccess = GitAccess.getInstance();
    GitController ctrl = new GitController(gitAccess);
    ctrl.addGitListener(new GitEventAdapter() {

        private Repository oldRepository;

        @Override
        public void operationAboutToStart(GitEventInfo info) {
            if (info.getGitOperation() == GitOperation.OPEN_WORKING_COPY) {
                try {
                    oldRepository = gitAccess.getRepository();
                } catch (NoRepositorySelected e) {
                // Ignore
                }
            }
        }

        @Override
        public void operationSuccessfullyEnded(GitEventInfo info) {
            if (info.getGitOperation() == GitOperation.OPEN_WORKING_COPY) {
                if (oldRepository != null) {
                    loadedRepos.remove(oldRepository);
                }
                oldRepository = null;
            }
        }

        @Override
        public void operationFailed(GitEventInfo info, Throwable t) {
            if (info.getGitOperation() == GitOperation.OPEN_WORKING_COPY) {
                oldRepository = null;
            }
        }
    });
    OptionsManager.getInstance().loadOptions(wsOptions);
    gitAccess.getStatusCache().installEditorsHook(pluginWSMock);
}
Also used : ImageUtilities(ro.sync.exml.workspace.api.images.ImageUtilities) ImageIcon(javax.swing.ImageIcon) GitController(com.oxygenxml.git.view.event.GitController) UtilAccess(ro.sync.exml.workspace.api.util.UtilAccess) XMLUtilAccess(ro.sync.exml.workspace.api.util.XMLUtilAccess) URL(java.net.URL) ColorTheme(ro.sync.exml.workspace.api.util.ColorTheme) XMLUtilAccess(ro.sync.exml.workspace.api.util.XMLUtilAccess) WSOptionsStorage(ro.sync.exml.workspace.api.options.WSOptionsStorage) ProjectController(ro.sync.exml.workspace.api.standalone.project.ProjectController) Repository(org.eclipse.jgit.lib.Repository) GitEventInfo(com.oxygenxml.git.view.event.GitEventInfo) PanelRefresh(com.oxygenxml.git.view.refresh.PanelRefresh) WSEditorChangeListener(ro.sync.exml.workspace.api.listeners.WSEditorChangeListener) WSEditor(ro.sync.exml.workspace.api.editor.WSEditor) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StandalonePluginWorkspace(ro.sync.exml.workspace.api.standalone.StandalonePluginWorkspace) File(java.io.File)

Example 27 with GitController

use of com.oxygenxml.git.view.event.GitController in project oxygen-git-client-addon by oxygenxml.

the class PullSubmoduleUpdateTest method testDoNotUpdate.

/**
 * <p><b>Description:</b> If the option is disabled, do not update submodules on pull.</p>
 * <p><b>Bug ID:</b> EXM-47461</p>
 *
 * @author alex_jitianu
 *
 * @throws Exception If it fails.
 */
public void testDoNotUpdate() throws Exception {
    try {
        Repository submoduleRepo = createRepository("target/test-resources/PullSubmoduleUpdate_sub");
        String fileName = "file.txt";
        TestUtil.commitOneFile(submoduleRepo, fileName, "version 1");
        // Committing a file in the remote makes required initializations.
        Repository remote = createRepository("target/test-resources/PullSubmodule_main_remote");
        TestUtil.commitOneFile(remote, "base.txt", "base");
        setupSubmodule(remote, submoduleRepo, "sub");
        Repository db2 = createRepository("target/test-resources/PullSubmoduleUpdate_main");
        bindLocalToRemote(db2, remote);
        GitController ctrl = new GitController();
        GitAccess.getInstance().setGit(new Git(db2));
        ctrl.pull().get();
        String content = TestUtil.read(new File(db2.getWorkTree(), "sub/file.txt").toURI().toURL());
        assertEquals("The submodules must be initialized and updated", "version 1", content);
        // Move the submodule target forward.
        TestUtil.commitOneFile(submoduleRepo, fileName, "version 2");
        // Change the submodule to the last commit from target.
        updateSubmoduleToBranchHead(remote, "sub");
        OptionsManager.getInstance().setUpdateSubmodulesOnPull(false);
        // Pull again.
        GitAccess.getInstance().setGit(new Git(db2));
        ctrl.pull().get();
        content = TestUtil.read(new File(db2.getWorkTree(), "sub/file.txt").toURI().toURL());
        assertEquals("The submodules must no be initialized and updated automatically", "version 1", content);
    } finally {
        GitAccess.getInstance().setGit(null);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) GitController(com.oxygenxml.git.view.event.GitController) File(java.io.File)

Example 28 with GitController

use of com.oxygenxml.git.view.event.GitController in project oxygen-git-client-addon by oxygenxml.

the class PullSubmoduleUpdateTest method testUpdate.

/**
 * <p><b>Description:</b> Update submodules on pull.</p>
 * <p><b>Bug ID:</b> EXM-47461</p>
 *
 * @author alex_jitianu
 *
 * @throws Exception If it fails.
 */
@Test
public void testUpdate() throws Exception {
    try {
        Repository submoduleRepo = createRepository("target/test-resources/PullSubmoduleUpdate_sub");
        String fileName = "file.txt";
        TestUtil.commitOneFile(submoduleRepo, fileName, "version 1");
        // Committing a file in the remote makes required initializations.
        Repository remote = createRepository("target/test-resources/PullSubmodule_main_remote");
        TestUtil.commitOneFile(remote, "base.txt", "base");
        setupSubmodule(remote, submoduleRepo, "sub");
        Repository db2 = createRepository("target/test-resources/PullSubmoduleUpdate_main");
        bindLocalToRemote(db2, remote);
        GitController ctrl = new GitController();
        GitAccess.getInstance().setGit(new Git(db2));
        ctrl.pull().get();
        String content = TestUtil.read(new File(db2.getWorkTree(), "sub/file.txt").toURI().toURL());
        assertEquals("The submodules must be initialized and updated", "version 1", content);
        // Move the submodule target forward.
        TestUtil.commitOneFile(submoduleRepo, fileName, "version 2");
        // Change the submodule to the last commit from target.
        updateSubmoduleToBranchHead(remote, "sub");
        // Pull again.
        GitAccess.getInstance().setGit(new Git(db2));
        ctrl.pull().get();
        content = TestUtil.read(new File(db2.getWorkTree(), "sub/file.txt").toURI().toURL());
        assertEquals("The submodules must be initialized and updated", "version 2", content);
    } finally {
        GitAccess.getInstance().setGit(null);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) GitController(com.oxygenxml.git.view.event.GitController) File(java.io.File) Test(org.junit.Test)

Example 29 with GitController

use of com.oxygenxml.git.view.event.GitController in project oxygen-git-client-addon by oxygenxml.

the class PullSubmoduleUpdateTest method testUpdateRecursively.

/**
 * <p><b>Description:</b> Update submodules on pull.</p>
 * <p><b>Bug ID:</b> EXM-47461</p>
 *
 * @author alex_jitianu
 *
 * @throws Exception If it fails.
 */
public void testUpdateRecursively() throws Exception {
    try {
        Repository submoduleRepoLvl1 = createRepository("target/test-resources/PullSubmoduleUpdate_sub");
        String fileName = "file.txt";
        TestUtil.commitOneFile(submoduleRepoLvl1, fileName, "version 1");
        // Committing a file in the remote makes required initializations.
        Repository submoduleRepoLvl2 = createRepository("target/test-resources/PullSubmodule_main_remote");
        TestUtil.commitOneFile(submoduleRepoLvl2, "base.txt", "base");
        setupSubmodule(submoduleRepoLvl2, submoduleRepoLvl1, "sub");
        // Committing a file in the remote makes required initializations.
        Repository remote2 = createRepository("target/test-resources/PullSubmodule_main_remote_2");
        TestUtil.commitOneFile(remote2, "main.txt", "main");
        setupSubmodule(remote2, submoduleRepoLvl2, "main");
        // Assert submodules.
        assertEquals("The submodules must be initialized and updated", "base", TestUtil.read(new File(remote2.getWorkTree(), "main/base.txt").toURI().toURL()));
        assertEquals("The submodules must be initialized and updated", "version 1", TestUtil.read(new File(remote2.getWorkTree(), "main/sub/file.txt").toURI().toURL()));
        // Main entry point.
        Repository db2 = createRepository("target/test-resources/PullSubmoduleUpdate_main");
        bindLocalToRemote(db2, remote2);
        GitController ctrl = new GitController();
        GitAccess.getInstance().setGit(new Git(db2));
        ctrl.pull().get();
        assertEquals("The submodules must be initialized and updated", "base", TestUtil.read(new File(db2.getWorkTree(), "main/base.txt").toURI().toURL()));
        assertEquals("The submodules must be initialized and updated", "version 1", TestUtil.read(new File(db2.getWorkTree(), "main/sub/file.txt").toURI().toURL()));
    } finally {
        GitAccess.getInstance().setGit(null);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) GitController(com.oxygenxml.git.view.event.GitController) File(java.io.File)

Example 30 with GitController

use of com.oxygenxml.git.view.event.GitController in project oxygen-git-client-addon by oxygenxml.

the class GitAccessConflictTest method testPullWithConflicts_Rebase_RestartMerge.

/**
 * Pull (rebase) with conflict. Restart merge.
 *
 * @throws Exception
 */
@Test
public void testPullWithConflicts_Rebase_RestartMerge() throws Exception {
    // ----------------
    // LOCAL 1
    // ----------------
    gitAccess = GitAccess.getInstance();
    gitAccess.setRepositorySynchronously(FIRST_LOCAL_TEST_REPOSITPRY);
    // Create a file in the remote.
    File remoteParent = new File(FIRST_LOCAL_TEST_REPOSITPRY);
    remoteParent.mkdirs();
    File local1File = new File(FIRST_LOCAL_TEST_REPOSITPRY, "test.txt");
    writeToFile(local1File, "original");
    gitAccess.add(new FileStatus(GitChangeType.ADD, "test.txt"));
    gitAccess.commit("Primul");
    push("", "");
    // ----------------
    // LOCAL 2
    // ----------------
    gitAccess.setRepositorySynchronously(SECOND_LOCAL_TEST_REPOSITORY);
    PullResponse pull = pull("", "", PullType.MERGE_FF, false);
    assertEquals(PullStatus.OK.toString(), pull.getStatus().toString());
    File local2File = new File(SECOND_LOCAL_TEST_REPOSITORY, "test.txt");
    assertEquals("original", getFileContent(local2File));
    writeToFile(local2File, "changed in local 2");
    gitAccess.add(new FileStatus(GitChangeType.ADD, "test.txt"));
    gitAccess.commit("Al doilea");
    push("", "");
    // ----------------
    // LOCAL 1
    // ----------------
    gitAccess.setRepositorySynchronously(FIRST_LOCAL_TEST_REPOSITPRY);
    writeToFile(local1File, "changed in local 1");
    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
    assertEquals("changed in local 1", getFileContent(local1File));
    gitAccess.add(new FileStatus(GitChangeType.ADD, "test.txt"));
    gitAccess.commit("Another");
    pc.pull(PullType.REBASE).get();
    assertNull(pullFailedMessage[0]);
    assertFalse(wasRebaseInterrupted[0]);
    assertEquals("Status: CONFLICTS Conflicting files: [test.txt]", pullWithConflictsSB.toString());
    assertTrue(getFileContent(local1File).startsWith("<<<<<<< Upstream, based on branch '" + GitAccess.DEFAULT_BRANCH_NAME + "' of file:"));
    // Show the "Interrupted rebase" dialog
    pc.pull(PullType.REBASE).get();
    assertTrue(wasRebaseInterrupted[0]);
    Status status = gitAccess.getGit().status().call();
    assertEquals("[test.txt]", status.getConflicting().toString());
    assertTrue(getFileContent(local1File).startsWith("<<<<<<< Upstream, based on branch '" + GitAccess.DEFAULT_BRANCH_NAME + "' of file:"));
    GitControllerBase gitCtrl = new GitControllerBase(gitAccess) {

        @Override
        protected boolean isUserOKWithResolvingRebaseConflictUsingMineOrTheirs(ConflictResolution cmd) {
            return cmd == ConflictResolution.RESOLVE_USING_THEIRS;
        }
    };
    gitCtrl.asyncResolveUsingTheirs(Arrays.asList(new FileStatus(GitChangeType.CONFLICT, "test.txt")));
    sleep(1000);
    // When having a conflict while rebasing, 'Mine' and 'Theirs' become reversed
    assertEquals("When having a conflict while rebasing, 'Mine' and 'Theirs' become reversed ", "changed in local 1", getFileContent(local1File));
    GitStatus gitStatus = gitAccess.getStatus();
    assertEquals(1, gitStatus.getStagedFiles().size());
    assertEquals("(changeType=CHANGED, fileLocation=test.txt)", gitStatus.getStagedFiles().get(0).toString());
    assertTrue(gitStatus.getUnstagedFiles().isEmpty());
    RepositoryState repositoryState = gitAccess.getRepository().getRepositoryState();
    assertEquals(RepositoryState.REBASING_MERGE, repositoryState);
    assertEquals("changed in local 1", getFileContent(local1File));
    // Restart merge
    gitAccess.restartMerge();
    sleep(1000);
    repositoryState = gitAccess.getRepository().getRepositoryState();
    assertEquals(RepositoryState.REBASING_MERGE, repositoryState);
    gitStatus = gitAccess.getStatus();
    assertTrue(gitStatus.getStagedFiles().isEmpty());
    assertEquals("[(changeType=CONFLICT, fileLocation=test.txt)]", gitStatus.getUnstagedFiles().toString());
    assertTrue(getFileContent(local1File).startsWith("<<<<<<< Upstream, based on branch '" + GitAccess.DEFAULT_BRANCH_NAME + "' of file:"));
}
Also used : Status(org.eclipse.jgit.api.Status) FileStatus(com.oxygenxml.git.service.entities.FileStatus) FileStatus(com.oxygenxml.git.service.entities.FileStatus) GitController(com.oxygenxml.git.view.event.GitController) RepositoryState(org.eclipse.jgit.lib.RepositoryState) List(java.util.List) File(java.io.File) Test(org.junit.Test)

Aggregations

GitController (com.oxygenxml.git.view.event.GitController)66 File (java.io.File)54 FileStatus (com.oxygenxml.git.service.entities.FileStatus)48 JButton (javax.swing.JButton)29 Test (org.junit.Test)28 JDialog (javax.swing.JDialog)24 Repository (org.eclipse.jgit.lib.Repository)21 AbstractAction (javax.swing.AbstractAction)19 GitTreeNode (com.oxygenxml.git.view.GitTreeNode)18 GitActionsManager (com.oxygenxml.git.view.actions.GitActionsManager)18 JFrame (javax.swing.JFrame)15 StagingPanel (com.oxygenxml.git.view.staging.StagingPanel)14 GitControllerBase (com.oxygenxml.git.service.GitControllerBase)13 ArrayList (java.util.ArrayList)12 List (java.util.List)12 JTextField (javax.swing.JTextField)10 BranchManagementPanel (com.oxygenxml.git.view.branches.BranchManagementPanel)8 BranchTreeMenuActionsProvider (com.oxygenxml.git.view.branches.BranchTreeMenuActionsProvider)7 ToolbarPanel (com.oxygenxml.git.view.staging.ToolbarPanel)7 JCheckBox (javax.swing.JCheckBox)7