You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2022/04/04 12:33:18 UTC

[pulsar] 09/15: [fix][admin] Fix NPE in PulsarAdminBuilder when the service is not set (#14769)

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

penghui pushed a commit to branch branch-2.8
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit e067e00808292f3330575aa6409c30e56f703e6b
Author: Zike Yang <zk...@streamnative.io>
AuthorDate: Tue Mar 22 18:29:23 2022 +0800

    [fix][admin] Fix NPE in PulsarAdminBuilder when the service is not set (#14769)
    
    (cherry picked from commit 0b6b1e2079a9afe92527df2cdb0e843a0ae391dd)
---
 .../client/admin/internal/PulsarAdminImpl.java     |  3 ++
 .../admin/internal/PulsarAdminBuilderImplTest.java | 38 ++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/PulsarAdminImpl.java b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/PulsarAdminImpl.java
index e4fdf132896..b80799fffc9 100644
--- a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/PulsarAdminImpl.java
+++ b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/PulsarAdminImpl.java
@@ -18,6 +18,7 @@
  */
 package org.apache.pulsar.client.admin.internal;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import java.io.IOException;
 import java.net.URL;
 import java.util.Map;
@@ -146,6 +147,8 @@ public class PulsarAdminImpl implements PulsarAdmin {
                        int autoCertRefreshTime,
                        TimeUnit autoCertRefreshTimeUnit,
                        ClassLoader clientBuilderClassLoader) throws PulsarClientException {
+        checkArgument(StringUtils.isNotBlank(serviceUrl), "Service URL needs to be specified");
+
         this.connectTimeout = connectTimeout;
         this.connectTimeoutUnit = connectTimeoutUnit;
         this.readTimeout = readTimeout;
diff --git a/pulsar-client-admin/src/test/java/org/apache/pulsar/client/admin/internal/PulsarAdminBuilderImplTest.java b/pulsar-client-admin/src/test/java/org/apache/pulsar/client/admin/internal/PulsarAdminBuilderImplTest.java
new file mode 100644
index 00000000000..1ea45401eec
--- /dev/null
+++ b/pulsar-client-admin/src/test/java/org/apache/pulsar/client/admin/internal/PulsarAdminBuilderImplTest.java
@@ -0,0 +1,38 @@
+/**
+ * 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.pulsar.client.admin.internal;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.fail;
+import org.apache.pulsar.client.admin.PulsarAdmin;
+import org.apache.pulsar.client.api.PulsarClientException;
+import org.testng.annotations.Test;
+
+public class PulsarAdminBuilderImplTest {
+
+    @Test
+    public void testAdminBuilderWithServiceUrlNotSet() throws PulsarClientException {
+        try{
+            PulsarAdmin.builder().build();
+            fail();
+        } catch (IllegalArgumentException exception) {
+            assertEquals("Service URL needs to be specified", exception.getMessage());
+        }
+    }
+}