Skip to content

Commit 6860521

Browse files
tomas-sexeniantomas-sexenian
andauthored
Format method fix and new unit tests (#671)
* Format method remake and new unit tests Issue:97362 --------- Co-authored-by: tomas-sexenian <tsexenian@genexus.com>
1 parent 4a7be74 commit 6860521

2 files changed

Lines changed: 125 additions & 4 deletions

File tree

common/src/main/java/com/genexus/CommonUtil.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,12 +2327,11 @@ public static String format(String value, String v1, String v2, String v3, Strin
23272327
{
23282328
String[] vs = {v1, v2, v3, v4, v5, v6, v7, v8, v9};
23292329
StringBuffer stringBuilder = new StringBuffer();
2330+
int valueLength = value.length();
23302331
if (value != null && !value.equals(""))
23312332
{
2332-
StringTokenizer tokenizer = new StringTokenizer(value, "%", false);
23332333
int lastIndex = 0;
23342334
int index = 0;
2335-
int idx = 0;
23362335
while((index = value.indexOf('%', lastIndex)) != -1)
23372336
{
23382337
if(index > 0 && value.charAt(index - 1) == '\\')
@@ -2341,8 +2340,12 @@ public static String format(String value, String v1, String v2, String v3, Strin
23412340
stringBuilder.append("%");
23422341
lastIndex = index + 1;
23432342
}
2344-
else
2343+
else if (index > 0 && index < valueLength - 1 && value.charAt(index) == '%' && value.charAt(index + 1) == '%' )
23452344
{
2345+
stringBuilder.append(value.substring(lastIndex, index));
2346+
stringBuilder.append("%");
2347+
lastIndex = index + 1;
2348+
} else {
23462349
try
23472350
{
23482351
stringBuilder.append(value.substring(lastIndex, index));
@@ -2353,7 +2356,8 @@ public static String format(String value, String v1, String v2, String v3, Strin
23532356
stringBuilder.append("%").append(value.charAt(index+1));
23542357
}catch(StringIndexOutOfBoundsException e)
23552358
{ // Si el value termina con un %, lo ignoro
2356-
lastIndex--;
2359+
lastIndex--;
2360+
stringBuilder.append("%");
23572361
}
23582362
}
23592363
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.genexus;
2+
3+
import com.genexus.specific.java.Connect;
4+
import com.genexus.specific.java.LogManager;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
public class TestCommonUtil {
9+
10+
private void initialize()
11+
{
12+
Connect.init();
13+
LogManager.initialize(".");
14+
}
15+
16+
@Test
17+
public void testFormat() {
18+
initialize();
19+
20+
// Test case 1: Pass in a string with no parameter markers
21+
String value = "Hello world";
22+
String expectedResult = "Hello world";
23+
String result = CommonUtil.format(value, "", "", "", "", "", "", "", "", "");
24+
Assert.assertEquals(expectedResult, result);
25+
26+
// Test case 2: Pass in a string with maximum parameter markers
27+
value = "This is a string with %1, %2, %3, %4, %5, %6, %7, %8, and %9";
28+
expectedResult = "This is a string with 1, 2, 3, 4, 5, 6, 7, 8, and 9";
29+
result = CommonUtil.format(value, "1", "2", "3", "4", "5", "6", "7", "8", "9");
30+
Assert.assertEquals(expectedResult, result);
31+
32+
// Test case 3: Pass in a string with one parameter marker that's also the end of the string
33+
value = "Hello %1";
34+
expectedResult = "Hello world";
35+
result = CommonUtil.format(value, "world", "", "", "", "", "", "", "", "");
36+
Assert.assertEquals(expectedResult, result);
37+
38+
// Test case 4: Pass in a string with one parameter marker that's not the end of the string
39+
value = "Alex is %1 years old";
40+
expectedResult = "Alex is 26 years old";
41+
result = CommonUtil.format(value, "26", "", "", "", "", "", "", "", "");
42+
Assert.assertEquals(expectedResult, result);
43+
44+
// Test case 5: Pass in a string with multiple parameter markers
45+
value = "%1 is %2 years old";
46+
expectedResult = "Alex is 26 years old";
47+
result = CommonUtil.format(value, "Alex", "26", "", "", "", "", "", "", "");
48+
Assert.assertEquals(expectedResult, result);
49+
50+
// Test case 6: Pass in an empty string
51+
value = "";
52+
expectedResult = "";
53+
result = CommonUtil.format(value, "1", "2", "3", "4", "5", "6", "7", "8", "9");
54+
Assert.assertEquals(expectedResult, result);
55+
56+
// Test case 7: Pass in a string containing the "/%" sequence
57+
value = "%1/%2/%3";
58+
expectedResult = "2022/12/31";
59+
result = CommonUtil.format(value, "2022","12","31", "", "", "", "", "", "");
60+
Assert.assertEquals(expectedResult, result);
61+
62+
// Test case 8: Pass in a string containing one parameter marker followed by a % sign at the end of the string
63+
value = "%1%";
64+
expectedResult = "10%";
65+
result = CommonUtil.format(value, "10","","", "", "", "", "", "", "");
66+
Assert.assertEquals(expectedResult, result);
67+
68+
// Test case 9: Pass in a string containing one parameter marker followed by a % sign that's not at the end of the string
69+
value = "The price is %1% off today";
70+
expectedResult = "The price is 10% off today";
71+
result = CommonUtil.format(value, "10","","", "", "", "", "", "", "");
72+
Assert.assertEquals(expectedResult, result);
73+
74+
// Test case 9: Pass in a string containing multiple parameter markers separated by a % sign that also are at the end of the string
75+
value = "Your discount code is %1%%2";
76+
expectedResult = "Your discount code is 10%10";
77+
result = CommonUtil.format(value, "10","10","", "", "", "", "", "", "");
78+
Assert.assertEquals(expectedResult, result);
79+
80+
// Test case 10: Pass in a string containing multiple parameter markers separated by a % sign that are not at the end of the string
81+
value = "sample text %1%%2%%3%%4%%5 sample text";
82+
expectedResult = "sample text 10%10%10%10%10 sample text";
83+
result = CommonUtil.format(value, "10","10","10", "10", "10", "", "", "", "");
84+
Assert.assertEquals(expectedResult, result);
85+
86+
// Test case 11: Pass in a string with a parameter marker that should be ignored
87+
value = "sample text \\%1 sample text";
88+
expectedResult = "sample text %1 sample text";
89+
result = CommonUtil.format(value, "10","","", "", "", "", "", "", "");
90+
Assert.assertEquals(expectedResult, result);
91+
92+
// Test case 12: Pass in a string with appended parameter markers at the end of the string
93+
value = "Your discount code is %1%2";
94+
expectedResult = "Your discount code is 1020";
95+
result = CommonUtil.format(value, "10","20","", "", "", "", "", "", "");
96+
Assert.assertEquals(expectedResult, result);
97+
98+
// Test case 13: Pass in a string with appended parameter markers at the middle of the string
99+
value = "Your discount code is %1%2, use it within 24 hours";
100+
expectedResult = "Your discount code is 1020, use it within 24 hours";
101+
result = CommonUtil.format(value, "10","20","", "", "", "", "", "", "");
102+
Assert.assertEquals(expectedResult, result);
103+
104+
// Test case 14: Pass in a string with appended parameter markers at the start of the string
105+
value = "%1%2 is your discount code";
106+
expectedResult = "1020 is your discount code";
107+
result = CommonUtil.format(value, "10","20","", "", "", "", "", "", "");
108+
Assert.assertEquals(expectedResult, result);
109+
110+
// Test case 15: Pass in more parameter markers than needed
111+
value = "%1 is %2 years old";
112+
expectedResult = "Alex is 26 years old";
113+
result = CommonUtil.format(value, "Alex", "26", "10", "10", "", "", "", "", "");
114+
Assert.assertEquals(expectedResult, result);
115+
}
116+
117+
}

0 commit comments

Comments
 (0)