Bael 2666 concatenate strings with groovy (#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
This commit is contained in:
Blake Ong 2019-07-16 19:30:55 +08:00 committed by maibin
parent 19df0fbb74
commit 38000941b5
2 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,52 @@
package com.baeldung.concatenate
class Wonder {
String numOfWonder = 'seven'
String operator_plus() {
return 'The ' + numOfWonder + ' wonders of the world'
}
String operator_left() {
return 'The ' << numOfWonder << ' wonders of ' << 'the world'
}
String interpolation_one() {
return "The $numOfWonder wonders of the world"
}
String interpolation_two() {
return "The ${numOfWonder} wonders of the world"
}
String multilineString() {
return """
There are $numOfWonder wonders of the world.
Can you name them all?
1. The Great Pyramid of Giza
2. Hanging Gardens of Babylon
3. Colossus of Rhode
4. Lighthouse of Alexendra
5. Temple of Artemis
6. Status of Zeus at Olympia
7. Mausoleum at Halicarnassus
"""
}
String method_concat() {
return 'The '.concat(numOfWonder).concat(' wonders of the world')
}
String method_builder() {
return new StringBuilder()
.append('The ').append(numOfWonder).append(' wonders of the world')
}
String method_buffer() {
return new StringBuffer()
.append('The ').append(numOfWonder).append(' wonders of the world')
}
}

View File

@ -0,0 +1,69 @@
package com.baeldung.concatenate
import org.junit.Before
import org.junit.Test
import static org.junit.Assert.*
class WonderUnitTest {
static final String EXPECTED_SINGLE_LINE = "The seven wonders of the world"
Wonder wonder
@Before
void before() {
wonder = new Wonder()
}
@Test
void whenUsingOperatorPlus_thenConcatCorrectly() {
assertEquals(EXPECTED_SINGLE_LINE, wonder.operator_plus())
}
@Test
void whenUsingOperatorLeft_thenConcatCorrectly() {
assertEquals(EXPECTED_SINGLE_LINE, wonder.operator_left())
}
@Test
void whenUsingInterpolationOne_thenConcatCorrectly() {
assertEquals(EXPECTED_SINGLE_LINE, wonder.interpolation_one())
}
@Test
void whenUsingInterpolationTwo_thenConcatCorrectly() {
assertEquals(EXPECTED_SINGLE_LINE, wonder.interpolation_two())
}
@Test
void whenUsingMultiline_thenConcatCorrectly() {
def expectedMultiline = """
There are seven wonders of the world.
Can you name them all?
1. The Great Pyramid of Giza
2. Hanging Gardens of Babylon
3. Colossus of Rhode
4. Lighthouse of Alexendra
5. Temple of Artemis
6. Status of Zeus at Olympia
7. Mausoleum at Halicarnassus
"""
assertEquals(expectedMultiline, wonder.multilineString())
}
@Test
void whenUsingMethodConcat_thenConcatCorrectly() {
assertEquals(EXPECTED_SINGLE_LINE, wonder.method_concat())
}
@Test
void whenUsingMethodBuilder_thenConcatCorrectly() {
assertEquals(EXPECTED_SINGLE_LINE, wonder.method_builder())
}
@Test
void whenUsingMethodBuffer_thenConcatCorrectly() {
assertEquals(EXPECTED_SINGLE_LINE, wonder.method_buffer())
}
}