/*
 * 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.net;

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 DomainTest {

    private Validator validator;
    private DomainBean domainBean;

    @Before
    public void setUp() throws Exception {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        validator = factory.getValidator();
        domainBean = new DomainBean();
    }

    @Test
    public void shouldValidateNullString() throws Exception {
        domainBean.domain = null;

        Set<ConstraintViolation<DomainBean>> violations = validate("domain");

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

    @Test
    public void shouldValidateBlankString() throws Exception {
        domainBean.domain = " ";

        Set<ConstraintViolation<DomainBean>> violations = validate("domain");

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

    @Test
    public void shouldValidateDomain() throws Exception {
        domainBean.domain = "www.example.com";

        Set<ConstraintViolation<DomainBean>> violations = validate("domain");

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

    @Test
    public void shouldNotValidateRandomString() throws Exception {
        domainBean.domain = "abcd";

        Set<ConstraintViolation<DomainBean>> violations = validate("domain");

        assertThat(violations, hasSize(1));
        ConstraintViolation<DomainBean> violation = getLast(violations);
        assertThat(violation.getMessage(), is(equalTo("must be a domain")));
    }

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

    private static final class DomainBean {
        @Domain
        private String domain;
    }
}

@@ -976,99 +976,68 @@
 java
-x.validation.Validation;%0Aimport javax.validation.Validator;%0Aimport javax.
+.util.Set;%0A%0Aimport com.github.britter.bean
 validat
-ion
+ors
 .Validat
 orFa
@@ -1036,41 +1036,19 @@
 idat
-orFactory;%0Aimport java.util.Set;%0A
+ionWrapper;
 %0Aimp
@@ -1139,18 +1139,38 @@
  Validat
-or
+ionWrapper%3CDomainBean%3E
  validat
@@ -1276,148 +1276,84 @@
     
-ValidatorFactory factory = Validation.buildDefaultValidatorFactory();%0A        validator = factory.getValidator();%0A        domainBean = new D
+domainBean = new DomainBean();%0A        validator = new ValidationWrapper%3C%3E(d
 omai
@@ -1353,25 +1353,24 @@
 %3E(domainBean
-(
 );%0A    %7D%0A%0A  
@@ -1524,32 +1524,42 @@
 %3E%3E violations = 
+validator.
 validate(%22domain
@@ -1772,32 +1772,42 @@
 %3E%3E violations = 
+validator.
 validate(%22domain
@@ -2029,32 +2029,42 @@
 %3E%3E violations = 
+validator.
 validate(%22domain
@@ -2288,24 +2288,34 @@
 iolations = 
+validator.
 validate(%22do
@@ -2528,157 +2528,8 @@
  %7D%0A%0A
-    private Set%3CConstraintViolation%3CDomainBean%3E%3E validate(String property) %7B%0A        return validator.validateProperty(domainBean, property);%0A    %7D%0A%0A
     

Use ValidationWrapper in DomainTest