Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
Add Kotlin demo app
Browse files Browse the repository at this point in the history
  • Loading branch information
mfvanek committed Apr 30, 2024
1 parent a6f3640 commit 233459b
Show file tree
Hide file tree
Showing 18 changed files with 347 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
plugins {
id("java")
id("jacoco")
}

val versionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")

dependencies {
versionCatalog.findLibrary("junit-bom").ifPresent {
testImplementation(platform(it))
}
}

java {
// Don't forget about Kotlin app
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

tasks {
withType<JavaCompile>().configureEach {
options.compilerArgs.add("-parameters")
}

test {
useJUnitPlatform()
finalizedBy(jacocoTestReport, jacocoTestCoverageVerification)
}

jacocoTestReport {
dependsOn(test)
reports {
xml.required.set(true)
html.required.set(true)
}
}

jacocoTestCoverageVerification {
dependsOn(jacocoTestReport)
violationRules {
rule {
limit {
counter = "CLASS"
value = "MISSEDCOUNT"
maximum = "0.0".toBigDecimal()
}
}
rule {
limit {
counter = "METHOD"
value = "MISSEDCOUNT"
maximum = "0.0".toBigDecimal()
}
}
rule {
limit {
counter = "LINE"
value = "MISSEDCOUNT"
maximum = "0.0".toBigDecimal()
}
}
rule {
limit {
counter = "INSTRUCTION"
value = "COVEREDRATIO"
minimum = "1.0".toBigDecimal()
}
}
}
}

check {
dependsOn(jacocoTestCoverageVerification)
}
}

jacoco {
toolVersion = "0.8.12"
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,18 @@ plugins {
id("net.ltgt.errorprone")
}

val versionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")

dependencies {
versionCatalog.findLibrary("junit-bom").ifPresent {
testImplementation(platform(it))
}

checkstyle("com.thomasjensen.checkstyle.addons:checkstyle-addons:7.0.1")

errorprone("com.google.errorprone:error_prone_core:2.26.1")
errorprone("com.google.errorprone:error_prone_core:2.27.0")
errorprone("jp.skypencil.errorprone.slf4j:errorprone-slf4j:0.1.23")

spotbugsPlugins("jp.skypencil.findbugs.slf4j:bug-pattern:1.5.0")
spotbugsPlugins("com.h3xstream.findsecbugs:findsecbugs-plugin:1.13.0")
spotbugsPlugins("com.mebigfatguy.sb-contrib:sb-contrib:7.6.4")
}

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
tasks.withType<JavaCompile>().configureEach {
options.compilerArgs.add("-parameters")
options.errorprone {
disableWarningsInGeneratedCode.set(true)
disable("Slf4jLoggerShouldBeNonStatic")
Expand All @@ -45,9 +34,7 @@ tasks.withType<JavaCompile>().configureEach {

tasks {
test {
useJUnitPlatform()
dependsOn(checkstyleMain, checkstyleTest, pmdMain, pmdTest, spotbugsMain, spotbugsTest)
finalizedBy(jacocoTestReport, jacocoTestCoverageVerification)
}

jar {
Expand All @@ -65,61 +52,11 @@ tasks {
}
}

jacocoTestReport {
dependsOn(test)
reports {
xml.required.set(true)
html.required.set(true)
}
}

jacocoTestCoverageVerification {
dependsOn(jacocoTestReport)
violationRules {
rule {
limit {
counter = "CLASS"
value = "MISSEDCOUNT"
maximum = "0.0".toBigDecimal()
}
}
rule {
limit {
counter = "METHOD"
value = "MISSEDCOUNT"
maximum = "0.0".toBigDecimal()
}
}
rule {
limit {
counter = "LINE"
value = "MISSEDCOUNT"
maximum = "0.0".toBigDecimal()
}
}
rule {
limit {
counter = "INSTRUCTION"
value = "COVEREDRATIO"
minimum = "1.0".toBigDecimal()
}
}
}
}

check {
dependsOn(jacocoTestCoverageVerification)
}

withType<SonarTask>().configureEach {
dependsOn(test, jacocoTestReport)
}
}

jacoco {
toolVersion = "0.8.12"
}

checkstyle {
toolVersion = "10.15.0"
configFile = file("../config/checkstyle/checkstyle.xml")
Expand All @@ -129,7 +66,7 @@ checkstyle {
}

pmd {
toolVersion = "7.0.0"
toolVersion = "7.1.0"
isConsoleOutput = true
ruleSetFiles = files("../config/pmd/pmd.xml")
ruleSets = listOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal class JavaConventionPluginTest : PluginTestBase() {
fun init() {
buildFile.appendText("""
|plugins {
| id("pg-index-health-test-starter.java-compilation")
| id("pg-index-health-test-starter.java-conventions")
|}
|
Expand Down
1 change: 1 addition & 0 deletions console-demo-app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
plugins {
id("pg-index-health-test-starter.java-compilation")
id("pg-index-health-test-starter.java-conventions")
alias(libs.plugins.spring.boot.gradlePlugin)
alias(libs.plugins.spring.dependency.management)
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pg-index-health-core = { group = "io.github.mfvanek", name = "pg-index-health",
pg-index-health-jdbcConnection = { group = "io.github.mfvanek", name = "pg-index-health-jdbc-connection", version.ref = "pg-index-health" }
pg-index-health-generator = { group = "io.github.mfvanek", name = "pg-index-health-generator", version.ref = "pg-index-health" }
pg-index-health-testing = { group = "io.github.mfvanek", name = "pg-index-health-testing", version.ref = "pg-index-health" }
testcontainers-bom = { group = "org.testcontainers", name = "testcontainers-bom", version = "1.19.7" }

[plugins]
spring-boot-gradlePlugin = { id = "org.springframework.boot", version.ref = "spring-boot" }
Expand Down
1 change: 1 addition & 0 deletions h2-demo-app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
plugins {
id("pg-index-health-test-starter.java-compilation")
id("pg-index-health-test-starter.java-conventions")
alias(libs.plugins.spring.boot.gradlePlugin)
alias(libs.plugins.spring.dependency.management)
Expand Down
32 changes: 32 additions & 0 deletions kotlin-demo-app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.9.23"
kotlin("plugin.spring") version "1.9.23"
id("pg-index-health-test-starter.java-compilation")
alias(libs.plugins.spring.boot.gradlePlugin)
alias(libs.plugins.spring.dependency.management)
}

ext["commons-lang3.version"] = libs.versions.commons.lang3.get()

dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation(project(":pg-index-health-test-starter"))
implementation(libs.spring.boot.starter.data.jdbc)
implementation(libs.pg.index.health.testing)
implementation(platform(libs.testcontainers.bom))
implementation("org.testcontainers:postgresql")

runtimeOnly(libs.database.postgresql)

testImplementation(libs.spring.boot.starter.test)
testImplementation(libs.assertj.core)
}

tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += "-Xjsr305=strict"
jvmTarget = "11"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2021-2024. Ivan Vakhrushev.
* https://github.com/mfvanek/pg-index-health-test-starter
*
* This file is a part of "pg-index-health-test-starter".
*
* Licensed under the Apache License 2.0
*/

package io.github.mfvanek.pg.spring.postgres.kt

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class PostgresDemoApplication

fun main(args: Array<String>) {
runApplication<PostgresDemoApplication>(*args)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2021-2024. Ivan Vakhrushev.
* https://github.com/mfvanek/pg-index-health-test-starter
*
* This file is a part of "pg-index-health-test-starter".
*
* Licensed under the Apache License 2.0
*/

package io.github.mfvanek.pg.spring.postgres.kt.config

import org.springframework.core.env.ConfigurableEnvironment
import org.springframework.core.env.Environment
import org.springframework.core.env.MapPropertySource
import org.testcontainers.containers.JdbcDatabaseContainer

internal const val DATASOURCE_URL_PROP_NAME: String = "spring.datasource.url"

fun addDatasourceUrlIfNeed(jdbcDatabaseContainer: JdbcDatabaseContainer<*>, environment: Environment): Boolean {
if (environment.getProperty(DATASOURCE_URL_PROP_NAME) == null && environment is ConfigurableEnvironment) {
val mps = environment.propertySources
mps.addFirst(
MapPropertySource(
"connectionString",
mapOf(DATASOURCE_URL_PROP_NAME to jdbcDatabaseContainer.jdbcUrl)
)
)
return true
}
return false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2021-2024. Ivan Vakhrushev.
* https://github.com/mfvanek/pg-index-health-test-starter
*
* This file is a part of "pg-index-health-test-starter".
*
* Licensed under the Apache License 2.0
*/

package io.github.mfvanek.pg.spring.postgres.kt.config

import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.env.Environment
import org.testcontainers.containers.JdbcDatabaseContainer
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.containers.wait.strategy.Wait
import javax.sql.DataSource

@Configuration(proxyBeanMethods = false)
class DatabaseConfig {
@Bean(initMethod = "start", destroyMethod = "stop")
fun jdbcDatabaseContainer(): JdbcDatabaseContainer<*> {
return PostgreSQLContainer("postgres:16.2")
.withDatabaseName("demo_for_pg_index_health_starter")
.withUsername("demo_user")
.withPassword("myUniquePassword")
.waitingFor(Wait.forListeningPort())
}

@Bean
fun dataSource(jdbcDatabaseContainer: JdbcDatabaseContainer<*>, environment: Environment): DataSource {
addDatasourceUrlIfNeed(jdbcDatabaseContainer, environment)
val hikariConfig = HikariConfig()
hikariConfig.jdbcUrl = jdbcDatabaseContainer.jdbcUrl
hikariConfig.username = jdbcDatabaseContainer.username
hikariConfig.password = jdbcDatabaseContainer.password
return HikariDataSource(hikariConfig)
}
}
5 changes: 5 additions & 0 deletions kotlin-demo-app/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
debug: false

spring:
main:
banner-mode: off
Loading

0 comments on commit 233459b

Please sign in to comment.