public VariableReference appendStringPrimitive(String string) { StringPrimitiveStatement primitiveStmt = new StringPrimitiveStatement( tc, string); tc.addStatement(primitiveStmt); return primitiveStmt.getReturnValue(); }
DefaultTestCase test = new DefaultTestCase(); StringPrimitiveStatement stmt0 = new StringPrimitiveStatement(test, className); VariableReference string0 = test.addStatement(stmt0); try {
public void constructPopulation(){ Properties.POPULATION = 16; TestSuiteChromosome[] testSuiteChromObjects = new TestSuiteChromosome[Properties.POPULATION]; for(int i=0; i<testSuiteChromObjects.length; i++){ testSuiteChromObjects[i] = new TestSuiteChromosome(); DefaultTestCase testcase = new DefaultTestCase(); StringPrimitiveStatement stringStatement = new StringPrimitiveStatement(testcase, "any string statatement for TS"+i); testcase.addStatement(stringStatement); testSuiteChromObjects[i].addTest(testcase); population.add(testSuiteChromObjects[i]); } }
statement = new BytePrimitiveStatement(tc); } else if (clazz.equals(String.class)) { statement = new StringPrimitiveStatement(tc); } else if (GenericTypeReflector.erase(clazz).isEnum()) { statement = new EnumPrimitiveStatement(tc, GenericTypeReflector.erase(clazz));
@Test public void testStringQuoting() throws NoSuchMethodException, SecurityException { DefaultTestCase test = new DefaultTestCase(); ConstructorStatement cs = new ConstructorStatement(test, new GenericConstructor(StringConstantInliningExample.class.getConstructor(), StringConstantInliningExample.class), new ArrayList<VariableReference>()); VariableReference objectVar = test.addStatement(cs); StringPrimitiveStatement stringStatement = new StringPrimitiveStatement(test, "EXAMPLE"); VariableReference stringParam = test.addStatement(stringStatement); List<VariableReference> parameters = new ArrayList<VariableReference>(); parameters.add(stringParam); test.addStatement(new MethodStatement(test, new GenericMethod(StringConstantInliningExample.class.getMethods()[0], StringConstantInliningExample.class), objectVar, parameters)); System.out.println(test.toCode()); ConstantInliner inliner = new ConstantInliner(); inliner.inline(test); String code = test.toCode(); System.out.println(code); assertFalse(code.contains("foo(EXAMPLE)")); assertTrue(code.contains("foo(\"EXAMPLE\")")); }
@Test public void testStringEndingWithClass() throws NoSuchMethodException, SecurityException { DefaultTestCase test = new DefaultTestCase(); ConstructorStatement cs = new ConstructorStatement(test, new GenericConstructor(StringConstantInliningExample.class.getConstructor(), StringConstantInliningExample.class), new ArrayList<VariableReference>()); VariableReference objectVar = test.addStatement(cs); StringPrimitiveStatement stringStatement = new StringPrimitiveStatement(test, "test.class"); VariableReference stringParam = test.addStatement(stringStatement); List<VariableReference> parameters = new ArrayList<VariableReference>(); parameters.add(stringParam); test.addStatement(new MethodStatement(test, new GenericMethod(StringConstantInliningExample.class.getMethods()[0], StringConstantInliningExample.class), objectVar, parameters)); System.out.println(test.toCode()); ConstantInliner inliner = new ConstantInliner(); inliner.inline(test); String code = test.toCode(); System.out.println(code); assertFalse(code.contains("foo(test.class)")); assertTrue(code.contains("foo(\"test.class\")")); }
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"); } }
@Test public void testSame(){ TestCase tc = new DefaultTestCase(); PrimitiveStatement<?> aInt = new IntPrimitiveStatement(tc,42); Assert.assertTrue(aInt.same(aInt)); Assert.assertFalse(aInt.same(null)); PrimitiveStatement<?> fooString = new StringPrimitiveStatement(tc,"foo"); Assert.assertFalse(aInt.same(fooString)); PrimitiveStatement<?> nullString = new StringPrimitiveStatement(tc,null); Assert.assertFalse(nullString.same(fooString)); Assert.assertFalse(fooString.same(nullString)); //TODO: how to make it work? //PrimitiveStatement<?> anotherNullString = new StringPrimitiveStatement(tc,null); //Assert.assertTrue(nullString.same(anotherNullString)); //Assert.assertTrue(anotherNullString.same(nullString)); } }
@Test public void testNoNullInputs_notNull() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("initServlet",Servlet.class), EvoServletState.class), 1, 0); StringPrimitiveStatement foo = new StringPrimitiveStatement(tc.getTestCase(), "foo"); tc.getTestCase().addStatement(foo); VariableReference con = factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getConfiguration"), EvoServletState.class), 3, 0); factory.addMethodFor(tc.getTestCase(), con, new GenericMethod(EvoServletConfig.class.getDeclaredMethod("createDispatcher", String.class), EvoServletConfig.class), 4); Assert.assertEquals(5, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); }
VariableReference stringVar = test.addStatement(new StringPrimitiveStatement(test, "baz5"));
VariableReference stringVar = test.addStatement(new StringPrimitiveStatement(test, "baz5"));
@Test public void testNoNullInputs_nullString() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("initServlet",Servlet.class), EvoServletState.class), 1, 0); //shouldn't be able to pass it to createDispatcher StringPrimitiveStatement foo = new StringPrimitiveStatement(tc.getTestCase(), null); tc.getTestCase().addStatement(foo); VariableReference con = factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getConfiguration"), EvoServletState.class), 3, 0); factory.addMethodFor(tc.getTestCase(), con, new GenericMethod(EvoServletConfig.class.getDeclaredMethod("createDispatcher", String.class), EvoServletConfig.class), 4); /* even if "foo" is null and we have the P of object reuse to 1, still "foo" will not be used, as "null" constraint is always enforced */ Assert.assertEquals(tc.getTestCase().toCode(), 6, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); }
VariableReference aString = tc.addStatement(new StringPrimitiveStatement(tc, MOCKED_VALUE)); ArrayReference mockedArray = (ArrayReference) tc.addStatement(new ArrayStatement(tc,String[].class,1)); ArrayIndex arrayIndex = new ArrayIndex(tc, mockedArray, 0);