|
| 1 | +package em.external.petclinic; |
| 2 | + |
| 3 | +import org.evomaster.client.java.controller.ExternalSutController; |
| 4 | +import org.evomaster.client.java.controller.InstrumentedSutStarter; |
| 5 | +import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType; |
| 6 | +import org.evomaster.client.java.controller.db.DbCleaner; |
| 7 | +import org.evomaster.client.java.controller.internal.db.DbSpecification; |
| 8 | +import org.evomaster.client.java.controller.problem.ProblemInfo; |
| 9 | +import org.evomaster.client.java.controller.problem.RestProblem; |
| 10 | +import org.evomaster.client.java.controller.api.dto.AuthenticationDto; |
| 11 | +import org.evomaster.client.java.controller.api.dto.SutInfoDto; |
| 12 | +import org.evomaster.client.java.controller.problem.WebProblem; |
| 13 | +import org.h2.tools.Server; |
| 14 | + |
| 15 | +import java.sql.Connection; |
| 16 | +import java.sql.DriverManager; |
| 17 | +import java.sql.SQLException; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +public class ExternalEvoMasterController extends ExternalSutController { |
| 22 | + |
| 23 | + public static void main(String[] args) { |
| 24 | + |
| 25 | + int controllerPort = 40100; |
| 26 | + if (args.length > 0) { |
| 27 | + controllerPort = Integer.parseInt(args[0]); |
| 28 | + } |
| 29 | + int sutPort = 12345; |
| 30 | + if (args.length > 1) { |
| 31 | + sutPort = Integer.parseInt(args[1]); |
| 32 | + } |
| 33 | + String jarLocation = "cs/web/spring-petclinic/target"; |
| 34 | + if (args.length > 2) { |
| 35 | + jarLocation = args[2]; |
| 36 | + } |
| 37 | + if(! jarLocation.endsWith(".jar")) { |
| 38 | + jarLocation += "/petclinic-sut.jar"; |
| 39 | + } |
| 40 | + int timeoutSeconds = 120; |
| 41 | + if(args.length > 3){ |
| 42 | + timeoutSeconds = Integer.parseInt(args[3]); |
| 43 | + } |
| 44 | + |
| 45 | + String command = "java"; |
| 46 | + if(args.length > 4){ |
| 47 | + command = args[4]; |
| 48 | + } |
| 49 | + |
| 50 | + ExternalEvoMasterController controller = |
| 51 | + new ExternalEvoMasterController(controllerPort, jarLocation, sutPort, timeoutSeconds, command); |
| 52 | + InstrumentedSutStarter starter = new InstrumentedSutStarter(controller); |
| 53 | + |
| 54 | + starter.start(); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + private final int timeoutSeconds; |
| 59 | + private final int sutPort; |
| 60 | + private final int dbPort; |
| 61 | + private String jarLocation; |
| 62 | + private Connection sqlConnection; |
| 63 | + private List<DbSpecification> dbSpecification; |
| 64 | + private Server h2; |
| 65 | + |
| 66 | + public ExternalEvoMasterController() { |
| 67 | + this(40100, "../core/target", 12345, 120, "java"); |
| 68 | + } |
| 69 | + |
| 70 | + public ExternalEvoMasterController(String jarLocation) { |
| 71 | + this(); |
| 72 | + this.jarLocation = jarLocation; |
| 73 | + } |
| 74 | + |
| 75 | + public ExternalEvoMasterController(int controllerPort, String jarLocation, int sutPort, int timeoutSeconds, String command) { |
| 76 | + this.sutPort = sutPort; |
| 77 | + this.dbPort = sutPort + 1; |
| 78 | + this.jarLocation = jarLocation; |
| 79 | + this.timeoutSeconds = timeoutSeconds; |
| 80 | + setControllerPort(controllerPort); |
| 81 | + setJavaCommand(command); |
| 82 | + } |
| 83 | + |
| 84 | + private String dbUrl( ) { |
| 85 | + |
| 86 | + String url = "jdbc"; |
| 87 | + url += ":h2:tcp://localhost:" + dbPort + "/mem:testdb_" + dbPort; |
| 88 | + |
| 89 | + return url; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String[] getInputParameters() { |
| 94 | + return new String[]{"--server.port=" + sutPort}; |
| 95 | + } |
| 96 | + |
| 97 | + public String[] getJVMParameters() { |
| 98 | + return new String[]{ |
| 99 | + "-Dspring.datasource.url=" + dbUrl() + ";DB_CLOSE_DELAY=-1", |
| 100 | + "-Dspring.jpa.database-platform=org.hibernate.dialect.H2Dialect", |
| 101 | + "-Dspring.datasource.username=sa", |
| 102 | + "-Dspring.datasource.password" |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public String getBaseURL() { |
| 108 | + return "http://localhost:" + sutPort; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public String getPathToExecutableJar() { |
| 113 | + return jarLocation; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public String getLogMessageOfInitializedServer() { |
| 118 | + return "Started Application in "; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public long getMaxAwaitForInitializationInSeconds() { |
| 123 | + return timeoutSeconds; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void preStart() { |
| 128 | + |
| 129 | + try { |
| 130 | + //starting H2 |
| 131 | + h2 = Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "" + dbPort); |
| 132 | + h2.start(); |
| 133 | + } catch (SQLException e) { |
| 134 | + throw new RuntimeException(e); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void postStart() { |
| 140 | + closeDataBaseConnection(); |
| 141 | + |
| 142 | + try { |
| 143 | + Class.forName("org.h2.Driver"); |
| 144 | + sqlConnection = DriverManager.getConnection(dbUrl(), "sa", ""); |
| 145 | + dbSpecification = Arrays.asList(new DbSpecification(DatabaseType.H2,sqlConnection)); |
| 146 | + } catch (Exception e) { |
| 147 | + throw new RuntimeException(e); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void resetStateOfSUT() { |
| 153 | + //DbCleaner.clearDatabase_H2(sqlConnection); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void preStop() { |
| 158 | + closeDataBaseConnection(); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void postStop() { |
| 163 | + if (h2 != null) { |
| 164 | + h2.stop(); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private void closeDataBaseConnection() { |
| 169 | + if (sqlConnection != null) { |
| 170 | + try { |
| 171 | + sqlConnection.close(); |
| 172 | + } catch (SQLException e) { |
| 173 | + e.printStackTrace(); |
| 174 | + } |
| 175 | + sqlConnection = null; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public String getPackagePrefixesToCover() { |
| 181 | + return "org.springframework.samples.petclinic."; |
| 182 | + } |
| 183 | + |
| 184 | + |
| 185 | + @Override |
| 186 | + public ProblemInfo getProblemInfo() { |
| 187 | + return new WebProblem("/"); |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
| 192 | + return SutInfoDto.OutputFormat.JAVA_JUNIT_5; |
| 193 | + } |
| 194 | + |
| 195 | + |
| 196 | + @Override |
| 197 | + public List<AuthenticationDto> getInfoForAuthentication() { |
| 198 | + return null; |
| 199 | + } |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | + @Override |
| 204 | + public List<DbSpecification> getDbSpecifications() { |
| 205 | + return dbSpecification; |
| 206 | + } |
| 207 | +} |
0 commit comments