@Test public void testSimpleValidationWithCustomProvider() throws Exception { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.setProviderClass(HibernateValidator.class); validator.afterPropertiesSet(); ValidPerson person = new ValidPerson(); Set<ConstraintViolation<ValidPerson>> result = validator.validate(person); assertEquals(2, result.size()); for (ConstraintViolation<ValidPerson> cv : result) { String path = cv.getPropertyPath().toString(); if ("name".equals(path) || "address.street".equals(path)) { assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull); } else { fail("Invalid constraint violation with path '" + path + "'"); } } validator.destroy(); }
@Override public RestlerConfig getConfiguration() { ObjectMapper objectMapper = Jackson.newObjectMapper(); ValidatorFactory validatorFactory = Validation .byProvider(HibernateValidator.class) .configure() .addValidatedValueHandler(new OptionalValidatedValueUnwrapper()) .buildValidatorFactory(); final ConfigurationFactory<RestlerConfig> configurationFactory = new DefaultConfigurationFactoryFactory<RestlerConfig>().create(RestlerConfig.class, validatorFactory.getValidator(), objectMapper, "dw"); try { return configurationFactory.build(new FileConfigurationSourceProvider(), TEST_CONFIG_FILE); } catch (Exception e) { throw new RuntimeException("Cannot get test configuration", e); } }
@Override public void start(BundleContext context) throws Exception { HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class) .configure(); configuration.parameterNameProvider(new ReflectionParameterNameProvider()); configuration.messageInterpolator(new LocaleSpecificMessageInterpolator(configuration .getDefaultMessageInterpolator())); validatorFactory = configuration.buildValidatorFactory(); Dictionary<String, Object> props = new Hashtable<>(); props.put(Constants.SERVICE_VENDOR, "AdeptJ"); props.put(Constants.SERVICE_DESCRIPTION, "AdeptJ ValidatorService"); servRegValidatorService = context.registerService(ValidatorService.class, new OSGiBeanValidatorService(this.validatorFactory.getValidator(), configuration.getBootstrapConfiguration()), props); }
public Object invoke(final MethodInvocation mi) throws Throwable { checkNotNull(methodValidator); final ClassLoader tccl = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(HibernateValidator.class.getClassLoader()); final Validate validate = mi.getMethod().getAnnotation(Validate.class); validateParameters(mi.getThis(), mi.getMethod(), mi.getArguments(), validate.groups()); final Object result = mi.proceed(); validateReturnValue(mi.getThis(), mi.getMethod(), result, validate.groups()); return result; } finally { Thread.currentThread().setContextClassLoader(tccl); } }
/** * create hibernate validation configuration * * @return */ protected HibernateValidatorConfiguration createConfiguration() { HibernateValidatorConfiguration configuration = null; if (this.isIgnoreClassHierarchy()) { ValidationProviderResolver resolver = new ValidationProviderResolver() { /** * (non-Javadoc) * * @see javax.validation.ValidationProviderResolver#getValidationProviders() */ @Override public List<ValidationProvider<?>> getValidationProviders() { List<ValidationProvider<?>> providers = new ArrayList<ValidationProvider<?>>(1); providers.add(new HibernateValidator513()); return providers; } }; configuration = Validation.byProvider(HibernateValidator513.class).providerResolver(resolver).configure(); } else { configuration = Validation.byProvider(HibernateValidator.class).configure(); } configuration.messageInterpolator(new ResourceBundleMessageInterpolator(new PlatformResourceBundleLocator( MESSAGES))); return configuration; }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysRole.class) .property("roleName", ElementType.FIELD) .constraint(new SizeDef().max(32)) .constraint(new NotNullDef()) .constraint(new PatternDef().regexp("^(?!root$).+$").message( "may not be root")) .property("roleCode", ElementType.FIELD) .constraint(new NotNullDef()).constraint(new SizeDef().max(32)); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysRoute.class) .property("routeId", ElementType.FIELD) .constraint(new NotNullDef()) .property("name", ElementType.FIELD) .constraint(new SizeDef().max(32)) .property("url", ElementType.FIELD) .constraint(new SizeDef().max(128)) .constraint(new PatternDef() .regexp("(/:?[0-9a-zA-Z_]+)*") .message("{msg.error.validation.routeUrl.pattern}")); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysMenu.class) .property("menuName", ElementType.FIELD) .constraint(new SizeDef().max(32)) .constraint(new NotNullDef()) .property("menuPath", ElementType.FIELD) .constraint(new SizeDef().max(128)) .constraint(new PatternDef() .regexp("(/:?[0-9a-zA-Z_]+)*") .message("{msg.error.validation.routeUrl.pattern}")) .property("parentId", ElementType.FIELD) .constraint(new NotNullDef()) .property("icon", ElementType.FIELD) .constraint(new SizeDef().max(32)); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysUser.class) .property("userId", ElementType.FIELD) .constraint(new NotNullDef()) .property("username", ElementType.FIELD) .constraint(new SizeDef().max(16).min(3)) .constraint(new PatternDef().regexp("[a-zA-Z][0-9a-zA-Z_]*") .message("{msg.error.validation.username.pattern}")) .property("password", ElementType.FIELD) .constraint(new SizeDef().max(16).min(6)) .property("fullName", ElementType.FIELD) .constraint(new SizeDef().max(32)) .property("email", ElementType.FIELD).constraint(new EmailDef()) .constraint(new SizeDef().max(64)); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping.type(SysRole.class).property("roleId", ElementType.FIELD) .constraint(new NotNullDef()) .property("roleName", ElementType.FIELD) .constraint(new SizeDef().max(32)) .constraint(new PatternDef().regexp("^(?!root$).+$").message("may not be root")) .property("roleCode", ElementType.FIELD) .constraint(new SizeDef().max(32)); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysUser.class) .property("username", ElementType.FIELD) .constraint(new NotNullDef()) .constraint(new SizeDef().max(16).min(3)) .constraint(new PatternDef().regexp("[a-zA-Z][0-9a-zA-Z_]*") .message("{msg.error.validation.username.pattern}")) .property("password", ElementType.FIELD) .constraint(new SizeDef().max(16).min(6)) .constraint(new NotNullDef()) .property("fullName", ElementType.FIELD) .constraint(new NotNullDef()).constraint(new SizeDef().max(32)) .property("email", ElementType.FIELD) .constraint(new SizeDef().max(64)).constraint(new EmailDef()); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping.type(ChangePasswordVo.class) .property("oldpassword", ElementType.FIELD) .constraint(new SizeDef().max(16).min(6)) .constraint(new NotNullDef()) .property("newpassword", ElementType.FIELD) .constraint(new SizeDef().max(16).min(6)) .constraint(new NotNullDef()) .property("retypepassword", ElementType.FIELD) .constraint(new SizeDef().max(16).min(6)) .constraint(new NotNullDef()); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysMenu.class) .property("menuId", ElementType.FIELD) .constraint(new NotNullDef()) .property("menuName", ElementType.FIELD) .constraint(new SizeDef().max(32)) .property("menuPath", ElementType.FIELD) .constraint(new SizeDef().max(128)) .constraint(new PatternDef() .regexp("(/:?[0-9a-zA-Z_]+)*") .message("{msg.error.validation.routeUrl.pattern}")) .property("icon", ElementType.FIELD) .constraint(new SizeDef().max(32)); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysRoute.class) .property("name", ElementType.FIELD) .constraint(new SizeDef().max(32)) .constraint(new NotNullDef()) .property("url", ElementType.FIELD) .constraint(new NotNullDef()) .constraint(new SizeDef().max(128)) .constraint(new PatternDef() .regexp("(/:?[0-9a-zA-Z_]+)*") .message("{msg.error.validation.routeUrl.pattern}")); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping.type(SysJob.class).property("jobId", ElementType.FIELD) .constraint(new NotNullDef()) .property("clazzName", ElementType.FIELD).constraint(new NullDef()) .property("jobName", ElementType.FIELD) .constraint(new SizeDef().max(32)) .property("cron", ElementType.FIELD) .constraint(new SizeDef().max(32)); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysJob.class) .property("clazzName", ElementType.FIELD) .constraint(new NotNullDef()) .constraint(new SizeDef().max(64)) .constraint(new PatternDef().regexp("[a-zA-Z.]+") .message("{msg.error.validation.className.pattern}")) .property("jobName", ElementType.FIELD) .constraint(new SizeDef().max(32)).constraint(new NotNullDef()) .property("cron", ElementType.FIELD) .constraint(new SizeDef().max(32)).constraint(new NotNullDef()); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysDict.class) .property("dictCode", ElementType.FIELD) .constraint(new SizeDef().max(32)) .constraint(new NotNullDef()) .constraint(new PatternDef().regexp("[0-9a-zA-Z_]+").message( "{msg.error.validation.dictCode.pattern}")) .property("dictName", ElementType.FIELD) .constraint(new SizeDef().max(32)).constraint(new NotNullDef()); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Test public void testSimpleValidationWithCustomProvider() throws Exception { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.setProviderClass(HibernateValidator.class); validator.afterPropertiesSet(); ValidPerson person = new ValidPerson(); Set<ConstraintViolation<ValidPerson>> result = validator.validate(person); assertEquals(2, result.size()); for (ConstraintViolation<ValidPerson> cv : result) { String path = cv.getPropertyPath().toString(); if ("name".equals(path) || "address.street".equals(path)) { assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull); } else { fail("Invalid constraint violation with path '" + path + "'"); } } }
@Override public void configure(final Env env, final Config config, final Binder binder) { HibernateValidatorConfiguration configuration = byProvider(HibernateValidator.class) .configure(); if (config.hasPath("hibernate.validator")) { config.getConfig("hibernate.validator").root().forEach((k, v) -> { configuration.addProperty("hibernate.validator." + k, v.unwrapped().toString()); }); } if (configurer != null) { configurer.accept(configuration, config); } binder.bind(HibernateValidatorConfiguration.class).toInstance(configuration); binder.bind(Validator.class).toProvider(HbvFactory.class).asEagerSingleton(); env.lifeCycle(HbvFactory.class); Multibinder.newSetBinder(binder, Parser.class).addBinding() .toInstance(new HbvParser(predicate)); }
@Before public void setUp(){ currentUser.setProxyUser("admin", "test", new String[] {"VIEW_ENTITY", "CREATE_ENTITY"}); assertNotNull(headOfHouseholdService); localValidatorFactory = new LocalValidatorFactoryBean(); localValidatorFactory.setProviderClass(HibernateValidator.class); localValidatorFactory.afterPropertiesSet(); oldHoh = genericDao.findByProperty(Individual.class, "extId", "NBAS1I"); newHoh = genericDao.findByProperty(Individual.class, "extId", "BJOH1J"); visit = genericDao.findByProperty(Visit.class, "extId", "VLOCMBI11J"); fieldWorker = genericDao.findByProperty(FieldWorker.class, "extId", "FWEK1D"); socialGroup = genericDao.findByProperty(SocialGroup.class, "extId", "MBI1", false); assertNotNull(oldHoh); assertNotNull(newHoh); assertNotNull(visit); assertNotNull(fieldWorker); assertNotNull(socialGroup); }
public IntegrationTest() { final DefaultConstraintMapping mapping = new DefaultConstraintMapping(); mapping.constraintDefinition(UniqueProperty.class).validatedBy(UniqnenessValidator.class); final ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class).configure() .addMapping(mapping).buildValidatorFactory(); validator = validatorFactory.getValidator(); }
@Before public void before() { validator.setProviderClass(HibernateValidator.class); validator.afterPropertiesSet(); sourceCatalog.setName("name"); }
@Before public void before() { validator.setProviderClass(HibernateValidator.class); validator.afterPropertiesSet(); tunnel.setKnownHosts("knownHosts"); tunnel.setPrivateKeys("privateKey"); tunnel.setRoute("hostA -> hostB"); }
@Before public void before() { validator.setProviderClass(HibernateValidator.class); validator.afterPropertiesSet(); sourceTable.setDatabaseName("databaseName"); sourceTable.setTableName("tableName"); }
@Before public void before() { validator.setProviderClass(HibernateValidator.class); validator.afterPropertiesSet(); replicaCatalog.setName("name"); replicaCatalog.setHiveMetastoreUris("hiveMetastoreUris"); }
/** * 线程安全的 * * @return {@link Validator} */ public static Validator getHibernateValidator () { return Validation.byProvider( HibernateValidator.class ) .configure() .buildValidatorFactory() .getValidator(); }
@Before public void before() { validator.setProviderClass(HibernateValidator.class); validator.afterPropertiesSet(); waggleDanceConfiguration.setPort(123); waggleDanceConfiguration.setVerbose(true); waggleDanceConfiguration.setDisconnectConnectionDelay(15); waggleDanceConfiguration.setDisconnectTimeUnit(TimeUnit.SECONDS); waggleDanceConfiguration .setConfigurationProperties(ImmutableMap.<String, String> builder().put("prop1", "val1").build()); }
@Before public void before() { validator.setProviderClass(HibernateValidator.class); validator.afterPropertiesSet(); metaStore.setRemoteMetaStoreUris("uri"); metaStore.setName("name"); }
public InputValidation() { this.configuration = Validation.byProvider(HibernateValidator.class).configure(); this.constraints = configuration.createConstraintMapping(); configureConstraints(); this.validator = this.configuration.addMapping(constraints) .buildValidatorFactory() .getValidator(); }
@Override public void initialize(ValidationContext context) { context.setValidatorDelegate(this); HibernateValidatorConfiguration configure = javax.validation.Validation.byProvider(HibernateValidator.class).configure(); if (context instanceof ValidationContext.Accessor) { configure.failFast(((ValidationContext.Accessor) context).isFailFast()); } ValidatorFactory factory = configure.buildValidatorFactory(); validator = factory.getValidator(); }
@Before public void setup() { job = new Job(); localValidatorFactory = new LocalValidatorFactoryBean(); localValidatorFactory.setProviderClass(HibernateValidator.class); localValidatorFactory.afterPropertiesSet(); }
protected HibernateValidatorConfiguration getValidatorFactoryConfiguration(Locale locale) { @SuppressWarnings("UnnecessaryLocalVariable") HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class) .configure() .timeProvider(new CubaValidationTimeProvider(timeSource)) .traversableResolver(new CubaValidationTraversableResolver(metadata, entityStates)) .messageInterpolator(new CubaValidationMessagesInterpolator(messages, locale)); return configuration; }
/** * Parses the given configuration file and returns a configuration object. * * @param configurationFileName The name of the configuration file. * @return A configuration object. * @throws IOException The IO error that contains detail information. * @throws ConfigurationException The configuration error that contains detail information. */ public static ApiConfiguration parse(String configurationFileName) throws IOException, ConfigurationException { if (StringUtils.isBlank(configurationFileName)) { throw new IllegalArgumentException("Configuration file cannot be blank"); } ObjectMapper objectMapper = null; if (configurationFileName.endsWith("yml") || configurationFileName.endsWith("yaml")) { objectMapper = Jackson.newObjectMapper(new YAMLFactory()); } else if (configurationFileName.endsWith("json")) { objectMapper = Jackson.newObjectMapper(new JsonFactory()); } else { throw new IllegalArgumentException("Unrecognized configuration file type"); } ValidatorFactory validatorFactory = Validation .byProvider(HibernateValidator.class) .configure() .addValidatedValueHandler(new OptionalValidatedValueUnwrapper()) .buildValidatorFactory(); final ConfigurationFactory<ApiConfiguration> configurationFactory = new DefaultConfigurationFactoryFactory<ApiConfiguration>() .create(ApiConfiguration.class, validatorFactory.getValidator(), objectMapper, "dw"); final File file = new File(configurationFileName); if (!file.exists()) { throw new FileNotFoundException("Configuration file " + configurationFileName + " not found"); } return configurationFactory.build(file); }
@Provides @Singleton ValidatorFactory validatorFactory(ConstraintValidatorFactory constraintValidatorFactory) { ClassLoader tccl = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(HibernateValidator.class.getClassLoader()); ValidatorFactory factory = Validation.byDefaultProvider().configure() .constraintValidatorFactory(constraintValidatorFactory) .parameterNameProvider(new AopAwareParanamerParameterNameProvider()) .traversableResolver(new AlwaysTraversableResolver()) .buildValidatorFactory(); // FIXME: Install custom MessageInterpolator that can properly find/merge ValidationMessages.properties for bundles // exercise interpolator to preload elements (avoids issues later when TCCL might be different) factory.getValidator().validate(new Object() { // minimal token message @NotNull(message = "${0}") String empty; }); return factory; } finally { Thread.currentThread().setContextClassLoader(tccl); } }
@Before public void setUp() throws Exception { validatorFactoryBean = new LocalValidatorFactoryBean(); validatorFactoryBean.setProviderClass(HibernateValidator.class); validatorFactoryBean.afterPropertiesSet(); }
@Before public void before() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.refresh(); ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); beanFactory.registerSingleton(PrimaryRepository.class.getCanonicalName(), primaryRepository); validator.setApplicationContext(context); validator.setProviderClass(HibernateValidator.class); validator.afterPropertiesSet(); }