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

wy471x pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 989a950180 fix(mock): sample zh lengths from the inclusive range like 
the en sibling (#7366)
989a950180 is described below

commit 989a9501809b7069a584c1f4d9b5124fcbdac446
Author: Sean-Walker0 <[email protected]>
AuthorDate: Wed Sep 30 06:48:29 2026 +0800

    fix(mock): sample zh lengths from the inclusive range like the en sibling 
(#7366)
    
    MockUtil.zh drew its length with random.nextInt(max - min - 1) + min,
    whose exclusive bound caps lengths at max-2, and throws
    IllegalArgumentException (bound must be positive) on every call when
    max == min + 1 - so the registered expression #zh(2,3) always failed
    instead of returning a short Chinese string. The en sibling and every
    other method in this class sample through RandomUtil.randomInt(min,
    max), which is inclusive on both ends; zh now does the same.
    
    testZhDataGenerate asserted the quoted expression output against the
    raw range, which only held under the truncated distribution; it now
    asserts the quoted content spans [min, max] exactly like the
    testEnDataGenerate contract. The new MockUtilTest cases fail on current
    master (IllegalArgumentException for zh(2,3), upper half of the range
    never reached for zh(2,5)) and pass with this change.
    
    Co-authored-by: Sean-Walker0 
<[email protected]>
    Co-authored-by: wy471x <[email protected]>
---
 .../apache/shenyu/plugin/mock/util/MockUtil.java   |  4 +-
 .../mock/generator/ExpressionGeneratorTest.java    |  3 +-
 .../shenyu/plugin/mock/util/MockUtilTest.java      | 47 ++++++++++++++++++++++
 3 files changed, 50 insertions(+), 4 deletions(-)

diff --git 
a/shenyu-plugin/shenyu-plugin-mock/src/main/java/org/apache/shenyu/plugin/mock/util/MockUtil.java
 
b/shenyu-plugin/shenyu-plugin-mock/src/main/java/org/apache/shenyu/plugin/mock/util/MockUtil.java
index b23a18c32e..7880149f4e 100644
--- 
a/shenyu-plugin/shenyu-plugin-mock/src/main/java/org/apache/shenyu/plugin/mock/util/MockUtil.java
+++ 
b/shenyu-plugin/shenyu-plugin-mock/src/main/java/org/apache/shenyu/plugin/mock/util/MockUtil.java
@@ -27,7 +27,6 @@ import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.Arrays;
 import java.util.Objects;
-import java.util.Random;
 
 import static 
org.apache.shenyu.plugin.mock.util.RandomUtil.randomLowerLetterString;
 
@@ -114,8 +113,7 @@ public final class MockUtil {
      * @return chinese string
      */
     public static String zh(final int min, final int max) {
-        Random random = new Random();
-        int len = random.nextInt(max - min - 1) + min;
+        int len = RandomUtil.randomInt(min, max);
         return RandomStringUtils.random(len, 0x4e00, 0x9fa5, false, false);
     }
     
diff --git 
a/shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/generator/ExpressionGeneratorTest.java
 
b/shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/generator/ExpressionGeneratorTest.java
index af4cbfcd20..01c7220617 100644
--- 
a/shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/generator/ExpressionGeneratorTest.java
+++ 
b/shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/generator/ExpressionGeneratorTest.java
@@ -189,7 +189,8 @@ public class ExpressionGeneratorTest {
         int minLength = 10;
         int maxLength = 20;
         String val = generator.generate(String.format("expression|#zh(%d,%d)", 
minLength, maxLength), mockRequest);
-        assertThat(val.length(), allOf(greaterThanOrEqualTo(minLength), 
lessThanOrEqualTo(maxLength)));
+        // same contract as testEnDataGenerate: the quoted content spans [min, 
max] inclusively
+        assertThat(val, matchesRegex("\"[\\u4e00-\\u9fa5]{" + minLength + "," 
+ maxLength + "}\""));
     }
     
     @Test
diff --git 
a/shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/util/MockUtilTest.java
 
b/shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/util/MockUtilTest.java
new file mode 100644
index 0000000000..e411d09129
--- /dev/null
+++ 
b/shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/util/MockUtilTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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.shenyu.plugin.mock.util;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Test case for {@link MockUtil}.
+ */
+public final class MockUtilTest {
+
+    @Test
+    public void zhShouldReturnLengthsWithinTheInclusiveRange() {
+        for (int i = 0; i < 500; i++) {
+            String value = MockUtil.zh(2, 3);
+            int length = value.length();
+            assertTrue(length >= 2 && length <= 3, "zh(2,3) length must be 2 
or 3 but was " + length);
+        }
+    }
+
+    @Test
+    public void zhShouldCoverTheUpperHalfOfTheRange() {
+        // the sibling en() samples [min, max] inclusively; over 500 draws 
zh(2,5) must reach 4 or 5
+        int maxLength = 0;
+        for (int i = 0; i < 500; i++) {
+            maxLength = Math.max(maxLength, MockUtil.zh(2, 5).length());
+        }
+        assertTrue(maxLength >= 4, "zh(2,5) must be able to produce lengths up 
to 5 but max observed was " + maxLength);
+    }
+}

Reply via email to