private static void reregisterNativeMethodsForRestoredClass(@Nonnull Class<?> realClass) { Method registerNatives = null; try { registerNatives = realClass.getDeclaredMethod("registerNatives"); } catch (NoSuchMethodException ignore) { try { registerNatives = realClass.getDeclaredMethod("initIDs"); } catch (NoSuchMethodException ignored) { } // OK } if (registerNatives != null) { try { registerNatives.setAccessible(true); registerNatives.invoke(null); } catch (IllegalAccessException ignore) { } // won't happen catch (InvocationTargetException ignore) { } // shouldn't happen either } // OK, although another solution will be required for this particular class if it requires // natives to be explicitly registered again (not all do, such as java.lang.Float). }
@Nullable public InstanceFactory findInstanceFactory(@Nonnull Type mockedType) { InstanceFactory instanceFactory = mockedTypesAndInstances.get(mockedType); if (instanceFactory != null) { return instanceFactory; } Class<?> mockedClass = getClassType(mockedType); //noinspection ReuseOfLocalVariable instanceFactory = mockedTypesAndInstances.get(mockedClass); if (instanceFactory != null) { return instanceFactory; } boolean abstractType = mockedClass.isInterface() || isAbstract(mockedClass.getModifiers()); for (Entry<Type, InstanceFactory> entry : mockedTypesAndInstances.entrySet()) { Type registeredMockedType = entry.getKey(); Class<?> registeredMockedClass = getClassType(registeredMockedType); if (abstractType) { registeredMockedClass = getMockedClassOrInterfaceType(registeredMockedClass); } if (mockedClass.isAssignableFrom(registeredMockedClass)) { instanceFactory = entry.getValue(); break; } } return instanceFactory; }
public void processAction(HttpServletRequest request, HttpServletResponse response) throws IOException { System.out.println("processing test driver request ... "); processParams(request); boolean status = false; System.out.println("tc:" + tc); response.setContentType("text/plain"); ServletOutputStream out = response.getOutputStream(); out.println("TestCase: " + tc); if (tc != null) { try { Class<?> c = getClass(); Object t = this; Method[] allMethods = c.getDeclaredMethods(); for (Method m : allMethods) { String mname = m.getName(); if (!mname.equals(tc.trim())) { continue; } System.out.println("Invoking : " + mname); try { m.setAccessible(true); Object o = m.invoke(t); System.out.println("Returned => " + (Boolean) o); status = new Boolean((Boolean) o).booleanValue(); // Handle any methods thrown by method to be invoked } catch (InvocationTargetException x) { Throwable cause = x.getCause(); System.err.format("invocation of %s failed: %s%n", mname, cause.getMessage()); } catch (IllegalAccessException x) { x.printStackTrace(); } } } catch (Exception ex) { ex.printStackTrace(); } if (status) { out.println(tc + ":pass"); } else { out.println(tc + ":fail"); } } }
public boolean isMockedClass(@Nonnull String targetClassName) { int n = mockedClasses.size(); for (int i = 0; i < n; i++) { Class<?> mockedClass = mockedClasses.get(i); if (targetClassName.equals(mockedClass.getName())) { return true; } } return false; }
TestedField( @Nonnull InjectionState injectionState, @Nonnull Field field, @Nonnull Tested metadata) { this.injectionState = injectionState; testedField = field; this.metadata = metadata; fullInjection = metadata.fullyInitialized() ? new FullInjection(injectionState) : null; Class<?> fieldType = field.getType(); if (fieldType.isInterface()) { testedObjectCreation = null; } else { testedObjectCreation = new TestedObjectCreation(injectionState, fullInjection, field); injectionState.lifecycleMethods.findLifecycleMethods(fieldType); } }
private void restoreDefinition(@Nonnull Class<?> redefinedClass) { if (redefinedClassesWithNativeMethods.contains(redefinedClass.getName())) { reregisterNativeMethodsForRestoredClass(redefinedClass); } removeMockedClass(redefinedClass); }
/** * register a class indicated by name * * @param kryo * @param s name of a class - might not exist * @param handled Set of classes already handles */ protected void doRegistration(@Nonnull Kryo kryo, @Nonnull String s) { Class c; try { c = Class.forName(s); doRegistration(kryo, c); } catch (ClassNotFoundException e) { return; } }
public void registerMockedClass(@Nonnull Class<?> mockedType) { if (!isMockedClass(mockedType)) { if (Proxy.isProxyClass(mockedType)) { mockedType = mockedType.getInterfaces()[0]; } mockedClasses.add(mockedType); } }