com.technophobia.substeps.database.impl.SQLSubStepImplementations Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of database-substeps Show documentation
Show all versions of database-substeps Show documentation
Database step implementations
The newest version!
/*
* Copyright Technophobia Ltd & Alan Raison 2013
*
* This file is part of Substeps.
*
* Substeps is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Substeps is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Substeps. If not, see .
*/
package com.technophobia.substeps.database.impl;
import static com.technophobia.substeps.database.runner.DatabaseSetupTearDown.getExecutionContext;
import static org.hamcrest.CoreMatchers.equalTo;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.technophobia.substeps.database.runner.DatabaseExecutionContext;
import com.technophobia.substeps.database.runner.DatabaseSetupTearDown;
import com.technophobia.substeps.model.SubSteps;
import com.technophobia.substeps.model.SubSteps.Step;
import com.technophobia.substeps.model.SubSteps.StepParameter;
import com.technophobia.substeps.model.parameter.IntegerConverter;
@SubSteps.StepImplementations(requiredInitialisationClasses = DatabaseSetupTearDown.class)
public class SQLSubStepImplementations {
private static final Logger LOG = LoggerFactory.getLogger(SQLSubStepImplementations.class);
/**
* Executes the given query putting the result in the execution context,
* this can then be accessed by other steps such as
* AssertQueryResultContains
*
* @example ExecuteQuery {SELECT COLOUR FROM CARS}
* @section Database
* @param sql
* the sql to execute
*/
@SubSteps.Step("ExecuteQuery \\{([^\\}]*)\\}")
public void executeQuery(final String sql) {
LOG.debug("Executing sql [{}]", sql);
Database.execute(sql);
}
@SubSteps.Step("ExecuteUpdate \\{([^\\}]*)\\}")
public void executeUpdate(final String sql) {
LOG.debug("Executing sql update [{}]", sql);
Database.update(sql);
}
/**
* Executes the given sql script
*
* @example ExecuteScript "/myScript.sql"
* @section Database
* @param script
* the sql script which should be on the classpath
*/
@SubSteps.Step("ExecuteScript \"([^\"]*)\"")
public void executeScript(final String script) {
LOG.debug("Executing sql script [{}]", script);
Connection connection = null;
InputStream scriptStream = null;
try {
connection = DatabaseSetupTearDown.getConnectionContext().getConnection();
final ScriptRunner scriptRunner = new ScriptRunner(connection);
scriptStream = SQLSubStepImplementations.class.getResourceAsStream(script);
Assert.assertNotNull("Unable to find script: " + script, scriptStream);
final Reader scriptReader = new InputStreamReader(scriptStream);
scriptRunner.runScript(scriptReader);
} finally {
Database.close(connection);
Database.close(scriptStream);
}
}
/**
* Executes the supplied sql query and store the results in the default
* stash
*
* @example ExecuteQueryAndStashResults {select * from wherever where
* something="something-else"}
* @section Database
* @param sql
* the sql query to run
*/
@Step("ExecuteQueryAndStashResults \\{([^\\}]*)\\}")
public void executeQueryAndStashResults(final String sql) {
final List