/*
 * Copyright 2015 Benedikt Ritter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.britter.beanvalidators.strings;

import static com.google.common.collect.Iterables.getLast;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;

public class NumericTest {

    private Validator validator;
    private NumericBean numericBean;

    @Before
    public void setUp() {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        validator = factory.getValidator();
        numericBean = new NumericBean();
    }

    @Test
    public void shouldValidateNumericString() throws Exception {
        numericBean.numeric = "123456";

        Set<ConstraintViolation<NumericBean>> violations = validate("numeric");

        assertThat(violations, is(empty()));
    }

    @Test
    public void shouldValidateNullString() throws Exception {
        numericBean.numeric = null;

        Set<ConstraintViolation<NumericBean>> violations = validate("numeric");

        assertThat(violations, is(empty()));
    }

    @Test
    public void shouldValidateBlankString() throws Exception {
        numericBean.numeric = " ";

        Set<ConstraintViolation<NumericBean>> violations = validate("numeric");

        assertThat(violations, is(empty()));
    }

    @Test
    public void shouldNotValidateNonNumericString() throws Exception {
        numericBean.numeric = "abc";

        Set<ConstraintViolation<NumericBean>> violations = validate("numeric");

        assertThat(violations, hasSize(1));
        ConstraintViolation<NumericBean> violation = getLast(violations);
        assertThat(violation.getMessage(), is("must be numeric"));
    }

    private Set<ConstraintViolation<NumericBean>> validate(String property) {
        return validator.validateProperty(numericBean, property);
    }

    private static final class NumericBean {
        @Numeric
        private String numeric;
    }
}

@@ -935,99 +935,68 @@
 java
-x.validation.Validation;%0Aimport javax.validation.Validator;%0Aimport javax.
+.util.Set;%0A%0Aimport com.github.britter.bean
 validat
-ion
+ors
 .Validat
 orFa
@@ -995,41 +995,19 @@
 idat
-orFactory;%0Aimport java.util.Set;%0A
+ionWrapper;
 %0Aimp
@@ -1099,18 +1099,39 @@
  Validat
-or
+ionWrapper%3CNumericBean%3E
  validat
@@ -1222,149 +1222,86 @@
     
-ValidatorFactory factory = Validation.buildDefaultValidatorFactory();%0A        validator = factory.getValidator();%0A        numericBean = new N
+numericBean = new NumericBean();%0A        validator = new ValidationWrapper%3C%3E(n
 umer
@@ -1302,25 +1302,24 @@
 (numericBean
-(
 );%0A    %7D%0A%0A  
@@ -1483,32 +1483,42 @@
 %3E%3E violations = 
+validator.
 validate(%22numeri
@@ -1735,32 +1735,42 @@
 %3E%3E violations = 
+validator.
 validate(%22numeri
@@ -1987,32 +1987,42 @@
 %3E%3E violations = 
+validator.
 validate(%22numeri
@@ -2253,24 +2253,34 @@
 iolations = 
+validator.
 validate(%22nu
@@ -2485,159 +2485,8 @@
  %7D%0A%0A
-    private Set%3CConstraintViolation%3CNumericBean%3E%3E validate(String property) %7B%0A        return validator.validateProperty(numericBean, property);%0A    %7D%0A%0A
     

Use ValidationWrapper in NumericTest