How to use CallbackFilter method of org.jmock.lib.legacy.ClassImposteriser class

Best Jmock-library code snippet using org.jmock.lib.legacy.ClassImposteriser.CallbackFilter

Source:ClassImposteriser.java Github

copy

Full Screen

...7import net.sf.cglib.core.DefaultNamingPolicy;8import net.sf.cglib.core.NamingPolicy;9import net.sf.cglib.core.Predicate;10import net.sf.cglib.proxy.Callback;11import net.sf.cglib.proxy.CallbackFilter;12import net.sf.cglib.proxy.Enhancer;13import net.sf.cglib.proxy.Factory;14import net.sf.cglib.proxy.InvocationHandler;15import net.sf.cglib.proxy.NoOp;16import org.jmock.api.Imposteriser;17import org.jmock.api.Invocation;18import org.jmock.api.Invokable;19import org.jmock.internal.SearchingClassLoader;20import org.objenesis.Objenesis;21import org.objenesis.ObjenesisStd;22/**23 * This class lets you imposterise abstract and concrete classes 24 * <em>without</em> calling the constructors of the mocked class.25 * 26 * @author npryce27 */28public class ClassImposteriser implements Imposteriser {29 public static final Imposteriser INSTANCE = new ClassImposteriser();30 31 private ClassImposteriser() {}32 33 private static final NamingPolicy NAMING_POLICY_THAT_ALLOWS_IMPOSTERISATION_OF_CLASSES_IN_SIGNED_PACKAGES = new DefaultNamingPolicy() {34 @Override35 public String getClassName(String prefix, String source, Object key, Predicate names) {36 return "org.jmock.codegen." + super.getClassName(prefix, source, key, names);37 }38 };39 40 private static final CallbackFilter IGNORE_BRIDGE_METHODS = new CallbackFilter() {41 public int accept(Method method) {42 return method.isBridge() ? 1 : 0;43 }44 };45 46 private final Objenesis objenesis = new ObjenesisStd();47 48 public boolean canImposterise(Class<?> type) {49 return !type.isPrimitive() && 50 !Modifier.isFinal(type.getModifiers()) && 51 (type.isInterface() || !toStringMethodIsFinal(type));52 }53 54 public <T> T imposterise(final Invokable mockObject, Class<T> mockedType, Class<?>... ancilliaryTypes) {55 if (!mockedType.isInterface() && toStringMethodIsFinal(mockedType)) {56 throw new IllegalArgumentException(mockedType.getName() + " has a final toString method");57 }58 59 try {60 setConstructorsAccessible(mockedType, true);61 Class<?> proxyClass = createProxyClass(mockedType, ancilliaryTypes);62 return mockedType.cast(createProxy(proxyClass, mockObject));63 }64 finally {65 setConstructorsAccessible(mockedType, false);66 }67 }68 69 private boolean toStringMethodIsFinal(Class<?> type) {70 try {71 Method toString = type.getMethod("toString");72 return Modifier.isFinal(toString.getModifiers());73 74 }75 catch (SecurityException e) {76 throw new IllegalStateException("not allowed to reflect on toString method", e);77 }78 catch (NoSuchMethodException e) {79 throw new Error("no public toString method found", e);80 }81 }82 private void setConstructorsAccessible(Class<?> mockedType, boolean accessible) {83 for (Constructor<?> constructor : mockedType.getDeclaredConstructors()) {84 constructor.setAccessible(accessible);85 }86 }87 88 private <T> Class<?> createProxyClass(Class<?> possibleMockedType, Class<?>... ancilliaryTypes) {89 Class<?> mockedType = 90 possibleMockedType == Object.class ? ClassWithSuperclassToWorkAroundCglibBug.class : possibleMockedType;91 92 Enhancer enhancer = new Enhancer() {93 @Override94 @SuppressWarnings("unchecked")95 protected void filterConstructors(Class sc, List constructors) {96 // Don't filter97 }98 };99 enhancer.setClassLoader(SearchingClassLoader.combineLoadersOf(mockedType, ancilliaryTypes));100 enhancer.setUseFactory(true);101 if (mockedType.isInterface()) {102 enhancer.setSuperclass(Object.class);103 enhancer.setInterfaces(prepend(mockedType, ancilliaryTypes));104 }105 else {106 enhancer.setSuperclass(mockedType);107 enhancer.setInterfaces(ancilliaryTypes);108 }109 enhancer.setCallbackTypes(new Class[]{InvocationHandler.class, NoOp.class});110 enhancer.setCallbackFilter(IGNORE_BRIDGE_METHODS);111 if (mockedType.getSigners() != null) {112 enhancer.setNamingPolicy(NAMING_POLICY_THAT_ALLOWS_IMPOSTERISATION_OF_CLASSES_IN_SIGNED_PACKAGES);113 }114 115 try {116 return enhancer.createClass();117 }118 catch (CodeGenerationException e) {119 // Note: I've only been able to manually test this. It exists to help people writing120 // Eclipse plug-ins or using other environments that have sophisticated class loader121 // structures.122 throw new IllegalArgumentException("could not imposterise " + mockedType, e);123 }124 }...

Full Screen

Full Screen

CallbackFilter

Using AI Code Generation

copy

Full Screen

1ClassImposteriser.INSTANCE.setCallbackFilter(new CallbackFilter() {2 public int accept(Method method) {3 return Modifier.isFinal(method.getModifiers()) ? 0 : 1;4 }5});6ClassImposteriser.INSTANCE.setCallbackFilter(new CallbackFilter() {7 public int accept(Method method) {8 return Modifier.isFinal(method.getModifiers()) ? 0 : 1;9 }10});11ClassImposteriser.INSTANCE.setCallbackFilter(new CallbackFilter() {12 public int accept(Method method) {13 return Modifier.isFinal(method.getModifiers()) ? 0 : 1;14 }15});16ClassImposteriser.INSTANCE.setCallbackFilter(new CallbackFilter() {17 public int accept(Method method) {18 return Modifier.isFinal(method.getModifiers()) ? 0 : 1;19 }20});21ClassImposteriser.INSTANCE.setCallbackFilter(new CallbackFilter() {22 public int accept(Method method) {23 return Modifier.isFinal(method.getModifiers()) ? 0 : 1;24 }25});26ClassImposteriser.INSTANCE.setCallbackFilter(new CallbackFilter() {27 public int accept(Method method) {28 return Modifier.isFinal(method.getModifiers()) ? 0 : 1;29 }30});31ClassImposteriser.INSTANCE.setCallbackFilter(new CallbackFilter() {32 public int accept(Method method) {33 return Modifier.isFinal(method.getModifiers()) ? 0 : 1;34 }35});36ClassImposteriser.INSTANCE.setCallbackFilter(new CallbackFilter() {

Full Screen

Full Screen

CallbackFilter

Using AI Code Generation

copy

Full Screen

1 public class CallbackFilterTest {2 private final Mockery context = new Mockery();3 private final CallbackFilter filter = context.mock(CallbackFilter.class);4 private final Class[] interfaces = new Class[]{Filter.class};5 private final Filter proxy = (Filter) context.mock(Filter.class, "proxy", new ClassImposteriser(filter, interfaces));6 private final Filter delegate = context.mock(Filter.class, "delegate");7 private final ServletRequest request = context.mock(ServletRequest.class);8 private final ServletResponse response = context.mock(ServletResponse.class);9 private final FilterChain chain = context.mock(FilterChain.class);10 public void testFilter() throws Exception {11 context.checking(new Expectations() {{12 allowing(filter).isHandled(with(any(Object.class)));13 will(returnValue(true));14 allowing(filter).invoke(with(any(Object.class)), with(any(Method.class)), with(any(Object[].class)));15 will(returnValue(null));16 oneOf(proxy).init(with(any(FilterConfig.class)));17 oneOf(proxy).doFilter(request, response, chain);18 oneOf(proxy).destroy();19 }});20 proxy.init(null);21 proxy.doFilter(request, response, chain);22 proxy.destroy();23 context.assertIsSatisfied();24 }25 }26}27public interface FilterConfig {28 public String getFilterName();29 public ServletContext getServletContext();30 public String getInitParameter(String name);31 public Enumeration getInitParameterNames();32}33getFilterName()34getServletContext()35getInitParameter(String name)36getInitParameterNames()37public class FilterConfig {38 private final Filter filter;39 private final String filterName;40 private final ServletContext servletContext;

Full Screen

Full Screen

CallbackFilter

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mockery2import org.jmock.Expectations3import org.jmock.lib.legacy.ClassImposteriser4import org.junit.Test5import org.junit.Before6import org.junit.After7import org.junit.Assert8import org.junit.Assert.*9import org.junit.Rule10import org.junit.rules.TemporaryFolder11import org.junit.rules.ExpectedException12import org.junit.rules.TestName13import org.junit.rules.TestRule14import org.junit.runner.Description15import org.junit.runner.RunWith16import org.junit.runners.Parameterized17import org.junit.runners.Parameterized.Parameters18import org.junit.runners.model.Statement19import org.junit.runners.model.FrameworkMethod20import org.junit.runners.model.TestClass21import org.junit.runners.model.InitializationError22import org.junit.runner.notification.RunNotifier23import org.junit.runner.notification.Failure24import org.junit.runner.notification.RunListener25import org.junit.runner.JUnitCore26import org.junit.runner.Result27import org.junit.runner.Request28import org.junit.runner.Description29import org.junit.internal.runners.statements.Fail30import org.junit.internal.runners.statements.ExpectException31import org.junit.internal.runners.statements.RunAfters32import org.junit.internal.runners.statements.RunBefores33import org.junit.internal.runners.statements.InvokeMethod34import org.junit.internal.runners.statements.RunAfters35import org.junit.internal.runners.statements.RunBefores36import org.junit.internal.runners.statements.FailOnTimeout37import org.junit.internal.runners.statements.FailOnTimeout$Builder38import org.junit.internal.runners.statements.InvokeMethod39import org.junit.internal.runners.statements.Fail40import org.junit.internal.runners.statements.ExpectException41import org.junit.internal.runners.statements.FailOnTimeout42import org.junit.internal.runners.statements.FailOnTimeout$Builder43import org.junit.internal.runners.statements.InvokeMethod44import org.junit.internal.runners.statements.Fail45import org.junit.internal.runners.statements.ExpectException46import org.junit.internal.runners.statements.FailOnTimeout47import org.junit.internal.runners.statements.FailOnTimeout$Builder48import org.junit.internal.runners.statements.InvokeMethod49import org.junit.internal.runners.statements.Fail50import org.junit.internal.runners.statements.ExpectException51import org.junit.internal.runners.statements.FailOnTimeout52import org.junit.internal.runners.statements.FailOnTimeout$Builder

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful