/*
 * 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.equalTo;
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 AlphaNumericTest {

    private AlphaNumericBean alphaNumBean;
    private Validator validator;

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

    @Test
    public void defaultSettingsShouldValidateAlphabeticString() throws Exception {
        alphaNumBean.alphaNum = "abcd";

        Set<ConstraintViolation<AlphaNumericBean>> violations = validate("alphaNum");

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

    @Test
    public void defaultSettingsShouldValidateAlphabeticNumericString() throws Exception {
        alphaNumBean.alphaNum = "abcd1234";

        Set<ConstraintViolation<AlphaNumericBean>> violations = validate("alphaNum");

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

    @Test
    public void defaultSettingsShouldNotValidateNonAlphabeticString() throws Exception {
        alphaNumBean.alphaNum = "abcd?";

        Set<ConstraintViolation<AlphaNumericBean>> violations = validate("alphaNum");

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

    @Test
    public void defaultSettingsShouldNotValidateAlphabeticStringWithSpaces() throws Exception {
        alphaNumBean.alphaNum = "ab cd";

        Set<ConstraintViolation<AlphaNumericBean>> violations = validate("alphaNum");

        assertThat(violations, hasSize(1));
    }

    @Test
    public void defaultSettingsShouldNotValidateAlphabeticNumericStringWithSpaces() throws Exception {
        alphaNumBean.alphaNum = "ab cd 1234";

        Set<ConstraintViolation<AlphaNumericBean>> violations = validate("alphaNum");

        assertThat(violations, hasSize(1));
    }

    @Test
    public void allowSpacesSettingsShouldValidateAlphabeticString() throws Exception {
        alphaNumBean.alphaNumSpace = "abcd";

        Set<ConstraintViolation<AlphaNumericBean>> violations = validate("alphaNumSpace");

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

    @Test
    public void allowSpacesSettingsShouldValidateAlphabeticNumericString() throws Exception {
        alphaNumBean.alphaNumSpace = "abcd1234";

        Set<ConstraintViolation<AlphaNumericBean>> violations = validate("alphaNumSpace");

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

    @Test
    public void allowSpacesSettingsShouldNotValidateNonAlphabeticString() throws Exception {
        alphaNumBean.alphaNumSpace = "abcd123?";

        Set<ConstraintViolation<AlphaNumericBean>> violations = validate("alphaNumSpace");

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

    @Test
    public void allowSpacesSettingsShouldValidateAlphabeticStringWithSpaces() throws Exception {
        alphaNumBean.alphaNumSpace = "ab cd";

        Set<ConstraintViolation<AlphaNumericBean>> violations = validate("alphaNumSpace");

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

    @Test
    public void allowSpacesSettingsShouldValidateAlphabeticNumericStringWithSpaces() throws Exception {
        alphaNumBean.alphaNumSpace = "ab cd 1234";

        Set<ConstraintViolation<AlphaNumericBean>> violations = validate("alphaNumSpace");

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

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

    private static class AlphaNumericBean {
        @AlphaNumeric
        private String alphaNum;

        @AlphaNumeric(allowSpaces = true)
        private String alphaNumSpace;
    }
}

@@ -980,99 +980,68 @@
 java
-x.validation.Validation;%0Aimport javax.validation.Validator;%0Aimport javax.
+.util.Set;%0A%0Aimport com.github.britter.bean
 validat
-ion
+ors
 .Validat
 orFa
@@ -1040,41 +1040,19 @@
 idat
-orFactory;%0Aimport java.util.Set;%0A
+ionWrapper;
 %0Aimp
@@ -1192,18 +1192,44 @@
  Validat
-or
+ionWrapper%3CAlphaNumericBean%3E
  validat
@@ -1283,166 +1283,103 @@
     
-V
 al
-idatorFactory factory = Validation.buildDefaultValidatorFactory();%0A        validator = factory.getValidator();%0A        alphaNumBean = new A
+phaNumBean = new AlphaNumericBean();%0A        validator = new ValidationWrapper%3C%3E(a
 lphaNum
-eric
 Bean
-(
 );%0A 
@@ -1574,32 +1574,42 @@
 %3E%3E violations = 
+validator.
 validate(%22alphaN
@@ -1868,32 +1868,42 @@
 %3E%3E violations = 
+validator.
 validate(%22alphaN
@@ -2158,32 +2158,42 @@
 %3E%3E violations = 
+validator.
 validate(%22alphaN
@@ -2615,32 +2615,42 @@
 %3E%3E violations = 
+validator.
 validate(%22alphaN
@@ -2923,32 +2923,42 @@
 %3E%3E violations = 
+validator.
 validate(%22alphaN
@@ -3214,32 +3214,42 @@
 %3E%3E violations = 
+validator.
 validate(%22alphaN
@@ -3522,32 +3522,42 @@
 %3E%3E violations = 
+validator.
 validate(%22alphaN
@@ -3829,32 +3829,42 @@
 %3E%3E violations = 
+validator.
 validate(%22alphaN
@@ -4297,32 +4297,42 @@
 %3E%3E violations = 
+validator.
 validate(%22alphaN
@@ -4621,24 +4621,34 @@
 iolations = 
+validator.
 validate(%22al
@@ -4719,165 +4719,8 @@
  %7D%0A%0A
-    private Set%3CConstraintViolation%3CAlphaNumericBean%3E%3E validate(String property) %7B%0A        return validator.validateProperty(alphaNumBean, property);%0A    %7D%0A%0A
     

Use ValidationWrapper in AlphaNumTest