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

Kotlin demo app #170

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 15 additions & 0 deletions config/detekt/detekt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
performance:
SpreadOperator:
active: false

libraries:
LibraryEntitiesShouldNotBePublic:
active: false

formatting:
MaximumLineLength:
maxLineLength: 200

style:
MaxLineLength:
maxLineLength: 200
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
5 changes: 5 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pitest-junit5Plugin = "1.2.1"
pitest-core = "1.15.3"
pg-index-health = "0.10.3"
commons-lang3 = "3.14.0"
detekt = "1.23.6"

[libraries]
junit-bom = "org.junit:junit-bom:5.10.2"
Expand All @@ -21,7 +22,11 @@ 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" }
detekt-formatting = { group = "io.gitlab.arturbosch.detekt", name = "detekt-formatting", version.ref = "detekt" }
detekt-libraries = { group = "io.gitlab.arturbosch.detekt", name = "detekt-rules-libraries", version.ref = "detekt" }

[plugins]
spring-boot-gradlePlugin = { id = "org.springframework.boot", version.ref = "spring-boot" }
spring-dependency-management = { id = "io.spring.dependency-management", version = "1.1.4" }
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
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
50 changes: 50 additions & 0 deletions kotlin-demo-app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import io.gitlab.arturbosch.detekt.Detekt
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)
alias(libs.plugins.detekt)
}

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)

detektPlugins(libs.detekt.formatting)
detektPlugins(libs.detekt.libraries)
}

tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += "-Xjsr305=strict"
jvmTarget = "11"
}
}

detekt {
toolVersion = libs.versions.detekt.get()
config.setFrom(file("../config/detekt/detekt.yml"))
buildUponDefaultConfig = true
}

tasks.withType<Detekt>().configureEach {
reports {
xml.required.set(true)
html.required.set(true)
}
}
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"

internal 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)
}
}
Loading