All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wesleyhome.test.jupiter.provider.datetime.AbstractAnnotatedRandomDateTimeDataProvider.kt Maven / Gradle / Ivy

Go to download

Library to help generate test parameter permutations for parameterized tests in JUnit. This version is an initial attempt to convert to building with Gradle.

The newest version!
package com.wesleyhome.test.jupiter.provider.datetime

import com.wesleyhome.test.jupiter.annotations.validation.datetime.TruncateChronoUnit
import com.wesleyhome.test.jupiter.propertyValue
import com.wesleyhome.test.jupiter.provider.AbstractAnnotatedParameterDataProvider
import com.wesleyhome.test.jupiter.provider.TestParameter
import com.wesleyhome.test.jupiter.temporalAmount
import java.time.temporal.ChronoUnit
import java.time.temporal.TemporalAmount
import kotlin.random.Random
import kotlin.random.nextLong

internal abstract class AbstractAnnotatedRandomDateTimeDataProvider, A : Annotation>
    : AbstractAnnotatedParameterDataProvider() {

    open val formatPropertyName: String = ""

    final override fun createParameterOptionsData(testParameter: TestParameter): List {
        val annotation = findAnnotation(testParameter)!!
        val minString = annotation.propertyValue("min")
        val maxString = annotation.propertyValue("max")
        val size = annotation.propertyValue("size")
        val useOffset = annotation.propertyValue("useOffset")
        val errors = validate(minString, maxString, size, useOffset)
        if (errors.isNotEmpty()) {
            throw IllegalArgumentException(errors.joinToString("\n"))
        }
        val truncateTo = annotation.propertyValue("truncateTo")
        val format = getFormatString(annotation)
        val truncationUnit = if (useOffset) truncateTo.chronoUnit else ChronoUnit.MILLIS
        val now: T = now(truncationUnit)
        var min: T = if (useOffset) {
            addOffset(now, minString.temporalAmount())
        } else {
            convert(minString, format)
        }
        var max = if (useOffset) {
            addOffset(now, maxString.temporalAmount())
        } else {
            convert(maxString, format)
        }
        if (min > max) {
            val temp = min
            min = max
            max = temp
        }
        val range = longRange(min..max)
        return (1..size)
            .map { Random.nextLong(range) }
            .map { it -> convert(it) }
            .toList()
    }

    abstract fun validate(minString: String, maxString: String, size: Int, useOffset: Boolean): List

    open fun getFormatString(annotation: A): String = annotation.propertyValue(formatPropertyName)

    abstract fun longRange(range: ClosedRange): LongRange

    abstract fun now(truncationUnit: ChronoUnit): T

    abstract fun addOffset(value: T, offset: TemporalAmount): T

    abstract fun convert(value: String, format: String): T

    abstract fun convert(longValue: Long): T

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy