6262import java .util .List ;
6363import java .util .Map ;
6464import java .util .Objects ;
65- import java .util .Random ;
6665import java .util .Set ;
6766import java .util .concurrent .ThreadLocalRandom ;
6867import java .util .function .Function ;
@@ -178,7 +177,7 @@ else if (fieldDefinition.getType().equals(FieldDefinition.ColumnType.String))
178177 else if (fieldDefinition .getType ().equals (FieldDefinition .ColumnType .TextChoice ))
179178 {
180179 FieldDefinition .TextChoiceValidator validator =
181- (FieldDefinition .TextChoiceValidator ) fieldDefinition .getValidators ().get ( 0 );
180+ (FieldDefinition .TextChoiceValidator ) fieldDefinition .getValidators ().getFirst ( );
182181 List <String > textChoices = validator .getValues ();
183182 int textChoiceIndex = i % textChoices .size ();
184183 if (forGridInsert )
@@ -432,10 +431,7 @@ private Map<String, Object> generateRow()
432431 final Map <String , Object > newRow = new CaseInsensitiveHashMap <>();
433432 for (String columnName : _columns .keySet ())
434433 {
435- if (!_dataSuppliers .containsKey (columnName ))
436- {
437- _dataSuppliers .put (columnName , getDefaultDataSupplier (_columns .get (columnName )));
438- }
434+ _dataSuppliers .computeIfAbsent (columnName , k -> getDefaultDataSupplier (_columns .get (k )));
439435
440436 if (_autoGeneratedFields .contains (columnName ))
441437 {
@@ -504,12 +500,18 @@ public static String randomString(int size)
504500 return randomString (size , null );
505501 }
506502
507- public static List <String > randomTextChoice (int size )
503+ public static List <String > randomTextChoice (int size , @ Nullable String exclusion )
508504 {
509505 Set <String > textChoices = new LinkedHashSet <>();
506+ int attempts = 0 ;
507+ final int maxTries = Math .max (MAX_RANDOM_TRIES , size * 2 );
510508 while (textChoices .size () < size )
511509 {
512- String generated = randomString (randomInt (1 , 25 ), ";" ).trim ();
510+ if (++attempts >= maxTries )
511+ {
512+ throw new IllegalStateException ("Failed to generate " + size + " unique text choices after " + maxTries + " attempts" );
513+ }
514+ String generated = randomString (randomInt (1 , 25 ), exclusion ).trim ();
513515 if (!generated .isEmpty ())
514516 {
515517 textChoices .add (generated );
@@ -518,6 +520,11 @@ public static List<String> randomTextChoice(int size)
518520 return List .copyOf (textChoices );
519521 }
520522
523+ public static List <String > randomTextChoice (int size )
524+ {
525+ return randomTextChoice (size , null );
526+ }
527+
521528 public static String randomString (int size , @ Nullable String exclusion )
522529 {
523530 return randomString (size , exclusion , CHARSET_STRING );
@@ -532,13 +539,13 @@ public static String randomString(int size, @Nullable String exclusion, @Nullabl
532539 StringBuilder val = new StringBuilder ();
533540 for (int i =0 ; i <size ; i ++)
534541 {
535- int randIndex = ( int ) (charSetFrom .length () * Math . random ());
542+ int randIndex = ThreadLocalRandom . current (). nextInt (charSetFrom .length ());
536543 char c = charSetFrom .charAt (randIndex );
537544 if (c == REPEAT_PLACEHOLDER )
538545 {
539- randIndex = ( int ) (charSetFrom .length () * Math . random ());
546+ randIndex = ThreadLocalRandom . current (). nextInt (charSetFrom .length ());
540547 c = charSetFrom .charAt (randIndex );
541- int repeatCount = randomInt (2 , 50 ); // repeat between 2 and 50 times
548+ int repeatCount = randomInt (2 , 5 ); // repeat between 2 and 5 times
542549 val .append (StringUtils .repeat (c , repeatCount ));
543550 }
544551 else if (c == ALL_CHARS_PLACEHOLDER )
@@ -548,7 +555,8 @@ else if (c == WIDE_PLACEHOLDER)
548555 else
549556 val .append (c );
550557 }
551- return val .toString ();
558+ // Collapse consecutive spaces into one to match what the UI displays.
559+ return val .toString ().replaceAll (" {2,}" , " " );
552560 }
553561
554562 public static String randomMultiLineString (int size )
@@ -684,7 +692,8 @@ public static String randomFieldName(@NotNull String part, @Nullable Integer num
684692 }
685693
686694 TestLogger .log ("Generated random field name for domainKind " + _domainKind + ": " + randomFieldName );
687- return randomFieldName .name ();
695+ // Consistent with randomDomainName: UI collapses multiple whitespace chars to a single space
696+ return randomFieldName .name ().replaceAll ("\\ s+" , " " );
688697 }
689698
690699 private static boolean isDomainAndFieldNameInvalid (DomainKind domainKind , @ Nullable RandomName domainName , @ Nullable RandomName fieldName )
@@ -994,7 +1003,7 @@ public ImportDataResponse importRows(Connection cn, List<Map<String, Object>> ro
9941003
9951004 public static <T > List <T > shuffleSelect (List <T > allFields )
9961005 {
997- int randomSize = new Random ().nextInt (allFields .size ()) + 1 ;
1006+ int randomSize = ThreadLocalRandom . current ().nextInt (allFields .size ()) + 1 ;
9981007 return shuffleSelect (allFields , randomSize );
9991008 }
10001009
@@ -1027,7 +1036,7 @@ public static <T> List<T> randomSelect(List<T> allOptions, int selectCount)
10271036 List <T > selected = new ArrayList <>();
10281037 for (int i = 0 ; i < selectCount ; i ++)
10291038 {
1030- selected .add (allOptions .get (randomInt (0 , allOptions .size ())));
1039+ selected .add (allOptions .get (randomInt (0 , allOptions .size () - 1 )));
10311040 }
10321041 return selected ;
10331042 }
0 commit comments