Repository: polygene-java Updated Branches: refs/heads/yeoman-work [created] a9de6c653
Adding @HostPort validation, based on Commons Validator (which may suggest a whole bunch of more validations) Signed-off-by: niclas <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/5b94aa44 Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/5b94aa44 Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/5b94aa44 Branch: refs/heads/yeoman-work Commit: 5b94aa441e23ef19b1570e197971f0d24b35b634 Parents: 955eaa9 Author: niclas <[email protected]> Authored: Sat May 20 21:30:22 2017 +0800 Committer: niclas <[email protected]> Committed: Sat May 20 21:30:22 2017 +0800 ---------------------------------------------------------------------- libraries/constraints/build.gradle | 2 + .../library/constraints/HostPortConstraint.java | 39 +++++++++ .../constraints/annotation/HostPort.java | 37 +++++++++ .../constraints/HostPortConstraintTest.java | 87 ++++++++++++++++++++ 4 files changed, 165 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/polygene-java/blob/5b94aa44/libraries/constraints/build.gradle ---------------------------------------------------------------------- diff --git a/libraries/constraints/build.gradle b/libraries/constraints/build.gradle index 5181d76..2aff446 100644 --- a/libraries/constraints/build.gradle +++ b/libraries/constraints/build.gradle @@ -27,6 +27,8 @@ jar { manifest { name = "Apache Polygene⢠Library - Constraints"}} dependencies { api polygene.core.bootstrap + implementation libraries.commons_validator + runtimeOnly polygene.core.runtime testImplementation polygene.core.testsupport http://git-wip-us.apache.org/repos/asf/polygene-java/blob/5b94aa44/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortConstraint.java ---------------------------------------------------------------------- diff --git a/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortConstraint.java b/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortConstraint.java new file mode 100644 index 0000000..bc84c7c --- /dev/null +++ b/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortConstraint.java @@ -0,0 +1,39 @@ +/* + * 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.polygene.library.constraints; + +import org.apache.commons.validator.routines.UrlValidator; +import org.apache.polygene.api.constraint.Constraint; +import org.apache.polygene.library.constraints.annotation.HostPort; + +/** + * Implement @HostPort constraint. + */ +public class HostPortConstraint + implements Constraint<HostPort, String> +{ + private static final UrlValidator VALIDATOR = new UrlValidator( new String[]{ "http" } ); + + @Override + public boolean isValid( HostPort annotation, String value ) + { + return VALIDATOR.isValid( "http://" + value ); + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/5b94aa44/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/annotation/HostPort.java ---------------------------------------------------------------------- diff --git a/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/annotation/HostPort.java b/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/annotation/HostPort.java new file mode 100644 index 0000000..62cac1f --- /dev/null +++ b/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/annotation/HostPort.java @@ -0,0 +1,37 @@ +/* + * 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.polygene.library.constraints.annotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import org.apache.polygene.api.constraint.ConstraintDeclaration; +import org.apache.polygene.api.constraint.Constraints; +import org.apache.polygene.library.constraints.HostPortConstraint; + +/** + * Marks a property as being a string, with a "host:port", where host is a valid hostname, IPv4 address or IPv6 address + * and port is in the range of 0 to 65535 + */ +@ConstraintDeclaration +@Retention( RetentionPolicy.RUNTIME ) +@Constraints( HostPortConstraint.class ) +public @interface HostPort +{ +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/5b94aa44/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortConstraintTest.java ---------------------------------------------------------------------- diff --git a/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortConstraintTest.java b/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortConstraintTest.java new file mode 100644 index 0000000..cacd34c --- /dev/null +++ b/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortConstraintTest.java @@ -0,0 +1,87 @@ +/* + * 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.polygene.library.constraints; + +import org.apache.polygene.api.common.Optional; +import org.apache.polygene.api.constraint.ConstraintViolationException; +import org.apache.polygene.api.property.Property; +import org.apache.polygene.bootstrap.AssemblyException; +import org.apache.polygene.bootstrap.ModuleAssembly; +import org.apache.polygene.library.constraints.annotation.HostPort; +import org.apache.polygene.test.AbstractPolygeneTest; +import org.junit.Test; + +public class HostPortConstraintTest extends AbstractPolygeneTest +{ + + @Test + public void givenValidHostPortWhenSettingPropertyExpectSuccess() + throws Exception + { + SomeValue someValue = transientBuilderFactory.newTransient( SomeValue.class ); + someValue.hostPort().set( "habba.zout.com:1234" ); + } + + @Test( expected = ConstraintViolationException.class ) + public void givenInvalidHostNameWhenSettingPropertyExpectConstrainViolation() + throws Exception + { + SomeValue someValue = transientBuilderFactory.newTransient( SomeValue.class ); + someValue.hostPort().set( "1:2:3_i:1234" ); + } + + @Test( expected = ConstraintViolationException.class ) + public void givenInvalidPortNumberWhenSettingPropertyExpectConstrainViolation() + throws Exception + { + SomeValue someValue = transientBuilderFactory.newTransient( SomeValue.class ); + someValue.hostPort().set( "1.2.3.4:123456" ); + } + + @Test + public void givenValidIp4NumberPortNumberWhenSettingPropertyExpectSuccess() + throws Exception + { + SomeValue someValue = transientBuilderFactory.newTransient( SomeValue.class ); + someValue.hostPort().set( "1.2.3.4:1234" ); + } + + @Test + public void givenValidIp6NumberPortNumberWhenSettingPropertyExpectSuccess() + throws Exception + { + SomeValue someValue = transientBuilderFactory.newTransient( SomeValue.class ); + someValue.hostPort().set( "[::1]:1234" ); + } + + @Override + public void assemble( ModuleAssembly module ) + throws AssemblyException + { + module.transients( SomeValue.class ); + } + + public interface SomeValue + { + @HostPort + @Optional + Property<String> hostPort(); + } +}
