@Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { Object argument = arguments[parameterContext.getIndex()]; Parameter parameter = parameterContext.getParameter(); Optional<ConvertWith> annotation = AnnotationUtils.findAnnotation(parameter, ConvertWith.class); // @formatter:off ArgumentConverter argumentConverter = annotation.map(ConvertWith::value) .map(clazz -> (ArgumentConverter) ReflectionUtils.newInstance(clazz)) .map(converter -> AnnotationConsumerInitializer.initialize(parameter, converter)) .orElse(DefaultArgumentConverter.INSTANCE); // @formatter:on try { return argumentConverter.convert(argument, parameterContext); } catch (Exception ex) { throw new ParameterResolutionException("参数转换出错:" + parameter.getType().getName(), ex); } }
@Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { List<ParameterFactory> validFactories = getSupportedFactories(parameterContext.getParameter()).collect(toList()); if (validFactories.size() > 1) { throw new ParameterResolutionException( String.format("Too many factories: %s for parameter: %s", validFactories, parameterContext.getParameter())); } return Iterables.getOnlyElement(validFactories) .getParameterValue(parameterContext.getParameter()); }
@Override // public AbstractVaadinPageObject resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) public AbstractVaadinPageObject resolve(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { Supplier<WebDriver> webDriverSupplier = webdriver().apply(extensionContext); Class<?> pageObject = parameterContext.getParameter().getType(); try { Constructor<?> constructor = pageObject.getConstructor(WebDriver.class); return AbstractVaadinPageObject.class.cast(constructor.newInstance(webDriverSupplier.get())); } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) { e.printStackTrace(); throw new ParameterResolutionException("was not able to create PageObjectInstance ", e); } }
private static ParameterContext parameterContextOf(final Parameter parameter) { return new ParameterContext() { @Override public Parameter getParameter() { return parameter; } @Override public int getIndex() { return 0; } @Override public Optional<Object> getTarget() { return Optional.empty(); } }; }
@Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { // we did our checks in supportsParameter() method, now we can do simple resolution if (getContainerFromStore(extensionContext) != null) { List<Annotation> qualifiers = resolveQualifiers(parameterContext, getContainerFromStore(extensionContext).getBeanManager()); return getContainerFromStore(extensionContext) .select(parameterContext.getParameter().getType(), qualifiers.toArray(new Annotation[qualifiers.size()])).get(); } return null; }
@Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { // if weld container isn't up yet or if its not Method, we don't resolve it if (getContainerFromStore(extensionContext) == null || (!(parameterContext.getDeclaringExecutable() instanceof Method))) { return false; } List<Annotation> qualifiers = resolveQualifiers(parameterContext, getContainerFromStore(extensionContext).getBeanManager()); // if we require explicit parameter injection (via global settings or annotation) and there are no qualifiers we don't resolve it if ((getExplicitInjectionInfoFromStore(extensionContext) || (methodRequiresExplicitParamInjection(parameterContext))) && qualifiers.isEmpty()) { return false; } else { return getContainerFromStore(extensionContext).select(parameterContext.getParameter().getType(), qualifiers.toArray(new Annotation[qualifiers.size()])) .isResolvable(); } }
private List<Annotation> resolveQualifiers(ParameterContext pc, BeanManager bm) { List<Annotation> qualifiers = new ArrayList<>(); if (pc.getParameter().getAnnotations().length == 0) { return Collections.emptyList(); } else { for (Annotation annotation : pc.getParameter().getAnnotations()) { // use BeanManager.isQualifier to be able to detect custom qualifiers which don't need to have @Qualifier if (bm.isQualifier(annotation.annotationType())) { qualifiers.add(annotation); } } } return qualifiers; }
private boolean methodRequiresExplicitParamInjection(ParameterContext pc) { for (Annotation annotation : pc.getDeclaringExecutable().getAnnotations()) { if (annotation.annotationType().equals(ExplicitParamInjection.class)) { return true; } } return false; }
@SuppressWarnings("unchecked") @Override public Object convert(Object input, ParameterContext context) throws ArgumentConversionException { TypeToken<?> parameterType = TypeToken.of(context.getParameter().getParameterizedType()); if (parameterType.getRawType() != Class.class) { throw new ArgumentConversionException( String.format("Could not convert: %s. Invalid parameter type.", input)); } return convert(input.toString(), (TypeToken<? extends Class<?>>) parameterType); }
@Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { Parameter parameter = parameterContext.getParameter(); if (getBindingAnnotations(parameter).size() > 1) { return false; } Key<?> key = getKey( extensionContext.getTestClass(), parameter); Optional<Injector> optInjector = getInjectorForParameterResolution(extensionContext); return optInjector.map(injector -> injector.getExistingBinding(key)).isPresent(); }
@Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { Parameter parameter = parameterContext.getParameter(); Key<?> key = getKey(extensionContext.getTestClass(), parameter); Injector injector = getInjectorForParameterResolution(extensionContext) .orElseThrow(() -> new ParameterResolutionException( String.format( "Could not create injector for: %s It has no annotated element.", extensionContext.getDisplayName()))); return injector.getInstance(key); }
@Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { Class<?> type = parameterContext.getParameter().getType(); return (WebDriver.class.isAssignableFrom(type) && !type.isInterface()) || type.equals(List.class); }
@Override public ApplicationConfiguration resolveParameter( ParameterContext parameterContext, ExtensionContext extensionContext ) throws ParameterResolutionException { return getConfiguration(); }
@Override public FilesLocator resolveParameter( ParameterContext parameterContext, ExtensionContext extensionContext ) throws ParameterResolutionException { if (applicationFilesLocator == null) { applicationFilesLocator = new ScenarioFilesLocator(null); } return applicationFilesLocator; }
@Override public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { if (extensionContext.getTestClass().isPresent()) { CasePack pack = extensionContext.getTestClass().get().getAnnotation(CasePack.class); return pack.value(); } return null; }
@Override public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { String tuneType = parameterContext.getParameter().getAnnotation(TuneProvider.class).value(); switch (tuneType) { case DEFAULT: return getDefaultTune(); case JACKSON: return getJacksonTune(); case INTERFACES: return getInterfacesTune(); case NO_MODIFIERS: return getNoModifiersTune(); case REQUIRED: return getRequiredTune(); case JSR_303: return getJsr303Tune(); case JSR_305: return getJsr305Tune(); case GSON: return getGsonTune(); case ANNOTATIONS: return getAnnotationsTune(); case ADDITIONAL_FIELD_PROPERTY_HANDLER: return getAdditionalFieldPropertiesHandlerTune(); default: return null; } }
@Override public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { String tuneType = parameterContext.getParameter().getAnnotation(TuneProvider.class).value(); switch (tuneType) { case DEFAULT: return getDefaultTune(); case JACKSON: return getJacksonTune(); case INTERFACES: return getInterfacesTune(); case NO_MODIFIERS: return getNoModifiersTune(); case REQUIRED: return getRequiredTune(); case JSR_303: return getJsr303Tune(); case JSR_305: return getJsr305Tune(); case ANDROID_SUPPORT: return getAndroidSpportTune(); case GSON: return getGsonTune(); case ANNOTATIONS: return getAnnotationsTune(); case ADDITIONAL_FIELD_PROPERTY_HANDLER: return getAdditionalFieldPropertiesHandlerTune(); default: return null; } }
@Override public boolean supports( ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { Class<?> targetType = parameterContext.getParameter().getType(); return targetType == Random.class || targetType == SeededRandom.class; }
@Override public Object resolve( ParameterContext parameterContext, ExtensionContext context) throws ParameterResolutionException { return randomByUniqueId(context) .computeIfAbsent(context.getUniqueId(), SeededRandom::create); }
@Override public Object convert(Object input, ParameterContext parameterContext) throws ArgumentConversionException { if (input instanceof Point) return input; if (input instanceof String) try { return Point.from((String) input); } catch (NumberFormatException ex) { String message = input + " is no correct string representation of a point."; throw new ArgumentConversionException(message, ex); } throw new ArgumentConversionException(input + " is no valid point"); }
@Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { AnnotatedElement annotatedElement = extensionContext.getElement().orElse(null); if (annotatedElement == null) return null; UITest annotation = annotatedElement.getAnnotation(UITest.class); FXMLLoader loader = new FXMLLoader(getClass().getResource(annotation.value())); try { return loader.load(); } catch (IOException e) { return null; } }
/** * Simple example that simply resolves the Parameter by returning the Class-Name based on the Parameter-Context. */ @Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { Class<?> contextClass = extensionContext.getTestClass().orElse(null); return contextClass == null ? null : contextClass.getSimpleName(); }
@Override public boolean supportsParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext) throws ParameterResolutionException { final Parameter parameter = parameterContext.getParameter(); if (getBindingAnnotations(parameter).size() > 1) return false; final Injector injector = getInjector(extensionContext); if (injector == null) return false; final Key<?> key = getKey(parameter); return injector.getExistingBinding(key) != null; }
@Override public Object resolveParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext) throws ParameterResolutionException { final Parameter parameter = parameterContext.getParameter(); final Key<?> key = getKey(parameter); final Injector injector = getInjector(extensionContext); return injector.getInstance(key); }
@Override public Object convert(Object source, ParameterContext context) throws ArgumentConversionException { try { return JavaVersion.class.getField(String.valueOf(source)).get(null); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { throw new ArgumentConversionException(e.toString()); } }
/** * Resolve a value for the {@link Parameter} in the supplied {@link ParameterContext} by * retrieving the corresponding dependency from the test's {@link ApplicationContext}. * <p>Delegates to {@link ParameterAutowireUtils#resolveDependency}. * @see #supportsParameter * @see ParameterAutowireUtils#resolveDependency */ @Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { Parameter parameter = parameterContext.getParameter(); Class<?> testClass = extensionContext.getRequiredTestClass(); ApplicationContext applicationContext = getApplicationContext(extensionContext); return ParameterAutowireUtils.resolveDependency(parameter, testClass, applicationContext); }
@Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { CommandRule commandRule = new CommandRule( parameterContext.getTarget().orElseThrow(() -> new ParameterResolutionException("no target"))); commandRule.init(); return commandRule; }
@Override public boolean supportsParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext) throws ParameterResolutionException { final Parameter parameter = parameterContext.getParameter(); return parameter.getType().equals(Temporality.class) || isSupportedJavaTimeParameterWithCurrentAnnotation(parameter); }
@Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { boolean supports = parameterTypes.stream().anyMatch(type -> supportsParameter(parameterContext, type)); if (!supports) { supports = parent.supportsParameter(parameterContext, extensionContext); } return supports; }
@Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { Optional<Object> resolved = parameterTypes.stream() .filter(type -> supportsParameter(parameterContext, type)) .map(type -> extensionContext.getStore(namespace).get(type, type)) .map(Object.class::cast) .findFirst(); return resolved.orElseGet(() -> parent.resolveParameter(parameterContext, extensionContext)); }
@Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { Object result = arguments.get(parameterContext.getIndex()); // TODO workaround for https://github.com/junit-team/junit5/issues/1092 Class<?> parameterType = parameterContext.getParameter().getType(); if (parameterType.isPrimitive()) { return convertToBoxedTypeAsWorkaroundForNotWorkingWideningAndUnboxingConversion(result, parameterType); } return result; }
@Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { return parameterContext.getParameter().isAnnotationPresent(Mock.class); }
@Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { return getMock(parameterContext.getParameter(), extensionContext); }
@Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { return true; }
@Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { return "my parameter"; }