Best EvoMaster code snippet using org.evomaster.client.java.instrumentation.external.JarAgentLocator.searchInFolder
Source:JarAgentLocator.java
...28 if(jarFilePath==null){29 /*30 * this could happen in Eclipse or during test execution in Maven, and so search in compilation 'target' folder31 */32 jarFilePath = searchInFolder("target");33 }34 if(jarFilePath==null){35 jarFilePath = searchInFolder("lib");36 }37 if(jarFilePath==null){38 //TODO could check in ~/.m2, but issue in finding right version39 }40 return jarFilePath;41 }42 //----------------------------------------------------------------------------------43 private static boolean isAgentJar(String path) throws IllegalArgumentException{44 if(path.endsWith("classes")){45 /*46 we need to treat this specially:47 eg, Jenkins/Maven on Linux on a module with only tests ended up48 with not creating "target/classes" (it does on Mac though) but still putting49 it on the classpath50 */51 return false;52 }53 File file = new File(path);54 if(!file.exists()){55 throw new IllegalArgumentException("Non-existing file "+path);56 }57 String name = file.getName();58 if(name.toLowerCase().contains("evomaster") &&59 name.endsWith(".jar")){60 try (JarFile jar = new JarFile(file)){61 Manifest manifest = jar.getManifest();62 if(manifest == null){63 return false;64 }65 Attributes attributes = manifest.getMainAttributes();66 String premain = attributes.getValue("Premain-Class");67 if(premain == null || premain.isEmpty()){68 return false;69 }70 String agentClass = attributes.getValue("Agent-Class");71 String agent = InstrumentingAgent.class.getName(); // this is hardcoded in the pom.xml file72 if(agentClass != null && agentClass.trim().equalsIgnoreCase(agent)){73 return true;74 }75 } catch (IOException e) {76 return false;77 }78 }79 return false;80 }81 private static String getFromProperty(){82 String path = System.getProperty("evomaster.instrumentation.jar.path");83 if(path == null){84 return null;85 }86 //if user specify a JAR path, but then it is invalid, then need to throw warning87 if(! isAgentJar(path)){88 throw new IllegalStateException("Specified instrumenting jar file is invalid");89 }90 return path;91 }92 private static String searchInAClassPath(String classPath){93 String[] tokens = classPath.split(File.pathSeparator);94 for(String entry : tokens){95 if(entry==null || entry.isEmpty()){96 continue;97 }98 if(isAgentJar(entry)){99 return entry;100 }101 }102 return null;103 }104 private static String searchInCurrentClassLoaderIfItProvidesClasspathAPI(){105 /*106 this could happen for AntClassLoader.107 Note: we cannot use instanceof here, as we do not want to add further third-party dependencies108 */109 ClassLoader loader = JarAgentLocator.class.getClassLoader();110 while(loader != null){111 try {112 Method m = loader.getClass().getMethod("getClasspath");113 String classPath = (String) m.invoke(loader);114 String jar = searchInAClassPath(classPath);115 if(jar != null){116 return jar;117 }118 } catch (Exception e) {119 //OK, this can happen, not really an error120 }121 loader = loader.getParent();122 }123 return null;124 }125 private static String searchInCurrentClassLoaderIfUrlOne() {126 Set<URI> uris = new HashSet<>();127 ClassLoader loader = JarAgentLocator.class.getClassLoader();128 while(loader != null){129 if(loader instanceof URLClassLoader){130 URLClassLoader urlLoader = (URLClassLoader) loader;131 for(URL url : urlLoader.getURLs()){132 try {133 URI uri = url.toURI();134 uris.add(uri);135 File file = new File(uri);136 String path = file.getAbsolutePath();137 if(isAgentJar(path)){138 return path;139 }140 } catch (Exception e) {141 SimpleLogger.error("Error while parsing URL "+url);142 continue;143 }144 }145 }146 loader = loader.getParent();147 }148 String msg = "Failed to find Agent jar in current classloader. URLs of classloader:";149 for(URI uri : uris){150 msg += "\n"+uri.toString();151 }152 SimpleLogger.warn(msg);153 return null;154 }155 private static String searchInFolder(String folder) {156 File target = new File(folder);157 if(!target.exists()){158 SimpleLogger.debug("No target folder "+target.getAbsolutePath());159 return null;160 }161 if(!target.isDirectory()){162 SimpleLogger.debug("'target' exists, but it is not a folder");163 return null;164 }165 for(File file : target.listFiles()){166 String path = file.getAbsolutePath();167 if(isAgentJar(path)){168 return path;169 }...
searchInFolder
Using AI Code Generation
1import org.evomaster.client.java.instrumentation.external.JarAgentLocator;2import org.evomaster.client.java.instrumentation.external.SearchAlgorithm;3import java.io.File;4import java.util.Arrays;5import java.util.List;6public class SearchInFolderExample {7 public static void main(String[] args) {8 List<File> jars = JarAgentLocator.searchInFolder(new File("C:\\Users\\user\\Documents\\agent\\evomaster-agent-1.0.2.jar"), SearchAlgorithm.BREADTH_FIRST);9 System.out.println(Arrays.toString(jars.toArray()));10 }11}12import org.evomaster.client.java.instrumentation.external.JarAgentLocator;13import org.evomaster.client.java.instrumentation.external.SearchAlgorithm;14import java.io.File;15import java.util.Arrays;16import java.util.List;17public class SearchInClasspathExample {18 public static void main(String[] args) {19 List<File> jars = JarAgentLocator.searchInClasspath(SearchAlgorithm.BREADTH_FIRST);20 System.out.println(Arrays.toString(jars.toArray()));21 }22}23import org.evomaster.client.java.instrumentation.external.JarAgentLocator;24import org.evomaster.client.java.instrumentation.external.SearchAlgorithm;25import java.io.File;26import java.util.Arrays;27import java.util.List;28public class SearchInJarExample {29 public static void main(String[] args) {30 List<File> jars = JarAgentLocator.searchInJar(new File("C:\\Users\\user\\Documents\\agent
searchInFolder
Using AI Code Generation
1import org.evomaster.client.java.instrumentation.external.JarAgentLocator;2import org.evomaster.client.java.instrumentation.external.SearchAlgorithm;3public class Test {4 public static void main(String[] args) {5 String path = JarAgentLocator.searchInFolder(SearchAlgorithm.LOCAL_FOLDER);6 System.out.println(path);7 }8}9import org.evomaster.client.java.instrumentation.external.JarAgentLocator;10import org.evomaster.client.java.instrumentation.external.SearchAlgorithm;11public class Test {12 public static void main(String[] args) {13 String path = JarAgentLocator.searchInClassPath(SearchAlgorithm.CLASSPATH);14 System.out.println(path);15 }16}17import org.evomaster.client.java.instrumentation.external.JarAgentLocator;18import org.evomaster.client.java.instrumentation.external.SearchAlgorithm;19public class Test {20 public static void main(String[] args) {21 String path = JarAgentLocator.searchInClassPath(SearchAlgorithm.CLASSPATH_AND_LOCAL_FOLDER);22 System.out.println(path);23 }24}
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!!