@Override public boolean isHandled(Method method) { return !isToString(method) && !isHashCode(method) && !isFinalize(method) && !isEquals(method); }
public PowerMockClassloaderObjectFactory() { classLoaderFactory = new TestNGMockClassLoaderFactory(); }
private boolean isZeroArgumentMethod(Method method) { return hasArgumentLength(method, 0); }
Object create() { try { initializeMockPolicy(); final Class<?> testClassLoadedByMockedClassLoader = createTestClass(testClass); final Constructor<?> con = testClassLoadedByMockedClassLoader.getConstructor(constructor.getParameterTypes()); final Object testInstance = con.newInstance(params); if (!extendsPowerMockTestCase(testClass)) { setInvocationHandler(testInstance); } return testInstance; } catch (Exception e) { throw new RuntimeException(String.format("Cannot create a new instance of test class %s", testClass), e); } }
/** * We proxy the test class in order to be able to clear state after each * test method invocation. It would be much better to be able to register a * testng listener programmtically but I cannot find a way to do so. */ private Class<?> createTestClass(Class<?> actualTestClass) throws Exception { final Class<?> testClassLoadedByMockedClassLoader = Class.forName(actualTestClass.getName(), false, mockLoader); if (extendsPowerMockTestCase(actualTestClass)) { return testClassLoadedByMockedClassLoader; } else { return createProxyTestClass(testClassLoadedByMockedClassLoader); } }
@Override public Object newInstance(Constructor constructor, Object... params) { /* * For extra safety clear the MockitoRepository on each new * instantiation of the object factory. This is good in cases where a * previous test has used e.g. PowerMock#createMock(..) to create a mock * without using this factory. That means that there's some state left in * the MockRepository that hasn't been cleared. Currently clearing the * MockRepository from any classloader will clear the previous state but * it's not certain that this is always the case. */ MockRepository.clear(); return new TestClassInstanceFactory(constructor, classLoaderFactory, params).create(); }
@Override public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable { injectMocksUsingAnnotationEnabler(self); try { final Object result = proceed.invoke(self, args); return result; } catch (InvocationTargetException e) { throw e.getTargetException(); } finally { if (thisMethod.isAnnotationPresent(Test.class)) { clearMockFields(); MockRepository.clear(); } } }
private boolean isFinalize(Method method) { return method.getName().equals("finalize") && isZeroArgumentMethod(method); }
TestClassInstanceFactory(Constructor constructor, TestNGMockClassLoaderFactory classLoaderFactory, Object... params) { this.constructor = constructor; this.params = params; this.testClass = constructor.getDeclaringClass(); this.mockLoader = classLoaderFactory.createClassLoader(testClass); }
private boolean isEquals(Method method) { return method.getName().equals("equals") && isOneArgumentMethodOfType(method, Object.class); }
classLoaderFactory.set(new ClassLoaderFactory(pathConfig, pathID)); try { super.startInternal();
TestNGMockClassLoaderFactory() { ignorePackagesExtractor = new PowerMockIgnorePackagesExtractorImpl(); expectedExceptionsExtractor = new PowerMockExpectedExceptionsExtractorImpl(); arrayMerger = new ArrayMergerImpl(); }
@Override public Object newInstance(Constructor constructor, Object... params) { final Object testInstance; Class<?> testClass = constructor.getDeclaringClass(); if (hasPowerMockAnnotation(testClass)) { testInstance = powerMockObjectFactory.newInstance(constructor, params); } else { testInstance = defaultObjectFactory.newInstance(constructor, params); } return testInstance; }
public String[] getPackagesToIgnore(AnnotatedElement element) { List<String> ignoredPackages = new LinkedList<String>(); if (element instanceof Class<?>) { Class<?> klazz = (Class<?>) element; for (Method method : klazz.getMethods()) { Test annotation = method.getAnnotation(Test.class); if (annotation != null) { Class<?>[] ignores = annotation.expectedExceptions(); if (ignores != null) { for (Class<?> ignorePackage : ignores) { ignoredPackages.add(ignorePackage.getName()); } } } } Class<?> superclass = klazz.getSuperclass(); if (superclass != null && !superclass.equals(Object.class)) { String[] packagesToIgnore = getPackagesToIgnore(superclass); ignoredPackages.addAll(Arrays.asList(packagesToIgnore)); } } return ignoredPackages.toArray(new String[ignoredPackages.size()]); } }
private boolean isToString(Method method) { return (method.getName().equals("toString") && isZeroArgumentMethod(method)); }
private boolean isOneArgumentMethodOfType(Method method, Class<?> type) { return hasArgumentLength(method, 1) && Object.class.equals(method.getParameterTypes()[0]); } }
private boolean isHashCode(Method method) { return method.getName().equals("hashCode") && isZeroArgumentMethod(method); }