You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2020/05/22 07:27:49 UTC

[servicecomb-pack] 01/01: SCB--1928 Fixed the ServiceInstance length check on ServiceConfig

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

ningjiang pushed a commit to branch SCB-1928
in repository https://gitbox.apache.org/repos/asf/servicecomb-pack.git

commit 6ff4f0d538cfc4db2786b91a77b88b37db1a2988
Author: Willem Jiang <wi...@gmail.com>
AuthorDate: Fri May 22 15:26:41 2020 +0800

    SCB--1928 Fixed the ServiceInstance length check on ServiceConfig
---
 .../pack/omega/context/ServiceConfig.java          |  7 +--
 .../pack/omega/context/ServiceConfigTest.java      | 51 ++++++++++++++++++++++
 2 files changed, 55 insertions(+), 3 deletions(-)

diff --git a/omega/omega-context/src/main/java/org/apache/servicecomb/pack/omega/context/ServiceConfig.java b/omega/omega-context/src/main/java/org/apache/servicecomb/pack/omega/context/ServiceConfig.java
index d9b0aa0..ef8f458 100644
--- a/omega/omega-context/src/main/java/org/apache/servicecomb/pack/omega/context/ServiceConfig.java
+++ b/omega/omega-context/src/main/java/org/apache/servicecomb/pack/omega/context/ServiceConfig.java
@@ -40,11 +40,12 @@ public class ServiceConfig {
       }
     }else{
       instanceId = instanceId.trim();
-      if (instanceId.length() > MAX_LENGTH) {
-        throw new IllegalArgumentException(String.format("The instanceId length exceeds maximum length limit [%d].", MAX_LENGTH));
-      }
       this.instanceId = instanceId;
     }
+
+    if (this.instanceId.length() > MAX_LENGTH) {
+      throw new IllegalArgumentException(String.format("The instanceId length exceeds maximum length limit [%d].", MAX_LENGTH));
+    }
   }
 
   public String serviceName() {
diff --git a/omega/omega-context/src/test/java/org/apache/servicecomb/pack/omega/context/ServiceConfigTest.java b/omega/omega-context/src/test/java/org/apache/servicecomb/pack/omega/context/ServiceConfigTest.java
new file mode 100644
index 0000000..edf0b72
--- /dev/null
+++ b/omega/omega-context/src/test/java/org/apache/servicecomb/pack/omega/context/ServiceConfigTest.java
@@ -0,0 +1,51 @@
+/*
+ * 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.servicecomb.pack.omega.context;
+
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class ServiceConfigTest {
+  final String LONG_NAME =  "ABCDEFGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABCDEFG"
+      + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
+      + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
+      + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
+      + "ABCDEFGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+  final String SERVICE_NAME = "Service";
+  final String INSTANCE_NAME = "Instance";
+
+  @Test
+  public void serviceConfigWithoutServiceInstance() {
+    ServiceConfig serviceConfig = new ServiceConfig(SERVICE_NAME);
+    serviceConfig.serviceName();
+    assertThat(serviceConfig.serviceName(), is(SERVICE_NAME));
+    assertTrue(serviceConfig.instanceId().startsWith(SERVICE_NAME));
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void serviceConfigWithLongServiceName() {
+    ServiceConfig serviceConfig = new ServiceConfig(LONG_NAME);
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void serviceConfigWithLongServiceInstanceName() {
+    ServiceConfig serviceConfig = new ServiceConfig(SERVICE_NAME, LONG_NAME);
+  }
+}