Repository: incubator-tamaya-sandbox Updated Branches: refs/heads/master 4214dd43b -> adc24cad2
TAMAYA-321 Enhanced vertx support. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/adc24cad Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/adc24cad Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/adc24cad Branch: refs/heads/master Commit: adc24cad25d7ea1580bf4948b7bd2072ca544323 Parents: 4214dd4 Author: Anatole Tresch <anat...@apache.org> Authored: Mon Nov 27 00:31:12 2017 +0100 Committer: Anatole Tresch <anat...@apache.org> Committed: Mon Nov 27 00:31:12 2017 +0100 ---------------------------------------------------------------------- .../vertx/AbstractConfiguredVerticle.java | 26 ++- .../org/apache/tamaya/vertx/ConfigVerticle.java | 88 ----------- .../vertx/ConfiguredJavaVerticleFactory.java | 57 +++++++ .../vertx/TamayaConfigurationProducer.java | 158 +++++++++++++++++++ .../services/io.vertx.core.spi.VerticleFactory | 19 +++ .../apache/tamaya/vertx/ConfigVerticleTest.java | 65 +++----- .../tamaya/vertx/TestInjectedVerticle.java | 94 +++++++++++ .../tamaya/vertx/TestInjectedVerticleTest.java | 132 ++++++++++++++++ 8 files changed, 493 insertions(+), 146 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/adc24cad/vertx/src/main/java/org/apache/tamaya/vertx/AbstractConfiguredVerticle.java ---------------------------------------------------------------------- diff --git a/vertx/src/main/java/org/apache/tamaya/vertx/AbstractConfiguredVerticle.java b/vertx/src/main/java/org/apache/tamaya/vertx/AbstractConfiguredVerticle.java index 495c62f..5dd0e66 100644 --- a/vertx/src/main/java/org/apache/tamaya/vertx/AbstractConfiguredVerticle.java +++ b/vertx/src/main/java/org/apache/tamaya/vertx/AbstractConfiguredVerticle.java @@ -23,6 +23,8 @@ import org.apache.tamaya.Configuration; import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.inject.ConfigurationInjection; +import java.util.Optional; + /** * Base verticle class that adds some convenience methods for accessing configuration. * The class also performs configuration injection using {@link ConfigurationInjection}. @@ -35,14 +37,14 @@ public abstract class AbstractConfiguredVerticle extends AbstractVerticle{ configure(); } - public Configuration getConfiguration(){ + protected Configuration getConfiguration(){ if(configuration==null){ return ConfigurationProvider.getConfiguration(); } return configuration; } - public void setConfiguration(Configuration configuration){ + protected void setConfiguration(Configuration configuration){ this.configuration = configuration; } @@ -50,27 +52,19 @@ public abstract class AbstractConfiguredVerticle extends AbstractVerticle{ ConfigurationInjection.getConfigurationInjector().configure(this, getConfiguration()); } - protected final String getConfigProperty(String key){ + protected final String getConfigValue(String key){ return getConfiguration().get(key); } - protected final String getConfigPropertyOrDefault(String key, String defaultValue){ - String val = getConfiguration().get(key); - if(val==null){ - return defaultValue; - } - return val; + protected final Optional<String> getOptionalConfigValue(String key){ + return Optional.ofNullable(getConfiguration().getOrDefault(key, null)); } - protected final <T> T getConfigProperty(String key, Class<T> type){ + protected final <T> T getConfigValue(String key, Class<T> type){ return getConfiguration().get(key, type); } - protected final <T> T getConfigPropertyOrDefault(String key, Class<T> type, T defaultValue){ - T val = getConfiguration().get(key, type); - if(val==null){ - return defaultValue; - } - return val; + protected final <T> Optional<T> getOptionalConfigValue(String key, Class<T> type){ + return Optional.ofNullable(getConfiguration().getOrDefault(key, type, null)); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/adc24cad/vertx/src/main/java/org/apache/tamaya/vertx/ConfigVerticle.java ---------------------------------------------------------------------- diff --git a/vertx/src/main/java/org/apache/tamaya/vertx/ConfigVerticle.java b/vertx/src/main/java/org/apache/tamaya/vertx/ConfigVerticle.java deleted file mode 100644 index 228aece..0000000 --- a/vertx/src/main/java/org/apache/tamaya/vertx/ConfigVerticle.java +++ /dev/null @@ -1,88 +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.tamaya.vertx; - -import io.vertx.core.Handler; -import io.vertx.core.eventbus.Message; -import io.vertx.core.eventbus.MessageConsumer; -import io.vertx.core.json.Json; -import org.apache.tamaya.Configuration; -import org.apache.tamaya.ConfigurationProvider; -import org.apache.tamaya.functions.ConfigurationFunctions; -import org.apache.tamaya.functions.PropertyMatcher; -import org.apache.tamaya.inject.api.Config; - -import java.util.Map; - -/** - * Small configured verticle for testing Tamaya's vertx support. - */ -public class ConfigVerticle extends AbstractConfiguredVerticle{ - - @Config(value = "tamaya.vertx.config.map", defaultValue = "CONFIG-MAP") - private String mapBusTarget; - - @Config(value = "tamaya.vertx.config.value", defaultValue = "CONFIG-VAL") - private String valBusTarget; - - private MessageConsumer<String> mapBusListener; - private MessageConsumer<String> valBusListener; - - @Override - public void start()throws Exception{ - mapBusListener = vertx.eventBus().consumer(mapBusTarget); - mapBusListener.handler(new Handler<Message<String>>(){ - @Override - public void handle(final Message<String> message) { - Configuration config = ConfigurationProvider.getConfiguration(); - Map<String,String> cfg = config.with(ConfigurationFunctions.filter( - new PropertyMatcher() { - @Override - public boolean test(String key, String value) { - return key.matches(message.body()); - } - } - )).getProperties(); - message.reply(Json.encodePrettily(cfg)); - } - }); - valBusListener = vertx.eventBus().consumer(valBusTarget); - valBusListener.handler(new Handler<Message<String>>(){ - @Override - public void handle(final Message<String> message) { - Configuration config = ConfigurationProvider.getConfiguration(); - message.reply(config.get(message.body())); - } - }); - } - - @Override - public void stop()throws Exception{ - mapBusListener.unregister(); - valBusListener.unregister(); - } - - @Override - public String toString() { - return "ConfigVerticle{" + - "mapBusTarget='" + mapBusTarget + '\'' + - ", valBusTarget='" + valBusTarget + '\'' + - '}'; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/adc24cad/vertx/src/main/java/org/apache/tamaya/vertx/ConfiguredJavaVerticleFactory.java ---------------------------------------------------------------------- diff --git a/vertx/src/main/java/org/apache/tamaya/vertx/ConfiguredJavaVerticleFactory.java b/vertx/src/main/java/org/apache/tamaya/vertx/ConfiguredJavaVerticleFactory.java new file mode 100644 index 0000000..efb3c70 --- /dev/null +++ b/vertx/src/main/java/org/apache/tamaya/vertx/ConfiguredJavaVerticleFactory.java @@ -0,0 +1,57 @@ +/* + * 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.tamaya.vertx; + +import io.vertx.core.Verticle; +import io.vertx.core.impl.verticle.CompilingClassLoader; +import io.vertx.core.spi.VerticleFactory; +import org.apache.tamaya.inject.ConfigurationInjection; + +/** + * Verticle factory that produces Java verticles that are configured with the Tamaya injection API. + */ +public class ConfiguredJavaVerticleFactory implements VerticleFactory{ + + @Override + public String prefix() { + return "java"; + } + + @Override + public int order() { + return 1; + } + + @Override + public Verticle createVerticle(String verticleName, ClassLoader classLoader) throws Exception { + verticleName = VerticleFactory.removePrefix(verticleName); + Class clazz; + if (verticleName.endsWith(".java")) { + CompilingClassLoader compilingLoader = new CompilingClassLoader(classLoader, verticleName); + String className = compilingLoader.resolveMainClassName(); + clazz = compilingLoader.loadClass(className); + } else { + clazz = classLoader.loadClass(verticleName); + } + + Verticle instance = (Verticle)clazz.newInstance(); + ConfigurationInjection.getConfigurationInjector().configure(instance); + return instance; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/adc24cad/vertx/src/main/java/org/apache/tamaya/vertx/TamayaConfigurationProducer.java ---------------------------------------------------------------------- diff --git a/vertx/src/main/java/org/apache/tamaya/vertx/TamayaConfigurationProducer.java b/vertx/src/main/java/org/apache/tamaya/vertx/TamayaConfigurationProducer.java new file mode 100644 index 0000000..f69fe62 --- /dev/null +++ b/vertx/src/main/java/org/apache/tamaya/vertx/TamayaConfigurationProducer.java @@ -0,0 +1,158 @@ +/* + * 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.tamaya.vertx; + +import io.netty.handler.codec.http.HttpResponseStatus; +import io.vertx.core.AbstractVerticle; +import io.vertx.core.Future; +import io.vertx.core.eventbus.EventBus; +import io.vertx.core.eventbus.MessageConsumer; +import io.vertx.core.json.Json; +import io.vertx.core.json.JsonArray; +import io.vertx.core.json.JsonObject; +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.functions.ConfigurationFunctions; +import org.apache.tamaya.functions.PropertyMatcher; +import org.apache.tamaya.inject.ConfigurationInjection; +import org.apache.tamaya.inject.api.Config; + +import java.util.Map; +import java.util.TreeMap; + +/** + * This is a simple verticle registering Tamaya event bus messaging for accessing configuration: + * <ul> + * <li>Don't pass anything, get a {@link JsonObject} with the full Tamaya configuration.</li> + * <li>Pass a {@code String} key, get a String return value, if present or a failure.</li> + * <li>Pass a {@link JsonArray} of keys, get a {@link JsonObject} return value, with the key/values found.</li> + * </ul> + */ +public class TamayaConfigurationProducer extends AbstractConfiguredVerticle{ + + public static final String DEFAULT_CONFIGRE_ADDRESS = "CONFIG.CONFIGURE"; + public static final String DEFAULT_CONFIG_GET_MULTI_ADDRESS = "CONFIG.GET.MAP"; + public static final String DEFAULT_CONFIG_GET_SINGLE_ADDRESS = "CONFIG.GET.SINGLE"; + + @Config(value = "tamaya.vertx.busaddress.inject", defaultValue = DEFAULT_CONFIGRE_ADDRESS) + private String injectionBusTarget; + + @Config(value = "tamaya.vertx.busaddress.multi", defaultValue = DEFAULT_CONFIG_GET_MULTI_ADDRESS) + private String mapBusTarget; + + @Config(value = "tamaya.vertx.busaddress.single", defaultValue = DEFAULT_CONFIG_GET_SINGLE_ADDRESS) + private String singleBusTarget; + + + /** + * Registers a handler for accessing single configuration keys (input: String, reply type: String). If no + * config value is present the consumer will reply with a NOT_FOUND failure. + * @param address the event bus address to register. + * @param eventBus the event bus. + * @return the consumer registered. + */ + public static MessageConsumer<String> registerSingleConfigEntryProvider(String address, EventBus eventBus){ + MessageConsumer<String> consumer = eventBus.consumer(address); + consumer.handler(h -> { + String key = (String) h.body(); + if (key == null) { + h.fail(HttpResponseStatus.BAD_REQUEST.code(), "Missing config key."); + } else { + String value = ConfigurationProvider.getConfiguration().getOrDefault(key, null); + if (value != null) { + h.reply(value); + } else { + h.fail(HttpResponseStatus.NOT_FOUND.code(), "Config key not found: " + key); + } + } + }); + return consumer; + } + + /** + * Registers a handler for accessing multiple configuration keys (input: String[] (Json), + * reply type: Map<String,String></String,String> (Json). + * @param address the event bus address to register. + * @param eventBus the event bus. + * @return the consumer registered. + */ + public static MessageConsumer<String> registerMultiConfigEntryProvider(String address, EventBus eventBus){ + MessageConsumer<String> consumer = eventBus.consumer(address); + consumer.handler(h -> { + String val = h.body(); + Configuration config = ConfigurationProvider.getConfiguration(); + Map<String,String> entries = new TreeMap<>(); + if(val!=null){ + String[] sections = Json.decodeValue(val, String[].class); + for (String section : sections) { + if(section!=null) { + entries.putAll(config.with(ConfigurationFunctions.section(section)).getProperties()); + } + } + }else{ + entries.putAll(config.getProperties()); + } + h.reply(Json.encode(entries)); + }); + return consumer; + } + + /** + * Registers a handler for configuring any objects sent via the message bus using Tamaya's injection API. + * @param address the event bus address to register. + * @param eventBus the event bus. + * @return the consumer registered. + */ + public static MessageConsumer<Object> registerConfigurationInjector(String address, EventBus eventBus){ + MessageConsumer<Object> consumer = eventBus.consumer(address); + consumer.handler(h -> { + Object o = h.body(); + if(o==null){ + h.fail(HttpResponseStatus.BAD_REQUEST.code(), "Required object to configure is missing."); + }else { + ConfigurationInjection.getConfigurationInjector().configure(o); + h.reply("OK"); + } + }); + return consumer; + } + + + + @Override + public void start(Future<Void> startFuture) throws Exception { + super.start(startFuture); + registerMultiConfigEntryProvider(mapBusTarget, vertx.eventBus()); + registerSingleConfigEntryProvider(singleBusTarget, vertx.eventBus()); + registerConfigurationInjector(injectionBusTarget, vertx.eventBus()); + startFuture.complete(); + } + + public String getInjectionBusTarget() { + return injectionBusTarget; + } + + public String getMapBusTarget() { + return mapBusTarget; + } + + public String getSingleBusTarget() { + return singleBusTarget; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/adc24cad/vertx/src/main/resources/META-INF/services/io.vertx.core.spi.VerticleFactory ---------------------------------------------------------------------- diff --git a/vertx/src/main/resources/META-INF/services/io.vertx.core.spi.VerticleFactory b/vertx/src/main/resources/META-INF/services/io.vertx.core.spi.VerticleFactory new file mode 100644 index 0000000..b335f38 --- /dev/null +++ b/vertx/src/main/resources/META-INF/services/io.vertx.core.spi.VerticleFactory @@ -0,0 +1,19 @@ +# +# 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 current 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. +# +org.apache.tamaya.vertx.ConfiguredJavaVerticleFactory \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/adc24cad/vertx/src/test/java/org/apache/tamaya/vertx/ConfigVerticleTest.java ---------------------------------------------------------------------- diff --git a/vertx/src/test/java/org/apache/tamaya/vertx/ConfigVerticleTest.java b/vertx/src/test/java/org/apache/tamaya/vertx/ConfigVerticleTest.java index 734d27a..7df8b85 100644 --- a/vertx/src/test/java/org/apache/tamaya/vertx/ConfigVerticleTest.java +++ b/vertx/src/test/java/org/apache/tamaya/vertx/ConfigVerticleTest.java @@ -48,18 +48,17 @@ public class ConfigVerticleTest { @Rule public RunTestOnContext vertxContext = new RunTestOnContext(); - private TestVerticle testVerticle = new TestVerticle(); + private TamayaConfigurationProducer producerVerticle = new TamayaConfigurationProducer(); @Before public void prepare(){ - vertxContext.vertx().deployVerticle(testVerticle); - vertxContext.vertx().deployVerticle(new ConfigVerticle()); + vertxContext.vertx().deployVerticle(producerVerticle); } @Test public void testSingle(final TestContext testContext){ final Async async = testContext.async(); - vertxContext.vertx().eventBus().send("CONFIG-VAL", + vertxContext.vertx().eventBus().send(TamayaConfigurationProducer.DEFAULT_CONFIG_GET_SINGLE_ADDRESS, "user.home", new Handler<AsyncResult<Message<String>>>() { @Override public void handle(AsyncResult<Message<String>> reply) { @@ -74,56 +73,38 @@ public class ConfigVerticleTest { @Test public void testMap(final TestContext testContext){ final Async async = testContext.async(); - String selector = "user."; - vertxContext.vertx().eventBus().send("CONFIG-MAP", - selector, new Handler<AsyncResult<Message<String>>>() { - @Override - public void handle(AsyncResult<Message<String>> reply) { - testContext.assertNotNull(reply.result()); - testContext.assertNotNull(reply.result().body()); - Map<String,String> config = Json.decodeValue(reply.result().body(), - Map.class); - Map<String,String> compareTo = ConfigurationProvider.getConfiguration() - .with(ConfigurationFunctions.filter((k,v) -> { - return k.matches("user."); - })).getProperties(); - testContext.assertEquals(config.size(), compareTo.size()); - for(Map.Entry<String,String> en:compareTo.entrySet()){ - testContext.assertEquals( - config.get(en.getKey()), en.getValue()); - } - async.complete(); + String selector = "[]{\"user.*\"}"; + vertxContext.vertx().eventBus().send(TamayaConfigurationProducer.DEFAULT_CONFIG_GET_MULTI_ADDRESS, + selector, reply -> { + testContext.assertNotNull(reply.result()); + testContext.assertNotNull(reply.result().body()); + Map<String,String> config = Json.decodeValue((String)reply.result().body(), + Map.class); + Map<String,String> compareTo = ConfigurationProvider.getConfiguration() + .with(ConfigurationFunctions.filter((k,v) -> { + return k.matches("user."); + })).getProperties(); + testContext.assertEquals(config.size(), compareTo.size()); + for(Map.Entry<String,String> en:compareTo.entrySet()){ + testContext.assertEquals( + config.get(en.getKey()), en.getValue()); } + async.complete(); }); } @Test public void testConfigCalls(TestContext testContext){ - testContext.assertNotNull(testVerticle.getConfiguration()); + testContext.assertNotNull(producerVerticle.getConfiguration()); testContext.assertEquals( - testVerticle.getConfigProperty("user.home"), + producerVerticle.getConfigValue("user.home"), System.getProperty("user.home")); testContext.assertEquals( - testVerticle.getConfigPropertyOrDefault("foo.bar", "blabla"), + producerVerticle.getOptionalConfigValue("foo.bar").orElse("blabla"), "blabla"); testContext.assertEquals( - testVerticle.getConfigPropertyOrDefault("foo.bar", BigDecimal.class, new BigDecimal("1.12345")), + producerVerticle.getOptionalConfigValue("foo.bar", BigDecimal.class).orElse(new BigDecimal("1.12345")), new BigDecimal("1.12345")); } - @Test - public void testInjection(TestContext testContext){ - testContext.assertNotNull(testVerticle.userHome); - testContext.assertNotNull(testVerticle.userName); - testContext.assertNotNull(testVerticle.anyNumber); - testContext.assertEquals( - testVerticle.userHome, - System.getProperty("user.home")); - testContext.assertEquals( - testVerticle.userName, - System.getProperty("user.name")); - testContext.assertEquals( - testVerticle.anyNumber, - new BigDecimal("1.123456789")); - } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/adc24cad/vertx/src/test/java/org/apache/tamaya/vertx/TestInjectedVerticle.java ---------------------------------------------------------------------- diff --git a/vertx/src/test/java/org/apache/tamaya/vertx/TestInjectedVerticle.java b/vertx/src/test/java/org/apache/tamaya/vertx/TestInjectedVerticle.java new file mode 100644 index 0000000..3e59dda --- /dev/null +++ b/vertx/src/test/java/org/apache/tamaya/vertx/TestInjectedVerticle.java @@ -0,0 +1,94 @@ +/* + * 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.tamaya.vertx; + +import io.vertx.core.AbstractVerticle; +import io.vertx.core.Future; +import io.vertx.core.Handler; +import io.vertx.core.buffer.Buffer; +import io.vertx.core.eventbus.DeliveryOptions; +import io.vertx.core.eventbus.Message; +import io.vertx.core.eventbus.MessageCodec; +import io.vertx.core.eventbus.MessageConsumer; +import io.vertx.core.json.Json; +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.functions.ConfigurationFunctions; +import org.apache.tamaya.functions.PropertyMatcher; +import org.apache.tamaya.inject.api.Config; + +import java.math.BigDecimal; +import java.util.Map; + +/** + * Small configured verticle for testing Tamaya's vertx support. + */ +public class TestInjectedVerticle extends AbstractVerticle{ + + @Config("user.name") + public String userName; + + @Config("user.home") + public String userHome; + + @Config(value = "any.number.BD", defaultValue = "1.123456789") + public BigDecimal anyNumber; + + @Override + public void start(Future<Void> startFuture)throws Exception{ + vertx.eventBus().registerCodec(new MessageCodec() { + @Override + public void encodeToWire(Buffer buffer, Object o) { + buffer.appendBytes(Json.encodePrettily(o).getBytes()); + } + + @Override + public Object decodeFromWire(int i, Buffer buffer) { + return null; + } + + @Override + public Object transform(Object o) { + return o; + } + + @Override + public String name() { + return "local"; + } + + @Override + public byte systemCodecID() { + return -1; + } + }); + vertx.eventBus().send(TamayaConfigurationProducer.DEFAULT_CONFIGRE_ADDRESS, this, + new DeliveryOptions().setCodecName("local"), + r -> startFuture.complete()); + } + + @Override + public String toString() { + return "TestInjectedVerticle{" + + "userName='" + userName + '\'' + + ", userHome='" + userHome + '\'' + + ", anyNumber='" + anyNumber + '\'' + + '}'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/adc24cad/vertx/src/test/java/org/apache/tamaya/vertx/TestInjectedVerticleTest.java ---------------------------------------------------------------------- diff --git a/vertx/src/test/java/org/apache/tamaya/vertx/TestInjectedVerticleTest.java b/vertx/src/test/java/org/apache/tamaya/vertx/TestInjectedVerticleTest.java new file mode 100644 index 0000000..751879e --- /dev/null +++ b/vertx/src/test/java/org/apache/tamaya/vertx/TestInjectedVerticleTest.java @@ -0,0 +1,132 @@ +/* + * 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.tamaya.vertx; + +import io.vertx.core.AsyncResult; +import io.vertx.core.Handler; +import io.vertx.core.eventbus.Message; +import io.vertx.core.json.Json; +import io.vertx.core.json.JsonArray; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.unit.Async; +import io.vertx.ext.unit.TestContext; +import io.vertx.ext.unit.junit.RunTestOnContext; +import io.vertx.ext.unit.junit.VertxUnitRunner; +import org.apache.tamaya.ConfigurationProvider; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.math.BigDecimal; +import java.util.Map; + +/** + * Tests the Tamaya Vertx configuration support. + * Created by atsticks on 08.03.17. + */ +@RunWith(VertxUnitRunner.class) +public class TestInjectedVerticleTest { + + @Rule + public RunTestOnContext vertxContext = new RunTestOnContext(); + + private TestInjectedVerticle testVerticle = new TestInjectedVerticle(); + + private TamayaConfigurationProducer producerVerticle = new TamayaConfigurationProducer(); + + @Before + public void prepare( TestContext testContext)throws Exception{ + Async async = testContext.async(); + vertxContext.vertx().deployVerticle(producerVerticle, h-> async.complete()); + } + + @Test + public void testSingle(final TestContext testContext){ + final Async async = testContext.async(); + final Async async2 = testContext.async(); + vertxContext.vertx().eventBus().send(TamayaConfigurationProducer.DEFAULT_CONFIG_GET_SINGLE_ADDRESS, + "user.home", reply -> { + testContext.assertNotNull(reply.result()); + testContext.assertNotNull(reply.result().body()); + testContext.assertEquals( + reply.result().body(), + System.getProperty("user.home")); + async.complete(); + }); + String[] arr = new String[]{"user."}; + vertxContext.vertx().eventBus().send(TamayaConfigurationProducer.DEFAULT_CONFIG_GET_MULTI_ADDRESS, + Json.encode(arr), reply -> { + testContext.assertTrue(reply.succeeded()); + testContext.assertNotNull(reply.result()); + testContext.assertNotNull(reply.result().body()); + Map<String,String> config = Json.decodeValue((String)reply.result().body(), + Map.class); + testContext.assertEquals( + config.get("user.home"), + System.getProperty("user.home")); + testContext.assertEquals( + config.get("user.name"), + System.getProperty("user.name")); + async2.complete(); + }); + } + + @Test + public void testMupltiple(final TestContext testContext){ + final Async async = testContext.async(); + String[] arr = new String[]{"user.home", "user.name","java.version"}; + vertxContext.vertx().eventBus().send(TamayaConfigurationProducer.DEFAULT_CONFIG_GET_MULTI_ADDRESS, + Json.encode(arr), reply -> { + testContext.assertNotNull(reply.result()); + testContext.assertNotNull(reply.result().body()); + Map<String,String> config = Json.decodeValue((String)reply.result().body(), Map.class); + Map<String,String> compareTo = ConfigurationProvider.getConfiguration().getProperties(); + testContext.assertEquals(config.size(), 3); + testContext.assertEquals( + config.get("user.name"), System.getProperty("user.name")); + testContext.assertEquals( + config.get("user.home"), System.getProperty("user.home")); + testContext.assertEquals( + config.get("java.version"), System.getProperty("java.version")); + async.complete(); + }); + } + + @Test + public void testInjection(TestContext testContext){ + Async async = testContext.async(); + vertxContext.vertx().deployVerticle(testVerticle, h-> { + testContext.assertNotNull(testVerticle.userHome); + testContext.assertNotNull(testVerticle.userName); + testContext.assertNotNull(testVerticle.anyNumber); + testContext.assertEquals( + testVerticle.userHome, + System.getProperty("user.home")); + testContext.assertEquals( + testVerticle.userName, + System.getProperty("user.name")); + testContext.assertEquals( + testVerticle.anyNumber, + new BigDecimal("1.123456789")); + async.complete(); + }); + + } +}