Best EvoMaster code snippet using org.evomaster.client.java.controller.db.SqlScriptRunner
Source:ExternalEvoMasterController.java
2import org.evomaster.client.java.controller.AuthUtils;3import org.evomaster.client.java.controller.ExternalSutController;4import org.evomaster.client.java.controller.InstrumentedSutStarter;5import org.evomaster.client.java.controller.db.DbCleaner;6import org.evomaster.client.java.controller.db.SqlScriptRunner;7import org.evomaster.client.java.controller.problem.ProblemInfo;8import org.evomaster.client.java.controller.problem.RestProblem;9import org.evomaster.client.java.controller.api.dto.AuthenticationDto;10import org.evomaster.client.java.controller.api.dto.HeaderDto;11import org.evomaster.client.java.controller.api.dto.SutInfoDto;12import org.h2.tools.Server;13import java.io.File;14import java.io.IOException;15import java.io.InputStream;16import java.io.InputStreamReader;17import java.nio.file.Files;18import java.nio.file.Path;19import java.nio.file.Paths;20import java.nio.file.StandardCopyOption;21import java.sql.Connection;22import java.sql.DriverManager;23import java.sql.SQLException;24import java.util.Arrays;25import java.util.List;26public class ExternalEvoMasterController extends ExternalSutController {27 public static void main(String[] args) {28 int controllerPort = 40100;29 if (args.length > 0) {30 controllerPort = Integer.parseInt(args[0]);31 }32 int sutPort = 12345;33 if (args.length > 1) {34 sutPort = Integer.parseInt(args[1]);35 }36 String jarLocation = "cs/rest/original/scout-api/api/target";37 if(args.length > 2){38 jarLocation = args[2];39 }40 if(! jarLocation.endsWith(".jar")) {41 jarLocation += "/scout-api-sut.jar";42 }43 int timeoutSeconds = 120;44 if(args.length > 3){45 timeoutSeconds = Integer.parseInt(args[3]);46 }47 ExternalEvoMasterController controller =48 new ExternalEvoMasterController(controllerPort, jarLocation, sutPort, timeoutSeconds);49 InstrumentedSutStarter starter = new InstrumentedSutStarter(controller);50 starter.start();51 }52 private final int timeoutSeconds;53 private final int sutPort;54 private final int dbPort;55 private final String jarLocation;56 private final String tmpDir;57 private final String CONFIG_FILE = "scout_api_evomaster.yml";58 private Connection connection;59 private List<String> sqlCommands;60 private Server h2;61 public ExternalEvoMasterController(){62 this(40100, "../api/target", 12345, 120);63 }64 public ExternalEvoMasterController(int controllerPort,65 String jarLocation,66 int sutPort,67 int timeoutSeconds) {68 this.sutPort = sutPort;69 this.dbPort = sutPort + 1;70 this.jarLocation = jarLocation;71 this.timeoutSeconds = timeoutSeconds;72 setControllerPort(controllerPort);73 String base = Paths.get(jarLocation).toAbsolutePath().getParent().normalize().toString();74 tmpDir = base + "/temp/tmp_scout_api/temp_"+dbPort;75 createConfigurationFile();76 try(InputStream in = getClass().getResourceAsStream("/init_db.sql")) {77 sqlCommands = (new SqlScriptRunner()).readCommands(new InputStreamReader(in));78 } catch (Exception e){79 throw new RuntimeException(e);80 }81 }82 private String dbUrl(boolean withP6Spy) {83 String url = "jdbc";84 if (withP6Spy) {85 url += ":p6spy";86 }87 url += ":h2:tcp://localhost:" + dbPort + "/./temp/tmp_scout_api/testdb_" + dbPort;88 return url;89 }90 /**91 Unfortunately, it seems like Dropwizard is buggy, and has92 problems with overriding params without a YML file :(93 */94 private void createConfigurationFile() {95 //save config to same folder of JAR file96 Path path = getConfigPath();97 try(InputStream is = this.getClass().getResourceAsStream("/"+ CONFIG_FILE )){98 Files.copy(is, path, StandardCopyOption.REPLACE_EXISTING);99 } catch (Exception e){100 throw new RuntimeException(e);101 }102 }103 private Path getConfigPath(){104 return Paths.get(jarLocation)105 .toAbsolutePath()106 .getParent()107 .resolve(CONFIG_FILE)108 .normalize();109 }110 @Override111 public String[] getInputParameters() {112 return new String[]{"server", getConfigPath().toAbsolutePath().toString()};113 }114 @Override115 public String[] getJVMParameters() {116 return new String[]{117 "-Ddw.server.connector.port="+sutPort,118 "-Ddw.mediaFilesFolder="+tmpDir+"/media-files",119 "-Ddw.tempFolder="+tmpDir,120 "-Ddw.database.url="+dbUrl(true)121 };122 }123 @Override124 public String getBaseURL() {125 return "http://localhost:"+sutPort;126 }127 @Override128 public String getPathToExecutableJar() {129 return jarLocation;130 }131 @Override132 public String getLogMessageOfInitializedServer() {133 return "Server: Started";134 }135 @Override136 public long getMaxAwaitForInitializationInSeconds() {137 return timeoutSeconds;138 }139 @Override140 public void preStart() {141 try {142 //starting H2143 h2 = Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "" + dbPort);144 h2.start();145 } catch (SQLException e) {146 throw new RuntimeException(e);147 }148 }149 @Override150 public void postStart() {151 try {152 Class.forName("org.h2.Driver");153 connection = DriverManager.getConnection(dbUrl(false), "sa", "");154 } catch (Exception e) {155 throw new RuntimeException(e);156 }157 resetStateOfSUT();158 }159 @Override160 public void preStop() {161 }162 @Override163 public void postStop() {164 if (h2 != null) {165 h2.stop();166 }167 }168 @Override169 public String getPackagePrefixesToCover() {170 return "se.devscout.";171 }172 @Override173 public void resetStateOfSUT() {174 deleteDir(new File(tmpDir));175 DbCleaner.clearDatabase_H2(connection);176 SqlScriptRunner.runCommands(connection, sqlCommands);177 }178 private void deleteDir(File file) {179 File[] contents = file.listFiles();180 if (contents != null) {181 for (File f : contents) {182 deleteDir(f);183 }184 }185 file.delete();186 }187 @Override188 public ProblemInfo getProblemInfo() {189 return new RestProblem(190 getBaseURL() + "/api/swagger.json",...
Source:EmbeddedEvoMasterController.java
2import org.evomaster.client.java.controller.AuthUtils;3import org.evomaster.client.java.controller.EmbeddedSutController;4import org.evomaster.client.java.controller.InstrumentedSutStarter;5import org.evomaster.client.java.controller.db.DbCleaner;6import org.evomaster.client.java.controller.db.SqlScriptRunner;7import org.evomaster.client.java.controller.problem.ProblemInfo;8import org.evomaster.client.java.controller.problem.RestProblem;9import org.evomaster.client.java.controller.api.dto.AuthenticationDto;10import org.evomaster.client.java.controller.api.dto.HeaderDto;11import org.evomaster.client.java.controller.api.dto.SutInfoDto;12import se.devscout.scoutapi.ScoutAPIApplication;13import java.io.File;14import java.io.InputStream;15import java.io.InputStreamReader;16import java.sql.Connection;17import java.sql.SQLException;18import java.util.Arrays;19import java.util.List;20/**21 * Class used to start/stop the SUT. This will be controller by the EvoMaster process22 */23public class EmbeddedEvoMasterController extends EmbeddedSutController {24 public static void main(String[] args) {25 int port = 40100;26 if (args.length > 0) {27 port = Integer.parseInt(args[0]);28 }29 EmbeddedEvoMasterController controller = new EmbeddedEvoMasterController(port);30 InstrumentedSutStarter starter = new InstrumentedSutStarter(controller);31 starter.start();32 }33 private ScoutAPIApplication application;34 private Connection connection;35 private List<String> sqlCommands;36 public EmbeddedEvoMasterController() {37 this(40100);38 }39 public EmbeddedEvoMasterController(int port) {40 setControllerPort(port);41 try (InputStream in = getClass().getResourceAsStream("/init_db.sql")) {42 sqlCommands = (new SqlScriptRunner()).readCommands(new InputStreamReader(in));43 } catch (Exception e) {44 throw new RuntimeException(e);45 }46 }47 @Override48 public String startSut() {49 application = new ScoutAPIApplication();50 //Dirty hack for DW...51 System.setProperty("dw.server.connector.port", "0");52 try {53 application.run("server", "src/main/resources/scout_api_evomaster.yml");54 } catch (Exception e) {55 e.printStackTrace();56 return null;57 }58 try {59 Thread.sleep(3_000);60 } catch (InterruptedException e) {61 }62 while (!application.getJettyServer().isStarted()) {63 try {64 Thread.sleep(1_000);65 } catch (InterruptedException e) {66 }67 }68 connection = application.getConnection();69 resetStateOfSUT();70 return "http://localhost:" + application.getJettyPort();71 }72 @Override73 public boolean isSutRunning() {74 if (application == null) {75 return false;76 }77 return application.getJettyServer().isRunning();78 }79 @Override80 public void stopSut() {81 if (application != null) {82 try {83 application.getJettyServer().stop();84 } catch (Exception e) {85 e.printStackTrace();86 }87 }88 if (connection != null) {89 try {90 connection.close();91 } catch (SQLException e) {92 e.printStackTrace();93 }94 }95 }96 @Override97 public String getPackagePrefixesToCover() {98 return "se.devscout.";99 }100 @Override101 public void resetStateOfSUT() {102 deleteDir(new File("./target/temp"));103 DbCleaner.clearDatabase_H2(connection);104 SqlScriptRunner.runCommands(connection, sqlCommands);105 }106 @Override107 public List<AuthenticationDto> getInfoForAuthentication() {108 return Arrays.asList(109 AuthUtils.getForAuthorizationHeader("user", "ApiKey user"),110 AuthUtils.getForAuthorizationHeader("moderator", "ApiKey moderator"),111 AuthUtils.getForAuthorizationHeader("administrator", "ApiKey administrator")112 );113 }114 @Override115 public Connection getConnection() {116 return connection;117 }118 @Override...
SqlScriptRunner
Using AI Code Generation
1import org.evomaster.client.java.controller.db.SqlScriptRunner;2public class 3 {3 public static void main(String[] args) {4 runner.run("" +5 "CREATE TABLE dbo.TestTable (id INT IDENTITY(1,1) PRIMARY KEY, value INT);" +6 "INSERT INTO dbo.TestTable (value) VALUES (1);" +7 "INSERT INTO dbo.TestTable (value) VALUES (2);" +8 "INSERT INTO dbo.TestTable (value) VALUES (3);");9 }10}11import org.evomaster.client.java.controller.db.SqlScriptRunner;12public class 4 {13 public static void main(String[] args) {14 runner.run("" +15 "CREATE TABLE dbo.TestTable (id INT IDENTITY(1,1) PRIMARY KEY, value INT);" +16 "INSERT INTO dbo.TestTable (value) VALUES (1);" +17 "INSERT INTO dbo.TestTable (value) VALUES (2);" +18 "INSERT INTO dbo.TestTable (value) VALUES (3);");19 }20}21import org.evomaster.client.java.controller.db.SqlScriptRunner;22public class 5 {23 public static void main(String[] args) {24 runner.run("" +25 "CREATE TABLE dbo.TestTable (id INT IDENTITY(1,1) PRIMARY KEY, value INT);" +26 "INSERT INTO dbo.TestTable (value) VALUES (1);" +27 "INSERT INTO dbo.TestTable (value) VALUES (2);" +28 "INSERT INTO dbo.TestTable (value) VALUES (3);");29 }30}
SqlScriptRunner
Using AI Code Generation
1import org.evomaster.client.java.controller.db.SqlScriptRunner;2public class 3 {3 public static void main(String[] args) throws Exception {4 SqlScriptRunner runner = new SqlScriptRunner();5 }6}7CREATE TABLE IF NOT EXISTS "public"."test" (8 "name" varchar(255) NOT NULL,9 "enum" varchar(255) NOT NULL,10 "set" varchar(255) NOT NULL,11 "char" varchar(255) NOT NULL,12 "varchar" varchar(255) NOT NULL,13 "tinytext" varchar(255) NOT NULL,14 "primary key" ("id")15);16INSERT INTO test (id, name, date, time, timestamp, bool, decimal, float, double, tinyint, smallint, mediumint, bigint, bit, binary, varbinary, blob, text, enum, set, char, varchar, tinytext, mediumtext, longtext, json,
SqlScriptRunner
Using AI Code Generation
1import org.evomaster.client.java.controller.db.SqlScriptRunner;2import org.evomaster.client.java.controller.db.SqlScriptRunnerConfig;3import java.io.File;4import java.io.IOException;5import java.sql.Connection;6import java.sql.SQLException;7public class Main {8 public static void main(String[] args) throws IOException, SQLException {9 SqlScriptRunnerConfig config = new SqlScriptRunnerConfig();10 config.setConnection(connection);11 config.setScriptFile(new File("/path/to/script.sql"));12 config.setThrowExceptionIfError(true);13 config.setContinueOnError(false);14 config.setPrintLog(true);15 config.setPrintError(true);16 config.setPrintInfo(true);17 config.setPrintWarning(true);18 SqlScriptRunner runner = new SqlScriptRunner(config);19 runner.run();20 }21}22throwExceptionIfError: if true, the method run() will throw a SQLException if an error is found in the script23run(): this method will run the SQL script24getConnection(): returns the connection to the database25getScriptFile(): returns the file containing the SQL script to be run26isThrowExceptionIfError(): returns true if an SQLException will be thrown if an error is found in the script27isContinueOnError(): returns true if the script will continue to run even if an error is found28isPrintLog(): returns true if the log messages will be printed to the standard output29isPrintError(): returns true if the error messages will be printed to the standard output30isPrintInfo(): returns true if the info messages will be printed to the standard output31isPrintWarning(): returns true if the warning messages will be printed to the standard output
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!