Best Testsigma code snippet using com.testsigma.util.JarExtractorUtil
Source:DatabaseMigrationConfig.java
1package com.testsigma.config;2import com.testsigma.util.JarExtractorUtil;3import lombok.extern.log4j.Log4j2;4import org.apache.commons.lang3.StringUtils;5import org.flywaydb.core.Flyway;6import org.flywaydb.core.internal.resolver.ChecksumCalculator;7import org.flywaydb.core.internal.resource.filesystem.FileSystemResource;8import org.flywaydb.core.internal.scanner.classpath.FileSystemClassPathLocationScanner;9import org.springframework.beans.factory.annotation.Value;10import org.springframework.context.annotation.Configuration;11import javax.annotation.PostConstruct;12import java.io.File;13import java.io.IOException;14import java.net.URL;15import java.nio.charset.Charset;16import java.sql.*;17import java.util.*;18import java.util.regex.Matcher;19import java.util.regex.Pattern;20@Log4j221@Configuration22public class DatabaseMigrationConfig {23 private static final String SCHEMA_PATH_RESOURCE = "classpath:db/bootstrap";24 private static final String MIGRATION_PATH = "db/migration/";25 private static final String VERSION_SEPARATOR = "__";26 private static final String VERSION_CHAR = "V";27 private static final String INSERT_HISTORY = "INSERT INTO `flyway_schema_history` (`installed_rank`, `version`, `description`, `type`, `script`, `checksum`, `installed_by`, `execution_time`, `success`)\n" +28 "VALUES (?, ?, ?, 'SQL', ?, ?, ?,20, 1)";29 private static final String DELETE_HISTORY = "DELETE FROM flyway_schema_history";30 private static final String CREATE_DATABASE = "CREATE DATABASE IF NOT EXISTS `%s` /*!40100 DEFAULT CHARACTER SET utf8mb4 */";31 @Value("${spring.datasource.url}")32 private String dataSourceUrl;33 @Value("${spring.datasource.username}")34 private String dataSourceUser;35 @Value("${spring.datasource.password}")36 private String dataSourcePassword;37 private Connection connection;38 @PostConstruct39 public void postConstruct() throws Exception {40 log.info("Setting up database");41 setUpDatabase();42 }43 private void setUpDatabase() throws Exception {44 Flyway flyway;45 try {46 connection = getConnection();47 if (isNewDatabase()) {48 log.debug("Initializing bootstrap data");49 executeBootStrap();50 addMigrationEntries();51 } else {52 log.info("Running any migrations if present on existing database");53 flyway = Flyway.configure().dataSource(dataSourceUrl, dataSourceUser, dataSourcePassword).placeholderReplacement(false).load();54 flyway.migrate();55 }56 } catch (Exception e) {57 log.error(e.getMessage(), e);58 throw e;59 } finally {60 if (connection != null)61 connection.close();62 }63 }64 /*65 If db is up to date, we just add migration entries if any without executing SQLs66 */67 private void addMigrationEntries() throws SQLException, IOException {68 log.debug("....Adding migration entries....");69 SortedMap<Integer, File> versionAndFileMap = getVersionAndFileMap();70 int installedRank = 1;71 for (Integer version : versionAndFileMap.keySet()) {72 File migrationFile = versionAndFileMap.get(version);73 int checksum = getChecksum(migrationFile.getAbsolutePath());74 addMigrationEntryToHistory(installedRank, version, migrationFile, checksum);75 installedRank++;76 }77 }78 private void executeBootStrap() throws SQLException {79 log.info("Executing bootstrap");80 Flyway flyway = Flyway.configure().dataSource(dataSourceUrl, dataSourceUser, dataSourcePassword)81 .locations(SCHEMA_PATH_RESOURCE)82 .placeholderReplacement(false)83 .load();84 flyway.migrate();85 clearHistoryForSchema();86 }87 private void clearHistoryForSchema() throws SQLException {88 Statement stmt = connection.createStatement();89 stmt.executeUpdate(DELETE_HISTORY);90 }91 private boolean isNewDatabase() throws SQLException {92 ResultSet resultSet = connection.createStatement().executeQuery("SHOW TABLES");93 return !(resultSet.next());94 }95 private int getChecksum(String absoluteFilePath) {96 FileSystemResource r = new FileSystemResource(null, absoluteFilePath, Charset.forName("UTF-8"), false);97 return ChecksumCalculator.calculate(r);98 }99 private SortedMap<Integer, File> getVersionAndFileMap() throws IOException {100 SortedMap<Integer, File> sortedByVersion = new TreeMap<>();//We need to sort by version for DB entry101 Set<File> migrationSQLFiles;102 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();103 URL url = classLoader.getResource(MIGRATION_PATH);104 if(url == null){105 return sortedByVersion;106 }107 String urlProtocol = url.getProtocol();108 log.debug(String.format("URL Path:%s, URL protocol:%s", url.getPath(),urlProtocol));109 if (urlProtocol.equalsIgnoreCase("jar")) {110 log.debug("URL type is jar, extracting migration files from jar");111 JarExtractorUtil jarExtractorUtil = new JarExtractorUtil();112 migrationSQLFiles = jarExtractorUtil.extractJarAndCreateTempFiles(url,MIGRATION_PATH,".sql");113 } else {114 log.debug("Extracting migration files from file system");115 FileSystemClassPathLocationScanner classPathLocationScanner = new FileSystemClassPathLocationScanner();116 Set<String> classpathResourceFilePaths = classPathLocationScanner.findResourceNames(MIGRATION_PATH, url);117 log.info("Migration files from file system::"+classpathResourceFilePaths);118 migrationSQLFiles = getFilesFromResourcePaths(classpathResourceFilePaths);119 }120 for (File migrationFile : migrationSQLFiles) {121 sortedByVersion.put(getVersionFromFilePath(migrationFile), migrationFile);122 }123 return sortedByVersion;124 }125 private Set<File> getFilesFromResourcePaths(Set<String> classpathResourceFilePaths) {...
Source:JarExtractorUtil.java
...19import java.util.jar.JarEntry;20import java.util.jar.JarFile;21import java.util.stream.Collectors;22@Log4j223public class JarExtractorUtil {24 public Set<File> extractJarAndCreateTempFiles(URL jarURL, String subFolderPath, String fileExtension) throws IOException {25 log.debug(String.format("Finding files in jar subdirectory:%s with extension %s",subFolderPath,fileExtension));26 Set<File> classpathResourceTempFiles = new TreeSet<>();27 URLConnection con = jarURL.openConnection();28 JarURLConnection jarCon = (JarURLConnection) con;29 JarFile jarFile = jarCon.getJarFile();30 Enumeration jarEntries = jarFile.entries();31 ClassLoader cl = this.getClass().getClassLoader();32 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);33 while (jarEntries.hasMoreElements()) {34 JarEntry jarEntry = (JarEntry) jarEntries.nextElement();35 String jarEntryName = jarEntry.getName();36 if (jarEntryName.startsWith(subFolderPath) && !jarEntry.isDirectory() && jarEntryName.toLowerCase().endsWith(fileExtension)) {37 log.debug("Found a matching extension file::"+jarEntryName);...
JarExtractorUtil
Using AI Code Generation
1package com.testsigma.util;2import java.io.File;3import java.io.FileInputStream;4import java.io.FileOutputStream;5import java.util.Enumeration;6import java.util.zip.ZipEntry;7import java.util.zip.ZipFile;8import java.util.zip.ZipInputStream;9public class JarExtractorUtil {10 public static void extractJar(String jarPath) {11 try {12 File file = new File(jarPath);13 ZipFile zip = new ZipFile(file);14 String newPath = jarPath.substring(0, jarPath.length() - 4);15 new File(newPath).mkdir();16 for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {17 ZipEntry entry = (ZipEntry) entries.nextElement();18 if (entry.isDirectory()) {19 new File(newPath + File.separator + entry.getName()).mkdir();20 } else {21 copyInputStream(zip.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(newPath + File.separator + entry.getName())));22 }23 }24 zip.close();25 } catch (Exception e) {26 e.printStackTrace();27 }28 }29 private static void copyInputStream(InputStream in, OutputStream out) throws IOException {30 byte[] buffer = new byte[1024];31 int len = in.read(buffer);32 while (len >= 0) {33 out.write(buffer, 0, len);34 len = in.read(buffer);35 }36 in.close();37 out.close();38 }39}40package com.testsigma.util;41import java.io.BufferedInputStream;42import java.io.File;43import java.io.FileInputStream;44import java.io.FileOutputStream;45import java.io.IOException;46import java.util.Enumeration;47import java.util.zip.ZipEntry;48import java.util.zip.ZipFile;49import java.util.zip.ZipInputStream;50public class JarExtractorUtil {51 public static void extractJar(String jarPath) {52 try {53 File file = new File(jarPath);54 ZipFile zip = new ZipFile(file);55 String newPath = jarPath.substring(0, jarPath.length() - 4);56 new File(newPath).mkdir();57 for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {58 ZipEntry entry = (ZipEntry) entries.nextElement();59 if (entry.isDirectory()) {60 new File(newPath + File.separator + entry.getName()).mkdir();61 } else {62 copyInputStream(zip.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(newPath + File.separator + entry.getName())));
JarExtractorUtil
Using AI Code Generation
1import com.testsigma.util.JarExtractorUtil;2import java.io.File;3import java.io.IOException;4import java.io.InputStream;5import java.util.jar.JarFile;6public class 2 {7 public static void main(String[] args) throws IOException {8 String jarFileName = "D:/test.jar";9 String destinationDirectory = "D:/test";10 JarExtractorUtil.extractJar(new JarFile(jarFileName), new File(destinationDirectory));11 }12}13package com.testsigma.util;14import java.io.File;15import java.io.IOException;16import java.io.InputStream;17import java.util.Enumeration;18import java.util.jar.JarEntry;19import java.util.jar.JarFile;20import org.apache.commons.io.FileUtils;21public class JarExtractorUtil {22 public static void extractJar(JarFile jarFile, File destinationDirectory) throws IOException {23 for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {24 JarEntry entry = entries.nextElement();25 File file = new File(destinationDirectory, entry.getName());26 if (entry.isDirectory()) {27 file.mkdirs();28 } else {29 InputStream inputStream = jarFile.getInputStream(entry);30 FileUtils.copyInputStreamToFile(inputStream, file);31 }32 }33 }34}35JarExtractorUtil.extractJar(new JarFile(jarFileName), new File(destinationDirectory));
JarExtractorUtil
Using AI Code Generation
1import com.testsigma.util.JarExtractorUtil;2import java.io.File;3import java.io.IOException;4public class JarExtractorUtilDemo {5public static void main(String[] args) {6try {7JarExtractorUtil.extractJar(new File("test.jar"), new File("test"));8} catch (IOException e) {9e.printStackTrace();10}11}12}13import com.testsigma.util.JarExtractorUtil;14import java.io.File;15import java.io.IOException;16public class JarExtractorUtilDemo {17public static void main(String[] args) {18try {19JarExtractorUtil.extractJar(new File("test.jar"), new File("test"));20} catch (IOException e) {21e.printStackTrace();22}23}24}25import com.testsigma.util.JarExtractorUtil;26import java.io.File;27import java.io.IOException;28public class JarExtractorUtilDemo {29public static void main(String[] args) {30try {31JarExtractorUtil.extractJar(new File("test.jar"), new File("test"));32} catch (IOException e) {33e.printStackTrace();34}35}36}37import com.testsigma.util.JarExtractorUtil;38import java.io.File;39import java.io.IOException;40public class JarExtractorUtilDemo {41public static void main(String[] args) {42try {43JarExtractorUtil.extractJar(new File("test.jar"), new File("test"));44} catch (IOException e) {45e.printStackTrace();46}47}48}49import com.testsigma.util.JarExtractorUtil;50import java.io.File;51import java.io.IOException;52public class JarExtractorUtilDemo {53public static void main(String[] args) {54try {55JarExtractorUtil.extractJar(new File("test.jar"), new File("test"));56} catch (IOException e) {57e.printStackTrace();58}59}60}61import com.testsigma.util.JarExtractorUtil;62import java.io.File;63import java.io.IOException;64public class JarExtractorUtilDemo {65public static void main(String[] args) {66try {67JarExtractorUtil.extractJar(new File("test.jar"), new File("test
JarExtractorUtil
Using AI Code Generation
1import com.testsigma.util.JarExtractorUtil;2import java.io.File;3public class TestJarExtractorUtil {4public static void main(String[] args) {5File jarFile = new File("C:/test/Test.jar");6File destDir = new File("C:/test/extracted");7JarExtractorUtil.extractJar(jarFile, destDir);8}9}10C:\test>java -cp Test.jar;. 2
JarExtractorUtil
Using AI Code Generation
1import com.testsigma.util.JarExtractorUtil;2public class 2 {3public static void main(String[] args) {4JarExtractorUtil.extractJar("C:/test.jar", "C:/test");5}6}
JarExtractorUtil
Using AI Code Generation
1import com.testsigma.util.JarExtractorUtil;2public class TestJarExtractorUtil {3 public static void main(String[] args) {4 JarExtractorUtil.extractJar("C:\\Users\\TestSigma\\Desktop\\TestSigma\\testsigma.jar", "C:\\Users\\TestSigma\\Desktop\\TestSigma\\");5 }6}7import com.testsigma.util.JarExtractorUtil;8public class TestJarExtractorUtil {9 public static void main(String[] args) {10 JarExtractorUtil.extractJar("C:\\Users\\TestSigma\\Desktop\\TestSigma\\testsigma.jar", "C:\\Users\\TestSigma\\Desktop\\TestSigma\\");11 }12}13import com.testsigma.util.JarExtractorUtil;14public class TestJarExtractorUtil {15 public static void main(String[] args) {16 JarExtractorUtil.extractJar("C:\\Users\\TestSigma\\Desktop\\TestSigma\\testsigma.jar", "C:\\Users\\TestSigma\\Desktop\\TestSigma\\");17 }18}19import com.testsigma.util.JarExtractorUtil;20public class TestJarExtractorUtil {21 public static void main(String[] args) {22 JarExtractorUtil.extractJar("C:\\Users\\TestSigma\\Desktop\\TestSigma\\testsigma.jar", "C:\\Users\\TestSigma\\Desktop\\TestSigma\\");23 }24}25import com.testsigma.util.JarExtractorUtil;26public class TestJarExtractorUtil {27 public static void main(String[] args) {28 JarExtractorUtil.extractJar("C:\\Users\\TestSigma\\Desktop\\TestSigma\\testsigma.jar", "C:\\Users\\TestSigma\\Desktop\\TestSigma\\");29 }30}31import com.testsigma.util.JarExtractorUtil;32public class TestJarExtractorUtil {33 public static void main(String[] args) {34 JarExtractorUtil.extractJar("C:\\Users\\TestSigma\\Desktop\\TestSigma\\testsigma.jar", "C:\\
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!!