use of com.oxygenxml.git.service.GitAccess 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));
}
use of com.oxygenxml.git.service.GitAccess in project oxygen-git-client-addon by oxygenxml.
the class DiffPresenterTest method testCompareSubmodule.
/**
* <p><b>Description:</b> Checks the URL used in a rebase conflicts.</p>
* <p><b>Bug ID:</b> EXM-45843</p>
*
* @author bogdan_dragici
*
* @throws Exception
*/
@Test
public void testCompareSubmodule() throws Exception {
GitAccess gitAccess = GitAccess.getInstance();
// PARENT repos
String localTestRepositoryP = "target/test-resources/localCS";
String remoteTestRepositoryP = "target/test-resources/remoteCS";
Repository remoteRepoP = createRepository(remoteTestRepositoryP);
Repository localRepoP = createRepository(localTestRepositoryP);
bindLocalToRemote(localRepoP, remoteRepoP);
// SUBMODULE repos
String remoteTestRepositorySubModule = "target/test-resources/remoteCS-SubModule/";
Repository remoteRepoSubModule = createRepository(remoteTestRepositorySubModule);
// Commit (very important)
gitAccess.commit("Commit");
// Set the PARENT repo as the current one, to which we'll add the submodule
gitAccess.setRepositorySynchronously(localTestRepositoryP);
// Add SUBMODULE
SubmoduleAddCommand addCommand = gitAccess.getGit().submoduleAdd();
addCommand.setURI(remoteRepoSubModule.getDirectory().toURI().toString());
addCommand.setPath("modules/submodule");
Repository subRepo = addCommand.call();
subRepo.close();
File parentWorkDir = gitAccess.getRepository().getWorkTree();
assertTrue(new File(parentWorkDir, "modules/submodule").isDirectory());
assertTrue(new File(parentWorkDir, ".gitmodules").isFile());
// Check the SUBMODULE
Map<String, SubmoduleStatus> submodules = gitAccess.getGit().submoduleStatus().call();
assertEquals(1, submodules.size());
SubmoduleStatus status = submodules.get("modules/submodule");
assertNotNull(status);
assertEquals(SubmoduleStatusType.INITIALIZED, status.getType());
// SHOW DIFF
DiffPresenter.showDiff(// The submodule
gitAccess.getStagedFiles().get(1), Mockito.mock(GitControllerBase.class));
assertNotNull(leftDiff);
assertNotNull(rightDiff);
String left = "git://CurrentSubmodule/modules/submodule.txt";
assertEquals(left, leftDiff.toString());
assertTrue(TestUtil.read(new URL(left)).startsWith("Subproject commit "));
String right = "git://PreviousSubmodule/modules/submodule.txt";
assertEquals(right, rightDiff.toString());
assertTrue(TestUtil.read(new URL(right)).startsWith("Subproject commit "));
}
use of com.oxygenxml.git.service.GitAccess in project oxygen-git-client-addon by oxygenxml.
the class RefreshProjectTest method testRefreshProjectOnDiscard_3.
/**
* Refresh on submodule discard.
*
* @throws Exception
*/
@PrepareForTest({ GitAccess.class })
public void testRefreshProjectOnDiscard_3() throws Exception {
File repoDir = new File(localTestRepoPath);
repoDir.mkdirs();
File subModule = new File(localTestRepoPath, "subModule");
subModule.mkdir();
try {
GitAccess gitAccessMock = PowerMockito.mock(GitAccess.class);
Whitebox.setInternalState(GitAccess.class, "instance", gitAccessMock);
SubmoduleAccess submoduleAccess = Mockito.mock(SubmoduleAccess.class);
Mockito.doNothing().when(submoduleAccess).discardSubmodule();
PowerMockito.when(gitAccessMock.getSubmoduleAccess()).thenReturn(submoduleAccess);
DiscardAction discardAction = new DiscardAction(new SelectedResourcesProvider() {
@Override
public List<FileStatus> getOnlySelectedLeaves() {
return null;
}
@Override
public List<FileStatus> getAllSelectedResources() {
return Arrays.asList(new FileStatus(GitChangeType.SUBMODULE, "subModule"));
}
}, // A mock that does nothing.
Mockito.mock(GitControllerBase.class));
discardAction.actionPerformed(null);
assertEquals(subModule.getCanonicalFile().getAbsolutePath(), refreshedFolder.getAbsolutePath());
} finally {
FileUtils.deleteDirectory(repoDir);
}
}
use of com.oxygenxml.git.service.GitAccess in project oxygen-git-client-addon by oxygenxml.
the class HistoryContextualActionsTest method testCheckoutFileAction.
/**
* <p>
* <b>Description:</b> Tests the
* com.oxygenxml.git.view.history.HistoryViewContextualMenuPresenter.createCheckoutFileAction(
* String, FileStatus, boolean) API.
* </p>
*
* <p>
* <b>Bug ID:</b> EXM-46986
* </p>
*
* @author Alex_Smarandache
*
* @throws Exception
*/
@Test
public void testCheckoutFileAction() throws Exception {
URL script = getClass().getClassLoader().getResource("scripts/history_script_actions.txt");
File wcTree = new File("target/gen/GitHistoryActionsTest_testHistoryContextualActions");
RepoGenerationScript.generateRepository(script, wcTree);
GitAccess.getInstance().setRepositorySynchronously(wcTree.getAbsolutePath());
List<CommitCharacteristics> commitsCharacteristics = GitAccess.getInstance().getCommitsCharacteristics(HistoryStrategy.CURRENT_BRANCH, null, null);
Iterator<CommitCharacteristics> iterator = commitsCharacteristics.iterator();
CommitCharacteristics commitCharacteristic = iterator.next();
HistoryViewContextualMenuPresenter presenter = new HistoryViewContextualMenuPresenter(null);
List<FileStatus> changedFiles = RevCommitUtil.getChangedFiles(commitCharacteristic.getCommitId());
List<Action> actions = presenter.getFileContextualActions(changedFiles.get(0), commitCharacteristic, true);
actions.removeIf(e -> e == null || !e.getValue(Action.NAME).toString().contains("Reset_file"));
String[] checkoutFile = new String[2];
try (MockedStatic<GitAccess> git = Mockito.mockStatic(GitAccess.class)) {
GitAccess gitAcc = Mockito.mock(GitAccess.class);
Mockito.doAnswer((Answer<Void>) invocation -> {
checkoutFile[0] = invocation.getArgument(0);
checkoutFile[1] = invocation.getArgument(1);
return null;
}).when(gitAcc).checkoutCommitForFile(Mockito.any(String.class), Mockito.any(String.class));
git.when(GitAccess::getInstance).thenReturn(gitAcc);
try (MockedStatic<GitOperationScheduler> sch = Mockito.mockStatic(GitOperationScheduler.class)) {
GitOperationScheduler scheduler = Mockito.mock(GitOperationScheduler.class);
Mockito.when(scheduler.schedule(Mockito.any(Runnable.class))).thenAnswer((Answer<ScheduledFuture<?>>) invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
});
sch.when(GitOperationScheduler::getInstance).thenReturn(scheduler);
actions.get(0).actionPerformed(null);
}
}
assertEquals(checkoutFile[0], changedFiles.get(0).getFileLocation());
assertEquals(checkoutFile[1], commitCharacteristic.getCommitId());
}
use of com.oxygenxml.git.service.GitAccess in project oxygen-git-client-addon by oxygenxml.
the class HistoryContextualActionsTest method testOpenWCMethod.
/**
* <p>
* <b>Description:</b> Tests the
* com.oxygenxml.git.view.history.HistoryViewContextualMenuPresenter.
* createOpenWorkingCopyFileAction(FileStatus, String, boolean) API.
* </p>
*
* <p>
* <b>Bug ID:</b> EXM-47571
* </p>
*
* @author Alex_Smarandache
*
* @throws Exception
*/
@Test
public void testOpenWCMethod() throws Exception {
gitAccess = GitAccess.getInstance();
gitAccess.createNewRepository(LOCAL_TEST_REPOSITORY);
File file = new File(LOCAL_TEST_REPOSITORY + "/test.xpr");
file.createNewFile();
gitAccess.add(new FileStatus(GitChangeType.ADD, file.getName()));
gitAccess.commit("file test added");
String[] urlOpenedFile = new String[1];
PluginWorkspace pluginWorkspace = Mockito.mock(PluginWorkspace.class);
Mockito.when(pluginWorkspace.open(Mockito.any(URL.class), Mockito.any(String.class), Mockito.any(String.class))).thenAnswer((Answer<Boolean>) invocation -> {
File file1 = new File(((URL) (invocation.getArgument(0))).getFile());
urlOpenedFile[0] = file1.getName();
return true;
});
try (MockedStatic<PluginWorkspaceProvider> provider = Mockito.mockStatic(PluginWorkspaceProvider.class);
MockedStatic<RevCommitUtil> revCommitUtil = Mockito.mockStatic(RevCommitUtil.class)) {
revCommitUtil.when(() -> RevCommitUtil.getNewPathInWorkingCopy((Git) Mockito.any(), Mockito.anyString(), Mockito.anyString())).thenReturn(LOCAL_TEST_REPOSITORY + "/test.xpr");
provider.when(() -> PluginWorkspaceProvider.getPluginWorkspace()).thenReturn(pluginWorkspace);
assertNotNull(PluginWorkspaceProvider.getPluginWorkspace());
HistoryViewContextualMenuPresenter historyContextualMenu = new HistoryViewContextualMenuPresenter(null);
Action openWCVersionAction = historyContextualMenu.createOpenWorkingCopyFileAction(new FileStatus(GitChangeType.RENAME, LOCAL_TEST_REPOSITORY + "/test.xpr"), LOCAL_TEST_REPOSITORY + "/test.xpr", false);
openWCVersionAction.actionPerformed(null);
}
assertEquals("test.xpr", urlOpenedFile[0]);
}
Aggregations