Skip to content

Commit 84be8ff

Browse files
authored
Merge branch 'master' into JAVA-35320
2 parents d9ce407 + 0e6386a commit 84be8ff

File tree

17 files changed

+576
-38
lines changed

17 files changed

+576
-38
lines changed

core-kotlin-modules/core-kotlin-advanced-3/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,40 @@
3131
<kotlinx-html-jvm.version>0.7.2</kotlinx-html-jvm.version>
3232
</properties>
3333

34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.jetbrains.kotlin</groupId>
38+
<artifactId>kotlin-maven-plugin</artifactId>
39+
<version>${kotlin.version}</version>
40+
<executions>
41+
<execution>
42+
<id>compile</id>
43+
<phase>compile</phase>
44+
<goals>
45+
<goal>compile</goal>
46+
</goals>
47+
<configuration>
48+
<args>
49+
<arg>-Xcontext-receivers</arg>
50+
</args>
51+
</configuration>
52+
</execution>
53+
<execution>
54+
<id>test-compile</id>
55+
<phase>test-compile</phase>
56+
<goals>
57+
<goal>test-compile</goal>
58+
</goals>
59+
<configuration>
60+
<args>
61+
<arg>-Xcontext-receivers</arg>
62+
</args>
63+
</configuration>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
3470
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.baeldung.contextreceivers
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
6+
context(StringBuilder)
7+
fun appendHello() {
8+
append("Hello, ")
9+
}
10+
11+
context(StringBuilder)
12+
fun appendWorld() {
13+
append("World!")
14+
}
15+
16+
class HtmlBuilder {
17+
private val elements = mutableListOf<String>()
18+
fun addElement(tag: String, content: String) {
19+
elements.add("<$tag>$content</$tag>")
20+
}
21+
22+
fun build(): String = elements.joinToString("\n")
23+
}
24+
25+
context(HtmlBuilder) fun p(content: String) {
26+
addElement("p", content)
27+
}
28+
29+
context(HtmlBuilder) fun h1(content: String) {
30+
addElement("h1", content)
31+
}
32+
33+
class ContextReceiversUnitTest {
34+
35+
fun html(content: HtmlBuilder.() -> Unit): String {
36+
val builder = HtmlBuilder()
37+
builder.content()
38+
return builder.build()
39+
}
40+
41+
@Test
42+
fun `test StringBuilder context receiver`() {
43+
val builder = StringBuilder()
44+
with(builder) {
45+
appendHello()
46+
appendWorld()
47+
}
48+
assertEquals("Hello, World!", builder.toString())
49+
}
50+
51+
@Test
52+
fun `test HTML DSL with context receivers`() {
53+
val htmlContent = html {
54+
h1("Welcome to My Website")
55+
p("This is a paragraph in my website.")
56+
}
57+
val expected = """
58+
<h1>Welcome to My Website</h1>
59+
<p>This is a paragraph in my website.</p>
60+
""".trimIndent()
61+
assertEquals(expected, htmlContent)
62+
}
63+
64+
}

core-kotlin-modules/core-kotlin-collections-2/src/test/kotlin/com/baeldung/mapvsflatmap/MapVsFlatMapUnitTest.kt

Lines changed: 0 additions & 37 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.mapvsflatmapvsflatten
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.jupiter.api.Test
5+
import kotlin.test.assertEquals
6+
7+
class MapVsFlatMapVsFlattenUnitTest {
8+
9+
@Test
10+
fun `map should convert each element to another element`() {
11+
val order = Order(
12+
listOf(OrderLine("Tomato", 2), OrderLine("Garlic", 3), OrderLine("Chives", 2))
13+
)
14+
15+
val names = order.lines.map { it.name }
16+
val totalPrice = order.lines.map { it.price }.sum()
17+
18+
assertThat(names).containsExactly("Tomato", "Garlic", "Chives")
19+
assertEquals(7, totalPrice)
20+
}
21+
22+
@Test
23+
fun `flatMap should flatten the one-to-many relation as expected`() {
24+
val orders = listOf(
25+
Order(listOf(OrderLine("Garlic", 1), OrderLine("Chives", 2))),
26+
Order(listOf(OrderLine("Tomato", 3), OrderLine("Garlic", 4))),
27+
Order(listOf(OrderLine("Potato", 5), OrderLine("Chives", 6))),
28+
)
29+
30+
val lines: List<OrderLine> = orders.flatMap { it.lines }
31+
val names = lines.map { it.name }.distinct()
32+
assertThat(names).containsExactlyInAnyOrder("Garlic", "Chives", "Tomato", "Potato")
33+
}
34+
35+
@Test
36+
fun `flatten should flatten the nested collections without transformation`() {
37+
val orderLines = listOf(
38+
listOf(OrderLine("Garlic", 1), OrderLine("Chives", 2)),
39+
listOf(OrderLine("Tomato", 3), OrderLine("Garlic", 4)),
40+
listOf(OrderLine("Potato", 5), OrderLine("Chives", 6)),
41+
)
42+
43+
val lines: List<OrderLine> = orderLines.flatten()
44+
val expected = listOf(
45+
OrderLine("Garlic", 1),
46+
OrderLine("Chives", 2),
47+
OrderLine("Tomato", 3),
48+
OrderLine("Garlic", 4),
49+
OrderLine("Potato", 5),
50+
OrderLine("Chives", 6),
51+
)
52+
assertThat(lines).hasSize(6).isEqualTo(expected)
53+
}
54+
55+
@Test
56+
fun `flatMap should get same result of map and then flatten`() {
57+
val orders = listOf(
58+
Order(listOf(OrderLine("Garlic", 1), OrderLine("Chives", 2))),
59+
Order(listOf(OrderLine("Tomato", 3), OrderLine("Garlic", 4))),
60+
Order(listOf(OrderLine("Potato", 5), OrderLine("Chives", 6))),
61+
)
62+
63+
val expected = listOf(
64+
OrderLine("Garlic", 1),
65+
OrderLine("Chives", 2),
66+
OrderLine("Tomato", 3),
67+
OrderLine("Garlic", 4),
68+
OrderLine("Potato", 5),
69+
OrderLine("Chives", 6),
70+
)
71+
72+
val resultMapAndFlatten: List<OrderLine> = orders.map { it.lines }.flatten()
73+
val resultFlatMap:List<OrderLine> = orders.flatMap { it.lines }
74+
75+
assertThat(resultFlatMap).isEqualTo(resultMapAndFlatten).hasSize(6).isEqualTo(expected)
76+
}
77+
}
78+
79+
data class Order(val lines: List<OrderLine>)
80+
data class OrderLine(val name: String, val price: Int)

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,19 @@
1313
<version>1.0.0-SNAPSHOT</version>
1414
</parent>
1515

16+
<dependencies>
17+
<dependency>
18+
<groupId>org.apache.commons</groupId>
19+
<artifactId>commons-compress</artifactId>
20+
<version>1.21</version>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>net.lingala.zip4j</groupId>
25+
<artifactId>zip4j</artifactId>
26+
<version>2.8.0</version>
27+
</dependency>
28+
29+
</dependencies>
30+
1631
</project>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.baeldung.createZip
2+
3+
import net.lingala.zip4j.ZipFile
4+
import net.lingala.zip4j.model.ZipParameters
5+
import net.lingala.zip4j.model.enums.CompressionLevel
6+
import net.lingala.zip4j.model.enums.CompressionMethod
7+
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
8+
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
9+
import org.apache.commons.compress.utils.IOUtils
10+
import org.junit.jupiter.api.Assertions.assertTrue
11+
import org.junit.jupiter.api.Test
12+
import java.io.File
13+
import java.io.FileInputStream
14+
import java.io.FileOutputStream
15+
import java.util.zip.ZipEntry
16+
import java.util.zip.ZipOutputStream
17+
18+
class CreateZipFileUnitTest {
19+
20+
val filesToZip = listOf(File("file1.txt").apply { writeText("Hello, World!") }, File("file2.txt").apply { writeText("Kotlin ZIP Test") })
21+
22+
@Test
23+
fun `create zip file with java standard library`() {
24+
val outputZipFile = File("test_output.zip")
25+
26+
createZipFile(filesToZip, outputZipFile)
27+
28+
assertTrue(outputZipFile.exists())
29+
assertTrue(outputZipFile.length() > 0)
30+
31+
filesToZip.forEach { it.delete() }
32+
outputZipFile.delete()
33+
}
34+
35+
@Test
36+
fun `create zip file with apache commons compress`() {
37+
val outputZipFile = File("test_output_with_apache.zip")
38+
39+
createZipFileWithApache(filesToZip, outputZipFile)
40+
41+
assertTrue(outputZipFile.exists())
42+
assertTrue(outputZipFile.length() > 0)
43+
44+
// Clean up
45+
filesToZip.forEach { it.delete() }
46+
outputZipFile.delete()
47+
}
48+
49+
@Test
50+
fun `create zip file with Zip4j library`() {
51+
val outputZipFile = File("test_output_with_zip4j.zip")
52+
53+
createZipFileWithZip4j(filesToZip, outputZipFile)
54+
55+
assertTrue(outputZipFile.exists())
56+
assertTrue(outputZipFile.length() > 0)
57+
58+
filesToZip.forEach { it.delete() }
59+
outputZipFile.delete()
60+
}
61+
}
62+
fun createZipFile(files: List<File>, outputZipFile: File) {
63+
ZipOutputStream(FileOutputStream(outputZipFile)).use { zipOut ->
64+
files.forEach { file ->
65+
FileInputStream(file).use { fis ->
66+
val zipEntry = ZipEntry(file.name)
67+
zipOut.putNextEntry(zipEntry)
68+
fis.copyTo(zipOut)
69+
zipOut.closeEntry()
70+
}
71+
}
72+
}
73+
}
74+
75+
fun createZipFileWithApache(files: List<File>, outputZipFile: File) {
76+
ZipArchiveOutputStream(FileOutputStream(outputZipFile)).use { zipOut ->
77+
files.forEach { file ->
78+
zipOut.putArchiveEntry(ZipArchiveEntry(file.name))
79+
FileInputStream(file).use { fis ->
80+
IOUtils.copy(fis, zipOut)
81+
}
82+
zipOut.closeArchiveEntry()
83+
}
84+
}
85+
}
86+
87+
fun createZipFileWithZip4j(files: List<File>, outputZipFile: File) {
88+
val zipFile = ZipFile(outputZipFile)
89+
val parameters = ZipParameters().apply {
90+
compressionMethod = CompressionMethod.DEFLATE
91+
compressionLevel = CompressionLevel.NORMAL
92+
}
93+
zipFile.addFiles(files, parameters)
94+
}

k2-compiler/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>k2-compiler</artifactId>
7+
<name>k2-compiler</name>
8+
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>kotlin-modules</artifactId>
12+
<version>1.0.0-SNAPSHOT</version>
13+
</parent>
14+
15+
<properties>
16+
<kotlin.version>2.0.0</kotlin.version>
17+
</properties>
18+
19+
</project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ChildClass : ParentClass() {
2+
fun greeting(s: String) = hello(s)
3+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.lang.reflect.InvocationTargetException
2+
3+
fun translateExecution(exception: Throwable?): Throwable? {
4+
val isInvocationTargetException = exception is InvocationTargetException
5+
6+
if (isInvocationTargetException) {
7+
return (exception as InvocationTargetException).targetException
8+
}
9+
10+
return exception
11+
}
12+
13+
fun translateExecutionV2(exception: Throwable?) : Throwable? {
14+
val isInvocationTargetException = exception is InvocationTargetException
15+
16+
if (isInvocationTargetException) {
17+
return exception.targetException
18+
}
19+
20+
return exception
21+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
open class ParentClass {
2+
internal fun hello(s: String) = println("Hello, $s!")
3+
}

0 commit comments

Comments
 (0)