Search in sources :

Example 46 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestLocalSearchMIMEType method createTestCase2.

private DefaultTestCase createTestCase2() throws NoSuchFieldException, SecurityException, NoSuchMethodException, ClassNotFoundException {
    final Class<?> mimeTypeClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(MIMEType.class.getName());
    final Field rdfField = mimeTypeClass.getDeclaredField("RDF");
    final Method hashCode = mimeTypeClass.getMethod("hashCode");
    final Field slashField = mimeTypeClass.getDeclaredField("slash");
    final Method getTypeMethod = mimeTypeClass.getMethod("getType");
    final Method getSubTypeMethod = mimeTypeClass.getMethod("getSubType");
    final Method equalsMethod = mimeTypeClass.getMethod("equals", Object.class);
    final Field mimeTypeField = mimeTypeClass.getDeclaredField("mimeType");
    final Constructor<?> constructorStringBoolean = mimeTypeClass.getConstructor(String.class, boolean.class);
    final TestCaseBuilder builder = new TestCaseBuilder();
    VariableReference mIMEType0 = builder.appendStaticFieldStmt(rdfField);
    VariableReference string0 = builder.appendStringPrimitive("");
    VariableReference int0 = builder.appendMethod(mIMEType0, hashCode);
    VariableReference string1 = builder.appendStringPrimitive("");
    VariableReference int1 = builder.appendIntPrimitive(0);
    VariableReference string2 = builder.appendStringPrimitive("com.examples.with.different.packagename.concolic.MalformedMIMETypeException");
    builder.appendAssignment(mIMEType0, slashField, int1);
    VariableReference int2 = builder.appendFieldStmt(mIMEType0, slashField);
    VariableReference string3 = builder.appendMethod(mIMEType0, getTypeMethod);
    VariableReference string4 = builder.appendMethod(mIMEType0, getSubTypeMethod);
    VariableReference boolean0 = builder.appendMethod(mIMEType0, equalsMethod, string3);
    VariableReference string5 = builder.appendFieldStmt(mIMEType0, mimeTypeField);
    VariableReference string6 = builder.appendMethod(mIMEType0, getSubTypeMethod);
    VariableReference int3 = builder.appendFieldStmt(mIMEType0, slashField);
    VariableReference string7 = builder.appendStringPrimitive("xT7vo\"<|[E{4");
    builder.appendConstructor(constructorStringBoolean, string7, boolean0);
    builder.addException(new Error());
    System.out.println("Test Case #2=" + builder.toCode());
    return builder.getDefaultTestCase();
}
Also used : Field(java.lang.reflect.Field) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) MIMEType(com.examples.with.different.packagename.concolic.MIMEType) VariableReference(org.evosuite.testcase.variable.VariableReference) Method(java.lang.reflect.Method)

Example 47 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestLocalSearchMIMEType method createTestCase1.

private DefaultTestCase createTestCase1() throws NoSuchFieldException, SecurityException, NoSuchMethodException, ClassNotFoundException {
    final TestCaseBuilder builder = new TestCaseBuilder();
    final Class<?> mimeTypeClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(MIMEType.class.getName());
    final Class<?> objectClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Object.class.getName());
    final Constructor<?> constructorStringBoolean = mimeTypeClass.getConstructor(String.class, boolean.class);
    final Constructor<?> constructorString = mimeTypeClass.getConstructor(String.class);
    final Field mimeTypeField = mimeTypeClass.getDeclaredField("mimeType");
    final Field slashField = mimeTypeClass.getDeclaredField("slash");
    final Method getSubTypeMethod = mimeTypeClass.getMethod("getSubType");
    final Method equalsMethod = mimeTypeClass.getMethod("equals", Object.class);
    final Method getTypeMethod = mimeTypeClass.getMethod("getType");
    VariableReference string0 = builder.appendStringPrimitive("Y.8p>:/]WybaL");
    VariableReference boolean0 = builder.appendBooleanPrimitive(false);
    VariableReference mIMEType0 = builder.appendConstructor(constructorStringBoolean, string0, boolean0);
    VariableReference int0 = builder.appendIntPrimitive(-1);
    VariableReference int1 = builder.appendIntPrimitive(-1);
    VariableReference int2 = builder.appendIntPrimitive(1);
    builder.appendAssignment(mIMEType0, slashField, int2);
    builder.appendAssignment(mIMEType0, slashField, int0);
    builder.appendAssignment(mIMEType0, slashField, int1);
    VariableReference int3 = builder.appendIntPrimitive(0);
    VariableReference string1 = builder.appendStringPrimitive("Jm");
    builder.appendAssignment(mIMEType0, mimeTypeField, string1);
    VariableReference int4 = builder.appendIntPrimitive(2556);
    VariableReference int5 = builder.appendIntPrimitive(2556);
    builder.appendAssignment(mIMEType0, slashField, int4);
    builder.appendAssignment(mIMEType0, slashField, int3);
    VariableReference string2 = builder.appendMethod(mIMEType0, getSubTypeMethod);
    VariableReference object0 = builder.appendNull(objectClass);
    VariableReference boolean1 = builder.appendMethod(mIMEType0, equalsMethod, object0);
    VariableReference object1 = builder.appendNull(objectClass);
    VariableReference boolean2 = builder.appendMethod(mIMEType0, equalsMethod, object1);
    VariableReference string3 = builder.appendStringPrimitive("");
    VariableReference mIMEType1 = builder.appendConstructor(constructorString, string3);
    builder.addException(new Exception());
    builder.appendAssignment(mIMEType1, slashField, int5);
    VariableReference string4 = builder.appendStringPrimitive("DI'XL>AQzq1");
    builder.appendAssignment(mIMEType1, mimeTypeField, string4);
    VariableReference string5 = builder.appendFieldStmt(mIMEType1, mimeTypeField);
    VariableReference string6 = builder.appendStringPrimitive("bjvXpt%");
    VariableReference boolean3 = builder.appendBooleanPrimitive(true);
    VariableReference mIMEType2 = builder.appendConstructor(constructorStringBoolean, string6, boolean3);
    builder.appendAssignment(mIMEType2, slashField, mIMEType0, slashField);
    VariableReference string7 = builder.appendMethod(mIMEType0, getTypeMethod);
    VariableReference mIMEType3 = builder.appendConstructor(constructorStringBoolean, string5, boolean1);
    builder.appendAssignment(mIMEType1, mimeTypeField, string7);
    VariableReference string8 = builder.appendStringPrimitive("g");
    VariableReference mIMEType4 = builder.appendConstructor(constructorString, string8);
    VariableReference string9 = builder.appendMethod(mIMEType1, getTypeMethod);
    System.out.println("Test Case #1=" + builder.toCode());
    return builder.getDefaultTestCase();
}
Also used : Field(java.lang.reflect.Field) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) MIMEType(com.examples.with.different.packagename.concolic.MIMEType) VariableReference(org.evosuite.testcase.variable.VariableReference) Method(java.lang.reflect.Method)

Example 48 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestClassInitialization method buildLoadTargetClassTestCase.

private static DefaultTestCase buildLoadTargetClassTestCase(String className) throws EvosuiteError {
    DefaultTestCase test = new DefaultTestCase();
    StringPrimitiveStatement stmt0 = new StringPrimitiveStatement(test, className);
    VariableReference string0 = test.addStatement(stmt0);
    try {
        Method currentThreadMethod = Thread.class.getMethod("currentThread");
        Statement currentThreadStmt = new MethodStatement(test, new GenericMethod(currentThreadMethod, currentThreadMethod.getDeclaringClass()), null, Collections.emptyList());
        VariableReference currentThreadVar = test.addStatement(currentThreadStmt);
        Method getContextClassLoaderMethod = Thread.class.getMethod("getContextClassLoader");
        Statement getContextClassLoaderStmt = new MethodStatement(test, new GenericMethod(getContextClassLoaderMethod, getContextClassLoaderMethod.getDeclaringClass()), currentThreadVar, Collections.emptyList());
        VariableReference contextClassLoaderVar = test.addStatement(getContextClassLoaderStmt);
        Method loadClassMethod = ClassLoader.class.getMethod("loadClass", String.class);
        Statement loadClassStmt = new MethodStatement(test, new GenericMethod(loadClassMethod, loadClassMethod.getDeclaringClass()), contextClassLoaderVar, Collections.singletonList(string0));
        test.addStatement(loadClassStmt);
        return test;
    } catch (NoSuchMethodException | SecurityException e) {
        throw new EvosuiteError("Unexpected exception while creating Class Initializer Test Case");
    }
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Example 49 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestDefaultValue method testLong.

@Test
public void testLong() throws SecurityException, NoSuchMethodException {
    TestCaseBuilder builder = new TestCaseBuilder();
    ArrayReference longArray0 = builder.appendArrayStmt(Long[].class, 10);
    VariableReference long0 = builder.appendNull(Long.class);
    builder.appendAssignment(longArray0, 0, long0);
    builder.appendAssignment(long0, longArray0, 0);
    builder.appendMethod(long0, Long.class.getMethod("toString"));
    DefaultTestCase tc = builder.getDefaultTestCase();
    ExecutionResult ret_val = TestCaseExecutor.runTest(tc);
    assertNotNull(ret_val);
    assertFalse(ret_val.explicitExceptions.isEmpty());
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 50 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestDefaultValue method testFloat.

@Test
public void testFloat() throws SecurityException, NoSuchMethodException {
    TestCaseBuilder builder = new TestCaseBuilder();
    ArrayReference floatArray0 = builder.appendArrayStmt(Float[].class, 10);
    VariableReference float0 = builder.appendNull(Float.class);
    builder.appendAssignment(floatArray0, 0, float0);
    builder.appendAssignment(float0, floatArray0, 0);
    builder.appendMethod(float0, Float.class.getMethod("toString"));
    DefaultTestCase tc = builder.getDefaultTestCase();
    System.out.println(tc.toCode());
    ExecutionResult ret_val = TestCaseExecutor.runTest(tc);
    assertNotNull(ret_val);
    assertFalse(ret_val.explicitExceptions.isEmpty());
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Aggregations

VariableReference (org.evosuite.testcase.variable.VariableReference)472 Method (java.lang.reflect.Method)289 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)143 Test (org.junit.Test)73 GenericMethod (org.evosuite.utils.generic.GenericMethod)68 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)55 MethodStatement (org.evosuite.testcase.statements.MethodStatement)44 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)38 ArrayList (java.util.ArrayList)31 GenericClass (org.evosuite.utils.generic.GenericClass)27 TestCase (org.evosuite.testcase.TestCase)26 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)26 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)25 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)19 Type (java.lang.reflect.Type)17 Statement (org.evosuite.testcase.statements.Statement)17 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)15 ArrayReference (org.evosuite.testcase.variable.ArrayReference)15 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)15 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)14