Package jodd.db.oom

Source Code of jodd.db.oom.LiveMapperDbTest$HsqlDb

// Copyright (c) 2003-2014, Jodd Team (jodd.org). All Rights Reserved.

package jodd.db.oom;

import jodd.datetime.JDateTime;
import jodd.db.DbSession;
import jodd.db.oom.sqlgen.DbEntitySql;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class LiveMapperDbTest extends DbBaseTest {

  /**
   * DATABASES TO TEST!
   */
  DbAccess[] databases = new DbAccess[]{
      new MySql(),
      new PostgreSql(),
      new HsqlDb(),
  };

  /**
   * MySql.
   */
  public class MySql extends MySqlDbAccess {

    @Override
    public String getCreateTableSql() {
      return "create table TESTER2 (" +
            "ID      INT UNSIGNED NOT NULL AUTO_INCREMENT," +
            "NAME    VARCHAR(20)  not null," +
            "VALUE    INT NULL," +
            "TIME    TIMESTAMP," +
            "TIME2    TIMESTAMP," +
            "primary key (ID)" +
            ')';
    }

    @Override
    public String getTableName() {
      return "TESTER2";
    }
  }

  /**
   * PostgreSql.
   */
  public class PostgreSql extends PostgreSqlDbAccess {

    @Override
    public void initDb() {
      super.initDb();
      dboom.getTableNames().setLowercase(true);
      dboom.getColumnNames().setLowercase(true);
    }

    @Override
    public String getCreateTableSql() {
      return "create table TESTER2 (" +
            "ID      SERIAL," +
            "NAME    varchar(20)  NOT NULL," +
            "VALUE    integer NULL," +
            "TIME    TIMESTAMP," +
            "TIME2    TIMESTAMP," +
            "primary key (ID)" +
            ')';
    }

    @Override
    public String getTableName() {
      return "TESTER2";
    }
  }

  /**
   * HsqlDB.
   */
  public class HsqlDb extends HsqlDbAccess {

    @Override
    public String getCreateTableSql() {
      return "create table TESTER2 (" +
            "ID      integer GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY," +
            "NAME    varchar(20)  NOT NULL," +
            "VALUE    integer NULL," +
            "TIME    TIMESTAMP," +
            "TIME2    TIMESTAMP" +
            ')';
    }

    @Override
    public String getTableName() {
      return "TESTER2";
    }
  }

  // ---------------------------------------------------------------- test

  @Test
  public void testLiveMapperDb() throws Exception {
    for (DbAccess db : databases) {
      System.out.println("\t" + db.getClass().getSimpleName());
      init();
      db.initDb();
      connect();

      dboom.registerEntity(Tester2.class);

      db.createTables();

      try {
        Tester2 tester2 = insertEntry();
        loadEntry(tester2);
      } finally {
        db.close();
      }
    }
  }


  protected Tester2 insertEntry() {
    DbSession session = new DbSession();

    Tester2 tester2 = new Tester2();
    tester2.id = 1;
    tester2.name = "Hello";
    tester2.value = Integer.valueOf(123);
    tester2.time = new JDateTime(2014, 1, 30, 10, 42, 34, 0).convertToSqlTimestamp();
    tester2.time2 = new JDateTime(2014, 1, 31, 11, 41, 32, 0);

    DbOomQuery dbOomQuery = DbOomQuery.query(session, DbEntitySql.insert(tester2));
    dbOomQuery.setGeneratedKey();
    int result = dbOomQuery.executeUpdate();

    assertEquals(1, result);
    session.closeSession();

    return tester2;
  }

  protected void loadEntry(Tester2 tester2) {
    DbSession session = new DbSession();

    DbOomQuery dbOomQuery = DbOomQuery.query(session, DbEntitySql.findById(Tester2.class, tester2.id));
    Tester2 tester21 = dbOomQuery.find(Tester2.class);

    assertNotNull(tester21);

    assertEquals(tester2.id, tester21.id);
    assertEquals(tester2.value, tester21.value);
    assertEquals(tester2.name, tester21.name);
    assertEquals(tester2.time, tester21.time);
    assertEquals(tester2.time2, tester21.time2);

    session.closeSession();
  }

}
TOP

Related Classes of jodd.db.oom.LiveMapperDbTest$HsqlDb

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.