示例#1
0
  @Rank(Integer.MAX_VALUE)
  @Singleton
  @Override
  public Client provide() {
    if (!httpServer.isStarted()) {
      throw new TestException("test server not started");
    }

    Object testInstance = testContext.getTestInstance();
    Class<? extends Object> testClass = testInstance.getClass();

    Optional<ClientConfig> optional =
        of(testClass.getDeclaredMethods())
            .filter(p -> p.getDeclaredAnnotation(Config.class) != null)
            .filter(p -> p.getParameterCount() == 0)
            .filter(p -> p.getReturnType().isAssignableFrom(ClientConfig.class))
            .findFirst()
            .map(p -> reflectionUtil.invokeMethod(testInstance, p))
            .map(ClientConfig.class::cast);

    ClientConfig clientConfig = optional.orElseGet(ClientConfig::new);

    JerseyTest jerseyTest =
        testContext.getTestInstance().getClass().getAnnotation(JerseyTest.class);

    if (jerseyTest.logTraffic() && !clientConfig.isRegistered(LoggingFilter.class)) {
      clientConfig.register(new LoggingFilter(log, jerseyTest.dumpEntity()));
    }

    return ClientBuilder.newClient(clientConfig);
  }
  @RuntimeType
  public Object intercept(@AllArguments Object[] args, @Origin Method method, @This Object proxy)
      throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    HK2TestContext testContext = context.getTestContext().get();
    SystemInjecteeImpl injectee = context.getInjectee();
    Object instance;

    ServiceLocatorImpl locator = testContext.getLocator();
    ActiveDescriptor<?> descriptor = locator.getInjecteeDescriptor(injectee);

    // if we don't find a descriptor it could be that the injectee is an
    // assisted injection and can be resolved using it's injection resolver.
    if (descriptor == null) {
      instance = LOCATOR_UTIL.getInjectionResolver(locator, injectee).resolve(injectee, null);
    } else {
      instance = locator.getService(descriptor, null, injectee);
    }

    // if any instance argument is a proxied service then unpack it by
    // getting the the acutal service and set it as the argument.
    for (int i = 0; i < args.length; i++) {
      Object arg = args[i];
      if (arg instanceof ProxyService) {
        args[i] = ((ProxyService) arg)._getService();
      }
    }

    return method.invoke(instance, args);
  }