<commit_before>package org.jenkinsci.plugins.gitclient;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.FileChannel;

import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.junit.Before;
import org.junit.Test;

public class NetrcTest
{
    private static final String TEST_NETRC_FILE_1 = "netrc_1";
    private static final String TEST_NETRC_FILE_1a = "netrc_1a";
    private static final String TEST_NETRC_FILE_2 = "netrc_2";
    private String testFilePath_1;
    private String testFilePath_1a;
    private String testFilePath_2;


    enum TestHost {
        H1_01("1-srvr-lp.example.com", "jenkins", "pw4jenkins"),
        H1_02("2-ldap-lp.example.com", "ldap", "pw4ldap"),
        H1_03("3-jenk-pl.example.com", "jenkins", "jenkinspwd"),
        H1_04("4-slv1-lp.example.com", "jenk", "slavepwd"),
        H1_05("5-sonr-l_.example.com", "sonar", null),
        H1_06("6-slv2-p_.example.com", null, "passwd"),
        H1_07("7-slv3-lp.example.com", "jenkins", "pw4jenkins"),
        H1_08("8-empt-__.nowhere.com", null, null),
        H1_09("9-ftps-lp", "james", "last"),
        H1_10("10-ftps-lp", "fred", "vargas"),
        H1_11("11-ftps-__", null, null),
        H1_12("12-last-lp", "lager", "topaz"),

        // H1_05 deleted
        H1a_06("6-slv2-p_.example.com", "builduser", "passwd"),
        H1a_08("8-empt-__.nowhere.com", "master", "key"),
        // H1_09 deleted
        // H1_10 deleted
        // H1_11 deleted

        H2_01("builder1.example.com", "jenkins", "secret"),
        H2_02("builder2.example.com", "builder", "buildpass"),
        H2_03("builder3.example.com", null, null),
        H2_04("builder4.example.com", "jenk", "myvoice");

        public String machine;
        public String login;
        public String password;

        private TestHost(String _machine, String _login, String _password)
        {
            this.machine = _machine;
            this.login = _login;
            this.password = _password;
        }
    }


    private void assertCredentials(TestHost host, Credentials cred)
    {
        if (cred == null) {
            assertTrue("Host." + host.name() + ": Credentials are null, although both login and password are set. (" + host.login + ":" + host.password + ")",
                    host.login == null || host.password == null);
        }
        else {
            assertEquals("Host." + host.name() + ": Login mismatch.", host.login, ((UsernamePasswordCredentials)cred).getUserName());
            assertEquals("Host." + host.name() + ": Password mismatch.", host.password, ((UsernamePasswordCredentials)cred).getPassword());
        }
    }


    private void copyFileContents(String _source, String _destination) throws IOException
    {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputChannel = new FileInputStream(_source).getChannel();
            outputChannel = new FileOutputStream(_destination).getChannel();
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        } finally {
            inputChannel.close();
            outputChannel.close();
        }
    }


    @Before
    public void setup() throws IOException
    {
        URL testNetrc = this.getClass().getClassLoader().getResource(TEST_NETRC_FILE_1 + ".in");
        if (testNetrc == null) {
            testFilePath_1 = null;<commit_msg>Avoid NPE in NetrcTest<commit_after>package org.jenkinsci.plugins.gitclient;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.FileChannel;

import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.junit.Before;
import org.junit.Test;

public class NetrcTest
{
    private static final String TEST_NETRC_FILE_1 = "netrc_1";
    private static final String TEST_NETRC_FILE_1a = "netrc_1a";
    private static final String TEST_NETRC_FILE_2 = "netrc_2";
    private String testFilePath_1;
    private String testFilePath_1a;
    private String testFilePath_2;


    enum TestHost {
        H1_01("1-srvr-lp.example.com", "jenkins", "pw4jenkins"),
        H1_02("2-ldap-lp.example.com", "ldap", "pw4ldap"),
        H1_03("3-jenk-pl.example.com", "jenkins", "jenkinspwd"),
        H1_04("4-slv1-lp.example.com", "jenk", "slavepwd"),
        H1_05("5-sonr-l_.example.com", "sonar", null),
        H1_06("6-slv2-p_.example.com", null, "passwd"),
        H1_07("7-slv3-lp.example.com", "jenkins", "pw4jenkins"),
        H1_08("8-empt-__.nowhere.com", null, null),
        H1_09("9-ftps-lp", "james", "last"),
        H1_10("10-ftps-lp", "fred", "vargas"),
        H1_11("11-ftps-__", null, null),
        H1_12("12-last-lp", "lager", "topaz"),

        // H1_05 deleted
        H1a_06("6-slv2-p_.example.com", "builduser", "passwd"),
        H1a_08("8-empt-__.nowhere.com", "master", "key"),
        // H1_09 deleted
        // H1_10 deleted
        // H1_11 deleted

        H2_01("builder1.example.com", "jenkins", "secret"),
        H2_02("builder2.example.com", "builder", "buildpass"),
        H2_03("builder3.example.com", null, null),
        H2_04("builder4.example.com", "jenk", "myvoice");

        public String machine;
        public String login;
        public String password;

        private TestHost(String _machine, String _login, String _password)
        {
            this.machine = _machine;
            this.login = _login;
            this.password = _password;
        }
    }


    private void assertCredentials(TestHost host, Credentials cred)
    {
        if (cred == null) {
            assertTrue("Host." + host.name() + ": Credentials are null, although both login and password are set. (" + host.login + ":" + host.password + ")",
                    host.login == null || host.password == null);
        }
        else {
            assertEquals("Host." + host.name() + ": Login mismatch.", host.login, ((UsernamePasswordCredentials)cred).getUserName());
            assertEquals("Host." + host.name() + ": Password mismatch.", host.password, ((UsernamePasswordCredentials)cred).getPassword());
        }
    }


    private void copyFileContents(String _source, String _destination) throws IOException
    {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputChannel = new FileInputStream(_source).getChannel();
            outputChannel = new FileOutputStream(_destination).getChannel();
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        } finally {
            if (inputChannel != null) inputChannel.close();
            if (outputChannel != null) outputChannel.close();
        }
    }


    @Before
    public void setup() throws IOException
    {
        URL testNetrc = this.getClass().getClassLoader().getResource(TEST_NETRC_FILE_1 + ".in");
        if (testNetrc == null) {
            testFilePath_1 = null;
{'fraction_non_alphanumeric': 0.13949626349294214, 'fraction_numerical': 0.02933849986161085, 'mean_word_length': 2.725257731958763, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '5277771', 'n_tokens_mistral': 2322, 'n_tokens_neox': 2236, 'n_words': 454}