@Override public Statement copy(TestCase newTestCase, int offset) { FunctionalMockStatement copy = new FunctionalMockStatement( newTestCase, retval.getType(), targetClass); for (VariableReference r : this.parameters) { copy.parameters.add(r.copy(newTestCase, offset)); } copy.listener = this.listener; //no need to clone, as only read, and created new instance at each new execution for (MethodDescriptor md : this.mockedMethods) { copy.mockedMethods.add(md.getCopy()); } for (Map.Entry<String, int[]> entry : methodParameters.entrySet()) { int[] array = entry.getValue(); int[] copiedArray = array == null ? null : new int[]{array[0], array[1]}; copy.methodParameters.put(entry.getKey(), copiedArray); } return copy; }
public VariableReference addFunctionalMock(TestCase test, Type type, int position, int recursionDepth) throws ConstructionFailedException, IllegalArgumentException{ Inputs.checkNull(test, type); if (recursionDepth > Properties.MAX_RECURSION) { logger.debug("Max recursion depth reached"); throw new ConstructionFailedException("Max recursion depth reached"); } //TODO this needs to be fixed once we handle Generics in mocks FunctionalMockStatement fms = new FunctionalMockStatement(test, type, new GenericClass(type)); VariableReference ref = test.addStatement(fms, position); //note: when we add a new mock, by default it will have no parameter at the beginning return ref; }
@Test public void testPackageLevel_differentPackage() throws Exception{ TestCase tc = new DefaultTestCase(); Class<?> example = Class.forName("com.examples.with.different.packagename.fm.ExamplePackageLevel"); VariableReference ref = new VariableReferenceImpl(tc, example); try { FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, new GenericClass(example)); fail(); } catch (java.lang.IllegalArgumentException e){ //expected } //tc.addStatement(mockStmt); //execute(tc); }
@Test public void testPackageLevel_local() throws Exception{ TestCase tc = new DefaultTestCase(); VariableReference ref = new VariableReferenceImpl(tc, PackageLevel.class); try { FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, new GenericClass(PackageLevel.class)); fail(); } catch (java.lang.IllegalArgumentException e){ //expected } //tc.addStatement(mockStmt); //execute(tc); }
@Test public void testAll_twice() throws Exception { TestCase tc = new DefaultTestCase(); VariableReference ref = new VariableReferenceImpl(tc, Foo.class); FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, new GenericClass(Foo.class)); VariableReference mock = tc.addStatement(mockStmt); VariableReference result = tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("all_twice", Foo.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock))); Assert.assertFalse(mockStmt.doesNeedToUpdateInputs()); Assert.assertEquals(0, mockStmt.getNumParameters()); //execute first time with default mock Scope scope = execute(tc); Assert.assertTrue(mockStmt.doesNeedToUpdateInputs()); List<Type> types = mockStmt.updateMockedMethods(); Assert.assertEquals(14,types.size()); }
@Test public void testAll_once() throws Exception { TestCase tc = new DefaultTestCase(); VariableReference ref = new VariableReferenceImpl(tc, Foo.class); FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, new GenericClass(Foo.class)); VariableReference mock = tc.addStatement(mockStmt); VariableReference result = tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("all_once", Foo.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock))); Assert.assertFalse(mockStmt.doesNeedToUpdateInputs()); Assert.assertEquals(0, mockStmt.getNumParameters()); //execute first time with default mock Scope scope = execute(tc); Assert.assertTrue(mockStmt.doesNeedToUpdateInputs()); List<Type> types = mockStmt.updateMockedMethods(); Assert.assertEquals(7,types.size()); }
VariableReference mockedInput = tc.addStatement(new IntPrimitiveStatement(tc, MOCKED_VALUE)); VariableReference ref = new VariableReferenceImpl(tc, Foo.class); FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, new GenericClass(Foo.class)); VariableReference mock = tc.addStatement(mockStmt); VariableReference result = tc.addStatement(new MethodStatement(tc,
@Test public void testPackageLevel_differentPackage_nonInstrumentation_package() throws Exception{ TestCase tc = new DefaultTestCase(); ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite(); NonInstrumentingClassLoader loader = new NonInstrumentingClassLoader(); Class<?> example = loader.loadClass("com.examples.with.different.packagename.fm.ExamplePackageLevel"); VariableReference ref = new VariableReferenceImpl(tc, example); try { FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, new GenericClass(example)); fail(); } catch (java.lang.IllegalArgumentException e){ //expected } //tc.addStatement(mockStmt); //execute(tc); }
@Test public void testPackageLevel_differentPackage_instrumentation_package() throws Exception{ TestCase tc = new DefaultTestCase(); ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite(); InstrumentingClassLoader loader = new InstrumentingClassLoader(); Class<?> example = loader.loadClass("com.examples.with.different.packagename.fm.ExamplePackageLevel"); VariableReference ref = new VariableReferenceImpl(tc, example); try { FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, new GenericClass(example)); fail(); } catch (java.lang.IllegalArgumentException e){ //expected } //tc.addStatement(mockStmt); //execute(tc); }
@Test public void testPackageLevel_differentPackage_instrumentation_public() throws Exception{ TestCase tc = new DefaultTestCase(); RuntimeInstrumentation.setAvoidInstrumentingShadedClasses(true); ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite(); InstrumentingClassLoader loader = new InstrumentingClassLoader(); Class<?> example = loader.loadClass("com.examples.with.different.packagename.fm.ExamplePublicLevel"); VariableReference ref = new VariableReferenceImpl(tc, example); FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, new GenericClass(example)); tc.addStatement(mockStmt); execute(tc); }
tc.addStatement(stmt); FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, Foo.class, new GenericClass(Foo.class)); VariableReference mock = tc.addStatement(mockStmt); VariableReference result = tc.addStatement(new MethodStatement(tc,
VariableReference boolRef = tc.addStatement(new BooleanPrimitiveStatement(tc,true)); VariableReference ref = new VariableReferenceImpl(tc, Foo.class); FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, new GenericClass(Foo.class)); VariableReference mock = tc.addStatement(mockStmt); tc.addStatement(new MethodStatement(tc,