utafrali commented on code in PR #11303:
URL: https://github.com/apache/rocketmq/pull/11303#discussion_r4143952960


##########
store/src/test/java/org/apache/rocketmq/store/plugin/MessageStoreFactoryTest.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.rocketmq.store.plugin;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.fail;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.concurrent.CompletableFuture;
+import org.apache.rocketmq.common.BrokerConfig;
+import org.apache.rocketmq.store.MessageStore;
+import org.apache.rocketmq.store.QueryMessageResult;
+import org.apache.rocketmq.store.config.MessageStoreConfig;
+import org.apache.rocketmq.store.rocksdb.MessageRocksDBStorage;
+import org.apache.rocketmq.store.timer.rocksdb.TimerMessageRocksDBStore;
+import org.apache.rocketmq.store.transaction.TransMessageRocksDBStore;
+import org.junit.Test;
+
+public class MessageStoreFactoryTest {
+
+    private static final String MISSING_PLUGIN_CLASS =
+        "org.apache.rocketmq.store.plugin.NoSuchPluginClassSurelyMissing";
+    private static final String THROWING_PLUGIN =
+        MessageStoreFactoryTest.class.getName() + "$ThrowingPlugin";
+    private static final String OUTER_PLUGIN =
+        MessageStoreFactoryTest.class.getName() + "$OuterPlugin";
+    private static final String INNER_PLUGIN =
+        MessageStoreFactoryTest.class.getName() + "$InnerPlugin";
+
+    private MessageStorePluginContext buildContext(String plugin) {
+        BrokerConfig brokerConfig = new BrokerConfig();
+        brokerConfig.setMessageStorePlugIn(plugin);
+        return new MessageStorePluginContext(new MessageStoreConfig(), null, 
null, brokerConfig, null);
+    }
+
+    @Test

Review Comment:
   The `try { build(...); fail(...); } catch (RuntimeException e) { 
assertThat(...) }` pattern can be replaced by AssertJ's `assertThatThrownBy` 
for a cleaner and less error-prone form, e.g.:
   
   ```java
   assertThatThrownBy(() -> MessageStoreFactory.build(context, null))
       .isInstanceOf(RuntimeException.class)
       .hasMessageContaining("Failed to initialize message store plugin")
       .hasMessageContaining("NoSuchPluginClassSurelyMissing")
       .hasCauseInstanceOf(ClassNotFoundException.class);
   ```
   
   This also removes the risk of a silently passing test if `fail(...)` ever 
gets refactored out. Same applies to `testPluginConstructorThrows`.



##########
store/src/test/java/org/apache/rocketmq/store/plugin/MessageStoreFactoryTest.java:
##########
@@ -0,0 +1,185 @@
+/*

Review Comment:
   This new file was committed with mode `100755` (executable). Java source 
files in this repo are `100644` — please `chmod 644` and re-commit so the file 
mode matches the rest of `store/src/test/java`.



##########
store/src/test/java/org/apache/rocketmq/store/plugin/MessageStoreFactoryTest.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.rocketmq.store.plugin;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.fail;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.concurrent.CompletableFuture;
+import org.apache.rocketmq.common.BrokerConfig;
+import org.apache.rocketmq.store.MessageStore;
+import org.apache.rocketmq.store.QueryMessageResult;
+import org.apache.rocketmq.store.config.MessageStoreConfig;
+import org.apache.rocketmq.store.rocksdb.MessageRocksDBStorage;
+import org.apache.rocketmq.store.timer.rocksdb.TimerMessageRocksDBStore;
+import org.apache.rocketmq.store.transaction.TransMessageRocksDBStore;
+import org.junit.Test;
+
+public class MessageStoreFactoryTest {
+
+    private static final String MISSING_PLUGIN_CLASS =
+        "org.apache.rocketmq.store.plugin.NoSuchPluginClassSurelyMissing";
+    private static final String THROWING_PLUGIN =
+        MessageStoreFactoryTest.class.getName() + "$ThrowingPlugin";
+    private static final String OUTER_PLUGIN =
+        MessageStoreFactoryTest.class.getName() + "$OuterPlugin";
+    private static final String INNER_PLUGIN =
+        MessageStoreFactoryTest.class.getName() + "$InnerPlugin";
+
+    private MessageStorePluginContext buildContext(String plugin) {
+        BrokerConfig brokerConfig = new BrokerConfig();
+        brokerConfig.setMessageStorePlugIn(plugin);
+        return new MessageStorePluginContext(new MessageStoreConfig(), null, 
null, brokerConfig, null);
+    }
+
+    @Test
+    public void testPluginClassNotExist() throws Exception {
+        MessageStorePluginContext context = buildContext(MISSING_PLUGIN_CLASS);
+        try {
+            MessageStoreFactory.build(context, null);
+            fail("build should fail when the plugin class does not exist");
+        } catch (RuntimeException e) {
+            assertThat(e.getMessage())
+                .contains("Failed to initialize message store plugin")
+                .contains("NoSuchPluginClassSurelyMissing");
+            
assertThat(e.getCause()).isInstanceOf(ClassNotFoundException.class);
+        }
+    }
+
+    @Test
+    public void testPluginConstructorThrows() throws Exception {
+        MessageStorePluginContext context = buildContext(THROWING_PLUGIN);
+        try {
+            MessageStoreFactory.build(context, null);
+            fail("build should fail when the plugin constructor throws");
+        } catch (RuntimeException e) {
+            assertThat(e.getMessage())
+                .contains("Failed to initialize message store plugin")
+                .contains(THROWING_PLUGIN)
+                .doesNotContain("not found");
+            
assertThat(e.getCause()).isInstanceOf(InvocationTargetException.class);
+            Throwable rootCause = e.getCause().getCause();
+            assertThat(rootCause).isInstanceOf(IllegalStateException.class);
+            assertThat(rootCause).hasMessage("boom");
+        }
+    }
+
+    @Test
+    public void testBuildSinglePlugin() throws Exception {
+        MessageStorePluginContext context = buildContext(INNER_PLUGIN);
+        MessageStore store = MessageStoreFactory.build(context, null);
+        assertThat(store).isInstanceOf(InnerPlugin.class);
+        assertThat(((InnerPlugin) store).getNext()).isNull();
+        assertThat(((InnerPlugin) store).context).isSameAs(context);
+    }
+
+    @Test
+    public void testBuildChainsPluginsInConfigOrder() throws Exception {
+        // no space after the comma: build splits the config on "," without 
trimming entries
+        MessageStorePluginContext context = buildContext(OUTER_PLUGIN + "," + 
INNER_PLUGIN);
+        MessageStore store = MessageStoreFactory.build(context, null);
+        assertThat(store).isInstanceOf(OuterPlugin.class);
+        AbstractPluginMessageStore outer = (AbstractPluginMessageStore) store;
+        assertThat(outer.getNext()).isInstanceOf(InnerPlugin.class);
+        AbstractPluginMessageStore inner = (AbstractPluginMessageStore) 
outer.getNext();
+        assertThat(inner.getNext()).isNull();
+        assertThat(outer.context).isSameAs(context);
+        assertThat(inner.context).isSameAs(context);
+    }
+

Review Comment:
   The Javadoc comment `"AbstractPluginMessageStore leaves a few MessageStore 
methods unimplemented in this branch"` is ambiguous — "this branch" reads like 
the PR branch, but the intent is presumably "in the current `develop` state of 
`AbstractPluginMessageStore`". Consider rephrasing to something like 
"`AbstractPluginMessageStore` does not delegate these methods; concrete plugins 
used by the tests fill the gap here." so future readers aren't confused.



##########
store/src/main/java/org/apache/rocketmq/store/plugin/MessageStoreFactory.java:
##########
@@ -36,7 +36,7 @@ public static MessageStore build(MessageStorePluginContext 
context,
                     AbstractPluginMessageStore pluginMessageStore = 
construct.newInstance(context, messageStore);
                     messageStore = pluginMessageStore;
                 } catch (Throwable e) {
-                    throw new RuntimeException("Initialize plugin's class: " + 
pluginClass + " not found!", e);
+                    throw new RuntimeException("Failed to initialize message 
store plugin: " + pluginClass, e);

Review Comment:
   Optional: since the original message hinted at the classpath, it may help 
operators to log at ERROR before throwing so that the cause chain shows up in 
the broker log even if the `RuntimeException` is later re-wrapped. A one-liner 
via `org.apache.rocketmq.logging.org.slf4j.Logger` would be consistent with 
other classes in this package.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to