<commit_before>/* Copyright (c) 2011 TOPP - www.openplans.org. All rights reserved.
 * This code is licensed under the LGPL 2.1 license, available at the root
 * application directory.
 */
package org.geogit.cli.test.functional;

import static org.geogit.cli.test.functional.GlobalState.currentDirectory;
import static org.geogit.cli.test.functional.GlobalState.geogit;
import static org.geogit.cli.test.functional.GlobalState.geogitCLI;
import static org.geogit.cli.test.functional.GlobalState.homeDirectory;
import static org.geogit.cli.test.functional.GlobalState.stdOut;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.geogit.api.GeoGIT;
import org.geogit.api.GlobalInjectorBuilder;
import org.geogit.api.ObjectId;
import org.geogit.api.Ref;
import org.geogit.api.plumbing.RefParse;
import org.geogit.api.plumbing.UpdateRef;

import com.google.common.base.Optional;
import com.google.inject.Injector;

import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;

/**
 *
 */
public class InitSteps extends AbstractGeogitFunctionalTest {

    @cucumber.annotation.After
    public void after() {
        if (GlobalState.geogit != null) {
            GlobalState.geogit.close();
        }
        deleteDirectories();
    }

    @Given("^I am in an empty directory$")
    public void I_am_in_an_empty_directory() throws Throwable {
        setUpDirectories();
        assertEquals(0, currentDirectory.list().length);
        setupGeogit();
    }

    @When("^I run the command \"([^\"]*)\"$")
    public void I_run_the_command_X(String commandSpec) throws Throwable {
        String[] args = commandSpec.split(" ");
        runCommand(args);
    }

    @Then("^it should answer \"([^\"]*)\"$")
    public void it_should_answer_exactly(String expected) throws Throwable {
        expected = expected.replace("${currentdir}", currentDirectory.getAbsolutePath())
                .toLowerCase();
        String actual = stdOut.toString().replaceAll("\n", "").trim().toLowerCase();
        assertEquals(expected, actual);
    }

    @Then("^the response should contain \"([^\"]*)\"$")
    public void the_response_should_contain(String expected) throws Throwable {
        String actual = stdOut.toString().replaceAll("\n", "");
        assertTrue(actual, actual.contains(expected));
    }

    @Then("^the response should not contain \"([^\"]*)\"$")
    public void the_response_should_not_contain(String expected) throws Throwable {
        String actual = stdOut.toString().replaceAll("\n", "");
        assertFalse(actual, actual.contains(expected));
    }

    @Then("^the response should contain ([^\"]*) lines$")
    public void the_response_should_contain_x_lines(int lines) throws Throwable {
        String[] lineStrings = stdOut.toString().split("\n");
        assertEquals(lines, lineStrings.length);
    }

    @Then("^the response should start with \"([^\"]*)\"$")
    public void the_response_should_start_with(String expected) throws Throwable {
        String actual = stdOut.toString().replaceAll("\n", "");
        assertTrue(actual, actual.startsWith(expected));
    }

    @Then("^the repository directory shall exist$")
    public void the_repository_directory_shall_exist() throws Throwable {
        List<String> output = runAndParseCommand("rev-parse", "--resolve-geogit-dir");
        assertEquals(output.toString(), 1, output.size());
        String location = output.get(0);
        assertNotNull(location);
        if (location.startsWith("Error:")) {
            fail(location);
        }
        File repoDir = new File(new URI(location));
        assertTrue("Repository directory not found: " + repoDir.getAbsolutePath(), repoDir.exists());
    }

    @Given("^I have a remote ref called \"([^\"]*)\"$")
    public void i_have_a_remote_ref_called(String expected) throws Throwable {
        String ref = "refs/remotes/origin/" + expected;
        geogit.command(UpdateRef.class).setName(ref).setNewValue(ObjectId.NULL).call();
        Optional<Ref> refValue = geogit.command(RefParse.class).setName(ref).call();
        assertTrue(refValue.isPresent());
        assertEquals(refValue.get().getObjectId(), ObjectId.NULL);
    }

    @Given("^I have an unconfigured repository$")
    public void I_have_an_unconfigured_repository() throws Throwable {
        setUpDirectories();
        setupGeogit();

        List<String> output = runAndParseCommand("init");
        assertEquals(output.toString(), 1, output.size());
        assertNotNull(output.get(0));
        assertTrue(output.get(0), output.get(0).startsWith("Initialized"));
    }

    @Given("^there is a remote repository$")
    public void there_is_a_remote_repository() throws Throwable {
        I_am_in_an_empty_directory();
        GeoGIT oldGeogit = geogit;
        Injector oldInjector = geogitCLI.getGeogitInjector();
        geogitCLI.setGeogitInjector(GlobalInjectorBuilder.builder.get());
        List<String> output = runAndParseCommand("init", "remoterepo");
        assertEquals(output.toString(), 1, output.size());
        assertNotNull(output.get(0));
        assertTrue(output.get(0), output.get(0).startsWith("Initialized"));
        geogit = geogitCLI.getGeogit();
        runCommand("config", "--global", "user.name", "<NAME>");
        runCommand("config", "--global", "user.email", "<EMAIL>");
        insertAndAdd(points1);
        runCommand(("commit -m Commit1").split(" "));
        runCommand(("branch -c branch1").split(" "));
        insertAndAdd(points2);
        runCommand(("commit -m Commit2").split(" "));
        insertAndAdd(points3);
        runCommand(("commit -m Commit3").split(" "));
        runCommand(("checkout master").split(" "));
        insertAndAdd(lines1);
        runCommand(("commit -m Commit4").split(" "));
        geogit.close();
        geogit = oldGeogit;
        geogitCLI.setGeogit(oldGeogit);
        geogitCLI.setGeogitInjector(oldInjector);
    }

    @Given("^I have a repository$")
    public void I_have_a_repository() throws Throwable {
        I_have_an_unconfigured_repository();
        runCommand("config", "--global", "user.name", "<NAME>");
        runCommand("config", "--global", "user.email", "<EMAIL>");
    }

    @Given("^I have a repository with a remote$")
    public void I_have_a_repository_with_a_remote() throws Throwable {
        there_is_a_remote_repository();

        List<String> output = runAndParseCommand("init", "localrepo");
        assertEquals(output.toString(), 1, output.size());
        assertNotNull(output.get(0));
        assertTrue(output.get(0), output.get(0).startsWith("Initialized"));

        runCommand("config", "--global", "user.name", "<NAME>");
        runCommand("config", "--global", "user.email", "<EMAIL>");

        runCommand("remote", "add", "origin", currentDirectory + "/remoterepo");
    }

    private void setUpDirectories() throws IOException {
        homeDirectory = new File("target", "fakeHomeDir");
        FileUtils.deleteDirectory(homeDirectory);
        assertFalse(homeDirectory.exists());
        assertTrue(homeDirectory.mkdirs());

        currentDirectory = new File("target", "testrepo");
        FileUtils.deleteDirectory(currentDirectory);
        assertFalse(currentDirectory.exists());
        assertTrue(currentDirectory.mkdirs());
    }

    private void deleteDirectories() {
        try {
            FileUtils.deleteDirectory(homeDirectory);
            assertFalse(homeDirectory.exists());

            FileUtils.deleteDirectory(currentDirectory);
            assertFalse(currentDirectory.exists());
        } catch (IOException e) {

        }
    }
<commit_msg>some fixes to make tests work also in windows<commit_after>/* Copyright (c) 2011 TOPP - www.openplans.org. All rights reserved.
 * This code is licensed under the LGPL 2.1 license, available at the root
 * application directory.
 */
package org.geogit.cli.test.functional;

import static org.geogit.cli.test.functional.GlobalState.currentDirectory;
import static org.geogit.cli.test.functional.GlobalState.geogit;
import static org.geogit.cli.test.functional.GlobalState.geogitCLI;
import static org.geogit.cli.test.functional.GlobalState.homeDirectory;
import static org.geogit.cli.test.functional.GlobalState.stdOut;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.geogit.api.GeoGIT;
import org.geogit.api.GlobalInjectorBuilder;
import org.geogit.api.ObjectId;
import org.geogit.api.Ref;
import org.geogit.api.plumbing.RefParse;
import org.geogit.api.plumbing.UpdateRef;

import com.google.common.base.Optional;
import com.google.inject.Injector;

import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;

/**
 *
 */
public class InitSteps extends AbstractGeogitFunctionalTest {

    private static final String LINE_SEPARATOR = System.getProperty("line.separator");

    @cucumber.annotation.After
    public void after() {
        if (GlobalState.geogit != null) {
            GlobalState.geogit.close();
        }
        deleteDirectories();
    }

    @Given("^I am in an empty directory$")
    public void I_am_in_an_empty_directory() throws Throwable {
        setUpDirectories();
        assertEquals(0, currentDirectory.list().length);
        setupGeogit();
    }

    @When("^I run the command \"([^\"]*)\"$")
    public void I_run_the_command_X(String commandSpec) throws Throwable {
        String[] args = commandSpec.split(" ");
        runCommand(args);
    }

    @Then("^it should answer \"([^\"]*)\"$")
    public void it_should_answer_exactly(String expected) throws Throwable {
        expected = expected.replace("${currentdir}", currentDirectory.getAbsolutePath())
                .toLowerCase().replaceAll("\\\\", "/");
        String actual = stdOut.toString().replaceAll(LINE_SEPARATOR, "").replaceAll("\\\\", "/")
                .trim().toLowerCase();
        assertEquals(expected, actual);
    }

    @Then("^the response should contain \"([^\"]*)\"$")
    public void the_response_should_contain(String expected) throws Throwable {
        String actual = stdOut.toString().replaceAll(LINE_SEPARATOR, "").replaceAll("\\\\", "/");
        expected.replaceAll("\\\\", "/");
        assertTrue(actual, actual.contains(expected));
    }

    @Then("^the response should not contain \"([^\"]*)\"$")
    public void the_response_should_not_contain(String expected) throws Throwable {
        String actual = stdOut.toString().replaceAll(LINE_SEPARATOR, "").replaceAll("\\\\", "/");
        expected.replaceAll("\\\\", "/");
        assertFalse(actual, actual.contains(expected));
    }

    @Then("^the response should contain ([^\"]*) lines$")
    public void the_response_should_contain_x_lines(int lines) throws Throwable {
        String[] lineStrings = stdOut.toString().split(LINE_SEPARATOR);
        assertEquals(lines, lineStrings.length);
    }

    @Then("^the response should start with \"([^\"]*)\"$")
    public void the_response_should_start_with(String expected) throws Throwable {
        String actual = stdOut.toString().replaceAll(LINE_SEPARATOR, "");
        assertTrue(actual, actual.startsWith(expected));
    }

    @Then("^the repository directory shall exist$")
    public void the_repository_directory_shall_exist() throws Throwable {
        List<String> output = runAndParseCommand("rev-parse", "--resolve-geogit-dir");
        assertEquals(output.toString(), 1, output.size());
        String location = output.get(0);
        assertNotNull(location);
        if (location.startsWith("Error:")) {
            fail(location);
        }
        File repoDir = new File(new URI(location));
        assertTrue("Repository directory not found: " + repoDir.getAbsolutePath(), repoDir.exists());
    }

    @Given("^I have a remote ref called \"([^\"]*)\"$")
    public void i_have_a_remote_ref_called(String expected) throws Throwable {
        String ref = "refs/remotes/origin/" + expected;
        geogit.command(UpdateRef.class).setName(ref).setNewValue(ObjectId.NULL).call();
        Optional<Ref> refValue = geogit.command(RefParse.class).setName(ref).call();
        assertTrue(refValue.isPresent());
        assertEquals(refValue.get().getObjectId(), ObjectId.NULL);
    }

    @Given("^I have an unconfigured repository$")
    public void I_have_an_unconfigured_repository() throws Throwable {
        setUpDirectories();
        setupGeogit();

        List<String> output = runAndParseCommand("init");
        assertEquals(output.toString(), 1, output.size());
        assertNotNull(output.get(0));
        assertTrue(output.get(0), output.get(0).startsWith("Initialized"));
    }

    @Given("^there is a remote repository$")
    public void there_is_a_remote_repository() throws Throwable {
        I_am_in_an_empty_directory();
        GeoGIT oldGeogit = geogit;
        Injector oldInjector = geogitCLI.getGeogitInjector();
        geogitCLI.setGeogitInjector(GlobalInjectorBuilder.builder.get());
        List<String> output = runAndParseCommand("init", "remoterepo");
        assertEquals(output.toString(), 1, output.size());
        assertNotNull(output.get(0));
        assertTrue(output.get(0), output.get(0).startsWith("Initialized"));
        geogit = geogitCLI.getGeogit();
        runCommand("config", "--global", "user.name", "<NAME>");
        runCommand("config", "--global", "user.email", "<EMAIL>");
        insertAndAdd(points1);
        runCommand(("commit -m Commit1").split(" "));
        runCommand(("branch -c branch1").split(" "));
        insertAndAdd(points2);
        runCommand(("commit -m Commit2").split(" "));
        insertAndAdd(points3);
        runCommand(("commit -m Commit3").split(" "));
        runCommand(("checkout master").split(" "));
        insertAndAdd(lines1);
        runCommand(("commit -m Commit4").split(" "));
        geogit.close();
        geogit = oldGeogit;
        geogitCLI.setGeogit(oldGeogit);
        geogitCLI.setGeogitInjector(oldInjector);
    }

    @Given("^I have a repository$")
    public void I_have_a_repository() throws Throwable {
        I_have_an_unconfigured_repository();
        runCommand("config", "--global", "user.name", "<NAME>");
        runCommand("config", "--global", "user.email", "<EMAIL>");
    }

    @Given("^I have a repository with a remote$")
    public void I_have_a_repository_with_a_remote() throws Throwable {
        there_is_a_remote_repository();

        List<String> output = runAndParseCommand("init", "localrepo");
        assertEquals(output.toString(), 1, output.size());
        assertNotNull(output.get(0));
        assertTrue(output.get(0), output.get(0).startsWith("Initialized"));

        runCommand("config", "--global", "user.name", "<NAME>");
        runCommand("config", "--global", "user.email", "<EMAIL>");

        runCommand("remote", "add", "origin", currentDirectory + "/remoterepo");
    }

    private void setUpDirectories() throws IOException {
        homeDirectory = new File("target", "fakeHomeDir" + new Random().nextInt());
        FileUtils.deleteDirectory(homeDirectory);
        assertFalse(homeDirectory.exists());
        assertTrue(homeDirectory.mkdirs());

        currentDirectory = new File("target", "testrepo" + new Random().nextInt());
        FileUtils.deleteDirectory(currentDirectory);
        assertFalse(currentDirectory.exists());
        assertTrue(currentDirectory.mkdirs());
    }

    private void deleteDirectories() {
        try {
            FileUtils.deleteDirectory(homeDirectory);
            assertFalse(homeDirectory.exists());

            FileUtils.deleteDirectory(currentDirectory);
            assertFalse(currentDirectory.exists());
        } catch (IOException e) {

        }
    }

{'fraction_non_alphanumeric': 0.14502138057422115, 'fraction_numerical': 0.0036652412950519244, 'mean_word_length': 3.488894982177132, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1694422', 'n_tokens_mistral': 4795, 'n_tokens_neox': 4744, 'n_words': 905}