DELTASPIKE-624 - removed CamelCase from beanValidation package name
Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/95826016 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/95826016 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/95826016 Branch: refs/heads/master Commit: 95826016ff1afc65dc573527a7ab0fc1baf956c6 Parents: b438558 Author: Rafael Benevides <[email protected]> Authored: Wed Jun 4 16:59:52 2014 -0300 Committer: Rafael Benevides <[email protected]> Committed: Wed Jun 4 16:59:52 2014 -0300 ---------------------------------------------------------------------- .../CDIAwareConstraintValidatorFactory.java | 70 ----------------- .../CDIAwareConstraintValidatorFactory.java | 70 +++++++++++++++++ .../beanValidation/test/ArchiveUtils.java | 33 -------- .../beanValidation/test/ArrayChecker.java | 29 ------- .../beanValidation/test/ArraySize.java | 46 ------------ .../beanValidation/test/BasicPojo.java | 36 --------- .../test/InjectableConstraintValidator.java | 45 ----------- .../test/InjectedValidationTest.java | 79 -------------------- .../beanvalidation/test/ArchiveUtils.java | 33 ++++++++ .../beanvalidation/test/ArrayChecker.java | 29 +++++++ .../beanvalidation/test/ArraySize.java | 46 ++++++++++++ .../beanvalidation/test/BasicPojo.java | 36 +++++++++ .../test/InjectableConstraintValidator.java | 45 +++++++++++ .../test/InjectedValidationTest.java | 79 ++++++++++++++++++++ 14 files changed, 338 insertions(+), 338 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/95826016/deltaspike/modules/bean-validation/impl/src/main/java/org/apache/deltaspike/beanValidation/impl/CDIAwareConstraintValidatorFactory.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/bean-validation/impl/src/main/java/org/apache/deltaspike/beanValidation/impl/CDIAwareConstraintValidatorFactory.java b/deltaspike/modules/bean-validation/impl/src/main/java/org/apache/deltaspike/beanValidation/impl/CDIAwareConstraintValidatorFactory.java deleted file mode 100644 index 434a19a..0000000 --- a/deltaspike/modules/bean-validation/impl/src/main/java/org/apache/deltaspike/beanValidation/impl/CDIAwareConstraintValidatorFactory.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.deltaspike.beanValidation.impl; - -import java.util.logging.Level; -import java.util.logging.Logger; - -import javax.validation.ConstraintValidator; -import javax.validation.ConstraintValidatorFactory; -import javax.validation.Validation; - -import org.apache.deltaspike.core.api.provider.BeanProvider; - -/** - * A factory for creating CDI Aware/Enabled ConstraintValidators. - * - */ -public class CDIAwareConstraintValidatorFactory implements - ConstraintValidatorFactory -{ - - private final Logger log = Logger - .getLogger(CDIAwareConstraintValidatorFactory.class.toString()); - - private final ConstraintValidatorFactory delegate; - - public CDIAwareConstraintValidatorFactory() - { - delegate = Validation.byDefaultProvider().configure().getDefaultConstraintValidatorFactory(); - if (log.isLoggable(Level.CONFIG)) - { - log.config("Setting up delegate ConstraintValidatorFactory as " + - delegate.getClass().getCanonicalName()); - } - } - - @Override - public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> validatorClass) - { - T resolvedInst = null; - resolvedInst = BeanProvider.getContextualReference(validatorClass,true); - if (resolvedInst == null) - { - if (log.isLoggable(Level.CONFIG)) - { - log.config("No contextual instances found for class " + validatorClass.getCanonicalName() + - " delegating to DefaultProvider behavior."); - } - resolvedInst = this.delegate.getInstance(validatorClass); - } - return resolvedInst; - } - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/95826016/deltaspike/modules/bean-validation/impl/src/main/java/org/apache/deltaspike/beanvalidation/impl/CDIAwareConstraintValidatorFactory.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/bean-validation/impl/src/main/java/org/apache/deltaspike/beanvalidation/impl/CDIAwareConstraintValidatorFactory.java b/deltaspike/modules/bean-validation/impl/src/main/java/org/apache/deltaspike/beanvalidation/impl/CDIAwareConstraintValidatorFactory.java new file mode 100644 index 0000000..d481081 --- /dev/null +++ b/deltaspike/modules/bean-validation/impl/src/main/java/org/apache/deltaspike/beanvalidation/impl/CDIAwareConstraintValidatorFactory.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.deltaspike.beanvalidation.impl; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorFactory; +import javax.validation.Validation; + +import org.apache.deltaspike.core.api.provider.BeanProvider; + +/** + * A factory for creating CDI Aware/Enabled ConstraintValidators. + * + */ +public class CDIAwareConstraintValidatorFactory implements + ConstraintValidatorFactory +{ + + private final Logger log = Logger + .getLogger(CDIAwareConstraintValidatorFactory.class.toString()); + + private final ConstraintValidatorFactory delegate; + + public CDIAwareConstraintValidatorFactory() + { + delegate = Validation.byDefaultProvider().configure().getDefaultConstraintValidatorFactory(); + if (log.isLoggable(Level.CONFIG)) + { + log.config("Setting up delegate ConstraintValidatorFactory as " + + delegate.getClass().getCanonicalName()); + } + } + + @Override + public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> validatorClass) + { + T resolvedInst = null; + resolvedInst = BeanProvider.getContextualReference(validatorClass,true); + if (resolvedInst == null) + { + if (log.isLoggable(Level.CONFIG)) + { + log.config("No contextual instances found for class " + validatorClass.getCanonicalName() + + " delegating to DefaultProvider behavior."); + } + resolvedInst = this.delegate.getInstance(validatorClass); + } + return resolvedInst; + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/95826016/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/ArchiveUtils.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/ArchiveUtils.java b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/ArchiveUtils.java deleted file mode 100644 index 5eb498d..0000000 --- a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/ArchiveUtils.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.deltaspike.beanValidation.test; - -import org.apache.deltaspike.test.utils.ShrinkWrapArchiveUtil; -import org.jboss.shrinkwrap.api.spec.JavaArchive; - -public class ArchiveUtils { - public static JavaArchive[] getDeltaSpikeCore() { - JavaArchive[] coreArchives = ShrinkWrapArchiveUtil.getArchives(null, - "META-INF/beans.xml", - new String[]{"org.apache.deltaspike.core", "org.apache.deltaspike.test.category"}, - null, - "ds-core"); - return coreArchives; - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/95826016/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/ArrayChecker.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/ArrayChecker.java b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/ArrayChecker.java deleted file mode 100644 index 35542df..0000000 --- a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/ArrayChecker.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.deltaspike.beanValidation.test; - -public class ArrayChecker { - public boolean checkArray(ArraySize as, String[] data) { - if (data.length < as.min() || data.length > as.max()) { - return false; - } else { - return true; - } - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/95826016/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/ArraySize.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/ArraySize.java b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/ArraySize.java deleted file mode 100644 index 4f85aa3..0000000 --- a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/ArraySize.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.deltaspike.beanValidation.test; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import javax.validation.Constraint; -import javax.validation.Payload; - -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.ElementType.METHOD; -import static java.lang.annotation.ElementType.PARAMETER; -import static java.lang.annotation.ElementType.TYPE; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -@Constraint(validatedBy = InjectableConstraintValidator.class) -@Target({ TYPE, METHOD, PARAMETER, FIELD }) -@Retention(RUNTIME) -@Documented -public @interface ArraySize { - int min() default 0; - int max() default Integer.MAX_VALUE; - String message() default "{org.apache.deltaspike.modules.beanval.constraints.arraySize}"; - - Class<?>[] groups() default {}; - - Class<? extends Payload>[] payload() default {}; -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/95826016/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/BasicPojo.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/BasicPojo.java b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/BasicPojo.java deleted file mode 100644 index 8064598..0000000 --- a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/BasicPojo.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.deltaspike.beanValidation.test; - -public class BasicPojo -{ - @ArraySize(min=1) - private String[] value; - - public String[] getValue() - { - return value; - } - - public void setValue(String[] value) - { - this.value = value; - } - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/95826016/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/InjectableConstraintValidator.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/InjectableConstraintValidator.java b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/InjectableConstraintValidator.java deleted file mode 100644 index 731c1f7..0000000 --- a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/InjectableConstraintValidator.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.deltaspike.beanValidation.test; - -import javax.inject.Inject; -import javax.validation.ConstraintValidator; -import javax.validation.ConstraintValidatorContext; - -public class InjectableConstraintValidator implements - ConstraintValidator<ArraySize, String[]> -{ - @Inject - private ArrayChecker arrayChecker; - - private ArraySize arraySize; - - @Override - public void initialize(ArraySize arraySize) - { - this.arraySize = arraySize; - } - - @Override - public boolean isValid(String[] values, ConstraintValidatorContext context) - { - return arrayChecker.checkArray(arraySize, values); - } - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/95826016/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/InjectedValidationTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/InjectedValidationTest.java b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/InjectedValidationTest.java deleted file mode 100644 index 58ee7f9..0000000 --- a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanValidation/test/InjectedValidationTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.deltaspike.beanValidation.test; - -import java.util.Set; - -import javax.validation.ConstraintViolation; -import javax.validation.Validation; -import javax.validation.Validator; - -import org.apache.deltaspike.beanValidation.impl.CDIAwareConstraintValidatorFactory; -import org.apache.deltaspike.test.category.WebProfileCategory; -import org.jboss.arquillian.container.test.api.Deployment; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.shrinkwrap.api.ShrinkWrap; -import org.jboss.shrinkwrap.api.asset.EmptyAsset; -import org.jboss.shrinkwrap.api.spec.WebArchive; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.categories.Category; -import org.junit.runner.RunWith; - -@RunWith(Arquillian.class) -public class InjectedValidationTest -{ - @Deployment - public static WebArchive createArchive() - { - return ShrinkWrap.create(WebArchive.class,"beanval.war") - .addClasses(BasicPojo.class,InjectableConstraintValidator.class, - CDIAwareConstraintValidatorFactory.class,ArraySize.class, - ArrayChecker.class) - .addAsWebInfResource("validation.xml", "classes/META-INF/validation.xml") - .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") - .addAsLibraries(ArchiveUtils.getDeltaSpikeCore()); - } - - private Validator validator; - - @Before - public void initValidator() - { - this.validator = Validation.byDefaultProvider().configure() - .constraintValidatorFactory(new CDIAwareConstraintValidatorFactory()) - .buildValidatorFactory().getValidator(); - } - - @Test - public void testValidate() - { - String[] entries = new String[]{"abc","def","ghi"}; - BasicPojo p = new BasicPojo(); - p.setValue(entries); - final Set<ConstraintViolation<BasicPojo>> violations = validator.validate(p); - StringBuilder sb = new StringBuilder(); - for(ConstraintViolation<BasicPojo> violation : violations) { - sb.append(violation.getMessage() + " "+ violation.getPropertyPath()); - } - Assert.assertTrue(sb.toString(),violations.isEmpty()); - - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/95826016/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/ArchiveUtils.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/ArchiveUtils.java b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/ArchiveUtils.java new file mode 100644 index 0000000..ebfde33 --- /dev/null +++ b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/ArchiveUtils.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.deltaspike.beanvalidation.test; + +import org.apache.deltaspike.test.utils.ShrinkWrapArchiveUtil; +import org.jboss.shrinkwrap.api.spec.JavaArchive; + +public class ArchiveUtils { + public static JavaArchive[] getDeltaSpikeCore() { + JavaArchive[] coreArchives = ShrinkWrapArchiveUtil.getArchives(null, + "META-INF/beans.xml", + new String[]{"org.apache.deltaspike.core", "org.apache.deltaspike.test.category"}, + null, + "ds-core"); + return coreArchives; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/95826016/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/ArrayChecker.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/ArrayChecker.java b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/ArrayChecker.java new file mode 100644 index 0000000..d2f288f --- /dev/null +++ b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/ArrayChecker.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.deltaspike.beanvalidation.test; + +public class ArrayChecker { + public boolean checkArray(ArraySize as, String[] data) { + if (data.length < as.min() || data.length > as.max()) { + return false; + } else { + return true; + } + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/95826016/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/ArraySize.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/ArraySize.java b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/ArraySize.java new file mode 100644 index 0000000..1f0c89a --- /dev/null +++ b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/ArraySize.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.deltaspike.beanvalidation.test; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.validation.Constraint; +import javax.validation.Payload; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Constraint(validatedBy = InjectableConstraintValidator.class) +@Target({ TYPE, METHOD, PARAMETER, FIELD }) +@Retention(RUNTIME) +@Documented +public @interface ArraySize { + int min() default 0; + int max() default Integer.MAX_VALUE; + String message() default "{org.apache.deltaspike.modules.beanval.constraints.arraySize}"; + + Class<?>[] groups() default {}; + + Class<? extends Payload>[] payload() default {}; +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/95826016/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/BasicPojo.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/BasicPojo.java b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/BasicPojo.java new file mode 100644 index 0000000..580089d --- /dev/null +++ b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/BasicPojo.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.deltaspike.beanvalidation.test; + +public class BasicPojo +{ + @ArraySize(min=1) + private String[] value; + + public String[] getValue() + { + return value; + } + + public void setValue(String[] value) + { + this.value = value; + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/95826016/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/InjectableConstraintValidator.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/InjectableConstraintValidator.java b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/InjectableConstraintValidator.java new file mode 100644 index 0000000..79ffaf5 --- /dev/null +++ b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/InjectableConstraintValidator.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.deltaspike.beanvalidation.test; + +import javax.inject.Inject; +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; + +public class InjectableConstraintValidator implements + ConstraintValidator<ArraySize, String[]> +{ + @Inject + private ArrayChecker arrayChecker; + + private ArraySize arraySize; + + @Override + public void initialize(ArraySize arraySize) + { + this.arraySize = arraySize; + } + + @Override + public boolean isValid(String[] values, ConstraintValidatorContext context) + { + return arrayChecker.checkArray(arraySize, values); + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/95826016/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/InjectedValidationTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/InjectedValidationTest.java b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/InjectedValidationTest.java new file mode 100644 index 0000000..89fc7c8 --- /dev/null +++ b/deltaspike/modules/bean-validation/impl/src/test/java/org/apache/deltaspike/beanvalidation/test/InjectedValidationTest.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.deltaspike.beanvalidation.test; + +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.Validator; + +import org.apache.deltaspike.beanvalidation.impl.CDIAwareConstraintValidatorFactory; +import org.apache.deltaspike.test.category.WebProfileCategory; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +@RunWith(Arquillian.class) +public class InjectedValidationTest +{ + @Deployment + public static WebArchive createArchive() + { + return ShrinkWrap.create(WebArchive.class,"beanval.war") + .addClasses(BasicPojo.class,InjectableConstraintValidator.class, + CDIAwareConstraintValidatorFactory.class,ArraySize.class, + ArrayChecker.class) + .addAsWebInfResource("validation.xml", "classes/META-INF/validation.xml") + .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") + .addAsLibraries(ArchiveUtils.getDeltaSpikeCore()); + } + + private Validator validator; + + @Before + public void initValidator() + { + this.validator = Validation.byDefaultProvider().configure() + .constraintValidatorFactory(new CDIAwareConstraintValidatorFactory()) + .buildValidatorFactory().getValidator(); + } + + @Test + public void testValidate() + { + String[] entries = new String[]{"abc","def","ghi"}; + BasicPojo p = new BasicPojo(); + p.setValue(entries); + final Set<ConstraintViolation<BasicPojo>> violations = validator.validate(p); + StringBuilder sb = new StringBuilder(); + for(ConstraintViolation<BasicPojo> violation : violations) { + sb.append(violation.getMessage() + " "+ violation.getPropertyPath()); + } + Assert.assertTrue(sb.toString(),violations.isEmpty()); + + } +}
