Search in sources :

Example 1 with HistoryController

use of com.oxygenxml.git.view.history.HistoryController in project oxygen-git-client-addon by oxygenxml.

the class BlameTest method testBlameFromProjectAndEditorPage_askUserSaveEditor.

/**
 * <p><b>Description:</b> ask save editor before blame if editor modified.</p>
 * <p><b>Bug ID:</b> EXM-45008</p>
 *
 * @author sorin_carbunaru
 *
 * @throws Exception
 */
@Test
public void testBlameFromProjectAndEditorPage_askUserSaveEditor() throws Exception {
    URL script = getClass().getClassLoader().getResource("scripts/blame_script.txt");
    File wcTree = new File("target/gen/BlameTest_testBlame");
    RepoGenerationScript.generateRepository(script, wcTree);
    try {
        StandalonePluginWorkspace pluginWSMock = Mockito.mock(StandalonePluginWorkspace.class);
        PluginWorkspaceProvider.setPluginWorkspace(pluginWSMock);
        Mockito.when(pluginWSMock.getParentFrame()).thenReturn(new JFrame());
        Mockito.when(pluginWSMock.open(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(true);
        // Mock util access
        UtilAccess utilAcces = Mockito.mock(UtilAccess.class);
        Mockito.when(utilAcces.correctURL(Mockito.anyString())).thenReturn("");
        Mockito.when(pluginWSMock.getUtilAccess()).thenReturn(utilAcces);
        // Mock confirmation
        boolean[] confirmDialogShown = new boolean[1];
        Mockito.doAnswer(new Answer<Integer>() {

            @Override
            public Integer answer(InvocationOnMock invocation) throws Throwable {
                confirmDialogShown[0] = true;
                return 0;
            }
        }).when(pluginWSMock).showConfirmDialog(Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.any());
        GitAccess.getInstance().setRepositorySynchronously(wcTree.getAbsolutePath());
        String content = "Line 1\n" + "Line 2\n" + "Line 3\n" + "Line 4\n" + "Line 5";
        HashMap<Integer, int[]> line2offsets = computeLineMappings(content);
        // Mock editor and page
        boolean[] editorSaved = new boolean[1];
        WSEditor wsEditor = Mockito.mock(WSEditor.class);
        Mockito.when(wsEditor.isModified()).thenReturn(true);
        Mockito.when(pluginWSMock.getEditorAccess(Mockito.any(), Mockito.anyInt())).thenReturn(wsEditor);
        Mockito.doAnswer(new Answer<Void>() {

            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                editorSaved[0] = true;
                return null;
            }
        }).when(wsEditor).save();
        Mockito.doNothing().when(wsEditor).changePage(Mockito.anyString());
        WSXMLTextEditorPage page = Mockito.mock(WSXMLTextEditorPage.class);
        JTextArea textArea = new JTextArea();
        Mockito.when(page.getTextComponent()).thenReturn(textArea);
        Mockito.when(wsEditor.getCurrentPage()).thenReturn(page);
        // Methods for mapping lines to offsets and back.
        Mockito.when(page.getOffsetOfLineStart(Mockito.anyInt())).thenAnswer(new Answer<Integer>() {

            @Override
            public Integer answer(InvocationOnMock invocation) throws Throwable {
                Object key = invocation.getArguments()[0];
                return line2offsets.get(key)[0];
            }
        });
        Mockito.when(page.getOffsetOfLineEnd(Mockito.anyInt())).thenAnswer(new Answer<Integer>() {

            @Override
            public Integer answer(InvocationOnMock invocation) throws Throwable {
                return line2offsets.get(invocation.getArguments()[0])[1];
            }
        });
        Mockito.when(page.getLineOfOffset(Mockito.anyInt())).thenAnswer(new Answer<Integer>() {

            @Override
            public Integer answer(InvocationOnMock invocation) throws Throwable {
                int offset = (int) invocation.getArguments()[0];
                for (Integer line : line2offsets.keySet()) {
                    int[] is = line2offsets.get(line);
                    if (is[0] <= offset && offset < is[1]) {
                        return line;
                    }
                }
                return -1;
            }
        });
        // Intercept history view requests.
        final List<RevCommit> commits = new ArrayList<>();
        HistoryController historyController = Mockito.mock(HistoryController.class);
        Mockito.doAnswer(new Answer<Void>() {

            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                RevCommit rev = (RevCommit) invocation.getArguments()[1];
                commits.add(rev);
                return null;
            }
        }).when(historyController).showCommit(Mockito.anyString(), Mockito.any());
        Mockito.when(historyController.isHistoryShowing()).thenReturn(Boolean.TRUE);
        textArea.setText(content);
        flushAWT();
        // Execute blame.
        ProjectAndEditorPageMenuActionsUtil.showBlame(new File(wcTree, "file1.txt"), historyController, Collections.emptyList());
        assertTrue(confirmDialogShown[0]);
        Highlight[] highlights = textArea.getHighlighter().getHighlights();
        assertEquals(5, highlights.length);
        String expected = dumpOffsetMap(line2offsets);
        String actual = dumpHighlights(highlights);
        assertEquals(expected, actual);
        // Activate each highlight and collect the requests done to the history view.
        for (Highlight highlight : highlights) {
            textArea.setCaretPosition(highlight.getStartOffset());
            // Wait for the thread that presents the revision.
            sleep(400);
            flushAWT();
        }
        // Asserts the revisions activated in the history view.
        assertEquals("First commit.\n" + "Change 2\n" + "Change 3\n" + "Change 4\n" + "Change 5\n" + "", dumpCommits(commits));
    } finally {
        GitAccess.getInstance().closeRepo();
        FileUtils.deleteDirectory(wcTree);
    }
}
Also used : JTextArea(javax.swing.JTextArea) Highlight(javax.swing.text.Highlighter.Highlight) ArrayList(java.util.ArrayList) UtilAccess(ro.sync.exml.workspace.api.util.UtilAccess) URL(java.net.URL) HistoryController(com.oxygenxml.git.view.history.HistoryController) JFrame(javax.swing.JFrame) RevCommit(org.eclipse.jgit.revwalk.RevCommit) WSXMLTextEditorPage(ro.sync.exml.workspace.api.editor.page.text.xml.WSXMLTextEditorPage) 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) Test(org.junit.Test)

Example 2 with HistoryController

use of com.oxygenxml.git.view.history.HistoryController in project oxygen-git-client-addon by oxygenxml.

the class BlameTest method testShowBlameFromStagingArea_askUserConfirmation.

/**
 * <p><b>Description:</b> Show blame from staging area. Ask save editor before blame if editor modified.</p>
 * <p><b>Bug ID:</b> EXM-45008</p>
 *
 * @author sorin_carbunaru
 *
 * @throws Exception
 */
@Test
public void testShowBlameFromStagingArea_askUserConfirmation() throws Exception {
    URL script = getClass().getClassLoader().getResource("scripts/blame_script.txt");
    File wcTree = new File("target/gen/BlameTest_testBlame");
    RepoGenerationScript.generateRepository(script, wcTree);
    try {
        GitAccess.getInstance().setRepositorySynchronously(wcTree.getAbsolutePath());
        String content = "Line 1\n" + "Line 2\n" + "Line 3\n" + "Line 4\n" + "Line 5";
        HashMap<Integer, int[]> line2offsets = computeLineMappings(content);
        // Mock plugin workspace
        StandalonePluginWorkspace pluginWSMock = Mockito.mock(StandalonePluginWorkspace.class);
        PluginWorkspaceProvider.setPluginWorkspace(pluginWSMock);
        // Mock "open()"
        Mockito.when(pluginWSMock.open(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(true);
        // Mock util access
        UtilAccess utilAcces = Mockito.mock(UtilAccess.class);
        Mockito.when(utilAcces.correctURL(Mockito.anyString())).thenReturn("");
        Mockito.when(pluginWSMock.getUtilAccess()).thenReturn(utilAcces);
        // Mock confirmation
        boolean[] confirmDialogShown = new boolean[1];
        Mockito.doAnswer(new Answer<Integer>() {

            @Override
            public Integer answer(InvocationOnMock invocation) throws Throwable {
                confirmDialogShown[0] = true;
                return 0;
            }
        }).when(pluginWSMock).showConfirmDialog(Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.any());
        // Mock editor and page
        boolean[] editorSaved = new boolean[1];
        WSEditor wsEditor = Mockito.mock(WSEditor.class);
        Mockito.when(wsEditor.isModified()).thenReturn(true);
        Mockito.when(pluginWSMock.getEditorAccess(Mockito.any(), Mockito.anyInt())).thenReturn(wsEditor);
        Mockito.doAnswer(new Answer<Void>() {

            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                editorSaved[0] = true;
                return null;
            }
        }).when(wsEditor).save();
        Mockito.doNothing().when(wsEditor).changePage(Mockito.anyString());
        WSXMLTextEditorPage page = Mockito.mock(WSXMLTextEditorPage.class);
        JTextArea textArea = new JTextArea();
        Mockito.when(page.getTextComponent()).thenReturn(textArea);
        Mockito.when(wsEditor.getCurrentPage()).thenReturn(page);
        // Methods for mapping lines to offsets and back.
        Mockito.when(page.getOffsetOfLineStart(Mockito.anyInt())).thenAnswer(new Answer<Integer>() {

            @Override
            public Integer answer(InvocationOnMock invocation) throws Throwable {
                Object key = invocation.getArguments()[0];
                return line2offsets.get(key)[0];
            }
        });
        Mockito.when(page.getOffsetOfLineEnd(Mockito.anyInt())).thenAnswer(new Answer<Integer>() {

            @Override
            public Integer answer(InvocationOnMock invocation) throws Throwable {
                return line2offsets.get(invocation.getArguments()[0])[1];
            }
        });
        Mockito.when(page.getLineOfOffset(Mockito.anyInt())).thenAnswer(new Answer<Integer>() {

            @Override
            public Integer answer(InvocationOnMock invocation) throws Throwable {
                int offset = (int) invocation.getArguments()[0];
                for (Integer line : line2offsets.keySet()) {
                    int[] is = line2offsets.get(line);
                    if (is[0] <= offset && offset < is[1]) {
                        return line;
                    }
                }
                return -1;
            }
        });
        // Intercept history view requests.
        final List<RevCommit> commits = new ArrayList<>();
        HistoryController historyController = Mockito.mock(HistoryController.class);
        Mockito.doAnswer(new Answer<Void>() {

            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                RevCommit rev = (RevCommit) invocation.getArguments()[1];
                commits.add(rev);
                return null;
            }
        }).when(historyController).showCommit(Mockito.anyString(), Mockito.any());
        Mockito.when(historyController.isHistoryShowing()).thenReturn(Boolean.TRUE);
        textArea.setText(content);
        flushAWT();
        // >>> Show blame <<<
        ShowBlameForUnstagedResourceAction showBlameAction = new ShowBlameForUnstagedResourceAction(historyController, new SelectedResourcesProvider() {

            @Override
            public List<FileStatus> getOnlySelectedLeaves() {
                return null;
            }

            @Override
            public List<FileStatus> getAllSelectedResources() {
                return Arrays.asList(new FileStatus(GitChangeType.MODIFIED, "file1.txt"));
            }
        });
        showBlameAction.actionPerformed(null);
        assertTrue(confirmDialogShown[0]);
        assertTrue(editorSaved[0]);
        Highlight[] highlights = textArea.getHighlighter().getHighlights();
        assertEquals(5, highlights.length);
        String expected = dumpOffsetMap(line2offsets);
        String actual = dumpHighlights(highlights);
        assertEquals(expected, actual);
    } finally {
        GitAccess.getInstance().closeRepo();
        FileUtils.deleteDirectory(wcTree);
    }
}
Also used : JTextArea(javax.swing.JTextArea) FileStatus(com.oxygenxml.git.service.entities.FileStatus) Highlight(javax.swing.text.Highlighter.Highlight) SelectedResourcesProvider(com.oxygenxml.git.view.staging.ChangesPanel.SelectedResourcesProvider) ArrayList(java.util.ArrayList) UtilAccess(ro.sync.exml.workspace.api.util.UtilAccess) URL(java.net.URL) HistoryController(com.oxygenxml.git.view.history.HistoryController) ArrayList(java.util.ArrayList) List(java.util.List) RevCommit(org.eclipse.jgit.revwalk.RevCommit) WSXMLTextEditorPage(ro.sync.exml.workspace.api.editor.page.text.xml.WSXMLTextEditorPage) ShowBlameForUnstagedResourceAction(com.oxygenxml.git.view.staging.actions.ShowBlameForUnstagedResourceAction) 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) Test(org.junit.Test)

Example 3 with HistoryController

use of com.oxygenxml.git.view.history.HistoryController in project oxygen-git-client-addon by oxygenxml.

the class BlameTest method testBlame.

/**
 * Tests the blame highlights on the text page.
 *
 * @throws Exception If it fails.
 */
@Test
public void testBlame() throws Exception {
    URL script = getClass().getClassLoader().getResource("scripts/blame_script.txt");
    File wcTree = new File("target/gen/BlameTest_testBlame");
    RepoGenerationScript.generateRepository(script, wcTree);
    try {
        GitAccess.getInstance().setRepositorySynchronously(wcTree.getAbsolutePath());
        String content = "Line 1\n" + "Line 2\n" + "Line 3\n" + "Line 4\n" + "Line 5";
        HashMap<Integer, int[]> line2offsets = computeLineMappings(content);
        // Use Mockito to mock the Oxygen API.
        WSEditor wsEditor = Mockito.mock(WSEditor.class);
        WSXMLTextEditorPage page = Mockito.mock(WSXMLTextEditorPage.class);
        JTextArea textArea = new JTextArea();
        Mockito.when(page.getTextComponent()).thenReturn(textArea);
        Mockito.when(wsEditor.getCurrentPage()).thenReturn(page);
        // Methods for mapping lines to offsets and back.
        Mockito.when(page.getOffsetOfLineStart(Mockito.anyInt())).thenAnswer(new Answer<Integer>() {

            @Override
            public Integer answer(InvocationOnMock invocation) throws Throwable {
                Object key = invocation.getArguments()[0];
                return line2offsets.get(key)[0];
            }
        });
        Mockito.when(page.getOffsetOfLineEnd(Mockito.anyInt())).thenAnswer(new Answer<Integer>() {

            @Override
            public Integer answer(InvocationOnMock invocation) throws Throwable {
                return line2offsets.get(invocation.getArguments()[0])[1];
            }
        });
        Mockito.when(page.getLineOfOffset(Mockito.anyInt())).thenAnswer(new Answer<Integer>() {

            @Override
            public Integer answer(InvocationOnMock invocation) throws Throwable {
                int offset = (int) invocation.getArguments()[0];
                for (Integer line : line2offsets.keySet()) {
                    int[] is = line2offsets.get(line);
                    if (is[0] <= offset && offset < is[1]) {
                        return line;
                    }
                }
                return -1;
            }
        });
        // Intercept history view requests.
        final List<RevCommit> commits = new ArrayList<>();
        HistoryController historyController = Mockito.mock(HistoryController.class);
        Mockito.doAnswer(new Answer<Void>() {

            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                RevCommit rev = (RevCommit) invocation.getArguments()[1];
                commits.add(rev);
                return null;
            }
        }).when(historyController).showCommit(Mockito.anyString(), Mockito.any());
        Mockito.when(historyController.isHistoryShowing()).thenReturn(Boolean.TRUE);
        textArea.setText(content);
        flushAWT();
        // Execute blame.
        new BlamePerformer().doit(GitAccess.getInstance().getRepository(), "file1.txt", wsEditor, historyController);
        Highlight[] highlights = textArea.getHighlighter().getHighlights();
        assertEquals(5, highlights.length);
        String expected = dumpOffsetMap(line2offsets);
        String actual = dumpHighlights(highlights);
        assertEquals(expected, actual);
        // Activate each highlight and collect the requests done to the historyview.
        for (Highlight highlight : highlights) {
            textArea.setCaretPosition(highlight.getStartOffset());
            // Wait for the thread that presents the revision.
            sleep(400);
            flushAWT();
        }
        // Asserts the revisions activated in the history view.
        assertEquals("First commit.\n" + "Change 2\n" + "Change 3\n" + "Change 4\n" + "Change 5\n" + "", dumpCommits(commits));
    } finally {
        GitAccess.getInstance().closeRepo();
        FileUtils.deleteDirectory(wcTree);
    }
}
Also used : WSXMLTextEditorPage(ro.sync.exml.workspace.api.editor.page.text.xml.WSXMLTextEditorPage) BlamePerformer(com.oxygenxml.git.view.blame.BlamePerformer) JTextArea(javax.swing.JTextArea) Highlight(javax.swing.text.Highlighter.Highlight) ArrayList(java.util.ArrayList) URL(java.net.URL) HistoryController(com.oxygenxml.git.view.history.HistoryController) WSEditor(ro.sync.exml.workspace.api.editor.WSEditor) InvocationOnMock(org.mockito.invocation.InvocationOnMock) File(java.io.File) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Aggregations

HistoryController (com.oxygenxml.git.view.history.HistoryController)3 File (java.io.File)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 JTextArea (javax.swing.JTextArea)3 Highlight (javax.swing.text.Highlighter.Highlight)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 Test (org.junit.Test)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 WSEditor (ro.sync.exml.workspace.api.editor.WSEditor)3 WSXMLTextEditorPage (ro.sync.exml.workspace.api.editor.page.text.xml.WSXMLTextEditorPage)3 StandalonePluginWorkspace (ro.sync.exml.workspace.api.standalone.StandalonePluginWorkspace)2 UtilAccess (ro.sync.exml.workspace.api.util.UtilAccess)2 FileStatus (com.oxygenxml.git.service.entities.FileStatus)1 BlamePerformer (com.oxygenxml.git.view.blame.BlamePerformer)1 SelectedResourcesProvider (com.oxygenxml.git.view.staging.ChangesPanel.SelectedResourcesProvider)1 ShowBlameForUnstagedResourceAction (com.oxygenxml.git.view.staging.actions.ShowBlameForUnstagedResourceAction)1 List (java.util.List)1 JFrame (javax.swing.JFrame)1