package org.ovirt.engine.core.common.utils;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.ovirt.engine.core.common.validation.annotation.ValidName;

@RunWith(Parameterized.class)
public class ValidNameValidatorTest {

    private Validator validator;
    private String name;
    private boolean expectedResult;

    public ValidNameValidatorTest(String name, boolean expectedResult) {
        this.name = name;
        this.expectedResult = expectedResult;
    }

    @Before
    public void setup() throws Exception {
        validator = ValidationUtils.getValidator();
    }

    @Test
    public void checkName() {
        Set<ConstraintViolation<ValidNameContainer>> result = validator.validate(new ValidNameContainer(name));
        assertEquals(expectedResult, result.isEmpty());
    }

    @Parameterized.Parameters
    public static Collection<Object[]> namesParams() {
        return Arrays.asList(new Object[][] {
                { "abc", true },
                { "123", true },
                { "abc123", true },
                { "123abc", true },
                { null, true },
                { " ", false },
                { "", false },
                { "abc ", false },
                { " abc", false },
                { "abc cde", false },
                { "abc*", false }
        });
    }

    private class ValidNameContainer {

        @ValidName
        private String name;

        public ValidNameContainer(String name) {
            this.name = name;
        }
    }
}

@@ -899,24 +899,93 @@
 eckName() %7B%0A
+        ValidNameContainer container = new ValidNameContainer(name);%0A
         Set%3C
@@ -1053,38 +1053,25 @@
 alidate(
-new ValidNameC
+c
 ontainer
 (name));
@@ -1062,22 +1062,16 @@
 ontainer
-(name)
 );%0A     
@@ -1086,16 +1086,67 @@
 tEquals(
+%22Failed to validate name: %22 + container.getName(), 
 expected
@@ -1716,16 +1716,23 @@
 private 
+static 
 class Va
@@ -1888,16 +1888,86 @@
        %7D
+%0A%0A        public String getName() %7B%0A            return name;%0A        %7D
 %0A    %7D%0A%7D

Fix coverity warnings