ohmydengzi
8/5/2018 - 11:33 PM

AbstractAssertionsExamples1.java

/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * Copyright 2012-2016 the original author or authors.
 */
package den1_1;

import java.sql.Connection;
import java.sql.SQLException;
import org.h2.jdbcx.JdbcConnectionPool;
import org.junit.Before;


import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
/**
 * 
 * Init data for assertions examples.
 * 
 * @author Régis Pouiller
 */
public class AbstractAssertionsExamples1 {

  public static DataSource getMySQLDataSource() {
    Properties props = new Properties();
    FileInputStream fis = null;
    MysqlDataSource mysqlDS = null;
    try {
        fis = new FileInputStream("G:\\java\\program\\deng_2\\src\\main\\java\\den1_1\\db.properties");
        props.load(fis);
        mysqlDS = new MysqlDataSource();
        mysqlDS.setURL(props.getProperty("MYSQL_DB_URL"));
        mysqlDS.setUser(props.getProperty("MYSQL_DB_USERNAME"));
        mysqlDS.setPassword(props.getProperty("MYSQL_DB_PASSWORD"));
        } catch (IOException e) {
        e.printStackTrace();
        }
    return mysqlDS;
  }
  DataSource dataSource = getMySQLDataSource();
  private static final String MEMBERS_CREATE_REQUEST = "CREATE TABLE members ( id_number int PRIMARY KEY, firstname VARCHAR(20) NOT NULL, surname VARCHAR(19));";
  private static final String[] MEMBERS_INSERT_REQUESTS = {
    "insert into members values (1, 'Hewson', 'Paul David');",
    "insert into members values (2, 'Evans', 'David Howell');",
  };
  private static final String ALBUMS_CREATE_REQUEST = "CREATE TABLE albums ( id_number int PRIMARY KEY, firstname VARCHAR(20) NOT NULL, surname VARCHAR(19));";
  private static final String[] ALBUMS_INSERT_REQUESTS = {
    "insert into albums values (1, 'Boy', 12);",
    "insert into albums values (2, 'October', 11);",
  };

  @Before
  public void setUp() throws SQLException {
    if (dataSource == null) {
      dataSource = getMySQLDataSource();
    }
    else {
      Connection conn = dataSource.getConnection();
      conn.createStatement().executeUpdate("drop table if exists albums;");
      conn.createStatement().executeUpdate("drop table if exists members;");
      conn.close();
    }
    Connection conn = dataSource.getConnection();
    conn.createStatement().executeUpdate(MEMBERS_CREATE_REQUEST);
    for (String request : MEMBERS_INSERT_REQUESTS) {
      conn.createStatement().executeUpdate(request);
    }
    conn.createStatement().executeUpdate(ALBUMS_CREATE_REQUEST);
    for (String request : ALBUMS_INSERT_REQUESTS) {
      conn.createStatement().executeUpdate(request);
    }
    conn.close();
  }

  protected void makeChangesInTheData() throws SQLException {
    Connection conn = dataSource.getConnection();
    conn.createStatement().executeUpdate("insert into members values (5, 'McGuiness', 'Paul', null, PARSEDATETIME('17/06/1951', 'dd/MM/yyyy'), null);");
    conn.createStatement().executeUpdate("update members set surname = 'Bono Vox' where id = 1;");
    conn.createStatement().executeUpdate("delete from albums where id = 15;");
    conn.close();
  }
}