This is an automated email from the ASF dual-hosted git repository.
mikexue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/eventmesh.git
The following commit(s) were added to refs/heads/master by this push:
new a091017d9 [ISSUE#4515] Add test case for ProtocolPluginFactory. (#4516)
a091017d9 is described below
commit a091017d969204af05a0ac972193a21e7cf7b040
Author: yanrongzhen <[email protected]>
AuthorDate: Mon Oct 30 22:26:35 2023 +0800
[ISSUE#4515] Add test case for ProtocolPluginFactory. (#4516)
* Add test case for ProtocolPluginFactory.
* Add aftereach, change test case name.
* Add license header.
* Merge test cases.
* Rm @aftereach.
---
.../protocol/api/MockProtocolAdaptorImpl.java | 48 +++++++++++++++
.../protocol/api/ProtocolPluginFactoryTest.java | 69 ++++++++++++++++++++++
...g.apache.eventmesh.protocol.api.ProtocolAdaptor | 17 ++++++
3 files changed, 134 insertions(+)
diff --git
a/eventmesh-protocol-plugin/eventmesh-protocol-api/src/test/java/org/apache/eventmesh/protocol/api/MockProtocolAdaptorImpl.java
b/eventmesh-protocol-plugin/eventmesh-protocol-api/src/test/java/org/apache/eventmesh/protocol/api/MockProtocolAdaptorImpl.java
new file mode 100644
index 000000000..648f3e222
--- /dev/null
+++
b/eventmesh-protocol-plugin/eventmesh-protocol-api/src/test/java/org/apache/eventmesh/protocol/api/MockProtocolAdaptorImpl.java
@@ -0,0 +1,48 @@
+/*
+ * 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.eventmesh.protocol.api;
+
+import org.apache.eventmesh.common.protocol.ProtocolTransportObject;
+import org.apache.eventmesh.protocol.api.exception.ProtocolHandleException;
+
+import java.util.List;
+
+import io.cloudevents.CloudEvent;
+
+public class MockProtocolAdaptorImpl implements
ProtocolAdaptor<ProtocolTransportObject> {
+
+ @Override
+ public CloudEvent toCloudEvent(ProtocolTransportObject protocol) throws
ProtocolHandleException {
+ return null;
+ }
+
+ @Override
+ public List<CloudEvent> toBatchCloudEvent(ProtocolTransportObject
protocol) throws ProtocolHandleException {
+ return null;
+ }
+
+ @Override
+ public ProtocolTransportObject fromCloudEvent(CloudEvent cloudEvent)
throws ProtocolHandleException {
+ return null;
+ }
+
+ @Override
+ public String getProtocolType() {
+ return null;
+ }
+}
diff --git
a/eventmesh-protocol-plugin/eventmesh-protocol-api/src/test/java/org/apache/eventmesh/protocol/api/ProtocolPluginFactoryTest.java
b/eventmesh-protocol-plugin/eventmesh-protocol-api/src/test/java/org/apache/eventmesh/protocol/api/ProtocolPluginFactoryTest.java
new file mode 100644
index 000000000..1aa35e0c6
--- /dev/null
+++
b/eventmesh-protocol-plugin/eventmesh-protocol-api/src/test/java/org/apache/eventmesh/protocol/api/ProtocolPluginFactoryTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.eventmesh.protocol.api;
+
+import org.apache.eventmesh.common.protocol.ProtocolTransportObject;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import com.google.common.collect.Maps;
+
+public class ProtocolPluginFactoryTest {
+
+ private static final String PROTOCOL_TYPE_NAME = "testProtocolType";
+
+ private static final String MODIFIERS = "modifiers";
+
+ private static final String PROTOCOL_ADAPTER_MAP = "PROTOCOL_ADAPTOR_MAP";
+
+ @Test
+ public void testGetProtocolAdaptor() throws IllegalAccessException,
NoSuchFieldException {
+ Map<String, ProtocolAdaptor<ProtocolTransportObject>>
mockProtocolAdaptorMap =
+ new ConcurrentHashMap<>(16);
+ ProtocolAdaptor<ProtocolTransportObject> expectedAdaptor = new
MockProtocolAdaptorImpl();
+ mockProtocolAdaptorMap.put(PROTOCOL_TYPE_NAME, expectedAdaptor);
+
+ Field field =
ProtocolPluginFactory.class.getDeclaredField(PROTOCOL_ADAPTER_MAP);
+ field.setAccessible(true);
+ Field modifiersField = Field.class.getDeclaredField(MODIFIERS);
+ modifiersField.setAccessible(true);
+ modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
+ final Object originMap = field.get(null);
+ field.set(null, mockProtocolAdaptorMap);
+
+ ProtocolAdaptor<ProtocolTransportObject> actualAdaptor =
ProtocolPluginFactory.getProtocolAdaptor(PROTOCOL_TYPE_NAME);
+ Assertions.assertEquals(expectedAdaptor, actualAdaptor);
+
+ field.set(null, Maps.newHashMap());
+ ProtocolAdaptor<ProtocolTransportObject> adaptor =
ProtocolPluginFactory.getProtocolAdaptor(PROTOCOL_TYPE_NAME);
+ Assertions.assertEquals(adaptor.getClass(),
MockProtocolAdaptorImpl.class);
+
+ field.set(null, originMap);
+ }
+
+ @Test
+ public void testGetProtocolAdaptorThrowsException() {
+ Assertions.assertThrows(IllegalArgumentException.class, () ->
ProtocolPluginFactory.getProtocolAdaptor("empty_type_name"));
+ }
+}
diff --git
a/eventmesh-protocol-plugin/eventmesh-protocol-api/src/test/resources/META-INF/eventmesh/org.apache.eventmesh.protocol.api.ProtocolAdaptor
b/eventmesh-protocol-plugin/eventmesh-protocol-api/src/test/resources/META-INF/eventmesh/org.apache.eventmesh.protocol.api.ProtocolAdaptor
new file mode 100644
index 000000000..6dd5895bf
--- /dev/null
+++
b/eventmesh-protocol-plugin/eventmesh-protocol-api/src/test/resources/META-INF/eventmesh/org.apache.eventmesh.protocol.api.ProtocolAdaptor
@@ -0,0 +1,17 @@
+#
+# 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.
+#
+testProtocolType=org.apache.eventmesh.protocol.api.MockProtocolAdaptorImpl
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]