/** * Reports report reflection argument matchers for all arguments of the given method invocation. * An exception will be thrown if there were already matchers reported for the invocation. * * @param invocation the method invocation, not null */ private void createMatchers(Invocation invocation) { List<IArgumentMatcher> matchers = LastControl.pullMatchers(); if (matchers != null && !matchers.isEmpty()) { if (matchers.size() != invocation.getArguments().length) { throw new IllegalStateException("This mock control does not support mixing of no-argument matchers and per-argument matchers. " + "Either no matchers are defined and the reflection argument matcher is used by default or all matchers are defined explicitly (Eg by using refEq())."); } // put all matchers back since pull removes them for (IArgumentMatcher matcher : matchers) { LastControl.reportMatcher(matcher); } return; } Object[] arguments = invocation.getArguments(); if (arguments == null) { return; } for (Object argument : arguments) { LastControl.reportMatcher(new ReflectionArgumentMatcher<Object>(argument, modes)); } }
/** * Overriden to report reflection argument matchers for all arguments of the given method invocation. * * @param invocation the method invocation, not null * @return the result of the invocation */ @Override public Object invoke(Invocation invocation) { LastControl.reportLastControl(LenientMocksControl.this); createMatchers(invocation); return recordState.invoke(invocation); }
private void clearStateFromOtherClassLoaders() { for(ClassLoader cl : classloadersToClear()) { try { final Class<?> lastControlClassByCL = Class.forName(LastControl.class.getName(), false, cl); final Class<?> mocksControlClassByCL = Class.forName(MocksControl.class.getName(), false, cl); final Method reportLastControl = lastControlClassByCL.getMethod("reportLastControl", mocksControlClassByCL); reportLastControl.invoke(lastControlClassByCL, new Object[]{null}); } catch (Exception e) { // Should never happen throw new RuntimeException("Failed to clean up state", e); } } }
private Iterable<ClassLoader> classloadersToClear() { List<ClassLoader> loaders = new ArrayList<ClassLoader>(); final ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); if(classFoundInClassloader(LastControl.class, systemClassLoader)) { loaders.add(systemClassLoader); } if(classFoundInClassloader(LastControl.class, contextClassLoader)) { loaders.add(contextClassLoader); } return loaders; }
public void run() { LastControl.reportLastControl(null); clearStateFromOtherClassLoaders(); }
@Test public void testMatcher() { SmockEasyMockClient.is(payload(new StringSource("<a/>"))); assertEquals(SmockArgumentMatcher.class, LastControl.pullMatchers().get(0).getClass()); }