# Testing page This is a block with images (taken from Getting started with Compose Multiplatform tutorial). ![First step](images/icon-1-done.svg) [Create a Spring Boot project with Kotlin](jvm-create-project-with-spring-boot.html) ![Second step](images/icon-2-done.svg) [Add a data class to the Spring Boot project](jvm-spring-boot-add-data-class.html) ![Third step](images/icon-3.svg) Add database support for Spring Boot project ![Fourth step](images/icon-4-todo.svg) Use Spring Data CrudRepository for database access> ## Synchronized tabs Kotlin: ```KOTLIN plugins { kotlin("kapt") version "1.9.23" } ``` Groovy: ```GROOVY plugins { id "org.jetbrains.kotlin.kapt" version "1.9.23" } ``` Kotlin: ```KOTLIN plugins { kotlin("plugin.noarg") version "1.9.23" } ``` Groovy: ```GROOVY plugins { id "org.jetbrains.kotlin.plugin.noarg" version "1.9.23" } ``` ## Sections ### Collapsed section Some text here and a codeblock: ```KOTLIN plugins { kotlin("plugin.noarg") version "1.9.23" } ``` ## Codeblocks Just a codeblock: ```KOTLIN import java.util.* @Service class MessageService(val db: MessageRepository) { fun findMessages(): List = db.findAll().toList() fun findMessageById(id: String): List = db.findById(id).toList() fun save(message: Message) { db.save(message) } fun Optional.toList(): List = if (isPresent) listOf(get()) else emptyList() } ``` ### Expandable codeblock ```KOTLIN package com.example.demo import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController @SpringBootApplication class DemoApplication fun main(args: Array) { runApplication(*args) } @RestController class MessageController { @GetMapping("/") fun index(@RequestParam("name") name: String) = "Hello, $name!" } ``` ### Runnable codeblock ```KOTLIN data class User(val name: String, val id: Int) fun main() { val user = User("Alex", 1) //sampleStart // Automatically uses toString() function so that output is easy to read println(user) // User(name=Alex, id=1) //sampleEnd } ``` ## Tables ### Markdown table | Primitive-type array |Equivalent in Java | -------------------------------------------- | [BooleanArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean-array/) |`boolean[]` | | [ByteArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/) |`byte[]` | | [CharArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-array/) |`char[]` | | [DoubleArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double-array/) |`double[]` | | [FloatArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-float-array/) |`float[]` | | [IntArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int-array/) |`int[]` | | [LongArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long-array/) |`long[]` | | [ShortArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-short-array/) |`short[]` | ### XML table | Last modified on |December 2023 | ----------------------------------- | Next update |June 2024 | ### XML table with codeblocks inside Simple table: | Before |Now | --------------- | ```KOTLIN kotlin { targets { configure(['windows', 'linux']) { } } } ``` | ```KOTLIN kotlin { targets { configure([findByName('windows'), findByName('linux')]) { } } } ``` | More complex table: | |Before |Now | ----------------- | Dependencies of the `jvmMain` compilation | ```KOTLIN jvm ``` | ```KOTLIN jvmCompilation ``` | | ```KOTLIN dependencies { add("jvmImplementation", "foo.bar.baz:1.2.3") } ``` | ```KOTLIN dependencies { add("jvmCompilationImplementation", "foo.bar.baz:1.2.3") } ``` | | Dependencies of the `jvmMain` source set | ```KOTLIN jvmMain ``` | | Dependencies of the `jvmTest` compilation | ```KOTLIN jvmTest ``` | ```KOTLIN jvmTestCompilation ``` | | Dependencies of the `jvmTest` source set | ```KOTLIN jvmTest ``` | ## Lists ### Ordered list 1. One 2. Two 3. Three 1. Three point 1 2. Three point 2 3. Three point 3 1. Three point 1 and 1 4. With a codeblock inside: ```KOTLIN jvmTest ``` ### Non-ordered list * First bullet * Second bullet * Third bullet * One more * Another one * Wow, one more * With a codeblock inside: ```KOTLIN jvmTest ``` ### Definition list Collapsible item #1 : The return type of the `findById()` function in the `CrudRepository` interface is an instance of the `Optional` class. However, it would be convenient to return a `List` with a single message for consistency. For that, you need to unwrap the `Optional` value if it's present, and return a list with the value. This can be implemented as an [extension function](extensions.html#extension-functions) to the `Optional` type. : In the code, `Optional.toList()`, `.toList()` is the extension function for `Optional`. Extension functions allow you to write additional functions to any classes, which is especially useful when you want to extend functionality of some library class. Collapsible item #2 : [This function works](https://docs.spring.io/spring-data/relational/reference/#jdbc.entity-persistence) with an assumption that the new object doesn't have an id in the database. Hence, the id should be null for insertion. : If the id isn't null, `CrudRepository` assumes that the object already exists in the database and this is an update operation as opposed to an insert operation. After the insert operation, the `id` will be generated by the data store and assigned back to the `Message` instance. This is why the `id` property should be declared using the `var` keyword. ### Clear definition list Expandable item #1 : An expandable item without numbering. Its body is plain text so that only the widget chrome is under test. Expandable item #2 : A second expandable item. Expanding it must not change the state of its siblings. Expandable item #3 : A third expandable item, closing the non-numbered list. ### Numbered clear definition list Numbered item #1 : Description of the numbered item rendered with the generated number 1. Numbered item #2 : Description of the numbered item rendered with the generated number 2. Numbered item #3 : Description of the numbered item rendered with the generated number 3. Numbered item #4 : Description of the numbered item rendered with the generated number 4. Numbered item #5 : Description of the numbered item rendered with the generated number 5. Numbered item #6 : Description of the numbered item rendered with the generated number 6. A deliberately long item title that has to wrap onto several lines on narrow viewports so that the hanging indent of the generated number stays intact : Description of the numbered item rendered with the generated number 7. Numbered item #8 : Description of the numbered item rendered with the generated number 8. Numbered item #9 : Description of the numbered item rendered with the generated number 9. Numbered item #10 : Description of the numbered item rendered with the generated number 10. Numbered item #11 : Description of the numbered item rendered with the generated number 11. ## Practice Print a greeting using string templates : Complete the code to make the program print `"Mary is 20 years old"` to standard output: : ```KOTLIN fun main() { val name = "Mary" val age = 20 // Write your code here } ``` : ```KOTLIN fun main() { val name = "Mary" val age = 20 println("$name is $age years old") } ``` Check whether two results match : Use `if` to print `You win :)` when both results are equal and `You lose :(` otherwise. : Tip: This exercise also carries a nested hint, so the inner collapsible list must open without collapsing the exercise itself. : Hint : Use the equality operator ( : `==` : ) to compare the results. : ```KOTLIN fun main() { val firstResult = 3 val secondResult = 3 // Write your code here } ``` : ```KOTLIN fun main() { val firstResult = 3 val secondResult = 3 if (firstResult == secondResult) println("You win :)") else println("You lose :(") } ``` Filter a list of numbers : Print only the even numbers of the list using `filter()`: : ```KOTLIN fun main() { val numbers = listOf(1, 2, 3, 4, 5) println(numbers) // Write your code here } ``` : ```KOTLIN fun main() { val numbers = listOf(1, 2, 3, 4, 5) println(numbers) println(numbers.filter { it % 2 == 0 }) } ``` ## Text elements * Bold text * italic text * `inline code` * [internal anchor](#lists) * [internal link](roadmap.html) * [external link](https://jetbrains.com) * emojis ❌✅🆕 ## Variables * variable using: latest Kotlin version is 2.4.21 ## Embedded elements ### Video from YouTube [Video: What's new in Kotlin 1.9.20](https://www.youtube.com/v/Ol_96CHKqg8) ### Pictures Regular (Markdown): ![Create a test](images/create-test.png) Regular (XML): ![Multiplatform web wizard](images/multiplatform-web-wizard.png) Inline: ![YouTrack](images/youtrack-logo.png) Zoomable: ![class diagram](images/ksp-class-diagram.svg) Button-style: [https://kmp.jetbrains.com](https://kmp.jetbrains.com) ## Notes Warning: Warning: Support for K2 in the kapt compiler plugin is [Experimental](components-stability.html). Opt-in is required (see details below), and you should use it only for evaluation purposes. Note: Note: As for native platform libraries shipped with Kotlin/Native (like Foundation, UIKit, and POSIX), only some of their APIs need an opt-in with `@ExperimentalForeignApi`. In such cases, you get a warning with an opt-in requirement. Tip: Tip: As for native platform libraries shipped with Kotlin/Native (like Foundation, UIKit, and POSIX), only some of their APIs need an opt-in with `@ExperimentalForeignApi`. In such cases, you get a warning with an opt-in requirement.