Best EvoMaster code snippet using org.evomaster.client.java.controller.db.SqlScriptRunner.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
1package org.evomaster.client.java.controller.db;2import java.io.BufferedReader;3import java.io.FileReader;4import java.io.IOException;5import java.sql.Connection;6import java.sql.DriverManager;7import java.sql.SQLException;8public class SqlScriptRunner {9 public static void main(String[] args) throws IOException {10 String dbAddress = args[0];11 String dbPort = args[1];12 String dbName = args[2];13 String dbUser = args[3];14 String dbPassword = args[4];15 String sqlScript = args[5];16 String sql = "";17 try (Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);18 BufferedReader br = new BufferedReader(new FileReader(sqlScript))) {19 String line;20 while ((line = br.readLine()) != null) {21 sql += line;22 }23 conn.createStatement().execute(sql);24 } catch (SQLException e) {25 e.printStackTrace();26 }27 }28}29package org.evomaster.client.java.controller.db;30import java.io.BufferedReader;31import java.io.FileReader;32import java.io.IOException;33import java.sql.Connection;34import java.sql.DriverManager;35import java.sql.SQLException;36public class SqlScriptRunner {37 public static void main(String[] args) throws IOException {38 String dbAddress = args[0];39 String dbPort = args[1];40 String dbName = args[2];41 String dbUser = args[3];42 String dbPassword = args[4];43 String sqlScript = args[5];44 String sql = "";45 try (Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);46 BufferedReader br = new BufferedReader(new FileReader(sqlScript))) {47 String line;48 while ((line = br.readLine()) != null) {49 sql += line;50 }51 conn.createStatement().execute(sql);52 } catch (SQLException e) {53 e.printStackTrace();54 }55 }56}57package org.evomaster.client.java.controller.db;
SqlScriptRunner
Using AI Code Generation
1import org.evomaster.client.java.controller.db.SqlScriptRunner;2import java.sql.Connection;3import java.sql.DriverManager;4import java.sql.SQLException;5public class 3{6 public static void main(String[] args) throws SQLException {7 SqlScriptRunner runner = new SqlScriptRunner(connection);8 runner.run("src/main/resources/sql_script.sql");9 }10}11import org.evomaster.client.java.controller.db.SqlScriptRunner;12import java.sql.Connection;13import java.sql.DriverManager;14import java.sql.SQLException;15public class 4{16 public static void main(String[] args) throws SQLException {17 SqlScriptRunner runner = new SqlScriptRunner(connection);18 runner.run("src/main/resources/sql_script.sql");19 }20}21import org.evomaster.client.java.controller.db.SqlScriptRunner;22import java.sql.Connection;23import java.sql.DriverManager;24import java.sql.SQLException
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!!