Skip to content

Commit

Permalink
feat: add unit tests for common jvm module (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen authored Apr 25, 2022
1 parent 8d63c28 commit dcc0e9d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
8 changes: 7 additions & 1 deletion common-jvm/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ java {
targetCompatibility = JavaVersion.VERSION_1_8
}

test {
useJUnitPlatform()
}

dependencies {
implementation project(":common")
api project(":common")
testImplementation platform("org.junit:junit-bom:5.7.2")
testImplementation "org.junit.jupiter:junit-jupiter"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.amplitude.common.jvm

import com.amplitude.common.Logger

/**
* Console logger
*/
class ConsoleLogger() : Logger {
override var logMode: Logger.LogMode = Logger.LogMode.INFO

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.amplitude.common.jvm

import com.amplitude.common.Logger
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.Arguments.arguments
import org.junit.jupiter.params.provider.MethodSource
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.PrintStream
import java.util.stream.Stream

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ConsoleLoggerTest {
private var outContent: ByteArrayOutputStream? = null
private val originalOut: PrintStream = System.out

@BeforeEach
fun setUpStreams() {
outContent = ByteArrayOutputStream()
System.setOut(PrintStream(outContent))
}

@AfterEach
@Throws(IOException::class)
fun restoreStreams() {
outContent!!.close()
System.setOut(originalOut)
}

@ParameterizedTest
@MethodSource("logArguments")
fun `test log with various modes`(
logMode: Logger.LogMode,
expectedErrorLog: String,
expectedWarnLog: String,
expectedDebugLog: String,
expectedInfoLog: String
) {
val logger = ConsoleLogger.logger
logger.logMode = logMode
logger.error("error message")
assertEquals(expectedErrorLog, outContent.toString().trim())
logger.warn("warn message")
assertEquals(expectedWarnLog, outContent.toString().trim())
logger.debug("debug message")
assertEquals(expectedDebugLog, outContent.toString().trim())
logger.info("info message")
assertEquals(expectedInfoLog, outContent.toString().trim())
}

fun logArguments(): Stream<Arguments?>? {
return Stream.of(
arguments(Logger.LogMode.ERROR, "error message", "error message", "error message", "error message"),
arguments(
Logger.LogMode.WARN,
"error message",
"error message\nwarn message",
"error message\nwarn message",
"error message\nwarn message"
),
arguments(
Logger.LogMode.DEBUG,
"error message",
"error message\nwarn message",
"error message\nwarn message\ndebug message",
"error message\nwarn message\ndebug message\ninfo message"
),
arguments(
Logger.LogMode.INFO,
"error message",
"error message\nwarn message",
"error message\nwarn message",
"error message\nwarn message\ninfo message"
),
arguments(Logger.LogMode.OFF, "", "", "", "")
)
}
}

0 comments on commit dcc0e9d

Please sign in to comment.