Skip to content

Commit

Permalink
Use KotlinLogging for output logging #3338
Browse files Browse the repository at this point in the history
  • Loading branch information
fritschldwg committed Nov 23, 2023
1 parent 0a44fd4 commit 5dc1d0e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import de.maibornwolff.codecharta.tools.interactiveparser.InteractiveParser
import de.maibornwolff.codecharta.tools.interactiveparser.ParserDialogInterface
import de.maibornwolff.codecharta.tools.interactiveparser.util.CodeChartaConstants
import de.maibornwolff.codecharta.util.InputHelper
import mu.KotlinLogging
import picocli.CommandLine
import java.io.BufferedWriter
import java.io.File
Expand Down Expand Up @@ -43,6 +44,8 @@ class CSVExporter() : Callable<Void>, InteractiveParser {
override val name = NAME
override val description = DESCRIPTION

private val logger = KotlinLogging.logger {}

companion object {
const val NAME = "csvexport"
const val DESCRIPTION = "generates csv file with header"
Expand Down Expand Up @@ -83,7 +86,7 @@ class CSVExporter() : Callable<Void>, InteractiveParser {

if (outputFile.isNotEmpty()) {
val absoluteFilePath = File(outputFile).absolutePath
println("Created output file at $absoluteFilePath")
logger.info("Created output file at $absoluteFilePath")
}

return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package de.maibornwolff.codecharta.exporter.csvexporter
import de.maibornwolff.codecharta.exporter.csv.CSVExporter
import de.maibornwolff.codecharta.util.InputHelper
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.unmockkAll
import io.mockk.verify
import mu.KLogger
import mu.KotlinLogging
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -267,15 +271,18 @@ class CSVExporterTest {
val absoluteOutputFilePath = File(outputFilePath).absolutePath
val outputFile = File(outputFilePath)
outputFile.deleteOnExit()
System.setOut(PrintStream(outContent))

val loggerMock = mockk<KLogger>()
val infoMessagesLogged = mutableListOf<String>()
mockkObject(KotlinLogging)
every { KotlinLogging.logger(any<(() -> Unit)>()) } returns loggerMock
every { loggerMock.info(capture(infoMessagesLogged)) } returns Unit

// when
CommandLine(CSVExporter()).execute(inputFilePath, "-o", outputFilePath)

// then
Assertions.assertThat(outContent.toString().contains(absoluteOutputFilePath)).isTrue()

// clean up
System.setOut(originalOut)
verify { loggerMock.info(any<String>()) }
Assertions.assertThat(infoMessagesLogged.any { e -> e.contains(absoluteOutputFilePath) }).isTrue()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import de.maibornwolff.codecharta.tools.interactiveparser.ParserDialogInterface
import de.maibornwolff.codecharta.tools.interactiveparser.util.CodeChartaConstants
import de.maibornwolff.codecharta.util.InputHelper
import de.maibornwolff.codecharta.util.ResourceSearchHelper
import mu.KotlinLogging
import picocli.CommandLine
import java.io.BufferedWriter
import java.io.File
Expand Down Expand Up @@ -75,6 +76,8 @@ class SourceCodeParserMain(
override val name = NAME
override val description = DESCRIPTION

private val logger = KotlinLogging.logger {}

companion object {
const val NAME = "sourcecodeparser"
const val DESCRIPTION = "generates cc.json from source code"
Expand Down Expand Up @@ -145,7 +148,7 @@ class SourceCodeParserMain(
} else {
OutputFileHandler.checkAndFixFileExtension(nonNullOutputFile.absolutePath, compress, FileExtension.JSON)
}
println("Created output file at $absoluteFilePath")
logger.info("Created output file at $absoluteFilePath")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package de.maibornwolff.codecharta.importer.sourcecodeparser

import de.maibornwolff.codecharta.util.InputHelper
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.unmockkAll
import io.mockk.verify
import mu.KLogger
import mu.KotlinLogging
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
Expand All @@ -20,14 +24,13 @@ import java.io.PrintStream
class SourceCodeParserMainTest {
val errContent = ByteArrayOutputStream()
val originalErr = System.err
private var outContent = ByteArrayOutputStream()
private val originalOut = System.out

@AfterEach
fun afterTest() {
unmockkAll()
outContent = ByteArrayOutputStream()
}

companion object {
@JvmStatic
fun provideValidInputFiles(): List<Arguments> {
Expand Down Expand Up @@ -95,15 +98,18 @@ class SourceCodeParserMainTest {
val absoluteOutputFilePath = File(outputFilePath).absolutePath
val outputFile = File(outputFilePath)
outputFile.deleteOnExit()
System.setOut(PrintStream(outContent))

val loggerMock = mockk<KLogger>()
val infoMessagesLogged = mutableListOf<String>()
mockkObject(KotlinLogging)
every { KotlinLogging.logger(any<(() -> Unit)>()) } returns loggerMock
every { loggerMock.info(capture(infoMessagesLogged)) } returns Unit

// when
CommandLine(SourceCodeParserMain()).execute(inputFilePath, "-o", outputFilePath, "-nc").toString()

// then
Assertions.assertThat(outContent.toString().contains(absoluteOutputFilePath)).isTrue()

// clean up
System.setOut(originalOut)
verify { loggerMock.info(any<String>()) }
Assertions.assertThat(infoMessagesLogged.any { e -> e.endsWith(absoluteOutputFilePath) }).isTrue()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package de.maibornwolff.codecharta.serialization
import com.google.gson.GsonBuilder
import de.maibornwolff.codecharta.model.Project
import de.maibornwolff.codecharta.model.ProjectWrapper
import mu.KotlinLogging
import java.io.File
import java.io.IOException
import java.io.OutputStream
Expand All @@ -16,6 +17,7 @@ import java.util.zip.GZIPOutputStream
object ProjectSerializer {

private val GSON = GsonBuilder().create()
private val logger = KotlinLogging.logger {}

/**
* This method serializes a Project-Object to json and writes using given writer
Expand Down Expand Up @@ -62,7 +64,7 @@ object ProjectSerializer {
serializeProject(project, stream, reallyCompress)
if (!outputFilePath.isNullOrEmpty()) {
val absoluteFilePath = OutputFileHandler.checkAndFixFileExtension(File(outputFilePath).absolutePath, reallyCompress, FileExtension.JSON)
println("Created output file at $absoluteFilePath")
logger.info("Created output file at $absoluteFilePath")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,53 @@ package de.maibornwolff.codecharta.serialization

import de.maibornwolff.codecharta.model.Project
import io.mockk.called
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.unmockkAll
import io.mockk.verify
import mu.KLogger
import mu.KotlinLogging
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.skyscreamer.jsonassert.JSONAssert
import org.skyscreamer.jsonassert.JSONCompareMode
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream
import java.io.PrintStream
import kotlin.io.path.absolute
import kotlin.io.path.createTempDirectory
import kotlin.test.assertTrue

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ProjectSerializerTest {
private val EXAMPLE_JSON_VERSION_1_3 = "example_api_version_1.3.cc.json"
private val tempDir = createTempDirectory()
private val filename = tempDir.absolute().toString() + "test.cc.json"
private val project = mockk<Project>()
private var outContent = ByteArrayOutputStream()
private val originalOut = System.out
private val loggerMock = mockk<KLogger>()
private val infoMessagesLogged = mutableListOf<String>()

@BeforeAll
fun beforeTests() {
mockkObject(KotlinLogging)
every { KotlinLogging.logger(any<(() -> Unit)>()) } returns loggerMock
every { loggerMock.info(capture(infoMessagesLogged)) } returns Unit
}

@BeforeEach
fun beforeTest() {
infoMessagesLogged.clear()
}

@AfterEach
fun afterTest() {
@AfterAll
fun afterTests() {
unmockkAll()
outContent = ByteArrayOutputStream()
}

@Test
Expand Down Expand Up @@ -79,6 +97,7 @@ class ProjectSerializerTest {
// then
assertTrue { outputFile.exists() }
verify { mockStream wasNot called }
verify { loggerMock.info(any<String>()) }
}

@Test
Expand All @@ -95,22 +114,19 @@ class ProjectSerializerTest {
}

@Test
fun `should log the correct absolute path of the output file when serializing a project`() {
fun `should log the absolute path of the output file when output file is specified`() {
// given
val outputFilePath = "src/test/resources/output.cc.json"
val outputFile = File(outputFilePath)
val absoluteOutputFilePath = outputFile.absolutePath
outputFile.deleteOnExit()
val mockStream = mockk<OutputStream>()
System.setOut(PrintStream(outContent))

// when
ProjectSerializer.serializeToFileOrStream(project, outputFilePath, mockStream, false)

// then
Assertions.assertThat(outContent.toString().contains(absoluteOutputFilePath)).isTrue()

// clean up
System.setOut(originalOut)
verify { loggerMock.info(any<String>()) }
Assertions.assertThat(infoMessagesLogged.any { e -> e.endsWith(absoluteOutputFilePath) }).isTrue()
}
}

0 comments on commit 5dc1d0e

Please sign in to comment.