<commit_before>package jetbrains.buildServer.deployer.agent.ssh;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.Session;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import jetbrains.buildServer.agent.BuildProgressLogger;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class SSHExecProcessAdapterTest {

    private static final String DEFAULT_COMMAND = "echo hello";

    private Mockery context = new Mockery();
    private SSHSessionProvider sessionProvider;
    private Session session;
    private ChannelExec channel;
    private InputStream stdout;
    private InputStream stderr;

    @BeforeClass
    public void setup() {
        this.context.setImposteriser(ClassImposteriser.INSTANCE);

        this.sessionProvider = context.mock(SSHSessionProvider.class);
        this.session = context.mock(Session.class);
        this.channel = context.mock(ChannelExec.class);
        this.stdout = context.mock(InputStream.class, "stdout");
        this.stderr = context.mock(InputStream.class, "stderr");
    }

    @Test
    public void stdoutAndStderrShouldBeLogged() throws Exception {
        final BuildProgressLogger mockedLogger = context.mock(BuildProgressLogger.class);
        SSHExecProcessAdapter adapter = newAdapter(mockedLogger);

        commonExpectations();
        context.checking(new Expectations() {{
            oneOf(channel).getInputStream();
            will(returnValue(new ByteArrayInputStream("standard output\n".getBytes())));

            oneOf(channel).getErrStream();
            will(returnValue(new ByteArrayInputStream("standard error\n".getBytes())));

            oneOf(mockedLogger).message("Executing commands:\n" + DEFAULT_COMMAND + "\non host []");
            oneOf(mockedLogger).message("Exec output:\nstandard output\nstandard error\n");
            oneOf(mockedLogger).message("ssh exit-code: 0");
        }});

        adapter.runProcess();

        context.assertIsSatisfied();
    }

    @Test
    public void stderrShouldBeLoggedIfStdOutIsEmpty() throws Exception {
        final BuildProgressLogger mockedLogger = context.mock(BuildProgressLogger.class);
        SSHExecProcessAdapter adapter = newAdapter(mockedLogger);

        commonExpectations();
        context.checking(new Expectations() {{
            oneOf(channel).getInputStream();
            will(returnValue(new ByteArrayInputStream(new byte[]{11}) {
                @Override
                public synchronized int read(byte[] b, int off, int len) {
                    return -1;
                }
            }));

            oneOf(channel).getErrStream();
            will(returnValue(new ByteArrayInputStream("standard error\n".getBytes())));

            oneOf(mockedLogger).message("Executing commands:\n" + DEFAULT_COMMAND + "\non host []");
            oneOf(mockedLogger).message("Exec output:\nstandard error\n");
            oneOf(mockedLogger).message("ssh exit-code: 0");
        }});

        adapter.runProcess();

        context.assertIsSatisfied();
    }

    private void commonExpectations() throws Exception {
        context.checking(new Expectations() {{
            allowing(sessionProvider).getSession();
            will(returnValue(session));

            allowing(session).getHost();
            allowing(session).openChannel("exec");
            will(returnValue(channel));
            allowing(session).disconnect();

            allowing(channel).setCommand(DEFAULT_COMMAND);
            allowing(channel).connect();
            allowing(channel).isClosed();
            will(returnValue(true));
            allowing(channel).disconnect();
            allowing(channel).getExitStatus();
        }});
    }

    private SSHExecProcessAdapter newAdapter(BuildProgressLogger logger) throws Exception {
        return new SSHExecProcessAdapter(sessionProvider, DEFAULT_COMMAND, null, logger);
    }
}<commit_msg>fix test, align code to convention.<commit_after>package jetbrains.buildServer.deployer.agent.ssh;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import java.io.ByteArrayInputStream;

import jetbrains.buildServer.agent.BuildProgressLogger;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class SSHExecProcessAdapterTest {

    private static final String DEFAULT_COMMAND = "echo hello";

    private Mockery myContext;
    private SSHSessionProvider mySessionProvider;
    private Session mySession;
    private ChannelExec myChannel;
    private SSHExecProcessAdapter myAdapter;
    private BuildProgressLogger myLogger;

    @BeforeMethod
    public void setup() {
        myContext = new Mockery();
        myContext.setImposteriser(ClassImposteriser.INSTANCE);

        mySessionProvider = myContext.mock(SSHSessionProvider.class);
        mySession = myContext.mock(Session.class);
        myChannel = myContext.mock(ChannelExec.class);
        myLogger = myContext.mock(BuildProgressLogger.class);
        myAdapter = newAdapter(myLogger);
        commonExpectations();
    }

    @Test
    public void stdoutAndStderrShouldBeLogged() throws Exception {
        myContext.checking(new Expectations() {{
            oneOf(myChannel).getInputStream();
            will(returnValue(new ByteArrayInputStream("standard output\n".getBytes())));

            oneOf(myChannel).getErrStream();
            will(returnValue(new ByteArrayInputStream("standard error\n".getBytes())));

            oneOf(myLogger).message("Executing commands:\n" + DEFAULT_COMMAND + "\non host []");
            oneOf(myLogger).message("Exec output:\nstandard output\nstandard error\n");
            oneOf(myLogger).message("ssh exit-code: 0");
        }});

        myAdapter.runProcess();

        myContext.assertIsSatisfied();
    }

    @Test
    public void stderrShouldBeLoggedIfStdOutIsEmpty() throws Exception {
        myContext.checking(new Expectations() {{
            oneOf(myChannel).getInputStream();
            will(returnValue(new ByteArrayInputStream(new byte[]{11}) {
                @Override
                public synchronized int read(byte[] b, int off, int len) {
                    return -1;
                }
            }));

            oneOf(myChannel).getErrStream();
            will(returnValue(new ByteArrayInputStream("standard error\n".getBytes())));

            oneOf(myLogger).message("Executing commands:\n" + DEFAULT_COMMAND + "\non host []");
            oneOf(myLogger).message("Exec output:\nstandard error\n");
            oneOf(myLogger).message("ssh exit-code: 0");
        }});

        myAdapter.runProcess();

        myContext.assertIsSatisfied();
    }

    private void commonExpectations() {
        myContext.checking(new Expectations() {{
            try {
                allowing(mySessionProvider).getSession();
                will(returnValue(mySession));

                allowing(mySession).getHost();
                allowing(mySession).openChannel("exec");
                will(returnValue(myChannel));
                allowing(mySession).disconnect();

                allowing(myChannel).setCommand(DEFAULT_COMMAND);
                allowing(myChannel).connect();
                allowing(myChannel).isClosed();
                will(returnValue(true));
                allowing(myChannel).disconnect();
                allowing(myChannel).getExitStatus();
            } catch (JSchException e) {
                Assert.fail("Unexpected exception in jmock expectations list.", e);
            }
        }});

    }

    private SSHExecProcessAdapter newAdapter(BuildProgressLogger logger) {
        return new SSHExecProcessAdapter(mySessionProvider, DEFAULT_COMMAND, null, logger);
    }
}
{'fraction_non_alphanumeric': 0.10869565217391304, 'fraction_numerical': 0.0012493753123438282, 'mean_word_length': 2.884036875303251, 'pattern_counts': {'":': 0, '<': 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': '382425', 'n_tokens_mistral': 2099, 'n_tokens_neox': 2028, 'n_words': 372}