Skip to content

Commit 9f72391

Browse files
authored
Merge branch 'master' into master
2 parents a6e5082 + b88bc12 commit 9f72391

File tree

6 files changed

+182
-3
lines changed

6 files changed

+182
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.privateFunction
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
6+
class PrivateFunctionUnitTest {
7+
8+
@Test
9+
fun `Makes call to private method Using Reflection`() {
10+
val obj = MyClass()
11+
val privateMethod = MyClass::class.java.getDeclaredMethod("privateMethod")
12+
privateMethod.isAccessible = true
13+
14+
val output = privateMethod.invoke(obj)
15+
16+
assertEquals("This is a private method", output)
17+
}
18+
19+
@Test
20+
fun `Makes call to private method Using Inner Class`() {
21+
val obj = MyClassWithInnerClass()
22+
val innerObj = obj.MyInnerClass()
23+
val output = innerObj.callPrivateMethod()
24+
25+
assertEquals("This is a private method", output)
26+
}
27+
28+
@Test
29+
fun `Makes call to private method Using public method`() {
30+
val obj = MyPublicClass()
31+
32+
assertEquals("This is a private method", obj.callPrivateMethod())
33+
}
34+
}
35+
36+
class MyClass {
37+
private fun privateMethod(): String {
38+
return "This is a private method"
39+
}
40+
}
41+
42+
class MyClassWithInnerClass {
43+
private fun privateMethod(): String {
44+
return "This is a private method"
45+
}
46+
47+
inner class MyInnerClass {
48+
fun callPrivateMethod(): String {
49+
return privateMethod()
50+
}
51+
}
52+
}
53+
54+
class MyPublicClass {
55+
private fun privateMethod(): String {
56+
return "This is a private method"
57+
}
58+
59+
fun callPrivateMethod(): String {
60+
return privateMethod()
61+
}
62+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.whenenum
2+
3+
enum class Day {
4+
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
5+
}
6+
enum class Color {
7+
RED, GREEN, BLUE
8+
}
9+
fun getColorDescription(color: Color): String {
10+
return when (color) {
11+
Color.RED -> "The color is red."
12+
Color.GREEN -> "The color is green."
13+
Color.BLUE -> "The color is blue."
14+
}
15+
}
16+
17+
fun getDailyRoutine(day: Day): String {
18+
return when (day) {
19+
Day.MONDAY -> "Start of the workweek"
20+
Day.WEDNESDAY -> "Midweek - Keep pushing!"
21+
Day.FRIDAY -> "Almost there, the weekend is coming!"
22+
Day.SUNDAY -> "Relax and recharge"
23+
else -> "It's a regular day"
24+
}
25+
}
26+
27+
fun main(args: Array<String>) {
28+
val shape: Shape = Shape.Triangle
29+
when (shape) {
30+
Shape.Square -> println("I'm a square")
31+
Shape.Triangle -> println("I'm a triangle")
32+
}
33+
}
34+
sealed class Shape {
35+
object Triangle : Shape()
36+
object Square : Shape()
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.printAllElementsInOneLine
2+
3+
import org.junit.jupiter.api.AfterEach
4+
import org.junit.jupiter.api.BeforeEach
5+
import org.junit.jupiter.api.Test
6+
import java.io.ByteArrayOutputStream
7+
import java.io.PrintStream
8+
import kotlin.test.assertEquals
9+
10+
11+
class PrintAllElementsInOneLineUnitTest {
12+
val myArray = arrayOf("A", "B", "C", "D", "E", "F")
13+
val arrayWithComma = arrayOf("A", "B, C", "D, E", "F")
14+
15+
val newLine = System.lineSeparator()
16+
val stdOut = System.out
17+
val myOutput = ByteArrayOutputStream()
18+
19+
@BeforeEach
20+
fun setup() {
21+
System.setOut(PrintStream(myOutput))
22+
}
23+
24+
@AfterEach
25+
fun restore() {
26+
System.setOut(stdOut)
27+
}
28+
29+
30+
@Test
31+
fun `when using myOutput then we can verify println() result`() {
32+
println("Hello, world")
33+
assertEquals("Hello, world$newLine", myOutput.toString())
34+
35+
myOutput.reset()
36+
37+
println("Kotlin rocks")
38+
assertEquals("Kotlin rocks$newLine", myOutput.toString())
39+
}
40+
41+
@Test
42+
fun `when converting array to list then all elements are in one line`() {
43+
println(myArray.asList())
44+
assertEquals("[A, B, C, D, E, F]$newLine", myOutput.toString())
45+
46+
myOutput.reset()
47+
48+
println(arrayWithComma.asList())
49+
// cannot customize the output format
50+
assertEquals("[A, B, C, D, E, F]$newLine", myOutput.toString())
51+
}
52+
53+
54+
@Test
55+
fun `when using print() then get the expected result`() {
56+
myArray.forEachIndexed { idx, e ->
57+
print(if (idx == myArray.lastIndex) "$e$newLine" else "$e, ")
58+
}
59+
assertEquals("A, B, C, D, E, F$newLine", myOutput.toString())
60+
61+
myOutput.reset()
62+
63+
arrayWithComma.forEachIndexed { idx, e ->
64+
print(if (idx == arrayWithComma.lastIndex) """"$e"$newLine""" else """"$e", """)
65+
}
66+
assertEquals(""""A", "B, C", "D, E", "F"$newLine""", myOutput.toString())
67+
}
68+
69+
@Test
70+
fun `when using joinToString() then get the expected result`() {
71+
println(myArray.joinToString { it })
72+
assertEquals("A, B, C, D, E, F$newLine", myOutput.toString())
73+
74+
myOutput.reset()
75+
76+
println(arrayWithComma.joinToString { """"$it"""" })
77+
assertEquals(""""A", "B, C", "D, E", "F"$newLine""", myOutput.toString())
78+
}
79+
80+
}

core-kotlin-modules/core-kotlin-collections-5/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This module contains articles about core Kotlin collections.
44
- [in and !in Operators in Kotlin](https://www.baeldung.com/kotlin/in-notin-operators)
5-
- [Print Lists Content Separated By Comma](https://www.baeldung.com/kotlin/print-list-items-comma-separated)
5+
- [Print Lists Content Separated by Comma](https://www.baeldung.com/kotlin/print-list-items-comma-separated)
66
- [Rotate a List in Kotlin](https://www.baeldung.com/kotlin/list-rotation)
77
- [Remove Elements From a List While Iterating in Kotlin](https://www.baeldung.com/kotlin/list-remove-elements-while-iterating)
88
- [How to Start a for Loop From a Given Index in Kotlin](https://www.baeldung.com/kotlin/for-loop-from-index)

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@
441441
</profiles>
442442

443443
<properties>
444-
<kotlin.version>1.9.20</kotlin.version>
444+
<kotlin.version>1.9.22</kotlin.version>
445445
<kotlinx.version>1.7.1</kotlinx.version>
446446

447447
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

spring-boot-kotlin-2/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
- [Mapstruct With Kotlin’s Data Classes](https://www.baeldung.com/kotlin/mapstruct-data-classes)
66
- [Spring Dependency Injection With Kotlin](https://www.baeldung.com/kotlin/spring-dependency-injection)
77
- [RestTemplate Examples in Kotlin](https://www.baeldung.com/kotlin/spring-resttemplate-examples)
8-
- [Creating Spring Boot Console Application With Kotlin](https://www.baeldung.com/kotlin/spring-boot-console-application)
8+
- [Creating a Spring Boot Console Application With Kotlin](https://www.baeldung.com/kotlin/spring-boot-console-application)

0 commit comments

Comments
 (0)