@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; }
if(! fms.doesNeedToUpdateInputs()){ continue; List<Type> missing = fms.updateMockedMethods(); int pos = st.getPosition(); logger.debug("Generating parameters for mock call"); fms.addMissingInputs(refs); } catch (Exception e){ fms.fillWithNullRefs(); return changed;
protected void checkTarget() { if(! canBeFunctionalMocked(targetClass.getRawClass())){ throw new IllegalArgumentException("Cannot create a basic functional mock for class "+targetClass); } }
@Override public void visitFunctionalMockStatement(FunctionalMockStatement st) { VariableReference retval = st.getReturnValue(); Class<?> targetClass = st.getTargetClass(); assert rawClass.getName().equals(targetClass.getName()) : "Mismatch between variable raw type "+rawClass+" and mocked "+targetClass; if (st.doesNeedToUpdateInputs()) { try{ st.updateMockedMethods(); } catch (Exception e){ st.fillWithNullRefs(); for(MethodDescriptor md : st.getMockedMethods()){ if(!md.shouldBeMocked()){ continue; List<VariableReference> params = st.getParameters(md.getID());
@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()); }
FunctionalMockStatement.canBeFunctionalMocked(type)) { if (FunctionalMockStatement.canBeFunctionalMocked(type)) { logger.debug("Using mock for type {}", type); ret = addFunctionalMock(test, type, position, recursionDepth + 1); } else if (clazz.isAbstract() && FunctionalMockStatement.canBeFunctionalMockedIncludingSUT(type)) { logger.debug("No mock solution found: {}, {}, {}, {}", canUseFunctionalMocks, Properties.MOCK_IF_NO_GENERATOR, FunctionalMockStatement.canBeFunctionalMocked(type), FunctionalMockStatement.canBeFunctionalMockedIncludingSUT(type)); throw new ConstructionFailedException("Have no generator for " + type + " canUseFunctionalMocks=" + canUseFunctionalMocks + ", canBeMocked: " + FunctionalMockStatement.canBeFunctionalMocked(type));
listener = createInvocationListener(); logger.debug("Mockito: create mock for {}",targetClass); ret = mock(targetClass.getRawClass(), createMockSettings());
public FunctionalMockStatement(TestCase tc, VariableReference retval, GenericClass targetClass) throws IllegalArgumentException { super(tc, retval); Inputs.checkNull(targetClass); this.targetClass = targetClass; mockedMethods = new ArrayList<>(); methodParameters = new LinkedHashMap<>(); checkTarget(); assert parameters.isEmpty(); //setUpMockCreator(); }
public static boolean canBeFunctionalMocked(Type type) { Class<?> rawClass = new GenericClass(type).getRawClass(); final Class<?> targetClass = Properties.getTargetClassAndDontInitialise(); if (Properties.hasTargetClassBeenLoaded() && GenericClass.isAssignable(targetClass, rawClass)) { return false; } return canBeFunctionalMockedIncludingSUT(type); }
@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()); }
public FunctionalMockStatement(TestCase tc, Type retvalType, GenericClass targetClass) throws IllegalArgumentException { super(tc, retvalType); Inputs.checkNull(targetClass); Class<?> rawType = new GenericClass(retvalType).getRawClass(); if (!targetClass.getRawClass().equals(rawType)) { throw new IllegalArgumentException("Mismatch between raw type " + rawType + " and target class " + targetClass); } this.targetClass = targetClass; mockedMethods = new ArrayList<>(); methodParameters = new LinkedHashMap<>(); checkTarget(); assert parameters.isEmpty(); //setUpMockCreator(); }
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, Assert.assertFalse(mockStmt.doesNeedToUpdateInputs()); Assert.assertEquals(0, mockStmt.getNumParameters()); Assert.assertTrue(mockStmt.doesNeedToUpdateInputs()); List<Type> types = mockStmt.updateMockedMethods(); Assert.assertEquals(1,types.size()); Assert.assertEquals(int.class, types.get(0)); mockStmt.addMissingInputs(Arrays.asList(mockedInput)); Assert.assertEquals(1, mockStmt.getNumParameters()); Assert.assertTrue(mockStmt.getParameterReferences().get(0).same(mockedInput));
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 testAvoidMockingEnvironment(){ final boolean defaultValue = RuntimeSettings.useVFS; RuntimeSettings.useVFS = true; try { Assert.assertFalse(FunctionalMockStatement.canBeFunctionalMocked(File.class)); } catch(Throwable t){ RuntimeSettings.useVFS = defaultValue; } }
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, Assert.assertTrue(mockStmt.doesNeedToUpdateInputs()); List<Type> types = mockStmt.updateMockedMethods(); Assert.assertEquals(LOOP_3, types.size()); for(Type t : types){ mockStmt.addMissingInputs(Arrays.asList(boolRef, boolRef, boolRef)); Assert.assertTrue(mockStmt.doesNeedToUpdateInputs()); types = mockStmt.updateMockedMethods(); Assert.assertEquals(LOOP_5 - LOOP_3, types.size()); for(Type t : types){ mockStmt.addMissingInputs(Arrays.asList(boolRef, boolRef)); Assert.assertEquals(LOOP_5, mockStmt.getNumParameters()); Assert.assertFalse(mockStmt.doesNeedToUpdateInputs()); //no update should be required types = mockStmt.updateMockedMethods(); Assert.assertEquals(0, types.size()); Assert.assertEquals(LOOP_5, mockStmt.getNumParameters()); Assert.assertFalse(mockStmt.doesNeedToUpdateInputs()); //no update should be required types = mockStmt.updateMockedMethods(); Assert.assertEquals(0, types.size());
@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); }
@Ignore @Test public void testAClassWithPLMethod() { //FIXME once we support it assertFalse(FunctionalMockStatement.canBeFunctionalMocked(AClassWithPLMethod.class)); }
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, Assert.assertFalse(mockStmt.doesNeedToUpdateInputs()); Assert.assertEquals(0, mockStmt.getNumParameters()); Assert.assertTrue(mockStmt.doesNeedToUpdateInputs()); List<Type> types = mockStmt.updateMockedMethods(); Assert.assertEquals(1,types.size()); Assert.assertEquals(String[].class, types.get(0)); mockStmt.addMissingInputs(Arrays.asList(mockedArray)); Assert.assertEquals(1, mockStmt.getNumParameters()); Assert.assertTrue(mockStmt.getParameterReferences().get(0).same(mockedArray));
@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 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); }