Skip to content

Commit a59194f

Browse files
committed
Merge branch 'master' into JAVA-42094
# Conflicts: # core-kotlin-modules/core-kotlin-flows/pom.xml
2 parents 9e8dba6 + 150250a commit a59194f

File tree

23 files changed

+2766
-2
lines changed

23 files changed

+2766
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.resolvePrimaryConstructorCallExpectedIssue
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
6+
class resolvePrimaryConstructorCallExpectedIssueUnitTest {
7+
8+
@Test
9+
fun `correctly resolve compile error by properly defining constructors`() {
10+
val student = Student("Martial", 15)
11+
12+
assertEquals("Martial", student.name)
13+
assertEquals(15, student.age)
14+
}
15+
16+
@Test
17+
fun `correctly resolve compile error by properly using the secondary constructor`() {
18+
val dog = Dog("Max")
19+
assertEquals("Max", dog.species)
20+
}
21+
22+
@Test
23+
fun `correctly resolve compile error by properly using default parameters`() {
24+
val car1 = Car("Toyota", "Corolla")
25+
26+
assertEquals("Toyota", car1.make)
27+
assertEquals("Corolla", car1.model)
28+
29+
}
30+
}
31+
32+
class Student(var name: String) {
33+
var age: Int = 14
34+
35+
constructor(name: String, age: Int) : this(name) {
36+
this.age = age
37+
}
38+
39+
fun printMsg() {
40+
println("Name is $name. Age is $age.")
41+
}
42+
}
43+
44+
open class Animal(val species: String)
45+
46+
class Dog(species: String) : Animal(species)
47+
48+
class Car(val make: String, val model: String = "Unknown")

core-kotlin-modules/core-kotlin-flows/pom.xml

+19-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,30 @@
2121
<dependency>
2222
<groupId>org.jetbrains.kotlinx</groupId>
2323
<artifactId>kotlinx-coroutines-reactor</artifactId>
24-
<version>1.9.0</version>
24+
<version>${kotlinx.coroutines.version}</version>
25+
2526
</dependency>
2627
<dependency>
2728
<groupId>io.projectreactor</groupId>
2829
<artifactId>reactor-core</artifactId>
29-
<version>3.6.10</version>
30+
<version>${reactor.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.jetbrains.kotlinx</groupId>
34+
<artifactId>kotlinx-coroutines-core</artifactId>
35+
<version>${kotlinx.coroutines.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.jetbrains.kotlinx</groupId>
39+
<artifactId>kotlinx-coroutines-test</artifactId>
40+
<version>${kotlinx.coroutines.version}</version>
41+
<scope>test</scope>
3042
</dependency>
3143
</dependencies>
3244

45+
<properties>
46+
<junit.version>4.13.2</junit.version>
47+
<kotlinx.coroutines.version>1.7.3</kotlinx.coroutines.version>
48+
<reactor.version>3.4.11</reactor.version>
49+
</properties>
3350
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package com.baeldung.flowTest
2+
3+
import kotlinx.coroutines.*
4+
import kotlinx.coroutines.channels.onFailure
5+
import kotlinx.coroutines.flow.*
6+
import kotlinx.coroutines.test.advanceTimeBy
7+
import kotlinx.coroutines.test.runTest
8+
import org.junit.jupiter.api.Assertions.assertEquals
9+
import org.junit.jupiter.api.Test
10+
import org.junit.jupiter.api.assertThrows
11+
import java.util.concurrent.CancellationException
12+
13+
class FlowTestUnitTest {
14+
15+
@Test
16+
fun `simpleFlow should emit 1 2 3`() = runTest {
17+
val flow = simpleFlow().toList()
18+
19+
assertEquals(listOf(1, 2, 3), flow)
20+
}
21+
22+
@Test
23+
fun `transformedFlow should multiply values by 2`() = runTest {
24+
val result = transformedFlow().toList()
25+
assertEquals(listOf(2, 4, 6), result)
26+
}
27+
28+
@Test
29+
fun `errorFlow should emit values and recover from exception`() = runTest {
30+
val emittedValues = errorFlow().toList()
31+
32+
assertEquals(listOf(1, 2, -1), emittedValues)
33+
}
34+
35+
@Test
36+
fun `implicitCancellationFlow stops on cancellation`() = runTest {
37+
val emittedValues = mutableListOf<Int>()
38+
val job = launch {
39+
implicitCancellationFlow().collect { emittedValues.add(it) }
40+
}
41+
42+
advanceTimeBy(600)
43+
job.cancelAndJoin()
44+
45+
assertEquals(listOf(1,2), emittedValues)
46+
}
47+
48+
@Test
49+
fun `cancellableFlow stops emitting after external cancellation`() = runTest {
50+
val emittedValues = mutableListOf<Int>()
51+
val job = launch {
52+
cancellableFlow().collect { emittedValues.add(it) }
53+
}
54+
55+
advanceTimeBy(2000)
56+
job.cancelAndJoin()
57+
58+
assertEquals(listOf(0, 1), emittedValues)
59+
}
60+
61+
@Test
62+
fun `uncancellableFlow ensures cleanup occurs`() = runTest {
63+
val emittedValues = mutableListOf<Int>()
64+
val job = launch {
65+
uncancellableFlow().collect { emittedValues.add(it) }
66+
}
67+
68+
advanceTimeBy(400)
69+
job.cancelAndJoin()
70+
71+
assertEquals(listOf(1), emittedValues)
72+
}
73+
74+
@Test
75+
fun `delayedFlow should handle delayed emissions`() = runTest {
76+
val result = delayedFlow().toList()
77+
assertEquals(listOf(1, 2), result)
78+
}
79+
}
80+
81+
fun simpleFlow(): Flow<Int> = flow {
82+
emit(1)
83+
emit(2)
84+
emit(3)
85+
}
86+
87+
fun transformedFlow(): Flow<Int> = flow {
88+
emit(1)
89+
emit(2)
90+
emit(3)
91+
}.map { it * 2 }
92+
93+
fun errorFlow(): Flow<Int> = flow {
94+
emit(1)
95+
emit(2)
96+
throw Exception("Test Exception")
97+
}.catch { e ->
98+
emit(-1)
99+
}
100+
101+
fun cancellableFlow(): Flow<Int> = flow {
102+
try {
103+
repeat(5) {
104+
emit(it)
105+
delay(1000)
106+
}
107+
} finally {
108+
println("Cleanup: Emitting -1")
109+
emit(-1)
110+
}
111+
}.onEach { value ->
112+
if (value == 2) throw CancellationException("Flow was canceled at value 2")
113+
}.onCompletion { cause ->
114+
if (cause is CancellationException) {
115+
println("Flow canceled: Releasing resources")
116+
}
117+
}
118+
119+
fun delayedFlow(): Flow<Int> = flow {
120+
delay(500)
121+
emit(1)
122+
delay(500)
123+
emit(2)
124+
}
125+
126+
fun implicitCancellationFlow(): Flow<Int> = flow {
127+
emit(1)
128+
delay(500)
129+
emit(2)
130+
delay(500)
131+
emit(3)
132+
}
133+
134+
fun uncancellableFlow(): Flow<Int> = flow {
135+
try {
136+
emit(1)
137+
delay(500)
138+
emit(2)
139+
} finally {
140+
withContext(NonCancellable) {
141+
println("Releasing resources")
142+
}
143+
}
144+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.concatenateTwoFlows
2+
3+
import kotlinx.coroutines.flow.*
4+
import kotlinx.coroutines.runBlocking
5+
import org.junit.jupiter.api.Assertions
6+
import org.junit.jupiter.api.Test
7+
8+
class TakeWhileVsTransformWhileUnitTest {
9+
10+
@Test
11+
fun `takeWhile less than 3 returns 1 and 2`() {
12+
runBlocking {
13+
val numbersFlow = flowOf(1, 2, 3, 4, 5)
14+
15+
val taken = numbersFlow.takeWhile { it < 3 }.toList()
16+
17+
Assertions.assertEquals(listOf(1, 2), taken)
18+
}
19+
}
20+
21+
@Test
22+
fun `transformWhile less than 3 can return 1, 2 and 3`() {
23+
runBlocking {
24+
val numbersFlow = flowOf(1, 2, 3, 4, 5)
25+
26+
val transformed = numbersFlow.transformWhile {
27+
emit(it)
28+
it < 3
29+
}.toList()
30+
31+
Assertions.assertEquals(listOf(1, 2, 3), transformed)
32+
}
33+
}
34+
35+
@Test
36+
fun `transformWhile can transform elements`() {
37+
runBlocking {
38+
val numbersFlow = flowOf(1, 2, 3, 4, 5)
39+
40+
val transformed = numbersFlow.transformWhile {
41+
emit("" + it)
42+
emit("" + it * 2)
43+
it < 3
44+
}.toList()
45+
46+
Assertions.assertEquals(listOf("1", "2", "2", "4", "3", "6"), transformed)
47+
}
48+
}
49+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
plugins {
2+
kotlin("jvm") version "2.0.20"
3+
}
4+
5+
group = "com.baeldung"
6+
version = "1.0-SNAPSHOT"
7+
8+
repositories {
9+
mavenCentral()
10+
maven("https://packages.jetbrains.team/maven/p/kds/kotlin-ds-maven")
11+
}
12+
13+
dependencies {
14+
implementation("org.jetbrains.kotlinx:kandy-lets-plot:0.7.1")
15+
implementation("org.jetbrains.kotlinx:kotlin-statistics-jvm:0.3.1")
16+
testImplementation("org.assertj:assertj-core:3.6.1")
17+
testImplementation(kotlin("test"))
18+
}
19+
20+
tasks.test {
21+
useJUnitPlatform()
22+
}
23+
kotlin {
24+
jvmToolchain(21)
25+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Sat Nov 23 18:46:55 CET 2024
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)