This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-3.7.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-3.7.x by this push:
     new 837926a  CAMEL-16032 [camel-main] autoconfiguration does not bind 
dataformat in the registry (#4887)
837926a is described below

commit 837926ab18716cb7bd79e7e0835069758847c351
Author: Luigi De Masi <5583341+luigidem...@users.noreply.github.com>
AuthorDate: Fri Jan 15 14:30:24 2021 +0100

    CAMEL-16032 [camel-main] autoconfiguration does not bind dataformat in the 
registry (#4887)
---
 .../camel/impl/engine/AbstractCamelContext.java    | 26 ++++-----
 .../dataformat/DataFormatRegistrationTest.java     | 57 ++++++++++++++++++++
 .../camel/model/dataformat/DummyDataformat.java    | 63 ++++++++++++++++++++++
 .../services/org/apache/camel/dataformat/dummy     |  1 +
 4 files changed, 134 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 b5d7364..c4488fe 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;
@@ -3781,25 +3782,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/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

Reply via email to