How to use SqlHandler class of org.evomaster.client.java.controller.internal.db package

Best EvoMaster code snippet using org.evomaster.client.java.controller.internal.db.SqlHandler

Source:SutController.java Github

copy

Full Screen

...9import org.evomaster.client.java.controller.api.dto.*;10import org.evomaster.client.java.controller.db.DbCleaner;11import org.evomaster.client.java.controller.db.SqlScriptRunner;12import org.evomaster.client.java.controller.internal.db.SchemaExtractor;13import org.evomaster.client.java.controller.internal.db.SqlHandler;14import org.evomaster.client.java.controller.problem.ProblemInfo;15import org.evomaster.client.java.instrumentation.staticstate.UnitsInfoRecorder;16import org.evomaster.client.java.utils.SimpleLogger;17import org.evomaster.client.java.controller.api.ControllerConstants;18import org.evomaster.client.java.controller.api.dto.database.execution.ExecutionDto;19import org.evomaster.client.java.controller.api.dto.database.operations.InsertionDto;20import org.evomaster.client.java.controller.api.dto.database.schema.DbSchemaDto;21import org.evomaster.client.java.instrumentation.AdditionalInfo;22import org.evomaster.client.java.instrumentation.TargetInfo;23import org.glassfish.jersey.jackson.JacksonFeature;24import org.glassfish.jersey.logging.LoggingFeature;25import org.glassfish.jersey.server.ResourceConfig;26import org.glassfish.jersey.servlet.ServletContainer;27import java.net.InetSocketAddress;28import java.sql.Connection;29import java.sql.SQLException;30import java.util.*;31import java.util.concurrent.CopyOnWriteArrayList;32/**33 * Abstract class used to connect to the EvoMaster process, and34 * that is responsible to start/stop/restart the tested application,35 * ie the system under test (SUT)36 */37public abstract class SutController implements SutHandler {38 private int controllerPort = ControllerConstants.DEFAULT_CONTROLLER_PORT;39 private String controllerHost = ControllerConstants.DEFAULT_CONTROLLER_HOST;40 private final SqlHandler sqlHandler = new SqlHandler();41 private Server controllerServer;42 /**43 * If using a SQL Database, gather info about its schema44 */45 private DbSchemaDto schemaDto;46 /**47 * For each action in a test, keep track of the extra heuristics, if any48 */49 private final List<ExtraHeuristicsDto> extras = new CopyOnWriteArrayList<>();50 private int actionIndex = -1;51 /**52 * Start the controller as a RESTful server.53 * Use the setters of this class to change the default54 * port and host.55 * <br>56 * This method is blocking until the server is initialized.57 *58 * @return true if there was no problem in starting the controller59 */60 public final boolean startTheControllerServer() {61 //Jersey62 ResourceConfig config = new ResourceConfig();63 config.register(JacksonFeature.class);64 config.register(new EMController(this));65 config.register(LoggingFeature.class);66 //Jetty67 controllerServer = new Server(InetSocketAddress.createUnresolved(68 getControllerHost(), getControllerPort()));69 ErrorHandler errorHandler = new ErrorHandler();70 errorHandler.setShowStacks(true);71 controllerServer.setErrorHandler(errorHandler);72 ServletHolder servlet = new ServletHolder(new ServletContainer(config));73 ServletContextHandler context = new ServletContextHandler(controllerServer,74 ControllerConstants.BASE_PATH + "/*");75 context.addServlet(servlet, "/*");76 try {77 controllerServer.start();78 } catch (Exception e) {79 SimpleLogger.error("Failed to start Jetty: " + e.getMessage());80 controllerServer.destroy();81 }82 //just make sure we start from a clean state83 newSearch();84 SimpleLogger.info("Started controller server on: " + controllerServer.getURI());85 return true;86 }87 public final boolean stopTheControllerServer() {88 try {89 controllerServer.stop();90 return true;91 } catch (Exception e) {92 SimpleLogger.error("Failed to stop the controller server: " + e.toString());93 return false;94 }95 }96 /**97 * @return the actual port in use (eg, if it was an ephemeral 0)98 */99 public final int getControllerServerPort() {100 return ((AbstractNetworkConnector) controllerServer.getConnectors()[0]).getLocalPort();101 }102 public final int getControllerPort() {103 return controllerPort;104 }105 public final void setControllerPort(int controllerPort) {106 this.controllerPort = controllerPort;107 }108 public final String getControllerHost() {109 return controllerHost;110 }111 public final void setControllerHost(String controllerHost) {112 this.controllerHost = controllerHost;113 }114 @Override115 public void execInsertionsIntoDatabase(List<InsertionDto> insertions) {116 Connection connection = getConnection();117 if (connection == null) {118 throw new IllegalStateException("No connection to database");119 }120 try {121 SqlScriptRunner.execInsert(connection, insertions);122 } catch (SQLException e) {123 throw new RuntimeException(e);124 }125 }126 public int getActionIndex(){127 return actionIndex;128 }129 /**130 * Calculate heuristics based on intercepted SQL commands131 *132 * @param sql command as a string133 */134 public final void handleSql(String sql) {135 Objects.requireNonNull(sql);136 sqlHandler.handle(sql);137 }138 public final void enableComputeSqlHeuristicsOrExtractExecution(boolean enableSqlHeuristics, boolean enableSqlExecution){139 sqlHandler.setCalculateHeuristics(enableSqlHeuristics);140 sqlHandler.setExtractSqlExecution(enableSqlHeuristics || enableSqlExecution);141 }142 /**143 * This is needed only during test generation (not execution),144 * and it is automatically called by the EM controller after145 * the SUT is started.146 */147 public final void initSqlHandler() {148 sqlHandler.setConnection(getConnection());149 }150 public final void resetExtraHeuristics() {151 sqlHandler.reset();152 }153 public final List<ExtraHeuristicsDto> getExtraHeuristics() {154 if (extras.size() == actionIndex) {155 extras.add(computeExtraHeuristics());156 }157 return new ArrayList<>(extras);158 }159 public final ExtraHeuristicsDto computeExtraHeuristics() {160 ExtraHeuristicsDto dto = new ExtraHeuristicsDto();161 if(sqlHandler.isCalculateHeuristics()) {...

Full Screen

Full Screen

SqlHandler

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseCommandDto;2import org.evomaster.client.java.controller.api.dto.database.operations.InsertionDto;3import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;4import org.evomaster.client.java.controller.api.dto.database.schema.DbSchemaDto;5import org.evomaster.client.java.controller.api.dto.database.schema.TableDto;6import org.evomaster.client.java.controller.api.dto.database.schema.TableEntryDto;7import org.evomaster.client.java.controller.internal.db.SqlHandler;8import org.junit.jupiter.api.Test;9import java.sql.SQLException;10import java.util.List;11import static org.junit.jupiter.api.Assertions.assertEquals;12import static org.junit.jupiter.api.Assertions.assertTrue;13public class SqlHandlerTest {14 public void testSqlHandler() throws SQLException {15 DbSchemaDto schema = new DbSchemaDto();16 schema.type = DatabaseType.H2;17 schema.name = "PUBLIC";18 TableDto table = new TableDto();19 table.name = "my_table";20 TableEntryDto entry = new TableEntryDto();21 entry.name = "id";22 entry.type = "INTEGER";23 entry.primaryKey = true;24 table.columns.add(entry);25 entry = new TableEntryDto();26 entry.name = "name";27 entry.type = "VARCHAR";28 table.columns.add(entry);29 entry = new TableEntryDto();30 entry.name = "value";31 entry.type = "VARCHAR";32 table.columns.add(entry);33 schema.tables.add(table);34 SqlHandler handler = new SqlHandler(schema);35 InsertionDto insertion = new InsertionDto();36 insertion.table = "my_table";37 insertion.values.put("id", "1");38 insertion.values.put("name", "'foo'");39 insertion.values.put("value", "'bar'");40 List<DatabaseCommandDto> commands = handler.handleCommand(insertion);41 assertEquals(1, commands.size());42 assertTrue(commands.get(0) instanceof InsertionDto);43 insertion = (InsertionDto) commands.get(0);44 assertEquals("my_table", insertion.table);45 assertEquals(3, insertion.values.size());46 assertEquals("1", insertion.values.get("id"));47 assertEquals("'foo'", insertion.values.get("name"));48 assertEquals("'bar'", insertion.values.get("value"));49 }50}51INSERT INTO my_table (id, name, value) VALUES (1, 'foo', 'bar

Full Screen

Full Screen

SqlHandler

Using AI Code Generation

copy

Full Screen

1SqlHandler.executeStatement("CREATE TABLE IF NOT EXISTS TestTable (id INTEGER PRIMARY KEY, name VARCHAR(255));");2SqlHandler.executeStatement("INSERT INTO TestTable (id, name) VALUES (1, 'John');");3List<String[]> result = SqlHandler.executeStatement("SELECT * FROM TestTable WHERE id = 1;");4SqlHandler.executeStatement("UPDATE TestTable SET name = 'Jack' WHERE id = 1;");5SqlHandler.executeStatement("DELETE FROM TestTable WHERE id = 1;");6SqlHandler.executeStatement("DROP TABLE TestTable;");7long lastInsertedId = SqlHandler.getLastInsertedRowId();8long numberOfAffectedRows = SqlHandler.getNumberOfAffectedRows();9long numberOfReturnedRows = SqlHandler.getNumberOfReturnedRows();10long numberOfReturnedColumns = SqlHandler.getNumberOfReturnedColumns();11String value = SqlHandler.getReturnedValue(0, 0);12String columnName = SqlHandler.getReturnedColumnName(0);13String columnType = SqlHandler.getReturnedColumnType(0);14String value = SqlHandler.getReturnedValue(0, "name");15String value = SqlHandler.getReturnedValue(0, "name");16List<String> columnNames = SqlHandler.getReturnedColumnNames();17List<String> columnTypes = SqlHandler.getReturnedColumnTypes();18long numberOfReturnedRows = SqlHandler.getNumberOfReturnedRows();19long numberOfReturnedColumns = SqlHandler.getNumberOfReturnedColumns();20String value = SqlHandler.getReturnedValue(0, 0);21String columnName = SqlHandler.getReturnedColumnName(0);

Full Screen

Full Screen

SqlHandler

Using AI Code Generation

copy

Full Screen

1SqlHandler sqlHandler = new SqlHandler();2sqlHandler.insertInto("user")3 .column("id", 1)4 .column("name", "jack")5 .column("age", 20)6 .executeUpdate();7sqlHandler.deleteFrom("user")8 .where("id", 1)9 .executeUpdate();10ResultSet rs = sqlHandler.selectFrom("user")11 .where("id", 1)12 .executeQuery();13sqlHandler.update("user")14 .set("name", "jack")15 .where("id", 1)16 .executeUpdate();17sqlHandler.closeConnection();18String name = sqlHandler.getColumnValue(rs, 1, "name");19int age = sqlHandler.getColumnValue(rs, 1, "age");20int rowCount = sqlHandler.getRowCount(rs);

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run EvoMaster automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful