This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit 658c94da47c13f39641fbeb2489a925ac9779eb7 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Fri Jan 15 16:28:24 2021 +0100 CAMEL-16032 [camel-main] autoconfiguration does not bind dataformat in the registry (#4887) This reverts commit d145296dcfb875590407dbff4c7cc5f3ed3e7aee. --- .../camel/impl/engine/AbstractCamelContext.java | 26 ++++----- .../camel/impl/MultipleComponentInstancesTest.java | 16 ++++++ .../dataformat/DataFormatRegistrationTest.java | 57 ++++++++++++++++++++ .../camel/model/dataformat/DummyDataformat.java | 63 ++++++++++++++++++++++ .../services/org/apache/camel/dataformat/dummy | 1 + 5 files changed, 150 insertions(+), 13 deletions(-) diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java index 77db586..ca96cce 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java @@ -29,6 +29,7 @@ import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Properties; import java.util.Set; import java.util.TreeMap; @@ -3818,25 +3819,24 @@ public abstract class AbstractCamelContext extends BaseService @Override public DataFormat resolveDataFormat(String name) { - final DataFormat answer = dataformats.computeIfAbsent(name, new Function<String, DataFormat>() { - @Override - public DataFormat apply(String s) { - DataFormat df = ResolverHelper.lookupDataFormatInRegistryWithFallback(getCamelContextReference(), name); + final DataFormat answer = dataformats.computeIfAbsent(name, s -> { + DataFormat df = Optional + .ofNullable(ResolverHelper.lookupDataFormatInRegistryWithFallback(getCamelContextReference(), name)) + .orElseGet(() -> getDataFormatResolver().createDataFormat(name, getCamelContextReference())); - if (df != null) { - // inject CamelContext if aware - CamelContextAware.trySetCamelContext(df, getCamelContextReference()); + if (df != null) { + // inject CamelContext if aware + CamelContextAware.trySetCamelContext(df, getCamelContextReference()); - for (LifecycleStrategy strategy : lifecycleStrategies) { - strategy.onDataFormatCreated(name, df); - } + for (LifecycleStrategy strategy : lifecycleStrategies) { + strategy.onDataFormatCreated(name, df); } - - return df; } + + return df; }); - return answer != null ? answer : createDataFormat(name); + return answer; } @Override diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/MultipleComponentInstancesTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/MultipleComponentInstancesTest.java index 2fd0ef5..5b26a17 100644 --- a/core/camel-core/src/test/java/org/apache/camel/impl/MultipleComponentInstancesTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/impl/MultipleComponentInstancesTest.java @@ -1,3 +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 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.camel.impl; import org.apache.camel.CamelContext; diff --git a/core/camel-core/src/test/java/org/apache/camel/model/dataformat/DataFormatRegistrationTest.java b/core/camel-core/src/test/java/org/apache/camel/model/dataformat/DataFormatRegistrationTest.java new file mode 100644 index 0000000..8cafddd --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/model/dataformat/DataFormatRegistrationTest.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.camel.model.dataformat; + +import java.lang.reflect.Field; +import java.util.Map; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.impl.engine.AbstractCamelContext; +import org.apache.camel.spi.DataFormat; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +//CAMEL-16032 +public class DataFormatRegistrationTest extends ContextTestSupport { + + @Test + public void shouldRegisterDataformatInCacheTest() throws Exception { + DummyDataformat df = (DummyDataformat) context.resolveDataFormat("dummy"); + df.setName("DUMMMY"); + df.setVersion("2.3.6"); + Field field = AbstractCamelContext.class.getDeclaredField("dataformats"); + field.setAccessible(true); + Map<String, DataFormat> dataformats = (Map) field.get(context); + assertThat(dataformats).containsKey("dummy"); + + DummyDataformat df2 = (DummyDataformat) context.resolveDataFormat("dummy"); + dataformats = (Map) field.get(context); + assertThat(dataformats).containsKey("dummy"); + assertThat(df2.getName()).isEqualTo(df.getName()).isEqualTo("DUMMMY"); + assertThat(df2.getVersion()).isEqualTo(df.getVersion()).isEqualTo("2.3.6"); + } + + @Test + public void missingDataformatTest() throws Exception { + DataFormat df = context.resolveDataFormat("nonExistent"); + Field field = AbstractCamelContext.class.getDeclaredField("dataformats"); + field.setAccessible(true); + Map<String, DataFormat> dataformats = (Map) field.get(context); + assertThat(dataformats).isEmpty(); + } +} diff --git a/core/camel-core/src/test/java/org/apache/camel/model/dataformat/DummyDataformat.java b/core/camel-core/src/test/java/org/apache/camel/model/dataformat/DummyDataformat.java new file mode 100644 index 0000000..a736d23 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/model/dataformat/DummyDataformat.java @@ -0,0 +1,63 @@ +/* + * 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.camel.model.dataformat; + +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.camel.Exchange; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatName; +import org.apache.camel.support.service.ServiceSupport; + +public class DummyDataformat extends ServiceSupport implements DataFormat, DataFormatName { + + private String name; + + private String version; + + @Override + public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception { + + } + + @Override + public Object unmarshal(Exchange exchange, InputStream stream) throws Exception { + return null; + } + + @Override + public String getDataFormatName() { + return "dummy"; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } +} diff --git a/core/camel-core/src/test/resources/META-INF/services/org/apache/camel/dataformat/dummy b/core/camel-core/src/test/resources/META-INF/services/org/apache/camel/dataformat/dummy new file mode 100644 index 0000000..3a22a41 --- /dev/null +++ b/core/camel-core/src/test/resources/META-INF/services/org/apache/camel/dataformat/dummy @@ -0,0 +1 @@ +class=org.apache.camel.model.dataformat.DummyDataformat \ No newline at end of file