From 9744b236477bd19cd39e408b18f9935e91de8ee7 Mon Sep 17 00:00:00 2001 From: pderakhshanfar Date: Fri, 15 Jun 2018 02:31:53 +0200 Subject: [PATCH] Handle classloaders exceptions and load only java and target project classes from model. --- .../evosuite/seeding/ObjectPoolManager.java | 303 ++++++++++-------- 1 file changed, 164 insertions(+), 139 deletions(-) diff --git a/client/src/main/java/org/evosuite/seeding/ObjectPoolManager.java b/client/src/main/java/org/evosuite/seeding/ObjectPoolManager.java index 471e38d..437fce3 100755 --- a/client/src/main/java/org/evosuite/seeding/ObjectPoolManager.java +++ b/client/src/main/java/org/evosuite/seeding/ObjectPoolManager.java @@ -116,167 +116,192 @@ public void initialisePool() { File folder = new File(Properties.MODEL_PATH); File[] listOfFiles = folder.listFiles(); for (File file : listOfFiles) { - if (file.isFile() && !file.getName().startsWith(".") && file.getName().endsWith(".xml")) { - LoggingUtils.getEvoLogger().info("working on callSequences of " + file.getName()); - try{ - UsageModel um = Xml.loadUsageModel(Paths.get(folder.getAbsolutePath(),file.getName()).toString()); -// TestSet ts = Random.randomSelection(um,Properties.NUMBER_OF_MODEL_TESTS); // For random selection - TestSet ts = Dissimilar.from(um).withGlobalMaxDistance(Dissimilar.jaccard()).during(100).generate(Properties.NUMBER_OF_MODEL_TESTS); - for (be.vibes.ts.TestCase abstractTestCase : ts) { - LoggingUtils.getEvoLogger().info("Injecting the abstract test case transitions to EvoSuite TestCase object"); - TestCase newTestCase = new DefaultTestCase(); - GenericClass genericClass = null; - boolean addConstructor = true; + if (file.isFile() && !file.getName().startsWith(".") && file.getName().endsWith(".xml") ) { + String xmlClassName = file.getName().substring(0, file.getName().length() - 4); + if (xmlClassName.indexOf('.')== -1 || xmlClassName.contains("java") || xmlClassName.contains(Properties.PROJECT_KEYWORD)){ + LoggingUtils.getEvoLogger().info("working on callSequences of " + file.getName()); + try { + UsageModel um = Xml.loadUsageModel(Paths.get(folder.getAbsolutePath(), file.getName()).toString()); + // TestSet ts = Random.randomSelection(um,Properties.NUMBER_OF_MODEL_TESTS); // For random selection + TestSet ts = Dissimilar.from(um).withGlobalMaxDistance(Dissimilar.jaccard()).during(100).generate(Properties.NUMBER_OF_MODEL_TESTS); + for (be.vibes.ts.TestCase abstractTestCase : ts) { +// LoggingUtils.getEvoLogger().info("Injecting the abstract test case transitions to EvoSuite TestCase object"); + TestCase newTestCase = new DefaultTestCase(); + GenericClass genericClass = null; + boolean addConstructor = true; - for (Transition transition : abstractTestCase) { - Action sequence = transition.getAction(); - if (sequence.getName().indexOf(".") != -1) { - // Class name: - String className = sequence.getName().substring(0, sequence.getName().indexOf("(")); - className = className.substring(0, className.lastIndexOf('.')); - // Method name: - String methodName = StringUtils.substringAfterLast(sequence.getName().substring(0, sequence.getName().indexOf("(")), "."); - String paramString = sequence.getName().substring(sequence.getName().indexOf("(") + 1); + for (Transition transition : abstractTestCase) { + Action sequence = transition.getAction(); + if (sequence.getName().indexOf(".") != -1) { + // Class name: + String className = sequence.getName().substring(0, sequence.getName().indexOf("(")); + className = className.substring(0, className.lastIndexOf('.')); + // Method name: + String methodName = StringUtils.substringAfterLast(sequence.getName().substring(0, sequence.getName().indexOf("(")), "."); + String paramString = sequence.getName().substring(sequence.getName().indexOf("(") + 1); - if (methodName.equals("")){ - addConstructor = false; - break; - } - - Method target = null; - Class sequenceClass = Class.forName(className, true, TestGenerationContext.getInstance().getClassLoaderForSUT()); - Set methods = TestClusterUtils.getMethods(sequenceClass); - for (Method m : methods) { - if (m.getName().equals(methodName)) { - target = m; - break; - } else { - target = null; - } - } - - if (target != null) { - GenericMethod genericMethod = new GenericMethod(target, sequenceClass); - if (!genericMethod.isStatic()){ - break; - } - } - } - - } - - if (addConstructor){ - Transition transition = abstractTestCase.getFirst(); - Action sequence = transition.getAction(); - String className = sequence.getName().substring(0, sequence.getName().indexOf("(")); - className = className.substring(0, className.lastIndexOf('.')); - Class sequenceClass = Class.forName(className, true, TestGenerationContext.getInstance().getClassLoaderForSUT()); - Set> constructors = TestClusterUtils.getConstructors(sequenceClass); - int i = 0; - int chosenConstructorIndex = new Random().nextInt(constructors.size()); - Constructor chosenConstructor = null; - for (Constructor c: constructors){ - if (i==chosenConstructorIndex){ - chosenConstructor = c; - break; - } - i++; - } - GenericConstructor genericConstructor = new GenericConstructor(chosenConstructor, sequenceClass); - try { - TestFactory.getInstance().addConstructor(newTestCase, genericConstructor, newTestCase.size(), 0); - LoggingUtils.getEvoLogger().info("constructor {} is added", genericConstructor.getName()); - } catch (Exception e) { - LoggingUtils.getEvoLogger().info("Error in addidng " + genericConstructor.getName() + " " + e.getMessage()); - } - - - } - - for (Transition transition : abstractTestCase) { - Action sequence = transition.getAction(); - if (sequence.getName().indexOf(".") != -1) { - // Class name: - String className = sequence.getName().substring(0, sequence.getName().indexOf("(")); - className = className.substring(0, className.lastIndexOf('.')); - // Method name: - String methodName = StringUtils.substringAfterLast(sequence.getName().substring(0, sequence.getName().indexOf("(")), "."); - String paramString = sequence.getName().substring(sequence.getName().indexOf("(") + 1); - // Params: - paramString = paramString.substring(0, paramString.indexOf(")")); - String[] paramArr = paramString.split(","); - try { - //Getting the Class - Class sequenceClass = Class.forName(className, true, TestGenerationContext.getInstance().getClassLoaderForSUT()); - genericClass = new GenericClass(sequenceClass); - //Getting methods - Set methods = TestClusterUtils.getMethods(sequenceClass); - //Getting Constructors - Set> constructors = TestClusterUtils.getConstructors(sequenceClass); - - // find the method that we want - Method target = null; - for (Method m : methods) { - if (m.getName().equals(methodName)) { - target = m; + if (methodName.equals("")) { + addConstructor = false; break; - } else { - target = null; } - } - // Find the constructor that we want - Constructor targetC = null; - for (Constructor c : constructors) { - boolean same = true; - int counter = 0; + Method target = null; + Class sequenceClass = null; + try { + sequenceClass = Class.forName(className, true, TestGenerationContext.getInstance().getClassLoaderForSUT()); + } catch (ClassNotFoundException | ExceptionInInitializerError | NoClassDefFoundError e) { + LoggingUtils.getEvoLogger().info("coulds not load " + className); + } + if (sequenceClass != null) { + Set methods = TestClusterUtils.getMethods(sequenceClass); + for (Method m : methods) { + if (m.getName().equals(methodName)) { + target = m; + break; + } else { + target = null; + } + } - for (Class cl : c.getParameterTypes()) { - if (paramArr.length > counter && !cl.getName().equals(paramArr[counter])) { - same = false; + if (target != null) { + GenericMethod genericMethod = new GenericMethod(target, sequenceClass); + if (!genericMethod.isStatic()) { + break; + } } - counter++; - } - if (same) { - targetC = c; - break; } } + } - if (target != null) { - GenericMethod genericMethod = new GenericMethod(target, sequenceClass); - try { - TestFactory.getInstance().addMethod(newTestCase, genericMethod, newTestCase.size(), 0); - LoggingUtils.getEvoLogger().info("method call {} is added", genericMethod.getName()); - } catch (Exception e) { - LoggingUtils.getEvoLogger().info("Error in addidng " + genericMethod.getName() + " " + e.getMessage()); + if (addConstructor) { + Transition transition = abstractTestCase.getFirst(); + Action sequence = transition.getAction(); + String className = sequence.getName().substring(0, sequence.getName().indexOf("(")); + className = className.substring(0, className.lastIndexOf('.')); + Class sequenceClass = null; + try { + sequenceClass = Class.forName(className, true, TestGenerationContext.getInstance().getClassLoaderForSUT()); + } catch (ClassNotFoundException | ExceptionInInitializerError | NoClassDefFoundError e) { + LoggingUtils.getEvoLogger().info("coulds not load " + className); + } + if (sequenceClass != null) { + Set> constructors = TestClusterUtils.getConstructors(sequenceClass); + int i = 0; + int chosenConstructorIndex = new Random().nextInt(constructors.size()); + Constructor chosenConstructor = null; + for (Constructor c : constructors) { + if (i == chosenConstructorIndex) { + chosenConstructor = c; + break; + } + i++; } - } else if (targetC != null) { - GenericConstructor genericConstructor = new GenericConstructor(targetC, sequenceClass); + GenericConstructor genericConstructor = new GenericConstructor(chosenConstructor, sequenceClass); try { TestFactory.getInstance().addConstructor(newTestCase, genericConstructor, newTestCase.size(), 0); LoggingUtils.getEvoLogger().info("constructor {} is added", genericConstructor.getName()); } catch (Exception e) { LoggingUtils.getEvoLogger().info("Error in addidng " + genericConstructor.getName() + " " + e.getMessage()); } - - } else { - LoggingUtils.getEvoLogger().error("Fail to add the call to add!"); } + } + + for (Transition transition : abstractTestCase) { + Action sequence = transition.getAction(); + if (sequence.getName().indexOf(".") != -1) { + // Class name: + String className = sequence.getName().substring(0, sequence.getName().indexOf("(")); + className = className.substring(0, className.lastIndexOf('.')); + // Method name: + String methodName = StringUtils.substringAfterLast(sequence.getName().substring(0, sequence.getName().indexOf("(")), "."); + String paramString = sequence.getName().substring(sequence.getName().indexOf("(") + 1); + // Params: + paramString = paramString.substring(0, paramString.indexOf(")")); + String[] paramArr = paramString.split(","); + // try { + //Getting the Class + Class sequenceClass = null; + try { + sequenceClass = Class.forName(className, true, TestGenerationContext.getInstance().getClassLoaderForSUT()); + } catch (ClassNotFoundException | ExceptionInInitializerError | NoClassDefFoundError e) { + LoggingUtils.getEvoLogger().info("coulds not load " + className); + } + if (sequenceClass != null) { + genericClass = new GenericClass(sequenceClass); + //Getting methods + Set methods = TestClusterUtils.getMethods(sequenceClass); + //Getting Constructors + Set> constructors = TestClusterUtils.getConstructors(sequenceClass); + + // find the method that we want + Method target = null; + for (Method m : methods) { + if (m.getName().equals(methodName)) { + target = m; + break; + } else { + target = null; + } + } + + // Find the constructor that we want + Constructor targetC = null; + for (Constructor c : constructors) { + boolean same = true; + int counter = 0; + + for (Class cl : c.getParameterTypes()) { + if (paramArr.length > counter && !cl.getName().equals(paramArr[counter])) { + same = false; + } + counter++; + } + if (same) { + targetC = c; + break; + } + } + + + if (target != null) { + GenericMethod genericMethod = new GenericMethod(target, sequenceClass); + try { + TestFactory.getInstance().addMethod(newTestCase, genericMethod, newTestCase.size(), 0); + LoggingUtils.getEvoLogger().info("method call {} is added", genericMethod.getName()); + } catch (Exception e) { + LoggingUtils.getEvoLogger().info("Error in addidng " + genericMethod.getName() + " " + e.getMessage()); + } + } else if (targetC != null) { + GenericConstructor genericConstructor = new GenericConstructor(targetC, sequenceClass); + try { + TestFactory.getInstance().addConstructor(newTestCase, genericConstructor, newTestCase.size(), 0); + LoggingUtils.getEvoLogger().info("constructor {} is added", genericConstructor.getName()); + } catch (Exception e) { + LoggingUtils.getEvoLogger().info("Error in addidng " + genericConstructor.getName() + " " + e.getMessage()); + } + + } else { + LoggingUtils.getEvoLogger().error("Fail to add the call to add!"); + } + } + + // } catch (ClassNotFoundException e) { + // LoggingUtils.getEvoLogger().error(className + " did not found!"); + // } + + } - } catch (ClassNotFoundException e) { - LoggingUtils.getEvoLogger().error(className + " did not found!"); } + // Add test case to pool + if (genericClass != null) + this.addSequence(genericClass, newTestCase); } + } catch (Exception e) { + LoggingUtils.getEvoLogger().error("Could not load model " + file.getName()); } - // Add test case to pool - this.addSequence(genericClass, newTestCase); - } - }catch(Exception e){ - LoggingUtils.getEvoLogger().error("Could not load model "+file.getName()); - } + } } } @@ -286,8 +311,8 @@ public void initialisePool() { LoggingUtils.getEvoLogger().info("~~~~~~~~~~~~~~~"); LoggingUtils.getEvoLogger().info("Test for class "+clazz.getClassName()); Set tests = entry.getValue(); - for(TestCase test: tests) - LoggingUtils.getEvoLogger().info(test.toCode());LoggingUtils.getEvoLogger().info("------"); + for(TestCase test: tests){ + LoggingUtils.getEvoLogger().info(test.toCode());LoggingUtils.getEvoLogger().info("------");} LoggingUtils.getEvoLogger().info("~~~~~~~~~~~~~~~"); } }