当前位置: 首页>>代码示例>>Java>>正文


Java Validator类代码示例

本文整理汇总了Java中net.sf.oval.Validator的典型用法代码示例。如果您正苦于以下问题:Java Validator类的具体用法?Java Validator怎么用?Java Validator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Validator类属于net.sf.oval包,在下文中一共展示了Validator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testCheckWith1

import net.sf.oval.Validator; //导入依赖的package包/类
public void testCheckWith1() {
    final Validator validator = new Validator();

    final TestEntity1 t = new TestEntity1();

    List<ConstraintViolation> violations;

    violations = validator.validate(t);
    assertEquals(1, violations.size());

    t.name = "";
    violations = validator.validate(t);
    assertEquals(1, violations.size());

    t.name = "12345";
    violations = validator.validate(t);
    assertEquals(1, violations.size());

    t.name = "1234";
    violations = validator.validate(t);
    assertEquals(0, violations.size());
}
 
开发者ID:sebthom,项目名称:oval,代码行数:23,代码来源:CheckWithListConstraintTest.java

示例2: testCheckWith2

import net.sf.oval.Validator; //导入依赖的package包/类
public void testCheckWith2() {
    final Validator validator = new Validator();

    final TestEntity2 t = new TestEntity2();

    List<ConstraintViolation> violations;

    violations = validator.validate(t);
    assertEquals(1, violations.size());

    t.name = "";
    violations = validator.validate(t);
    assertEquals(1, violations.size());

    t.name = "12345";
    violations = validator.validate(t);
    assertEquals(1, violations.size());

    t.name = "1234";
    violations = validator.validate(t);
    assertEquals(0, violations.size());
}
 
开发者ID:sebthom,项目名称:oval,代码行数:23,代码来源:CheckWithListConstraintTest.java

示例3: testAddConstraintToField

import net.sf.oval.Validator; //导入依赖的package包/类
/**
 * programmatically add a NotNull constraint to the name field
 */
public void testAddConstraintToField() throws Exception {
    final Validator validator = new Validator();

    final TestEntity entity = new TestEntity(null);
    assertTrue(validator.validate(entity).size() == 0);

    final Field field = TestEntity.class.getDeclaredField("name");
    final NotNullCheck notNullCheck = new NotNullCheck();
    notNullCheck.setMessage("NOT_NULL");

    validator.addChecks(field, notNullCheck);

    final List<ConstraintViolation> violations = validator.validate(entity);
    assertTrue(violations.size() == 1);
    assertTrue(violations.get(0).getMessage().equals("NOT_NULL"));
}
 
开发者ID:sebthom,项目名称:oval,代码行数:20,代码来源:AddingChecksTest.java

示例4: validate

import net.sf.oval.Validator; //导入依赖的package包/类
default void validate(Validator ovalValidator) {
    if (ovalValidator != null) {
        List<ConstraintViolation> constraintViolations = ovalValidator.validate(this);
        if (constraintViolations.size() > 0) {
            final Map<String, List<String>> errorMap = new HashMap<>();
            final String className = this.getClass().getName() + ".";

            constraintViolations
                    .forEach(constraintViolation ->
                            errorMap.computeIfAbsent(constraintViolation.getCheckDeclaringContext().toString().replace(className, ""), k -> new ArrayList<>())
                                    .add(constraintViolation.getMessage().replace(className, "")));

            throw new ExceptionAdapter(new CustomOvalException(errorMap));
        }
    }
}
 
开发者ID:Pivopil,项目名称:spring-boot-oauth2-rest-service-password-encoding,代码行数:17,代码来源:EntityBuilder.java

示例5: isSatisfied

import net.sf.oval.Validator; //导入依赖的package包/类
public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context, final Validator validator)
        throws ReflectionException {
    if (valueToValidate == null && ignoreIfNull)
        return true;

    final Class<?> clazz = validatedObject.getClass();
    Method method;
    synchronized (validationMethodsByClass) {
        method = validationMethodsByClass.get(clazz);
        if (method == null) {
            method = ReflectionUtils.getMethodRecursive(clazz, methodName, parameterType);
            validationMethodsByClass.put(clazz, method);
        }
    }
    if (method == null)
        throw new InvalidConfigurationException("Method " + clazz.getName() + "." + methodName + "(" + parameterType + ") not found. Is [" + parameterType
                + "] the correct value for [@ValidateWithMethod.parameterType]?");
    //explicit cast to workaround: type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds boolean,java.lang.Object
    return (Boolean) ReflectionUtils.invokeMethod(method, validatedObject, valueToValidate);
}
 
开发者ID:sebthom,项目名称:oval,代码行数:21,代码来源:ValidateWithMethodCheck.java

示例6: isSatisfied

import net.sf.oval.Validator; //导入依赖的package包/类
public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context, final Validator validator) {
    if (valueToValidate == null)
        return true;

    if (valueToValidate instanceof Number) {
        if (valueToValidate instanceof Float || valueToValidate instanceof Double)
            return ((Number) valueToValidate).doubleValue() >= 0;
        if (valueToValidate instanceof BigDecimal)
            return ((BigDecimal) valueToValidate).compareTo(ZERO) >= 0;
        return ((Number) valueToValidate).longValue() >= 0;
    }

    final String stringValue = valueToValidate.toString();
    try {
        return Double.parseDouble(stringValue) >= 0;
    } catch (final NumberFormatException e) {
        return false;
    }
}
 
开发者ID:sebthom,项目名称:oval,代码行数:20,代码来源:NotNegativeCheck.java

示例7: renderMessage

import net.sf.oval.Validator; //导入依赖的package包/类
public static String renderMessage(final String messageKey, final Map<String, ?> messageValues) {
    String message = Validator.getMessageResolver().getMessage(messageKey);
    if (message == null)
        message = messageKey;

    final MessageValueFormatter formatter = Validator.getMessageValueFormatter();

    // if there are no place holders in the message simply return it
    if (message.indexOf('{') == -1)
        return message;

    if (messageValues != null && messageValues.size() > 0)
        for (final Entry<String, ?> entry : messageValues.entrySet())
        message = StringUtils.replaceAll(message, "{" + entry.getKey() + "}", formatter.format(entry.getValue()));
    return message;
}
 
开发者ID:sebthom,项目名称:oval,代码行数:17,代码来源:MessageRenderer.java

示例8: testCollectionValues

import net.sf.oval.Validator; //导入依赖的package包/类
public void testCollectionValues() {
    final Validator validator = new Validator(new CustomAnnotationConfigurer());

    final Person p = new Person();
    p.firstName = "John";
    p.lastName = "Doe";
    p.otherAddresses1 = new ArrayList<Address>();
    p.otherAddresses2 = new HashSet<Address>();
    p.otherAddresses3 = new HashSet<Address>();

    final Address a = new Address();
    a.street = "The Street";
    a.city = "The City";
    a.zipCode = null;
    assertEquals(1, validator.validate(a).size());

    p.otherAddresses1.add(a);
    assertEquals(1, validator.validate(p).size());

    p.otherAddresses1.remove(a);
    p.otherAddresses2.add(a);
    assertEquals(0, validator.validate(p).size());

    p.otherAddresses3.add(a);
    assertEquals(1, validator.validate(p).size());
}
 
开发者ID:sebthom,项目名称:oval,代码行数:27,代码来源:CustomAssertValidTest.java

示例9: testConcurrency

import net.sf.oval.Validator; //导入依赖的package包/类
public void testConcurrency() throws InterruptedException {
    final Validator validator = new Validator();

    final TestEntity1 sharedEntity = new TestEntity1();

    final boolean[] failed = { false };

    final Thread thread1 = new Thread(new TestRunner(validator, sharedEntity, failed));
    final Thread thread2 = new Thread(new TestRunner(validator, sharedEntity, failed));

    thread1.start();
    thread2.start();
    thread1.join();
    thread2.join();
    assertFalse(failed[0]);
}
 
开发者ID:sebthom,项目名称:oval,代码行数:17,代码来源:ConcurrencyTest.java

示例10: testMethodReturnValueConstraintValidation

import net.sf.oval.Validator; //导入依赖的package包/类
public void testMethodReturnValueConstraintValidation() {
    final Validator validator = new Validator();

    {
        final TestEntity t = new TestEntity();

        List<ConstraintViolation> violations = validator.validate(t);
        assertTrue(violations.size() == 1);
        assertTrue(violations.get(0).getMessage().equals("NOT_NULL"));

        t.name = "wqerwqer";
        violations = validator.validate(t);
        assertTrue(violations.size() == 1);
        assertTrue(violations.get(0).getMessage().equals("LENGTH"));
    }
}
 
开发者ID:sebthom,项目名称:oval,代码行数:17,代码来源:InvariantMethodConstraintsValidationTest.java

示例11: testCheckByMethod

import net.sf.oval.Validator; //导入依赖的package包/类
public void testCheckByMethod() {
    final Validator validator = new Validator();

    final TestEntity t = new TestEntity();

    List<ConstraintViolation> violations;

    violations = validator.validate(t);
    assertTrue(violations.size() == 1);

    t.name = "";
    violations = validator.validate(t);
    assertTrue(violations.size() == 1);

    t.name = "12345";
    violations = validator.validate(t);
    assertTrue(violations.size() == 1);

    t.name = "1234";
    violations = validator.validate(t);
    assertTrue(violations.size() == 0);
}
 
开发者ID:sebthom,项目名称:oval,代码行数:23,代码来源:ValidateWithMethodConstraintTest.java

示例12: testValidatorAssert

import net.sf.oval.Validator; //导入依赖的package包/类
public void testValidatorAssert() throws Exception {
    final TestEntity e = new TestEntity();
    final Validator v = new Validator();
    try {
        v.assertValid(e);
        fail();
    } catch (final ConstraintsViolatedException ex) {
        final ConstraintViolation[] violations = ex.getConstraintViolations();
        assertEquals(2, violations.length);
        assertEquals("NOT_NULL", violations[0].getMessage());
        assertEquals("NOT_NULL", violations[1].getMessage());
    }

    e.name = "asdads";
    e.value = 5;
    v.assertValid(e);
}
 
开发者ID:sebthom,项目名称:oval,代码行数:18,代码来源:ValidatorAssertValidTest.java

示例13: testValidatorAssertField

import net.sf.oval.Validator; //导入依赖的package包/类
public void testValidatorAssertField() throws Exception {
    final Field f = TestEntity.class.getField("name");

    final TestEntity e = new TestEntity();
    final Validator v = new Validator();
    try {
        v.assertValidFieldValue(e, f, null);
        fail();
    } catch (final ConstraintsViolatedException ex) {
        final ConstraintViolation[] violations = ex.getConstraintViolations();
        assertEquals(1, violations.length);
        assertEquals("NOT_NULL", violations[0].getMessage());
    }

    v.assertValidFieldValue(e, f, "test");
}
 
开发者ID:sebthom,项目名称:oval,代码行数:17,代码来源:ValidatorAssertValidTest.java

示例14: testSerialization

import net.sf.oval.Validator; //导入依赖的package包/类
public void testSerialization() throws IOException, ClassNotFoundException {
    final Validator validator = new Validator();

    final Person p = new Person();
    p.firstName = "123456";
    List<ConstraintViolation> violations = validator.validate(p);
    assertTrue(violations.size() == 1);

    // serialize the violations
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(violations);
    oos.flush();
    byte[] bytes = bos.toByteArray();

    // deserialize the violations
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bis);
    assertTrue(ois.readObject() instanceof List);
}
 
开发者ID:sebthom,项目名称:oval,代码行数:21,代码来源:SerializationTest.java

示例15: testNonstaticValidation

import net.sf.oval.Validator; //导入依赖的package包/类
public void testNonstaticValidation() {
    final Validator validator = new Validator();

    TestEntity.staticA = null;
    TestEntity.staticB = null;

    // test that only non static fields are validated
    final TestEntity t = new TestEntity();
    List<ConstraintViolation> violations = validator.validate(t);
    assertTrue(violations.size() == 2);

    t.nonstaticA = "";
    t.nonstaticB = "";

    violations = validator.validate(t);
    assertTrue(violations.size() == 0);
}
 
开发者ID:sebthom,项目名称:oval,代码行数:18,代码来源:StaticFieldsAndGettersTest.java


注:本文中的net.sf.oval.Validator类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。