Skip to content

Commit

Permalink
TS-38628 Dynamic logger creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Avanatiker committed Dec 4, 2024
1 parent 0f6cbf0 commit 9abca7e
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object LoggerUtils {
System.getProperty(JAVA_UTIL_LOGGING_CONFIG_FILE_SYSTEM_PROPERTY)
?: return

val logger = getLogger(LoggerUtils::class.java)
val logger = createLogger()
try {
val propertiesFilePath = Paths.get(loggingPropertiesFilePathString)
if (!propertiesFilePath.toFile().exists()) {
Expand All @@ -59,4 +59,10 @@ object LoggerUtils {
*/
@JvmStatic
fun getLogger(clazz: Class<*>) = Logger.getLogger(clazz.name)

/**
* Creates a logger for the given class.
*/
@JvmStatic
fun Any.createLogger(): Logger = Logger.getLogger(this::class.java.name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.teamscale.test_impacted.engine.executor
import com.teamscale.client.ClusteredTestDetails
import com.teamscale.client.PrioritizableTest
import com.teamscale.client.StringUtils.levenshteinDistance
import com.teamscale.test_impacted.commons.LoggerUtils.createLogger
import com.teamscale.test_impacted.commons.LoggerUtils.getLogger
import org.junit.platform.engine.UniqueId
import java.util.*
Expand All @@ -13,6 +14,8 @@ import java.util.logging.Logger
* by the Teamscale server to unique IDs used in JUnit Platform.
*/
class AvailableTests {
private val LOGGER = createLogger()

/**
* A mapping from the tests uniform path (Teamscale internal representation) to unique id (JUnit internal
* representation).
Expand Down Expand Up @@ -43,8 +46,4 @@ class AvailableTests {
}
return Optional.ofNullable(clusterUniqueId)
}

companion object {
private val LOGGER: Logger = getLogger(AvailableTests::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.teamscale.test_impacted.engine.executor;
package com.teamscale.test_impacted.engine.executor

import org.junit.platform.engine.TestDescriptor;

/** Interface for implementing different ways of ordering tests. */
public interface ITestSorter {
import org.junit.platform.engine.TestDescriptor

/** Interface for implementing different ways of ordering tests. */
interface ITestSorter {
/**
* Removes any tests from the test descriptor that should not be executed and changes the execution order of the
* remaining tests.
*/
void selectAndSort(TestDescriptor testDescriptor);

fun selectAndSort(testDescriptor: TestDescriptor)
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class TestEngineOptions {
File(reportDirectory, "server-request.txt")
)
return ImpactedTestsProvider(
client, baseline, baselineRevision, endCommit, endRevision, repository, partition,
client, baseline!!, baselineRevision!!, endCommit!!, endRevision!!, repository!!, partition!!,
runAllTests, includeAddedTests, includeFailedAndSkipped
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.teamscale.test_impacted.test_descriptor

import com.teamscale.test_impacted.commons.LoggerUtils.getLogger
import com.teamscale.test_impacted.commons.LoggerUtils.createLogger
import com.teamscale.test_impacted.test_descriptor.TestDescriptorUtils.getUniqueIdSegment
import org.junit.platform.engine.TestDescriptor
import java.lang.reflect.Field
import java.util.*
import java.util.logging.Logger

/**
* Test descriptor resolver for Cucumber. For details how we extract the uniform path, see comment in
* [getPickleName]. The cluster id is the .feature file in which the tests are defined.
*/
class CucumberPickleDescriptorResolver : ITestDescriptorResolver {
private val LOGGER = createLogger()

override fun getUniformPath(descriptor: TestDescriptor): Optional<String> {
val featurePath = descriptor.featurePath()
LOGGER.fine { "Resolved feature: $featurePath" }
Expand Down Expand Up @@ -145,8 +146,6 @@ class CucumberPickleDescriptorResolver : ITestDescriptorResolver {
/** Type of the unique id segment of a test descriptor representing a cucumber feature file */
const val FEATURE_SEGMENT_TYPE = "feature"

private val LOGGER: Logger = getLogger(CucumberPickleDescriptorResolver::class.java)

/**
* Escapes slashes (/) in a given input (usually a scenario name) with a backslash (\).
* <br></br><br></br>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.teamscale.test_impacted.test_descriptor

import com.teamscale.test_impacted.commons.LoggerUtils.getLogger
import com.teamscale.test_impacted.commons.LoggerUtils.createLogger
import org.junit.platform.engine.TestDescriptor
import java.util.*

/** Test descriptor resolver for JUnit based [org.junit.platform.engine.TestEngine]s. */
abstract class JUnitClassBasedTestDescriptorResolverBase : ITestDescriptorResolver {
private val LOGGER = createLogger()

override fun getUniformPath(descriptor: TestDescriptor): Optional<String> =
descriptor.getClassName().map { className ->
val dotName = className.replace(".", "/")
Expand All @@ -28,8 +30,4 @@ abstract class JUnitClassBasedTestDescriptorResolverBase : ITestDescriptorResolv

/** Returns the test class containing the test. */
protected abstract fun TestDescriptor.getClassName(): Optional<String>

companion object {
private val LOGGER = getLogger(JUnitClassBasedTestDescriptorResolverBase::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.teamscale.test_impacted.test_descriptor

import com.teamscale.test_impacted.commons.LoggerUtils.createLogger
import com.teamscale.test_impacted.commons.LoggerUtils.getLogger
import org.junit.platform.engine.TestDescriptor
import org.junit.platform.engine.UniqueId
Expand All @@ -24,7 +25,7 @@ class JUnitPlatformSuiteDescriptorResolver : ITestDescriptorResolver {
get() = "junit-platform-suite"

companion object {
private val LOGGER = getLogger(JUnitPlatformSuiteDescriptorResolver::class.java)
private val LOGGER = createLogger()

/** Type of the unique id segment of a test descriptor representing a test suite */
private const val SUITE_SEGMENT_TYPE: String = "suite"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.teamscale.test_impacted.test_descriptor

import com.teamscale.test_impacted.commons.LoggerUtils.createLogger
import com.teamscale.test_impacted.commons.LoggerUtils.getLogger
import org.junit.platform.commons.util.ClassLoaderUtils
import java.util.*
Expand All @@ -9,7 +10,7 @@ import java.util.*
* [ServiceLoader].
*/
object TestDescriptorResolverRegistry {
private val LOGGER = getLogger(TestDescriptorResolverRegistry::class.java)
private val LOGGER = createLogger()

private val TEST_DESCRIPTOR_RESOLVER_BY_ENGINE_ID = mutableMapOf<String, ITestDescriptorResolver>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.teamscale.test_impacted.test_descriptor

import com.teamscale.client.ClusteredTestDetails
import com.teamscale.test_impacted.commons.IndentingWriter
import com.teamscale.test_impacted.commons.LoggerUtils.createLogger
import com.teamscale.test_impacted.commons.LoggerUtils.getLogger
import com.teamscale.test_impacted.engine.executor.AvailableTests
import org.junit.platform.engine.TestDescriptor
Expand All @@ -13,7 +14,7 @@ import java.util.stream.Stream

/** Class containing utility methods for [TestDescriptor]s. */
object TestDescriptorUtils {
private val LOGGER = getLogger(TestDescriptorUtils::class.java)
private val LOGGER = createLogger()

/** Returns the test descriptor as a formatted string with indented children. */
@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@ import org.mockito.Mockito.mock
/** Base class for testing specific scenarios in the impacted test engine. */
abstract class ImpactedTestEngineTestBase {
private val testEngineRegistry = mock<TestEngineRegistry>()

private val testDataWriter = mock<TestDataWriter>()

private val impactedTestsProvider = mock<ImpactedTestsProvider>()

private val discoveryRequest = mock<EngineDiscoveryRequest>()

private val executionRequest = mock<ExecutionRequest>()

private val executionListener = mock<EngineExecutionListener>()

private val teamscaleAgentNotifier = mock<TeamscaleAgentNotifier>()

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ internal class ImpactedTestEngineWithDynamicTestsTest : ImpactedTestEngineTestBa
)
private val testRoot = SimpleTestDescriptor.testContainer(engineRootId, dynamicTestClassCase)

override val engines get() = listOf(DummyEngine(testRoot))
override val engines = listOf(DummyEngine(testRoot))

override val impactedTests get() =
override val impactedTests =
listOf(
PrioritizableTestCluster(
"example/DynamicTest",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ internal class ImpactedTestEngineWithTwoEnginesTest : ImpactedTestEngineTestBase

private val testEngine2Root = SimpleTestDescriptor.testContainer(engine2RootId, otherTestClass)

override val engines get() = listOf(
override val engines = listOf(
DummyEngine(testEngine1Root),
DummyEngine(testEngine2Root)
)

override val impactedTests get() =
override val impactedTests =
listOf(
PrioritizableTestCluster(
FIRST_TEST_CLASS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ internal class NoImpactedTestsTest : ImpactedTestEngineTestBase() {

private val testEngine1Root = SimpleTestDescriptor.testContainer(engine1RootId, firstTestClass)

override val engines get() = listOf(DummyEngine(testEngine1Root))
override val impactedTests get() = emptyList<PrioritizableTestCluster>()
override val engines = listOf(DummyEngine(testEngine1Root))
override val impactedTests = emptyList<PrioritizableTestCluster>()

override fun verifyCallbacks(executionListener: EngineExecutionListener) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal class TestwiseCoverageCollectingExecutionListenerTest {
executionListener.executionStarted(testClass)
Mockito.verify(executionListenerMock).executionStarted(testClass)

// Execution of impacted test case.
// Execution of an impacted test case.
executionListener.executionStarted(impactedTestCase)
Mockito.verify(mockApi).startTest("MyClass/impactedTestCase()")
Mockito.verify(executionListenerMock).executionStarted(impactedTestCase)
Expand Down Expand Up @@ -99,10 +99,10 @@ internal class TestwiseCoverageCollectingExecutionListenerTest {
val testCase1Id = testClassId.append("TEST_CASE", "testCase1()")
val testCase2Id = testClassId.append("TEST_CASE", "testCase2()")

val testCase1: TestDescriptor = SimpleTestDescriptor.testCase(testCase1Id)
val testCase2: TestDescriptor = SimpleTestDescriptor.testCase(testCase2Id)
val testClass: TestDescriptor = SimpleTestDescriptor.testContainer(testClassId, testCase1, testCase2)
val testRoot: TestDescriptor = SimpleTestDescriptor.testContainer(rootId, testClass)
val testCase1 = SimpleTestDescriptor.testCase(testCase1Id)
val testCase2 = SimpleTestDescriptor.testCase(testCase2Id)
val testClass = SimpleTestDescriptor.testContainer(testClassId, testCase1, testCase2)
val testRoot = SimpleTestDescriptor.testContainer(rootId, testClass)

Mockito.`when`(resolver.getUniformPath(testCase1))
.thenReturn(Optional.of("MyClass/testCase1()"))
Expand Down

0 comments on commit 9abca7e

Please sign in to comment.