Repository: polygene-java Updated Branches: refs/heads/develop 80f85ffc8 -> e430d0110
Replace a NullPointerException with one explaining that the ServiceComposite type has not been declared. Adding more tests around Configuration constraint validation. 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/e430d011 Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/e430d011 Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/e430d011 Branch: refs/heads/develop Commit: e430d01105155b5bda6bd4ac02a61a974da44e54 Parents: 80f85ff Author: niclas <[email protected]> Authored: Mon May 29 15:36:10 2017 +0800 Committer: niclas <[email protected]> Committed: Mon May 29 15:36:10 2017 +0800 ---------------------------------------------------------------------- .../distributions/DistributionsPlugin.groovy | 1 - .../api/configuration/Configuration.java | 4 + .../NoSuchConfigurationTypeException.java | 36 ++++++ .../service/ConfigurationConstraintTest.java | 124 +++++++++++++++++++ .../service/HelloWorldService.properties | 2 +- .../runtime/service/TestService1.properties | 20 +++ .../runtime/service/TestService2.properties | 20 +++ 7 files changed, 205 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e430d011/buildSrc/src/main/groovy/org/apache/polygene/gradle/structure/distributions/DistributionsPlugin.groovy ---------------------------------------------------------------------- diff --git a/buildSrc/src/main/groovy/org/apache/polygene/gradle/structure/distributions/DistributionsPlugin.groovy b/buildSrc/src/main/groovy/org/apache/polygene/gradle/structure/distributions/DistributionsPlugin.groovy index ff21578..f6eee1f 100644 --- a/buildSrc/src/main/groovy/org/apache/polygene/gradle/structure/distributions/DistributionsPlugin.groovy +++ b/buildSrc/src/main/groovy/org/apache/polygene/gradle/structure/distributions/DistributionsPlugin.groovy @@ -444,7 +444,6 @@ class DistributionsPlugin implements Plugin<Project> '.gradle/**', 'docs/**', 'etc/templates/**', - 'etc/codestyle-idea.jar', 'libs/**' ] } as Action<RatTask> ) http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e430d011/core/api/src/main/java/org/apache/polygene/api/configuration/Configuration.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/polygene/api/configuration/Configuration.java b/core/api/src/main/java/org/apache/polygene/api/configuration/Configuration.java index bc0e0ad..a47719f 100644 --- a/core/api/src/main/java/org/apache/polygene/api/configuration/Configuration.java +++ b/core/api/src/main/java/org/apache/polygene/api/configuration/Configuration.java @@ -252,6 +252,10 @@ public interface Configuration<T> catch( NoSuchEntityException | NoSuchEntityTypeException e ) { EntityDescriptor entityDescriptor = module.typeLookup().lookupEntityModel( configurationType ); + if( entityDescriptor == null ) + { + throw new NoSuchConfigurationTypeException( configurationType, module.descriptor() ); + } return (V) initializeConfigurationInstance( entityDescriptor, uow, serviceModel, serviceIdentity ); } return configuration; http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e430d011/core/api/src/main/java/org/apache/polygene/api/configuration/NoSuchConfigurationTypeException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/polygene/api/configuration/NoSuchConfigurationTypeException.java b/core/api/src/main/java/org/apache/polygene/api/configuration/NoSuchConfigurationTypeException.java new file mode 100644 index 0000000..bce3b06 --- /dev/null +++ b/core/api/src/main/java/org/apache/polygene/api/configuration/NoSuchConfigurationTypeException.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.polygene.api.configuration; + +import org.apache.polygene.api.structure.ModuleDescriptor; + +public class NoSuchConfigurationTypeException extends RuntimeException +{ + private final Class<?> configType; + + public NoSuchConfigurationTypeException( Class<?> configType, + ModuleDescriptor module + ) + { + super( "No configuration type " + configType.getName() + " has been registered, or is not visible from " + module.name() ); + this.configType = configType; + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e430d011/core/runtime/src/test/java/org/apache/polygene/runtime/service/ConfigurationConstraintTest.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/test/java/org/apache/polygene/runtime/service/ConfigurationConstraintTest.java b/core/runtime/src/test/java/org/apache/polygene/runtime/service/ConfigurationConstraintTest.java new file mode 100644 index 0000000..9c4d7ce --- /dev/null +++ b/core/runtime/src/test/java/org/apache/polygene/runtime/service/ConfigurationConstraintTest.java @@ -0,0 +1,124 @@ +/* + * 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.runtime.service; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import org.apache.polygene.api.configuration.Configuration; +import org.apache.polygene.api.configuration.ConfigurationComposite; +import org.apache.polygene.api.constraint.Constraint; +import org.apache.polygene.api.constraint.ConstraintDeclaration; +import org.apache.polygene.api.constraint.ConstraintViolationException; +import org.apache.polygene.api.constraint.Constraints; +import org.apache.polygene.api.injection.scope.This; +import org.apache.polygene.api.mixin.Mixins; +import org.apache.polygene.api.property.Property; +import org.apache.polygene.api.service.ServiceReference; +import org.apache.polygene.bootstrap.SingletonAssembler; +import org.apache.polygene.entitystore.memory.MemoryEntityStoreService; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +/** + * Test of configuration for services that Constraints are respected. + */ +public class ConfigurationConstraintTest +{ + @Test + public void givenConstrainedConfigurationWhenCorrectValueExpectNoFailure() + throws Exception + { + SingletonAssembler underTest = new SingletonAssembler( + module -> + { + module.defaultServices(); + module.services( MemoryEntityStoreService.class ); + module.services( TestService.class ).identifiedBy( "TestService1" ); + module.configurations( TestConfiguration.class ); + } + ); + ServiceReference<TestService> service = underTest.module().findService( TestService.class ); + service.get().test(); + } + + @Test( expected = ConstraintViolationException.class ) + public void givenConstrainedConfigurationWhenIncorrectValueExpectConstraintViolationFailure() + throws Exception + { + SingletonAssembler underTest = new SingletonAssembler( + module -> + { + module.defaultServices(); + module.services( MemoryEntityStoreService.class ); + module.services( TestService.class ).identifiedBy( "TestService2" ); + module.configurations( TestConfiguration.class ); + } + ); + ServiceReference<TestService> service = underTest.module().findService( TestService.class ); + service.get().test(); + fail( "Expected failure from constraint violation." ); + } + + @Mixins( TestMixin.class ) + public interface TestService + { + void test(); + } + + public interface TestConfiguration + extends ConfigurationComposite + { + @Constrained + Property<String> constrained(); + } + + public static class TestMixin + implements TestService + { + @This + Configuration<TestConfiguration> config; + + @Override + public void test() + { + assertThat( config.get().constrained().get(), equalTo( "constrained" ) ); + } + } + + @ConstraintDeclaration + @Retention( RetentionPolicy.RUNTIME ) + @Constraints( ConstrainedConstraint.class ) + public @interface Constrained + { + } + + public static class ConstrainedConstraint + implements Constraint<Constrained, String> + { + @Override + public boolean isValid( Constrained annotation, String value ) + { + return value.equals( "constrained" ); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e430d011/core/runtime/src/test/resources/org/apache/polygene/runtime/service/HelloWorldService.properties ---------------------------------------------------------------------- diff --git a/core/runtime/src/test/resources/org/apache/polygene/runtime/service/HelloWorldService.properties b/core/runtime/src/test/resources/org/apache/polygene/runtime/service/HelloWorldService.properties index a51ce6a..c017674 100644 --- a/core/runtime/src/test/resources/org/apache/polygene/runtime/service/HelloWorldService.properties +++ b/core/runtime/src/test/resources/org/apache/polygene/runtime/service/HelloWorldService.properties @@ -19,4 +19,4 @@ # phrase=Hello -name=World +name=World \ No newline at end of file http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e430d011/core/runtime/src/test/resources/org/apache/polygene/runtime/service/TestService1.properties ---------------------------------------------------------------------- diff --git a/core/runtime/src/test/resources/org/apache/polygene/runtime/service/TestService1.properties b/core/runtime/src/test/resources/org/apache/polygene/runtime/service/TestService1.properties new file mode 100644 index 0000000..b284cf8 --- /dev/null +++ b/core/runtime/src/test/resources/org/apache/polygene/runtime/service/TestService1.properties @@ -0,0 +1,20 @@ +# +# 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. +# +# +# +constrained=constrained \ No newline at end of file http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e430d011/core/runtime/src/test/resources/org/apache/polygene/runtime/service/TestService2.properties ---------------------------------------------------------------------- diff --git a/core/runtime/src/test/resources/org/apache/polygene/runtime/service/TestService2.properties b/core/runtime/src/test/resources/org/apache/polygene/runtime/service/TestService2.properties new file mode 100644 index 0000000..acbfb4c --- /dev/null +++ b/core/runtime/src/test/resources/org/apache/polygene/runtime/service/TestService2.properties @@ -0,0 +1,20 @@ +# +# 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. +# +# +# +constrained=free \ No newline at end of file
