EN · DE · RU · FR · ES

#347: SourceFileCheckerTest.kt

projectforge-application/src/test/kotlin/org/projectforge/development/SourceFileCheckerTest.kt Type: Kotlin · Role: Test · Source: projectforge-application/src/test/kotlin/org/projectforge/development/SourceFileCheckerTest.kt 82 lines · 43 code · 34 comments · 5 blank
Unit/integration test for SourceFileChecker. Validates correctness of the corresponding production class behavior.

Code Structure

Annotations: author, Test, Throws

Classes: SourceFileCheckerTest, should

Functions (1): checkLoggerDeclarations

Properties (2): loggerRegex, supportedExtensions

Imports: 6 packages

Package: org.projectforge.development

Source Code (abridged)

package org.projectforge.development

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.slf4j.LoggerFactory
import java.io.File
import java.io.IOException
import java.nio.charset.StandardCharsets

/**
 * Different source file checks.
 * @author Kai Reinhard
 */
class SourceFileCheckerTest {
    /**
     * Checks all Kotlin and Java sources for correct declaration of LoggerFactory.getLogger(SourceFileCheckerTest::class.java). If any
     * Logger declaration differs from the source file name, the test fails.
     * Example: Foo.java: private final Logger log = LoggerFactory.getLogger(Bar.class); // Bar.class should bee Foo.class.
     */
    @Test
    @Throws(IOException::class)
    fun checkLoggerDeclarations() {
        var baseDir = File(System.getProperty("user.dir"))
        if (!baseDir.exists() || !baseDir.isDirectory) return
        if (baseDir.name == "projectforge-common") {
            // Check parent directory for checking all other ProjectForge modules too.
            baseDir = baseDir.parentFile
        }
        log.info("Checking directory '${baseDir.absolutePath}...")
        var processedKotlinFiles = 0
        var processedJavaFiles = 0
        val errorFiles = mutableListOf<String>()
        baseDir.walkTopDown().filter { supportedExtensions.contains(it.extension) }.forEach { file ->
            if (file.extension == "kt") {
                ++processedKotlinFiles
            } else {
                ++processedJavaFiles
            }
            val content = file.readText(StandardCharsets.UTF_8)
            val matchResult = loggerRegex.find(content)
            val loggerClass = matchResult?.groups?.get(1)?.value?.trim()
            if (loggerClass != null && file.nameWithoutExtension != loggerClass) {
                errorFiles.add(file.path)
                log.warn("$file declares foreign logger class: '$loggerClass'")
            }
        }
        log.info("Checked $processedKotlinFiles Kotlin and $processedJavaFiles Java source files for Logger declaration.")
        Assertions.assertTrue( errorFiles.isEmpty(), "Source files with mismatching logger classes found: ${errorFiles.joinToString { it }}")
    }

    companion object {
        private val loggerRegex = """LoggerFactory\s*.\s*getLogger\s*\(\s*(.*)\s*(\.class|:{2}class)""".toRegex()
        private val supportedExtensions = arrayOf("kt", "java")

        // Kotlin:
        private val log = LoggerFactory.getLogger(SourceFileCheckerTest::class.java)
        // Java: private static final Logger log = LoggerFactory.getLogger(SourceFileCheckerTest.class);
    }
}

Git History

868d6abb7 2025 -> 2026
63081666f Source file headers: 2024-> 2025.
b6092df09 Copyright 2023 -> 2024
ab45d51fa Copyright 2001-2022 -> 2001-2023.
5e563fb80 EnsureSourceHeadersAndI18nMain calls now SourceFileHeadersMain, SortAndCheckI18nPropertiesMain and I18nKeysUsage(create). Call this main before every release.