Adding @HistPortList constraint as well. 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/105d37b6 Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/105d37b6 Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/105d37b6 Branch: refs/heads/develop Commit: 105d37b67fd0c1fea09068b0a5db4b49ac6d812f Parents: 5b94aa4 Author: niclas <[email protected]> Authored: Sat May 20 21:41:29 2017 +0800 Committer: niclas <[email protected]> Committed: Sat May 20 21:41:29 2017 +0800 ---------------------------------------------------------------------- .../constraints/HostPortListConstraint.java | 47 +++++++ .../constraints/annotation/HostPortList.java | 37 ++++++ .../constraints/HostPortListConstraintTest.java | 127 +++++++++++++++++++ 3 files changed, 211 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/polygene-java/blob/105d37b6/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortListConstraint.java ---------------------------------------------------------------------- diff --git a/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortListConstraint.java b/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortListConstraint.java new file mode 100644 index 0000000..6e81da4 --- /dev/null +++ b/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortListConstraint.java @@ -0,0 +1,47 @@ +/* + * 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 java.util.Arrays; +import org.apache.commons.validator.routines.UrlValidator; +import org.apache.polygene.api.constraint.Constraint; +import org.apache.polygene.library.constraints.annotation.HostPortList; + +/** + * Implement @HostPortList constraint. + */ +public class HostPortListConstraint + implements Constraint<HostPortList, String> +{ + private static final UrlValidator VALIDATOR = new UrlValidator( new String[]{ "http" } ); + + @Override + public boolean isValid( HostPortList annotation, String value ) + { + return Arrays.stream( value.split( "[ ,]" ) ) + .map( this::prefixProtocol ) + .allMatch( VALIDATOR::isValid ); + } + + private String prefixProtocol( String value ) + { + return "http://" + value; + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/105d37b6/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/annotation/HostPortList.java ---------------------------------------------------------------------- diff --git a/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/annotation/HostPortList.java b/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/annotation/HostPortList.java new file mode 100644 index 0000000..2d0b34b --- /dev/null +++ b/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/annotation/HostPortList.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.HostPortListConstraint; + +/** + * Marks a property as being a string, with a comma or space separated list of "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( HostPortListConstraint.class ) +public @interface HostPortList +{ +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/105d37b6/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortListConstraintTest.java ---------------------------------------------------------------------- diff --git a/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortListConstraintTest.java b/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortListConstraintTest.java new file mode 100644 index 0000000..82edd11 --- /dev/null +++ b/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortListConstraintTest.java @@ -0,0 +1,127 @@ +/* + * 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.HostPortList; +import org.apache.polygene.test.AbstractPolygeneTest; +import org.junit.Test; + +public class HostPortListConstraintTest 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" ); + } + + @Test + public void givenValidListHostPortWhenSettingPropertyExpectSuccess() + throws Exception + { + SomeValue someValue = transientBuilderFactory.newTransient( SomeValue.class ); + someValue.hostPort().set( "habba.zout.com:1234,12.34.56.78:1234" ); + } + + @Test( expected = ConstraintViolationException.class ) + public void givenInvalidListHostNameWhenSettingPropertyExpectConstrainViolation() + throws Exception + { + SomeValue someValue = transientBuilderFactory.newTransient( SomeValue.class ); + someValue.hostPort().set( "1.2.3.4:12,1:2:3_i:1234" ); + } + + @Test( expected = ConstraintViolationException.class ) + public void givenInvalidListPortNumberWhenSettingPropertyExpectConstrainViolation() + throws Exception + { + SomeValue someValue = transientBuilderFactory.newTransient( SomeValue.class ); + someValue.hostPort().set( "1.2.3.4:1234 1.2.3.4:123456" ); + } + + @Test + public void givenValidListIp4NumberPortNumberWhenSettingPropertyExpectSuccess() + throws Exception + { + SomeValue someValue = transientBuilderFactory.newTransient( SomeValue.class ); + someValue.hostPort().set( "1.2.3.4:1234 google.com" ); + } + + @Test + public void givenValidListIp6NumberPortNumberWhenSettingPropertyExpectSuccess() + 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 + { + @HostPortList + @Optional + Property<String> hostPort(); + } +}
