package org.col.db.mapper;

import jersey.repackaged.com.google.common.base.Throwables;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres;
import ru.yandex.qatools.embed.postgresql.distribution.Version;

import java.net.ServerSocket;
import java.time.Duration;
import java.time.Instant;

/**
 * A reusable base class for all mybatis mapper tests that takes care of postgres & mybatis.
 * It offers a mapper to test in the implementing subclass.
 */
public class MapperTestBaseNoRule<T> {
  private static EmbeddedPostgres postgres;

  T mapper;

  //@Rule
  //public DbInitRule dbInitRule = DbInitRule.empty();

  public MapperTestBaseNoRule(Class<T> mapperClazz) {
    //mapper = session.getMapper(mapperClazz);
  }

  public void commit() {
  }


  @BeforeClass
  public static void before() throws Throwable {
    startDb();
  }

  private static void startDb() {
    System.out.println("Starting Postgres");
    try {
      postgres = new EmbeddedPostgres(Version.V9_6_3);
      // assigned to some free port
      ServerSocket socket = new ServerSocket(0);
      final String database = "col";
      final String user = "col";
      final String password = "species2000";

      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());

    } catch (Exception e) {
      System.err.println("Pg startup error: " + e.getMessage());
      e.printStackTrace();

      if (postgres != null) {
        postgres.stop();
      }
      Throwables.propagate(e);
    }
  }

  @AfterClass
  public static void after() {
    System.out.println("Stopping Postgres");
    postgres.stop();
  }

}
@@ -25,68 +25,8 @@
 r;%0A%0A
-import jersey.repackaged.com.google.common.base.Throwables;%0A
 impo
@@ -46,24 +46,24 @@
 AfterClass;%0A
+
 import org.j
@@ -238,61 +238,8 @@
 ket;
-%0Aimport java.time.Duration;%0Aimport java.time.Instant;
 %0A%0A/*
@@ -765,119 +765,8 @@
 e %7B%0A
-    startDb();%0A  %7D%0A%0A  private static void startDb() %7B%0A    System.out.println(%22Starting Postgres%22);%0A    try %7B%0A  
     
@@ -818,18 +818,16 @@
 3);%0A    
-  
 // assig
@@ -848,18 +848,16 @@
 ee port%0A
-  
     Serv
@@ -895,26 +895,24 @@
 ket(0);%0A    
-  
 final String
@@ -926,34 +926,32 @@
 se = %22col%22;%0A    
-  
 final String use
@@ -961,26 +961,24 @@
  %22col%22;%0A    
- 
- 
 final String
@@ -1008,549 +1008,108 @@
 0%22;%0A
-%0A      Instant start = Instant.now();%0A      System.out.println(%22Start postgres on port %22+socket.getLocalPort());%0A      final String url = postgres.start(%22localhost%22, socket.getLocalPort(), database, user, password);%0A      System.out.format(%22Pg startup time: %25s ms%5Cn%22, Duration.between(start, Instant.now()).toMillis());%0A%0A    %7D catch (Exception e) %7B%0A      System.err.println(%22Pg startup error: %22 + e.getMessage());%0A      e.printStackTrace();%0A%0A      if (postgres != null) %7B%0A        postgres.stop();%0A      %7D%0A      Throwables.propagate(e);%0A    %7D
+    final String url = postgres.start(%22localhost%22, socket.getLocalPort(), database, user, password);
 %0A  %7D

Simplify test even more