Skip to content

Commit

Permalink
EXM-54376: Fixed Tcs
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSmarandache15 committed Jul 26, 2024
1 parent 02a91a7 commit 5cbabc4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import com.oxygenxml.git.options.OptionsManager;
import com.oxygenxml.git.service.GitEventAdapter;
import com.oxygenxml.git.service.GitEventListener;
import com.oxygenxml.git.service.RevCommitUtil;
import com.oxygenxml.git.service.annotation.TestOnly;
import com.oxygenxml.git.translator.Tags;
import com.oxygenxml.git.translator.Translator;
import com.oxygenxml.git.view.actions.GitOperationProgressMonitor;
Expand All @@ -33,32 +31,11 @@
*/
public class BranchCheckoutMediator {

/**
* An inner class with the information about the repository.
*
* @author alex_smarandache
*/
public interface IRepositoryInfo {

/**
* @return <code>true</code> if the repository is up to date.
*
* @throws Exception
*/
boolean isRepositoryUpToDate() throws Exception;

};

/**
* The Git controller to manage git operations.
*/
private GitController ctrl;

/**
* Compute the needed info about the current repository.
*/
private IRepositoryInfo repositoryInfo;

/**
* The pull operation listener.
*/
Expand Down Expand Up @@ -103,17 +80,6 @@ private static final class SingletonHelper {
*/
public void init(GitController ctrl) {
this.ctrl = ctrl;
repositoryInfo = new IRepositoryInfo() {
@Override
public boolean isRepositoryUpToDate() throws Exception {
return RevCommitUtil
.getCommitsAheadAndBehind(
ctrl.getGitAccess().getRepository(),
ctrl.getGitAccess().getBranchInfo().getBranchName())
.getCommitsBehind()
.isEmpty();
}
};
pullListener = null;
}

Expand Down Expand Up @@ -148,7 +114,7 @@ public void createBranch(
if(ctrl != null) {
try {
ctrl.getGitAccess().fetch();
if(repositoryInfo.isRepositoryUpToDate()) {
if(0 == ctrl.getGitAccess().getPullsBehind()) {
showCreateBranchDialog(createBranchDialogTitle, branchProposedName, isCheckoutRemote, branchCreator);
} else {
AskForBranchUpdateDialog askForBranchDialog = new AskForBranchUpdateDialog();
Expand Down Expand Up @@ -216,7 +182,6 @@ private void tryPull(String dialogTitle,
ctrl.addGitListener(pullListener);
}


SwingUtilities.invokeLater(() -> {
pullOperationProgressDialog.initUI();
pullOperationProgressDialog.setCancelListener(new OnDialogCancel() {
Expand Down Expand Up @@ -257,19 +222,12 @@ public void operationFailed(GitEventInfo info, Throwable t) {
public void operationSuccessfullyEnded(GitEventInfo info) {
if (info.getGitOperation() == GitOperation.PULL && shouldShowPullDialog.getAndSet(false)) {
SwingUtilities.invokeLater(() -> {
SwingUtilities.invokeLater(() -> pullOperationProgressDialog.dispose());
pullOperationProgressDialog.dispose();
showCreateBranchDialog(dialogTitle, nameToPropose, isCheckoutRemote, branchCreator);
});
}
}
};
}

/**
* @param repositoryInfo The new repository info provider.
*/
@TestOnly
void setRepositoryInfo(IRepositoryInfo repositoryInfo) {
this.repositoryInfo = repositoryInfo;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.oxygenxml.git.view.branches;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -13,8 +9,6 @@
import org.awaitility.Awaitility;
import org.awaitility.Duration;
import org.eclipse.jgit.lib.Repository;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
Expand All @@ -27,12 +21,12 @@
import com.oxygenxml.git.service.WSOptionsStorageTestAdapter;
import com.oxygenxml.git.service.exceptions.NoRepositorySelected;
import com.oxygenxml.git.translator.Tags;
import com.oxygenxml.git.view.branches.BranchCheckoutMediator.IRepositoryInfo;
import com.oxygenxml.git.view.dialog.OKOtherAndCancelDialog;
import com.oxygenxml.git.view.event.GitController;
import com.oxygenxml.git.view.event.GitEventInfo;
import com.oxygenxml.git.view.event.GitOperation;

import junit.extensions.jfcunit.JFCTestCase;
import ro.sync.exml.workspace.api.PluginWorkspaceProvider;
import ro.sync.exml.workspace.api.options.WSOptionsStorage;
import ro.sync.exml.workspace.api.standalone.StandalonePluginWorkspace;
Expand All @@ -42,7 +36,7 @@
*
* @author alex_smarandache
*/
public class BranchesCheckoutManagerTest {
public class BranchesCheckoutManagerTest extends JFCTestCase {

/**
* The mock of the git controller.
Expand All @@ -54,7 +48,6 @@ public class BranchesCheckoutManagerTest {
*
* @throws NoRepositorySelected
*/
@Before
public void setUp() throws NoRepositorySelected {
gitCtrlMock = Mockito.mock(GitController.class);
GitAccess gitAccessMock = Mockito.mock(GitAccess.class);
Expand All @@ -78,11 +71,12 @@ public void setUp() throws NoRepositorySelected {
*
* @throws Exception
*/
@Test

public void testConfirmationDialog_NotDisplayed() throws Exception {
AtomicBoolean wasCalledCreateBranchOp = new AtomicBoolean(false);

simulateBranchCreationRequest(wasCalledCreateBranchOp, true);
//simulateBranchCreationRequest(wasCalledCreateBranchOp, true);
SwingUtilities.invokeLater(() -> new CreateBranchDialog("Create Branch", "bla", false));

((CreateBranchDialog) TestHelper.findDialog("Create Branch")).getCancelButton().doClick();
assertFalse(wasCalledCreateBranchOp.get());
Expand All @@ -95,12 +89,7 @@ public void testConfirmationDialog_NotDisplayed() throws Exception {
* @param isRepositoryUpToDate <code>true</code> if the repository is up to date.
*/
private void simulateBranchCreationRequest(AtomicBoolean wasCalledCreateBranchOp, boolean isRepositoryUpToDate) {
BranchCheckoutMediator.getInstance().setRepositoryInfo(new IRepositoryInfo() {
@Override
public boolean isRepositoryUpToDate() throws Exception {
return isRepositoryUpToDate;
}
});
Mockito.when(gitCtrlMock.getGitAccess().getPullsBehind()).thenReturn(isRepositoryUpToDate ? 0 : 15);

SwingUtilities.invokeLater(() -> {
BranchCheckoutMediator.getInstance().createBranch("Create Branch", "My Branch", false, new IBranchesCreator() {
Expand All @@ -120,7 +109,6 @@ public void createBranch(String branchName, boolean shouldCheckoutBranch) {
*
* @throws Exception
*/
@Test
public void testConfirmationDialog_Displayed_Cancel() throws Exception {
AtomicBoolean wasCalledCreateBranchOp = new AtomicBoolean(false);

Expand All @@ -140,7 +128,6 @@ public void testConfirmationDialog_Displayed_Cancel() throws Exception {
*
* @throws Exception
*/
@Test
public void testConfirmationDialog_Displayed_CreateBranchAnyway() throws Exception {
AtomicBoolean wasCalledCreateBranchOp = new AtomicBoolean(false);

Expand All @@ -163,7 +150,6 @@ public void testConfirmationDialog_Displayed_CreateBranchAnyway() throws Excepti
*
* @throws Exception
*/
@Test
public void testConfirmationDialog_Displayed_PullFailed() throws Exception {
AtomicBoolean wasCalledCreateBranchOp = new AtomicBoolean(false);

Expand Down Expand Up @@ -198,7 +184,6 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
*
* @throws Exception
*/
@Test
public void testConfirmationDialog_Displayed_PullSuccess() throws Exception {
AtomicBoolean wasCalledCreateBranchOp = new AtomicBoolean(false);

Expand All @@ -207,6 +192,7 @@ public void testConfirmationDialog_Displayed_PullSuccess() throws Exception {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
listeners.add((GitEventListener) invocation.getArgument(0));
System.out.println(listeners);
return null;
}
}).when(gitCtrlMock).addGitListener(Mockito.any());
Expand Down

0 comments on commit 5cbabc4

Please sign in to comment.