<commit_before>package elegit;

import javafx.application.Application;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * @author shangd
 */
public class BranchManagerUiUpdateTest {

    private static final String REMOTE_URL = "https://bitbucket.org/makik/commitlabeltestrepo.git";
    private static final String BRANCH_NAME = "random";

    private CommitTreeModel localCommitTreeModel;
    private CommitTreeModel remoteCommitTreeModel;

    private Path directoryPath;
    private Path repoPath;
    private ClonedRepoHelper helper;

    @BeforeClass
    public static void setUpJFX() throws Exception{
        Thread t = new Thread("JavaFX Init Thread") {
            @Override
            public void run(){
                Application.launch(Main.class);
            }
        };
        t.setDaemon(true);
        t.start();

        // Sleep until the JavaFX environment is up and running
        Thread.sleep(5000);
    }

    @Before
    public void setUp() throws Exception {
        cloneTestRepo();
        openClonedRepo();
    }

    @After
    public void tearDown() throws Exception {
        removeAllFilesFromDirectory(directoryPath.toFile());
    }

    void cloneTestRepo() {
        try {
            directoryPath = Files.createTempDirectory("branchManagerUiUpdateTest");
        } catch (IOException e) {
            e.printStackTrace();
        }
        directoryPath.toFile().deleteOnExit();

        repoPath = directoryPath.resolve("testRepo");
        try {
            helper = new ClonedRepoHelper(repoPath, REMOTE_URL);
        } catch (Exception e) {
            e.printStackTrace();
        }

        assertNotNull(helper);
    }

    void openClonedRepo() {
        localCommitTreeModel = Main.sessionController.localCommitTreeModel;
        remoteCommitTreeModel = Main.sessionController.remoteCommitTreeModel;

        try {
            /*new Git(helper.getRepo()).branchCreate()
                    .setName(BRANCH_NAME)
                    .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
                    .setStartPoint("refs/remotes/origin/" + BRANCH_NAME)
                    .call();*/

            SessionModel.getSessionModel().openRepoFromHelper(helper);
            helper.fetch();
            for (RemoteBranchHelper r : helper.getListOfRemoteBranches()) {
                System.out.println(r.getRefPathString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        localCommitTreeModel.init();
        remoteCommitTreeModel.init();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    void removeAllFilesFromDirectory(File dir) {
        for (File file : dir.listFiles()) {
            if (file.isDirectory()) removeAllFilesFromDirectory(file);
            file.delete();
        }
    }

    @Test
    public void testMergeSelectedBranchWithCurrent() throws Exception {
        File file = Paths.get(this.repoPath.toString(), "test.txt").toFile();
        assertTrue(file.exists());

        try(PrintWriter fileTextWriter = new PrintWriter( file )){
            fileTextWriter.println("Add a line to the file");
        }

        this.helper.addFilePath(file.toPath());
        this.helper.commit("Modified test.txt in a unit test!");

        localCommitTreeModel.update();
        remoteCommitTreeModel.update();
    }
}<commit_msg>Finished unit test on UiUpdate<commit_after>package elegit;

import org.eclipse.jgit.api.CreateBranchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.MergeCommand;
import org.eclipse.jgit.api.MergeResult;
import org.eclipse.jgit.transport.RefSpec;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.junit.Assert.*;

/**
 * @author shangd
 */
public class BranchManagerUiUpdateTest {

    private static final String REMOTE_URL = "https://github.com/shangdrk/BranchManagerUiUpdateTestRepo";
    private static final String BRANCH_NAME = "random";

    Path logPath;

    private Path directoryPath;
    private Path repoPath;
    private ClonedRepoHelper helper;

    @Before
    public void setUp() throws Exception {
        initializeLogger();
        cloneTestRepo();
    }

    @After
    public void tearDown() throws Exception {
        removeAllFilesFromDirectory(directoryPath.toFile());
    }

    void initializeLogger() {
        // Create a temp directory for the files to be placed in
        try {
            this.logPath = Files.createTempDirectory("elegitLogs");
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.logPath.toFile().deleteOnExit();
        System.setProperty("logFolder", logPath.toString());
    }

    void cloneTestRepo() {
        try {
            directoryPath = Files.createTempDirectory("branchManagerUiUpdateTest");
        } catch (IOException e) {
            e.printStackTrace();
        }
        directoryPath.toFile().deleteOnExit();

        repoPath = directoryPath.resolve("testRepo");
        try {
            helper = new ClonedRepoHelper(repoPath, REMOTE_URL);
        } catch (Exception e) {
            e.printStackTrace();
        }

        assertNotNull(helper);
    }

    void removeAllFilesFromDirectory(File dir) {
        for (File file : dir.listFiles()) {
            if (file.isDirectory()) removeAllFilesFromDirectory(file);
            file.delete();
        }
    }

    @Test
    public void testMergeSelectedBranchWithCurrent() throws Exception {
        // first fetch from remote
        RefSpec fetchSpec = new RefSpec
                ("+refs/heads/*:refs/remotes/origin/*");
        new Git(helper.getRepo()).fetch().setRefSpecs(fetchSpec).call();

        // start tracking the remote branch "random"
        new Git(helper.getRepo()).checkout().
                setCreateBranch(true).
                setForce(true).
                setName("random").
                setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).
                setStartPoint("origin/random").
                call();

        // make changes to local file and commit
        File file = Paths.get(this.repoPath.toString(), "test.txt").toFile();
        assertTrue(file.exists());

        try(PrintWriter fileTextWriter = new PrintWriter( file )){
            fileTextWriter.println("Add a line to the file");
        }

        this.helper.addFilePath(file.toPath());
        this.helper.commit("Modified test.txt in a unit test!");

        new Git(helper.getRepo()).checkout().setName("master").call();

        // merge master into random
        MergeCommand merge = new Git(helper.getRepo()).merge();
        merge.include(helper.getRepo().resolve("refs/heads/random"));

        MergeResult mergeResult = merge.call();
        assertEquals(mergeResult.getMergeStatus(), MergeResult.MergeStatus.FAST_FORWARD);

        CommitTreeController.update(helper);

        // after update, should expect the heads of two branches
        // are the same commit
        assertEquals(helper.getListOfLocalBranches().size(), 2);

        String masterHead = helper.getListOfLocalBranches().get(0).getHeadId().getName();
        String randomHead = helper.getListOfLocalBranches().get(1).getHeadId().getName();

        assertEquals(masterHead, randomHead);
    }
}
{'fraction_non_alphanumeric': 0.10858746969503637, 'fraction_numerical': 0.0014035983156820211, 'mean_word_length': 2.4944271065537227, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5211729', 'n_tokens_mistral': 2127, 'n_tokens_neox': 2089, 'n_words': 441}