Search in sources :

Example 6 with MysqlEnvironmentHandler

use of com.axway.ats.environment.database.MysqlEnvironmentHandler in project ats-framework by Axway.

the class Test_MysqlEnvironmentHandler method restoreBackupNegativeExceptionThrownOnExecuteBatch.

@Test(expected = DatabaseEnvironmentCleanupException.class)
public void restoreBackupNegativeExceptionThrownOnExecuteBatch() throws DatabaseEnvironmentCleanupException, SQLException, DbException {
    expect(mockDbProvider.getConnection()).andReturn(mockConnection);
    expect(mockConnection.getMetaData()).andReturn(metaData);
    expect(metaData.getDriverMajorVersion()).andReturn(5);
    expect(metaData.getDriverMinorVersion()).andReturn(1);
    expect(mockDbConnection.getConnHash()).andReturn(Long.toString(System.nanoTime()));
    expect(mockDbConnection.getDataSource()).andReturn(mockDataSource);
    expect(mockDbConnection.getUser()).andReturn("user");
    expect(mockDbConnection.getPassword()).andReturn("password");
    expect(mockDataSource.getConnection("user", "password")).andReturn(mockConnection);
    //now the restore begins
    expect(mockConnection.getAutoCommit()).andReturn(true);
    mockConnection.setAutoCommit(false);
    //the backup restore
    expect(mockConnection.prepareStatement("SET FOREIGN_KEY_CHECKS = 0;")).andReturn(mockStatement);
    expect(mockStatement.execute()).andReturn(true);
    mockStatement.close();
    PreparedStatement mockStatement2 = createMock(PreparedStatement.class);
    expect(mockConnection.prepareStatement(LINE_SEPARATOR + "LOCK TABLES `Revision` WRITE;")).andReturn(mockStatement2);
    expect(mockStatement2.execute()).andReturn(true);
    mockStatement2.close();
    PreparedStatement mockStatement3 = createMock(PreparedStatement.class);
    expect(mockConnection.prepareStatement("DELETE FROM `Revision`;")).andReturn(mockStatement3);
    expect(mockStatement3.execute()).andReturn(true);
    mockStatement3.close();
    PreparedStatement mockStatement4 = createMock(PreparedStatement.class);
    expect(mockConnection.prepareStatement("INSERT INTO `Revision` (id,SchemaVersion,STVersion,InstallDate) " + LINE_SEPARATOR + "VALUES(1,2,'4.9  206','2008-11-27 12:35:49.0');")).andReturn(mockStatement4);
    expect(mockStatement4.execute()).andReturn(true);
    mockStatement4.close();
    PreparedStatement mockStatement5 = createMock(PreparedStatement.class);
    expect(mockConnection.prepareStatement("UNLOCK TABLES;")).andReturn(mockStatement5);
    expect(mockStatement5.execute()).andThrow(new SQLException());
    mockStatement5.close();
    mockConnection.rollback();
    mockConnection.setAutoCommit(true);
    mockConnection.close();
    replayAll();
    MysqlEnvironmentHandler envHandler = new MysqlEnvironmentHandler(mockDbConnection, mockDbProvider);
    envHandler.restore(Test_MysqlEnvironmentHandler.class.getResource("backupFile.txt").getFile());
    envHandler.disconnect();
    verifyAll();
}
Also used : MysqlEnvironmentHandler(com.axway.ats.environment.database.MysqlEnvironmentHandler) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Example 7 with MysqlEnvironmentHandler

use of com.axway.ats.environment.database.MysqlEnvironmentHandler in project ats-framework by Axway.

the class Test_MysqlEnvironmentHandler method escapeSQL.

@Test
public void escapeSQL() throws DatabaseEnvironmentCleanupException, DbException, IOException, ParseException, SQLException {
    expect(mockDbProvider.getConnection()).andReturn(mockConnection);
    expect(mockConnection.getMetaData()).andReturn(metaData);
    expect(metaData.getDriverMajorVersion()).andReturn(5);
    expect(metaData.getDriverMinorVersion()).andReturn(1);
    replayAll();
    String source = "\\\\NO_FP_NO_IP){0}\\W((ip)|(subnet))\" \"?((no)|(number)|(mask)|(range)|(addr)|(address)|(net)|(network)|(subnet))?.{0,8}\\W\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\W";
    String expected = "\\\\\\\\NO_FP_NO_IP){0}\\\\W((ip)|(subnet))\\\" \\\"?((no)|(number)|(mask)|(range)|(addr)|(address)|(net)|(network)|(subnet))?.{0,8}\\\\W\\\\d{1,3}\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}\\\\W";
    MysqlEnvironmentHandler envHandler = new MysqlEnvironmentHandler(mockDbConnection, mockDbProvider);
    String result = envHandler.escapeValue(source);
    Assert.assertEquals(expected, result);
    verifyAll();
}
Also used : MysqlEnvironmentHandler(com.axway.ats.environment.database.MysqlEnvironmentHandler) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Example 8 with MysqlEnvironmentHandler

use of com.axway.ats.environment.database.MysqlEnvironmentHandler in project ats-framework by Axway.

the class Test_MysqlEnvironmentHandler method createBackupPositive.

@Test
public void createBackupPositive() throws DatabaseEnvironmentCleanupException, DbException, IOException, ParseException, SQLException {
    //the columns meta data
    DbRecordValuesList column1MetaData = new DbRecordValuesList();
    column1MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name1"));
    column1MetaData.add(new DbRecordValue("", "COLUMN_TYPE", "varchar(32)"));
    column1MetaData.add(new DbRecordValue("", "COLUMN_DEFAULT", true));
    DbRecordValuesList column2MetaData = new DbRecordValuesList();
    column2MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name2"));
    column2MetaData.add(new DbRecordValue("", "COLUMN_TYPE", "varchar(32)"));
    column2MetaData.add(new DbRecordValue("", "COLUMN_DEFAULT", true));
    DbRecordValuesList column3MetaData = new DbRecordValuesList();
    column3MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name3"));
    column3MetaData.add(new DbRecordValue("", "COLUMN_TYPE", "bit"));
    column3MetaData.add(new DbRecordValue("", "COLUMN_DEFAULT", 1));
    DbRecordValuesList[] columnsMetaData = new DbRecordValuesList[] { column1MetaData, column2MetaData, column3MetaData };
    DbRecordValuesList record1Value = new DbRecordValuesList();
    record1Value.add(new DbRecordValue("table1", "name1", "value1"));
    record1Value.add(new DbRecordValue("table1", "name2", null));
    record1Value.add(new DbRecordValue("table1", "name3", "1"));
    DbRecordValuesList[] recordValues = new DbRecordValuesList[] { record1Value };
    expect(mockDbProvider.getConnection()).andReturn(mockConnection);
    expect(mockConnection.getMetaData()).andReturn(metaData);
    expect(mockDbConnection.getUser()).andReturn("myUserName").atLeastOnce();
    expect(metaData.getDriverMajorVersion()).andReturn(5);
    expect(metaData.getDriverMinorVersion()).andReturn(1);
    expect(mockDbProvider.select(isA(String.class))).andReturn(columnsMetaData);
    expect(mockDbProvider.select(isA(DbQuery.class), eq(DbReturnModes.ESCAPED_STRING))).andReturn(recordValues);
    expect(mockDbProvider.select(isA(String.class))).andReturn(columnsMetaData);
    expect(mockDbProvider.select(isA(DbQuery.class), eq(DbReturnModes.ESCAPED_STRING))).andReturn(recordValues);
    //expect the file writer calls
    //foreign keys check start
    mockFileWriter.write("SET FOREIGN_KEY_CHECKS = 0;" + EOL_MARKER + LINE_SEPARATOR);
    //table1
    mockFileWriter.write("LOCK TABLES `table1` WRITE;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("DELETE FROM `table1`;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("INSERT INTO `table1` (name1,name2,name3) VALUES('value1',NULL,0x1);" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.flush();
    mockFileWriter.write("UNLOCK TABLES;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write(LINE_SEPARATOR);
    //table2
    mockFileWriter.write("LOCK TABLES `table2` WRITE;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("DELETE FROM `table2`;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("INSERT INTO `table2` (name1,name2,name3) VALUES('value1',NULL,0x1);" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.flush();
    mockFileWriter.write("UNLOCK TABLES;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write(LINE_SEPARATOR);
    //foreign keys check end
    mockFileWriter.write("");
    replayAll();
    DbTable table1 = new DbTable("table1");
    DbTable table2 = new DbTable("table2");
    MysqlEnvironmentHandler envHandler = new MysqlEnvironmentHandler(mockDbConnection, mockDbProvider);
    envHandler.addTable(table1);
    envHandler.addTable(table2);
    envHandler.writeBackupToFile(mockFileWriter);
    verifyAll();
}
Also used : MysqlEnvironmentHandler(com.axway.ats.environment.database.MysqlEnvironmentHandler) DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbQuery(com.axway.ats.common.dbaccess.DbQuery) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) DbTable(com.axway.ats.environment.database.model.DbTable) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Aggregations

BaseTest (com.axway.ats.environment.BaseTest)8 MysqlEnvironmentHandler (com.axway.ats.environment.database.MysqlEnvironmentHandler)8 Test (org.junit.Test)8 DbTable (com.axway.ats.environment.database.model.DbTable)5 DbQuery (com.axway.ats.common.dbaccess.DbQuery)4 DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)4 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)4 PreparedStatement (java.sql.PreparedStatement)2 ArrayList (java.util.ArrayList)2 SQLException (java.sql.SQLException)1