/*
 * 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.ValidationException;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

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

public class PortTest {

    private Validator validator;
    private PortBean portBean;

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

    // String

    @Test
    public void shouldValidateNullString() throws Exception {
        portBean.portString = null;

        Set<ConstraintViolation<PortBean>> violations = validate("portString");

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

    @Test
    public void shouldValidateBlankString() throws Exception {
        portBean.portString = " ";

        Set<ConstraintViolation<PortBean>> violations = validate("portString");

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

    @Test
    public void shouldNotValidateRandomString() throws Exception {
        portBean.portString = "abcd";

        Set<ConstraintViolation<PortBean>> violations = validate("portString");

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

    @Test
    public void shouldValidateValidPortString() throws Exception {
        portBean.portString = "8080";

        Set<ConstraintViolation<PortBean>> violations = validate("portString");

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

    @Test
    public void shouldNotValidateNegativePortString() throws Exception {
        portBean.portString = "-8080";

        Set<ConstraintViolation<PortBean>> violations = validate("portString");

        assertThat(violations, hasSize(1));
    }

    @Test
    public void shouldNotValidateInvalidPortString() throws Exception {
        portBean.portString = "65537";

        Set<ConstraintViolation<PortBean>> violations = validate("portString");

        assertThat(violations, hasSize(1));
    }

    // int

    @Test
    public void shouldValidateValidPortInt() throws Exception {
        portBean.portInt = 8080;

        Set<ConstraintViolation<PortBean>> violations = validate("portInt");

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

    @Test
    public void shouldNotValidateNegativePortInt() throws Exception {
        portBean.portInt = -8080;

        Set<ConstraintViolation<PortBean>> violations = validate("portInt");

        assertThat(violations, hasSize(1));
    }

    @Test
    public void shouldNotValidateInvalidPortInt() throws Exception {
        portBean.portInt = 65537;

        Set<ConstraintViolation<PortBean>> violations = validate("portInt");

        assertThat(violations, hasSize(1));
    }

    // Integer
    @Test
    public void shouldValidateNullPortInteger() throws Exception {
        portBean.portInteger = null;

        Set<ConstraintViolation<PortBean>> violations = validate("portInteger");

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

    @Test
    public void shouldNotValidateNegativePortInteger() throws Exception {
        portBean.portInteger = -8080;

        Set<ConstraintViolation<PortBean>> violations = validate("portInteger");

        assertThat(violations, hasSize(1));
    }

    @Test
    public void shouldNotValidateInvalidPortInteger() throws Exception {
        portBean.portInteger = 65537;

        Set<ConstraintViolation<PortBean>> violations = validate("portInteger");

        assertThat(violations, hasSize(1));
    }

    // others

    @Test(expected = ValidationException.class)
    public void shouldThrowExceptionWhenAppliedToOtherTypes() throws Exception {
        portBean.portObject = new Object();

        validate("portObject");
    }

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

    private static final class PortBean {
        @Port
        private String portString;
        @Port
        private int portInt;
        @Port
        private Integer portInteger;
        @Port
        private Object portObject;
    }
}

@@ -999,121 +999,90 @@
 tion
-;%0Aimport javax.validation.ValidationException;%0Aimport javax.validation.Validator;%0Aimport javax.
+Exception;%0Aimport java.util.Set;%0A%0Aimport com.github.britter.bean
 validat
-ion
+ors
 .Validat
 orFa
@@ -1081,41 +1081,19 @@
 idat
-orFactory;%0Aimport java.util.Set;%0A
+ionWrapper;
 %0Aimp
@@ -1182,18 +1182,36 @@
  Validat
-or
+ionWrapper%3CPortBean%3E
  validat
@@ -1313,154 +1313,87 @@
     
-ValidatorFactory factory = Validation.buildDefaultValidatorFactory();%0A        validator = factory.getValidator();%0A        portBean = new PortBean(
+portBean = new PortBean();%0A        validator = new ValidationWrapper%3C%3E(portBean
 );%0A 
@@ -1570,32 +1570,42 @@
 %3E%3E violations = 
+validator.
 validate(%22portSt
@@ -1822,32 +1822,42 @@
 %3E%3E violations = 
+validator.
 validate(%22portSt
@@ -2081,32 +2081,42 @@
 %3E%3E violations = 
+validator.
 validate(%22portSt
@@ -2492,32 +2492,42 @@
 %3E%3E violations = 
+validator.
 validate(%22portSt
@@ -2758,32 +2758,42 @@
 %3E%3E violations = 
+validator.
 validate(%22portSt
@@ -3022,32 +3022,42 @@
 %3E%3E violations = 
+validator.
 validate(%22portSt
@@ -3284,32 +3284,42 @@
 %3E%3E violations = 
+validator.
 validate(%22portIn
@@ -3539,32 +3539,42 @@
 %3E%3E violations = 
+validator.
 validate(%22portIn
@@ -3792,32 +3792,42 @@
 %3E%3E violations = 
+validator.
 validate(%22portIn
@@ -4061,32 +4061,42 @@
 %3E%3E violations = 
+validator.
 validate(%22portIn
@@ -4328,32 +4328,42 @@
 %3E%3E violations = 
+validator.
 validate(%22portIn
@@ -4597,24 +4597,34 @@
 iolations = 
+validator.
 validate(%22po
@@ -4881,24 +4881,34 @@
 );%0A%0A        
+validator.
 validate(%22po
@@ -4930,153 +4930,8 @@
  %7D%0A%0A
-    private Set%3CConstraintViolation%3CPortBean%3E%3E validate(String property) %7B%0A        return validator.validateProperty(portBean, property);%0A    %7D%0A%0A
     

Use ValidationWrapper in PortTest