Skip to content

Commit

Permalink
added test for better coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuth committed Oct 5, 2023
1 parent 8d14240 commit 4e0366f
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 127 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/de/gmuth/ipp/core/IppAttribute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ data class IppAttribute<T>(val name: String, val tag: IppTag) : IppAttributeBuil
fun enumNameOrValue(value: Any) =
if (tag == IppTag.Enum) getEnumName(name, value) else value

fun log(logger: Logger, level: Level = INFO, prefix: String = "") = logger.run {
fun log(logger: Logger, level: Level = INFO, prefix: String = "") = logger.also {
val string = toString()
if (string.length < 160) {
log(level) { "$prefix$string" }
it.log(level) { "$prefix$string" }
} else {
log(level) { "$prefix$name ($tag) =" }
it.log(level) { "$prefix$name ($tag) =" }
for (value in values) {
if (value is IppCollection) {
(value as IppCollection).log(logger, level, "$prefix ")
} else {
log(level) { "$prefix ${enumNameOrValue(value as Any)}" }
it.log(level) { "$prefix ${enumNameOrValue(value as Any)}" }
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/de/gmuth/ipp/core/IppAttributesGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package de.gmuth.ipp.core

import java.io.File
import java.time.Instant
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.util.logging.Level
import java.util.logging.Level.INFO
Expand Down Expand Up @@ -53,8 +53,8 @@ class IppAttributesGroup(val tag: IppTag) : LinkedHashMap<String, IppAttribute<*
fun getTextValue(name: String) =
getValue<IppString>(name).text

fun getTimeValue(name: String, zoneId: ZoneId = ZoneId.systemDefault()): ZonedDateTime =
Instant.ofEpochSecond(getValue<Int>(name).toLong()).atZone(zoneId)
fun getTimeValue(name: String): ZonedDateTime =
Instant.ofEpochSecond(getValue<Int>(name).toLong()).atZone(ZoneOffset.UTC)

fun put(attributesGroup: IppAttributesGroup) =
attributesGroup.values.forEach { put(it) }
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/de/gmuth/ipp/core/IppDateTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package de.gmuth.ipp.core
*/

import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.time.temporal.ChronoField
Expand Down Expand Up @@ -137,6 +138,10 @@ data class IppDateTime(
internal fun getTimeZoneId() =
"GMT%c%02d%02d".format(directionFromUTC, hoursFromUTC, minutesFromUTC)

companion object {
fun now(zone: ZoneId = ZoneId.systemDefault()) =
IppDateTime(ZonedDateTime.now(zone))
}
}

// Calendar extension
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/de/gmuth/ipp/core/IppInputStream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import java.util.logging.Logger.getLogger

class IppInputStream(inputStream: BufferedInputStream) : DataInputStream(inputStream) {

private val log = getLogger(javaClass.name)
internal val log = getLogger(javaClass.name)

// character encoding for text and name attributes, RFC 8011 4.1.4.1
internal lateinit var attributesCharset: Charset
Expand Down Expand Up @@ -236,7 +236,7 @@ class IppInputStream(inputStream: BufferedInputStream) : DataInputStream(inputSt
charStringBuilder.clear()
}
}
if (isNotEmpty()) dumpLine()
if (hexStringBuilder.isNotEmpty()) dumpLine()
}

}
2 changes: 1 addition & 1 deletion src/main/kotlin/de/gmuth/ipp/core/IppMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import java.util.logging.Logger.getLogger

abstract class IppMessage() {

private val log = getLogger(javaClass.name)
internal val log = getLogger(javaClass.name)
var code: Int? = null // unsigned short (16 bits)
var requestId: Int? = null
var version: String? = null
Expand Down
4 changes: 4 additions & 0 deletions src/test/kotlin/de/gmuth/ipp/client/IppClientTests.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package de.gmuth.ipp.client

/**
* Copyright (c) 2023 Gerhard Muth
*/

import de.gmuth.ipp.core.IppOperation.GetPrinterAttributes
import org.junit.Test
import java.net.URI
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package de.gmuth.ipp.core
package de.gmuth.ipp.client

/**
* Copyright (c) 2020 Gerhard Muth
*/

import de.gmuth.ipp.client.IppExchangeException
import de.gmuth.ipp.core.IppOperation
import de.gmuth.ipp.core.IppRequest
import java.util.logging.Logger.getLogger
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down
51 changes: 26 additions & 25 deletions src/test/kotlin/de/gmuth/ipp/core/IppAttributeTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ package de.gmuth.ipp.core
*/

import de.gmuth.ipp.core.IppTag.*
import de.gmuth.log.Logging
import java.io.File
import java.util.logging.Logger.getLogger
import kotlin.test.*

class IppAttributeTests {

private val ippAttribute = IppAttribute("printer-state-reasons", Keyword, "none")
val tlog = getLogger(javaClass.name)
init {
Logging.configure()
}

private val log = getLogger(javaClass.name)
private val attribute = IppAttribute("printer-state-reasons", Keyword, "none")

@Test
fun constructorFailsDueToDelimiterTag() {
Expand All @@ -26,28 +31,28 @@ class IppAttributeTests {

@Test
fun accessingSetAsValueFails() {
assertFailsWith<IppException> { ippAttribute.value }
assertFailsWith<IppException> { attribute.value }
}

@Test
fun additionalValue() {
ippAttribute.additionalValue(IppAttribute("", Keyword, "media-empty"))
assertEquals(2, ippAttribute.values.size)
attribute.additionalValue(IppAttribute("", Keyword, "media-empty"))
assertEquals(2, attribute.values.size)
}

@Test
fun additionalValueIgnore1() {
ippAttribute.additionalValue(IppAttribute("", Integer, 2.1))
attribute.additionalValue(IppAttribute("", Integer, 2.1))
}

@Test
fun additionalValueFails2() {
assertFailsWith<IppException> { ippAttribute.additionalValue(IppAttribute<Unit>("", Keyword)) }
assertFailsWith<IppException> { attribute.additionalValue(IppAttribute<Unit>("", Keyword)) }
}

@Test
fun additionalValueFails3() {
assertFailsWith<IppException> { ippAttribute.additionalValue(IppAttribute("invalid-name", Keyword, "wtf")) }
assertFailsWith<IppException> { attribute.additionalValue(IppAttribute("invalid-name", Keyword, "wtf")) }
}

@Test
Expand All @@ -60,13 +65,13 @@ class IppAttributeTests {

@Test
fun buildAttribute() {
assertEquals(ippAttribute, ippAttribute.buildIppAttribute(IppAttributesGroup(Printer)))
assertEquals(attribute, attribute.buildIppAttribute(IppAttributesGroup(Printer)))
}

@Test
fun toStringNoValue() {
ippAttribute.values.clear()
assertTrue(ippAttribute.toString().endsWith("no-value"))
attribute.values.clear()
assertTrue(attribute.toString().endsWith("no-value"))
}

@Test
Expand All @@ -81,21 +86,10 @@ class IppAttributeTests {
}

@Test
fun toStringTime1() {
IppAttribute("some-time", Integer, 1000).toString()
fun toStringTest() {
assertEquals("christmas-time (integer) = 1608160102", IppAttribute("christmas-time", Integer, 1608160102).toString())
}

@Test
fun toStringTime2() {
IppAttribute("christmas-time", Integer, 1608160102).toString()
}

@Test
fun toStringTimeOut() {
IppAttribute("some-time-out", Integer, 1000).toString()
}


@Test
fun enumNameOrValue() {
assertEquals("processing", IppAttribute("printer-state", IppTag.Enum, 0).enumNameOrValue(4))
Expand All @@ -104,7 +98,7 @@ class IppAttributeTests {
@Test
fun log() {
// cover an output with more than 160 characters and a collection value
IppAttribute("media-col".padEnd(160, '-'), BegCollection, IppCollection()).log(tlog)
IppAttribute("media-col".padEnd(160, '-'), BegCollection, IppCollection()).log(log)
}

@Test
Expand All @@ -122,4 +116,11 @@ class IppAttributeTests {
assertEquals("foo (1setOf integer) = 1,2,3", IppAttribute("foo", Integer, 1, 2, 3).toString())
}

@Test
fun attributeWithDateTimeHasValidValueClass() {
IppAttribute("datetime-now", DateTime, IppDateTime.now()).run {
assertTrue(tag.valueHasValidClass(value))
}
}

}
27 changes: 18 additions & 9 deletions src/test/kotlin/de/gmuth/ipp/core/IppAttributesGroupTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import kotlin.test.assertTrue

class IppAttributesGroupTests {

val log = getLogger(javaClass.name)
private val log = getLogger(javaClass.name)
private val group = IppAttributesGroup(Operation)

@Test
Expand All @@ -28,8 +28,9 @@ class IppAttributesGroupTests {
}

@Test
fun putWithReplacementAllowed() {
with(IppAttributesGroup(Printer)) {
fun putWithReplacement() {
group.run {
onReplaceWarn = false
attribute("number", Integer, 0)
attribute("number", Integer, 1, 2)
assertEquals(1, size)
Expand All @@ -39,11 +40,13 @@ class IppAttributesGroupTests {

@Test
fun putWithReplacementWarning() {
group.put(IppAttribute("number", Integer, 0))
group.onReplaceWarn = true
group.put(IppAttribute("number", Integer, 1, 2))
assertEquals(1, group.size)
assertEquals(group["number"]!!.values.size, 2)
group.run {
onReplaceWarn = true
put(IppAttribute("number", Integer, 0))
put(IppAttribute("number", Integer, 1, 2))
assertEquals(1, size)
assertEquals(get("number")!!.values.size, 2)
}
}

@Test
Expand All @@ -70,7 +73,7 @@ class IppAttributesGroupTests {
@Test
fun getValue() {
group.attribute("foo", Keyword, "bar")
assertEquals("bar", group.getValue("foo") as String)
assertEquals("bar", group.getValue<String>("foo"))
}

@Test
Expand All @@ -79,6 +82,12 @@ class IppAttributesGroupTests {
assertEquals("bar", group.getTextValue("foo"))
}

@Test
fun getEpochTimeValue() {
group.attribute("epoch-seconds", Integer, 62)
assertEquals("1970-01-01T00:01:02", group.getTimeValue("epoch-seconds").toLocalDateTime().toString())
}

@Test
fun getValueOrNull() {
group.attribute("foo0", Keyword, "bar0")
Expand Down
10 changes: 5 additions & 5 deletions src/test/kotlin/de/gmuth/ipp/core/IppCollectionTests.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.gmuth.ipp.core

/**
* Copyright (c) 2020 Gerhard Muth
* Copyright (c) 2020-2023 Gerhard Muth
*/

import java.util.NoSuchElementException
Expand All @@ -12,7 +12,7 @@ import kotlin.test.assertFailsWith

class IppCollectionTests {

val tlog = getLogger(javaClass.name)
private val log = getLogger(javaClass.name)
private val collection = IppCollection(IppAttribute("foo", IppTag.Keyword, "a", "b"))

@Test
Expand All @@ -21,7 +21,7 @@ class IppCollectionTests {
}

@Test
fun attribute() {
fun addAttribute() {
collection.addAttribute("year", IppTag.Integer, 2021)
assertEquals(2, collection.members.size)
}
Expand All @@ -41,13 +41,13 @@ class IppCollectionTests {

@Test
fun logNarrow() {
collection.log(tlog)
collection.log(log)
}

@Test
fun logWide() {
collection.addAll(listOf(IppAttribute("bar", IppTag.Keyword, "c".repeat(160))))
collection.log(tlog)
collection.log(log)
}

}
Loading

0 comments on commit 4e0366f

Please sign in to comment.