Package javax.validation

Examples of javax.validation.Validator


    private SessionContext sessionContext;

    @AroundInvoke public Object aroundInvoke(final InvocationContext ejbContext) throws Exception {
        Object validatorObject = null;
        Validator validator = null;
        try {
            validator = (Validator) new InitialContext().lookup("java:comp/Validator");
            sessionContext = (SessionContext) new InitialContext().lookup("java:comp/EJBContext"); // injection doesn't work
        } catch (NamingException ne) {
            // no-op
        }

        // get bval annotation informations
        Class<?> bvalClazzToValidate = ejbContext.getTarget().getClass();
        if (sessionContext != null && ejbContext.getTarget().getClass().getInterfaces().length > 0) {
            bvalClazzToValidate = sessionContext.getInvokedBusinessInterface();
        }
        Method method = ejbContext.getMethod();
        if (!bvalClazzToValidate.equals(ejbContext.getTarget().getClass())) {
            method = bvalClazzToValidate.getMethod(method.getName(), method.getParameterTypes());
        }

        Set<?> violations = Collections.emptySet();
        if (APACHE_BVAL_METHOD_CLASS != null && validator != null) {
            validatorObject = validator.unwrap(APACHE_BVAL_METHOD_CLASS);
            violations = call(Set.class, validatorObject, "validateParameters",
                new Object[]{
                    bvalClazzToValidate, method, ejbContext.getParameters(), new Class[0]
                },
                new Class<?>[]{
                    Class.class, Method.class, Object[].class, Class[].class
                });
        } else if (HIBERNATE_METHOD_CLASS != null && validator != null) {
            validatorObject = validator.unwrap(HIBERNATE_METHOD_CLASS);
            violations = call(Set.class, validatorObject, "validateAllParameters",
                new Object[]{
                    ejbContext.getTarget(), ejbContext.getMethod(), ejbContext.getParameters(), new Class[0]
                },
                new Class<?>[]{
View Full Code Here


    public static Test suite() {
        return new TestSuite(MethodValidatorImplTest.class);
    }

    public void testUnwrap() {
        Validator v = getValidator();
        ClassValidator cv = v.unwrap(ClassValidator.class);
        assertTrue(v == cv);
        assertTrue(v == v.unwrap(Validator.class));
        MethodValidatorImpl mvi = v.unwrap(MethodValidatorImpl.class);
        assertNotNull(mvi);
        MethodValidator mv = v.unwrap(MethodValidator.class);
        assertNotNull(mv);
        assertTrue(mv == mv.unwrap(MethodValidatorImpl.class));
        assertTrue(mv == mv.unwrap(ClassValidator.class));
    }
View Full Code Here

  @DecimalMax("922392239223.09")
  public double dmax;


  public void testDecimalMinValue() {
    Validator v = Validation.buildDefaultValidatorFactory().getValidator();

    this.dmin = 922392239223.05;
    this.dmax = 922392239223.08;

    Set<ConstraintViolation<DecimalMinMaxValidatorsTest>> res = v.validate(this);
    assertFalse("Min validation failed", res.isEmpty());
  }
View Full Code Here

    Set<ConstraintViolation<DecimalMinMaxValidatorsTest>> res = v.validate(this);
    assertFalse("Min validation failed", res.isEmpty());
  }

  public void testDecimalMaxValue() {
    Validator v = Validation.buildDefaultValidatorFactory().getValidator();

    this.dmin = Double.MAX_VALUE;
    this.dmax = 922392239223.1;

    Set<ConstraintViolation<DecimalMinMaxValidatorsTest>> res = v.validate(this);
    assertFalse("Max validation failed", res.isEmpty());
  }
View Full Code Here

  @Max(value = 9223372036854775806l)
  public long max;


  public void testMinBoundaryValue() {
    Validator v = Validation.buildDefaultValidatorFactory().getValidator();

    this.min = 9223372036854775806l;
    this.max = 0l;

    // Current min value is smaller, should fail, but it doesn't
    Set<ConstraintViolation<MinMaxValidatorsForNumberTest>> res = v.validate(this);
    assertFalse("Min validation failed", res.isEmpty());
  }
View Full Code Here

    Set<ConstraintViolation<MinMaxValidatorsForNumberTest>> res = v.validate(this);
    assertFalse("Min validation failed", res.isEmpty());
  }

  public void testMaxBoundaryValue() {
    Validator v = Validation.buildDefaultValidatorFactory().getValidator();

    this.min = Long.MAX_VALUE;
    this.max = 9223372036854775807l;

    // Current max value is bigger, should fail, but it doesn't
    Set<ConstraintViolation<MinMaxValidatorsForNumberTest>> res = v.validate(this);
    assertFalse("Max validation failed", res.isEmpty());
  }
View Full Code Here

    public EmailValidatorTest(String name) {
        super(name);
    }

    public void testEmail() {
        Validator validator = ApacheValidatorFactory.getDefault().getValidator();
        Customer customer = new Customer();
        customer.setCustomerId("id-1");
        customer.setFirstName("Mary");
        customer.setLastName("Do");
        customer.setPassword("12345");

        Assert.assertEquals(0, validator.validate(customer).size());

        customer.setEmailAddress("some@invalid@address");
        Assert.assertEquals(1, validator.validate(customer).size());

        customer.setEmailAddress("some.valid-012345@address_at-test.org");
        Assert.assertEquals(0, validator.validate(customer).size());
    }
View Full Code Here

    interpolator.setLocale(Locale.ENGLISH);
  }

  public void testCreateResolver() {

    final Validator gvalidator = getValidator();

    assertTrue(!gvalidator.getConstraintsForClass(PreferredGuest.class)
        .getConstraintsForProperty("guestCreditCardNumber")
        .getConstraintDescriptors().isEmpty());

    MessageInterpolator.Context ctx = new MessageInterpolator.Context() {

      public ConstraintDescriptor<?> getConstraintDescriptor() {
        return (ConstraintDescriptor<?>) gvalidator
            .getConstraintsForClass(PreferredGuest.class).
                getConstraintsForProperty("guestCreditCardNumber")
            .getConstraintDescriptors().toArray()[0];
      }

      public Object getValidatedValue() {
        return "12345678";
      }
    };
    String msg = interpolator.interpolate("{validator.creditcard}", ctx);
    Assert.assertEquals("credit card is not valid", msg);

    ctx = new MessageInterpolator.Context() {
      public ConstraintDescriptor<?> getConstraintDescriptor() {
        return (ConstraintDescriptor) gvalidator
            .getConstraintsForClass(Author.class).
                getConstraintsForProperty("lastName")
            .getConstraintDescriptors().toArray()[0];
      }
View Full Code Here

     * Checks that strings containing special characters are correctly
     * substituted when interpolating.
     */
    public void testReplacementWithSpecialChars() {
       
        final Validator validator = getValidator();
        MessageInterpolator.Context ctx;

        // Try to interpolate an annotation attribute containing $
        ctx = new MessageInterpolator.Context() {

            public ConstraintDescriptor<?> getConstraintDescriptor() {
                return (ConstraintDescriptor<?>) validator
                        .getConstraintsForClass(Person.class)
                        .getConstraintsForProperty("idNumber")
                        .getConstraintDescriptors().toArray()[0];
            }

            public Object getValidatedValue() {
                return "12345678";
            }
        };

        String result = this.interpolator.interpolate("Id number should match {regexp}", ctx);
        Assert.assertEquals("Incorrect message interpolation when $ is in an attribute", "Id number should match ....$", result);
       
        // Try to interpolate an annotation attribute containing \
        ctx = new MessageInterpolator.Context() {

            public ConstraintDescriptor<?> getConstraintDescriptor() {
                return (ConstraintDescriptor<?>) validator
                        .getConstraintsForClass(Person.class)
                        .getConstraintsForProperty("otherId")
                        .getConstraintDescriptors().toArray()[0];
            }

View Full Code Here

    return ApacheValidatorFactory.getDefault().getValidator();
  }

  public void testPropertyAccessOnNonPublicClass()
      throws Exception {
    Validator validator = getValidator();
    Car car = new Car("USd-298");
    assertEquals(car.getLicensePlateNumber(), PropertyAccess.getProperty(car, "licensePlateNumber"));

    Set<ConstraintViolation<Car>> violations = validator.validateProperty(
        car, "licensePlateNumber", First.class, org.apache.bval.jsr303.example.Second.class
    );
    assertCorrectNumberOfViolations(violations, 1);

    car.setLicensePlateNumber("USD-298");
    violations = validator.validateProperty(
        car, "licensePlateNumber", First.class, org.apache.bval.jsr303.example.Second.class
    );
    assertCorrectNumberOfViolations(violations, 0);
  }
View Full Code Here

TOP

Related Classes of javax.validation.Validator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.