private void determineInspectors(Class<?> clazz) { if (!TestUsageChecker.canUse(clazz)) { inspectors.put(clazz, Collections.emptyList()); } if (!TestUsageChecker.canUse(clazz)) return; List<Inspector> inspectorList = new ArrayList<Inspector>(); for (Method method : clazz.getMethods()) { if (isInspectorMethod(method)) { // FIXXME logger.debug("Inspector for class " + clazz.getSimpleName() + ": " + method.getName() + " defined in " + method.getDeclaringClass().getCanonicalName()); inspectorList.add(new Inspector(clazz, method)); } else { logger.debug("Not an inspector: "+method.getName()); } } inspectors.put(clazz, inspectorList); }
@Override public void changeClassLoader(ClassLoader loader) { super.changeClassLoader(loader); inspector.changeClassLoader(loader); } }
/** {@inheritDoc} */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; InspectorAssertion other = (InspectorAssertion) obj; if (inspector == null) { if (other.inspector != null) return false; } else if (!inspector.equals(other.inspector)) return false; return true; }
/** * <p>addValue</p> * * @param inspector a {@link org.evosuite.assertion.Inspector} object. * @param value a {@link java.lang.Object} object. */ public void addValue(Inspector inspector, Object value) { inspectorMap.put(inspector, value); methodInspectorMap.put(inspector.getClassName() + " " + inspector.getMethodCall(), inspector); }
Collection<Inspector> inspectors = InspectorManager.getInstance().getInspectors(argValue.getClass()); for (Inspector inspector : inspectors) { String insp = inspector.getMethodCall() + Type.getMethodDescriptor(inspector.getMethod()); try { Object val = inspector.getValue(argValue); if (val instanceof Boolean) { String valDesc = ((boolean) val) ? BOOL_TRUE : BOOL_FALSE;
Class<?> returnClazz = method.getReturnType(); for(Inspector inspector : InspectorManager.getInstance().getInspectors(returnClazz)) { String insp = inspector.getMethodCall() + Type.getMethodDescriptor(inspector.getMethod()); Type t = Type.getReturnType(inspector.getMethod()); if (t.getSort() == Type.BOOLEAN) { goals.add(createGoal(className, methodName, returnType, REF_NONNULL + ":" + returnType.getClassName() + ":" + insp + ":" + BOOL_TRUE));
Object value = assertion.getValue(); Inspector inspector = assertion.getInspector(); Class<?> generatedType = inspector.getReturnType(); + inspector.getMethodCall() + "());"; } else if (value.getClass().equals(Long.class)) { testCode += "assertEquals(" + NumberFormatter.getNumberString(value, this) + ", "; if(ClassUtils.isPrimitiveWrapper(generatedType)) testCode += "(long)"; testCode += getVariableName(source) + "." + inspector.getMethodCall() + "());"; } else if (value.getClass().equals(Short.class)) { testCode += "assertEquals(" + NumberFormatter.getNumberString(value, this) + ", "; if(ClassUtils.isPrimitiveWrapper(generatedType)) testCode += "(short)"; testCode += getVariableName(source) + "." + inspector.getMethodCall() + "());"; } else if (value.getClass().equals(Integer.class)) { testCode += "assertEquals(" + NumberFormatter.getNumberString(value, this) + ", "; if(ClassUtils.isPrimitiveWrapper(generatedType)) testCode += "(int)"; testCode += getVariableName(source) + "." + inspector.getMethodCall() + "());"; } else if (value.getClass().equals(Byte.class)) { testCode += "assertEquals(" + NumberFormatter.getNumberString(value, this) + ", "; if(ClassUtils.isPrimitiveWrapper(generatedType)) testCode += "(byte)"; testCode += getVariableName(source) + "." + inspector.getMethodCall() + "());"; } else if (value.getClass().equals(Float.class)) { testCode += "assertEquals(" + NumberFormatter.getNumberString(value, this) + ", "; testCode += getVariableName(source) + "." + inspector.getMethodCall() + "(), "+NumberFormatter.getNumberString(Properties.FLOAT_PRECISION, this)+");";
private boolean assertionsNeedDownCast(Statement s, VariableReference var, Class<?> abstractClass) { for(Assertion assertion : s.getAssertions()) { if(assertion instanceof InspectorAssertion && assertion.getSource().equals(var)) { InspectorAssertion inspectorAssertion = (InspectorAssertion)assertion; Method inspectorMethod = inspectorAssertion.getInspector().getMethod(); if(!ClassUtils.hasMethod(abstractClass, inspectorMethod.getName(), inspectorMethod.getParameterTypes())) { return true; } } else if(assertion instanceof PrimitiveFieldAssertion && assertion.getSource().equals(var)) { PrimitiveFieldAssertion fieldAssertion = (PrimitiveFieldAssertion)assertion; if(!fieldAssertion.getField().getDeclaringClass().isAssignableFrom(abstractClass)) { return true; } } } return false; }
/** {@inheritDoc} */ @Override public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + ((inspector == null) ? 0 : inspector.hashCode()); return result; }
/** {@inheritDoc} */ @Override public boolean evaluate(Scope scope) { try { if (source.getObject(scope) == null) return true; // TODO - true or false? else { try { Object val = inspector.getValue(source.getObject(scope)); if (val == null) return value == null; else return val.equals(value); } catch (Exception e) { logger.error("* Exception during call to inspector",e); return true; } } } catch (CodeUnderTestException e) { throw new UnsupportedOperationException(); } }
/** {@inheritDoc} */ @Override public String getCode() { if (value == null) { return "assertNull(" + source.getName() + "." + inspector.getMethodCall() + "());"; } else if (value.getClass().equals(Long.class)) { return "assertEquals(" + NumberFormatter.getNumberString(value) + ", " + source.getName() + "." + inspector.getMethodCall() + "());"; } else if (value.getClass().equals(Float.class)) { return "assertEquals(" + NumberFormatter.getNumberString(value) + ", " + source.getName() + "." + inspector.getMethodCall() + "(), 0.01F);"; } else if (value.getClass().equals(Double.class)) { return "assertEquals(" + NumberFormatter.getNumberString(value) + ", " + source.getName() + "." + inspector.getMethodCall() + "(), 0.01D);"; } else if (value.getClass().equals(Character.class)) { return "assertEquals(" + NumberFormatter.getNumberString(value) + ", " + source.getName() + "." + inspector.getMethodCall() + "());"; } else if (value.getClass().equals(String.class)) { return "assertEquals(" + NumberFormatter.getNumberString(value) + ", " + source.getName() + "." + inspector.getMethodCall() + "());"; } else if (value.getClass().isEnum()) { return "assertEquals(" + NumberFormatter.getNumberString(value) + ", " + source.getName() + "." + inspector.getMethodCall() + "());"; } else return "assertEquals(" + value + ", " + source.getName() + "." + inspector.getMethodCall() + "());"; }
String insp = inspector.getMethodCall() + Type.getMethodDescriptor(inspector.getMethod()); try { Object val = inspector.getValue(returnValue); if (val instanceof Boolean) { String valDesc = ((boolean)val) ? BOOL_TRUE : BOOL_FALSE;
/** * {@inheritDoc} */ @Override public boolean isDetectedBy(Assertion assertion) { if (assertion instanceof InspectorAssertion) { InspectorAssertion ass = (InspectorAssertion) assertion; if (ass.source.same(var)) { if (Properties.isRegression()) { // Use the internal map to locate the inspector assertion String key = ass.getInspector().getClassName() + " " + ass.getInspector().getMethodCall(); Inspector inspector = methodInspectorMap.get(key); if (inspector != null && inspectorMap.get(inspector) != null && ass.value != null) { return !inspectorMap.get(inspector).equals(ass.value); } } else { if (inspectorMap.containsKey(ass.inspector) && inspectorMap.get(ass.inspector) != null && ass.value != null) { return !inspectorMap.get(ass.inspector).equals(ass.value); } } } } return false; }
Class<?> paramClazz = argumentClasses[i]; for(Inspector inspector : InspectorManager.getInstance().getInspectors(paramClazz)) { String insp = inspector.getMethodCall() + Type.getMethodDescriptor(inspector.getMethod()); Type t = Type.getReturnType(inspector.getMethod()); if (t.getSort() == Type.BOOLEAN) { goals.add(createGoal(className, methodName, i, argType, REF_NONNULL + ":" + argType.getClassName() + ":" + insp + ":" + BOOL_TRUE));
InspectorAssertion ia = (InspectorAssertion) a; try { aType = ia.getInspector().getMethod().getName(); } catch (NullPointerException e) {
@Test public void testLoaderOfEnumsAreChanged() throws NoSuchMethodException, SecurityException { InspectorAssertion assertion = new InspectorAssertion(); assertion.inspector = new Inspector(ExampleEnum.class, ExampleEnum.class.getMethod("testMe", new Class<?>[] {})); assertion.value = ExampleEnum.VALUE1; Assert.assertEquals(ExampleEnum.VALUE1, assertion.value); ClassLoader loader = TestGenerationContext.getInstance().getClassLoaderForSUT(); assertion.changeClassLoader(loader); Assert.assertNotEquals(ExampleEnum.VALUE1, assertion.value); } }
if (i.getMethod().getDeclaringClass().equals(Object.class)) continue; return; Object value = i.getValue(target); logger.debug("Inspector " + i.getMethodCall() + " is: " + value); if (e instanceof TimeoutException) { logger.debug("Timeout during inspector call - deactivating inspector " + i.getMethodCall()); InspectorManager.getInstance().removeInspector(var.getVariableClass(), i);
if (assertion instanceof InspectorAssertion) { InspectorAssertion ia = (InspectorAssertion) assertion; if (ia.getInspector().getMethod().equals(methodStatement.getMethod().getMethod())) { statement.removeAssertion(assertion); return;
@Test public void testDownCastNecessaryForInspectorAssertion() throws NoSuchMethodException { TestCaseBuilder builder = new TestCaseBuilder(); VariableReference var = builder.appendConstructor(DowncastExample.class.getConstructor()); VariableReference num0 = builder.appendMethod(var, DowncastExample.class.getMethod("getAbstractFoo")); num0.setType(ConcreteSubclass.class); // This would be set during execution DefaultTestCase test = builder.getDefaultTestCase(); Inspector inspector = new Inspector(ConcreteSubclass.class, ConcreteSubclass.class.getMethod("getBar")); InspectorAssertion assertion = new InspectorAssertion(inspector, test.getStatement(num0.getStPosition()), num0, true); test.getStatement(num0.getStPosition()).addAssertion(assertion); test.removeDownCasts(); System.out.println(test); assertEquals(ConcreteSubclass.class, test.getStatement(1).getReturnClass()); }
if (((InspectorAssertion) ass).inspector.getMethod().equals(inspectorMethod)) { continue;