<commit_before>package org.col.db.mapper;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import jersey.repackaged.com.google.common.base.Throwables;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.col.commands.initdb.InitDbCmd;
import org.col.db.MybatisBundle;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres;
import ru.yandex.qatools.embed.postgresql.distribution.Version;

import java.io.IOException;
import java.net.ServerSocket;
import java.sql.Connection;
import java.sql.SQLException;
import java.time.Duration;
import java.time.Instant;

/**
 * A junit test rule that starts up an {@link EmbeddedPostgres} server together with a
 * {@link HikariDataSource} and stops both at the end.
 * The rule was designed to share the server across all tests of a test class if it runs as
 * a static {@link org.junit.ClassRule}.
 *
 * It can even be used to share the same postgres server across several test classes
 * if it is used in as a {@link org.junit.ClassRule} in a TestSuite.
 */
public class PgMybatisRule implements TestRule {
  private static EmbeddedPostgres postgres;
  private static HikariDataSource dataSource;
  private static SqlSession session;
  private boolean startedHere = false;

  public <T> T getMapper(Class<T> mapperClazz) {
    return session.getMapper(mapperClazz);
  }

  public static Connection getConnection() throws SQLException {
    return dataSource.getConnection();
  }

  public void commit() {
    session.commit();
  }

  @Override
  public Statement apply(Statement base, Description description) {
    return new Statement() {

      @Override
      public void evaluate() throws Throwable {
        try {
          before();
          base.evaluate();
        } finally {
          after();
        }
      }
    };
  }


  private void before() {
    if (postgres == null) {
      startDb();
      initDb();
      initMyBatis();
    }
  }

  private void startDb() {
    System.out.println("Starting Postgres");
    try {
      postgres = new EmbeddedPostgres(Version.Main.V9_6);
      startedHere = true;
      // assigned to some free port
      ServerSocket socket = new ServerSocket(0);
      final String database = "col";
      final String user = "col";
      final String password = "<PASSWORD>";

      Instant start = Instant.now();
      //      System.out.println("Start postgres on port "+socket.getLocalPort());
      final String url = postgres.start("localhost", socket.getLocalPort(), database, user, password);
      System.out.format("Pg startup time: %s ms\n", Duration.between(start, Instant.now()).toMillis());

      HikariConfig hikari = new HikariConfig();
      hikari.setJdbcUrl(url);
      //hikari.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
      hikari.setUsername(user);
      hikari.setPassword(password);
      hikari.setMaximumPoolSize(2);<commit_msg>Try with older pg version 9.4<commit_after>package org.col.db.mapper;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import jersey.repackaged.com.google.common.base.Throwables;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.col.commands.initdb.InitDbCmd;
import org.col.db.MybatisBundle;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres;
import ru.yandex.qatools.embed.postgresql.distribution.Version;

import java.io.IOException;
import java.net.ServerSocket;
import java.sql.Connection;
import java.sql.SQLException;
import java.time.Duration;
import java.time.Instant;

/**
 * A junit test rule that starts up an {@link EmbeddedPostgres} server together with a
 * {@link HikariDataSource} and stops both at the end.
 * The rule was designed to share the server across all tests of a test class if it runs as
 * a static {@link org.junit.ClassRule}.
 *
 * It can even be used to share the same postgres server across several test classes
 * if it is used in as a {@link org.junit.ClassRule} in a TestSuite.
 */
public class PgMybatisRule implements TestRule {
  private static EmbeddedPostgres postgres;
  private static HikariDataSource dataSource;
  private static SqlSession session;
  private boolean startedHere = false;

  public <T> T getMapper(Class<T> mapperClazz) {
    return session.getMapper(mapperClazz);
  }

  public static Connection getConnection() throws SQLException {
    return dataSource.getConnection();
  }

  public void commit() {
    session.commit();
  }

  @Override
  public Statement apply(Statement base, Description description) {
    return new Statement() {

      @Override
      public void evaluate() throws Throwable {
        try {
          before();
          base.evaluate();
        } finally {
          after();
        }
      }
    };
  }


  private void before() {
    if (postgres == null) {
      startDb();
      initDb();
      initMyBatis();
    }
  }

  private void startDb() {
    System.out.println("Starting Postgres");
    try {
      postgres = new EmbeddedPostgres(Version.V9_4_10);
      startedHere = true;
      // assigned to some free port
      ServerSocket socket = new ServerSocket(0);
      final String database = "col";
      final String user = "col";
      final String password = "<PASSWORD>";

      Instant start = Instant.now();
      //      System.out.println("Start postgres on port "+socket.getLocalPort());
      final String url = postgres.start("localhost", socket.getLocalPort(), database, user, password);
      System.out.format("Pg startup time: %s ms\n", Duration.between(start, Instant.now()).toMillis());

      HikariConfig hikari = new HikariConfig();
      hikari.setJdbcUrl(url);
      //hikari.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
      hikari.setUsername(user);
      hikari.setPassword(password);
      hikari.setMaximumPoolSize(2);
{'fraction_non_alphanumeric': 0.10512982900569981, 'fraction_numerical': 0.0018999366687777073, 'mean_word_length': 3.5446043165467627, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '6608104', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1873, 'n_words': 500}