github.com/dolthub/go-mysql-server@v0.18.0/_integration/jdbc-mariadb/src/test/java/tech/sourced/jdbcmariadb/MySQLTest.java (about)

     1  package tech.sourced.jdbcmariadb;
     2  
     3  import org.junit.jupiter.api.Test;
     4  
     5  import java.sql.*;
     6  import java.util.ArrayList;
     7  import java.util.List;
     8  import java.util.Objects;
     9  
    10  import static org.junit.jupiter.api.Assertions.assertEquals;
    11  import static org.junit.jupiter.api.Assertions.fail;
    12  
    13  class MySQLTest {
    14  
    15      @Test
    16      void test() {
    17          String dbUrl = "jdbc:mariadb://127.0.0.1:3306/mydb?user=root&password=";
    18          String query = "SELECT name, email FROM mytable ORDER BY name, email";
    19          List<Result> expected = new ArrayList<>();
    20          expected.add(new Result("Evil Bob", "evilbob@gmail.com"));
    21          expected.add(new Result("Jane Doe", "jane@doe.com"));
    22          expected.add(new Result("John Doe", "john@doe.com"));
    23          expected.add(new Result("John Doe", "johnalt@doe.com"));
    24  
    25          List<Result> result = new ArrayList<>();
    26  
    27          try (Connection connection = DriverManager.getConnection(dbUrl)) {
    28              try (PreparedStatement stmt = connection.prepareStatement(query)) {
    29                  try (ResultSet rs = stmt.executeQuery()) {
    30                      while (rs.next()) {
    31                          result.add(new Result(rs.getString(1), rs.getString(2)));
    32                      }
    33                  }
    34              }
    35          } catch (SQLException e) {
    36              fail(e);
    37          }
    38  
    39          assertEquals(expected, result);
    40      }
    41  
    42      class Result {
    43          String name;
    44          String email;
    45  
    46          Result(String name, String email) {
    47              this.name = name;
    48              this.email = email;
    49          }
    50  
    51          @Override
    52          public boolean equals(Object o) {
    53              if (this == o) return true;
    54              if (o == null || getClass() != o.getClass()) return false;
    55              Result result = (Result) o;
    56              return Objects.equals(name, result.name) &&
    57                      Objects.equals(email, result.email);
    58          }
    59  
    60          @Override
    61          public String toString() {
    62              return "Result{" +
    63                      "name='" + name + '\'' +
    64                      ", email='" + email + '\'' +
    65                      '}';
    66          }
    67      }
    68  }