Skip to content

Commit 3800094

Browse files
burekumaibin
authored andcommitted
Bael 2666 concatenate strings with groovy (eugenp#7307)
* BAEL-2666 code for concatenate strings with groovy * BAEL-2666 add placeholder link to readme * BAEL-2666 remove article link in README, move code to core-groovy-2
1 parent 19df0fb commit 3800094

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.concatenate
2+
3+
class Wonder {
4+
5+
String numOfWonder = 'seven'
6+
7+
String operator_plus() {
8+
return 'The ' + numOfWonder + ' wonders of the world'
9+
}
10+
11+
String operator_left() {
12+
return 'The ' << numOfWonder << ' wonders of ' << 'the world'
13+
}
14+
15+
String interpolation_one() {
16+
return "The $numOfWonder wonders of the world"
17+
18+
}
19+
20+
String interpolation_two() {
21+
return "The ${numOfWonder} wonders of the world"
22+
}
23+
24+
String multilineString() {
25+
return """
26+
There are $numOfWonder wonders of the world.
27+
Can you name them all?
28+
1. The Great Pyramid of Giza
29+
2. Hanging Gardens of Babylon
30+
3. Colossus of Rhode
31+
4. Lighthouse of Alexendra
32+
5. Temple of Artemis
33+
6. Status of Zeus at Olympia
34+
7. Mausoleum at Halicarnassus
35+
"""
36+
}
37+
38+
String method_concat() {
39+
return 'The '.concat(numOfWonder).concat(' wonders of the world')
40+
41+
}
42+
43+
String method_builder() {
44+
return new StringBuilder()
45+
.append('The ').append(numOfWonder).append(' wonders of the world')
46+
}
47+
48+
String method_buffer() {
49+
return new StringBuffer()
50+
.append('The ').append(numOfWonder).append(' wonders of the world')
51+
}
52+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.concatenate
2+
3+
import org.junit.Before
4+
import org.junit.Test
5+
6+
import static org.junit.Assert.*
7+
8+
class WonderUnitTest {
9+
10+
static final String EXPECTED_SINGLE_LINE = "The seven wonders of the world"
11+
12+
Wonder wonder
13+
14+
@Before
15+
void before() {
16+
wonder = new Wonder()
17+
}
18+
19+
@Test
20+
void whenUsingOperatorPlus_thenConcatCorrectly() {
21+
assertEquals(EXPECTED_SINGLE_LINE, wonder.operator_plus())
22+
}
23+
24+
@Test
25+
void whenUsingOperatorLeft_thenConcatCorrectly() {
26+
assertEquals(EXPECTED_SINGLE_LINE, wonder.operator_left())
27+
}
28+
29+
@Test
30+
void whenUsingInterpolationOne_thenConcatCorrectly() {
31+
assertEquals(EXPECTED_SINGLE_LINE, wonder.interpolation_one())
32+
}
33+
34+
@Test
35+
void whenUsingInterpolationTwo_thenConcatCorrectly() {
36+
assertEquals(EXPECTED_SINGLE_LINE, wonder.interpolation_two())
37+
}
38+
39+
@Test
40+
void whenUsingMultiline_thenConcatCorrectly() {
41+
def expectedMultiline = """
42+
There are seven wonders of the world.
43+
Can you name them all?
44+
1. The Great Pyramid of Giza
45+
2. Hanging Gardens of Babylon
46+
3. Colossus of Rhode
47+
4. Lighthouse of Alexendra
48+
5. Temple of Artemis
49+
6. Status of Zeus at Olympia
50+
7. Mausoleum at Halicarnassus
51+
"""
52+
assertEquals(expectedMultiline, wonder.multilineString())
53+
}
54+
55+
@Test
56+
void whenUsingMethodConcat_thenConcatCorrectly() {
57+
assertEquals(EXPECTED_SINGLE_LINE, wonder.method_concat())
58+
}
59+
60+
@Test
61+
void whenUsingMethodBuilder_thenConcatCorrectly() {
62+
assertEquals(EXPECTED_SINGLE_LINE, wonder.method_builder())
63+
}
64+
65+
@Test
66+
void whenUsingMethodBuffer_thenConcatCorrectly() {
67+
assertEquals(EXPECTED_SINGLE_LINE, wonder.method_buffer())
68+
}
69+
}

0 commit comments

Comments
 (0)