Skip to content

Commit 75701d6

Browse files
author
paweliwanow
committed
Fixes in serialization tests for LoggerTakingImplicit
1 parent 845d801 commit 75701d6

File tree

4 files changed

+89
-83
lines changed

4 files changed

+89
-83
lines changed

src/main/scala/com/typesafe/scalalogging/Logger.scala

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ object Logger {
3232
new Logger(underlying)
3333

3434
/**
35-
* Create a [[LoggerTakingImplicit]] wrapping the given underlying `org.slf4j.Logger`.
36-
*/
35+
* Create a [[LoggerTakingImplicit]] wrapping the given underlying `org.slf4j.Logger`.
36+
*/
3737
def takingImplicit[A](underlying: Underlying)(implicit ev: CanLog[A]): LoggerTakingImplicit[A] =
3838
new LoggerTakingImplicit[A](underlying)
3939

@@ -48,12 +48,12 @@ object Logger {
4848
new Logger(LoggerFactory.getLogger(name))
4949

5050
/**
51-
* Create a [[LoggerTakingImplicit]] for the given name.
52-
* Example:
53-
* {{{
54-
* val logger = Logger.takingImplicit[CorrelationId]("application")
55-
* }}}
56-
*/
51+
* Create a [[LoggerTakingImplicit]] for the given name.
52+
* Example:
53+
* {{{
54+
* val logger = Logger.takingImplicit[CorrelationId]("application")
55+
* }}}
56+
*/
5757
def takingImplicit[A](name: String)(implicit ev: CanLog[A]): LoggerTakingImplicit[A] =
5858
new LoggerTakingImplicit[A](LoggerFactory.getLogger(name))
5959

@@ -64,8 +64,8 @@ object Logger {
6464
new Logger(LoggerFactory.getLogger(clazz.getName))
6565

6666
/**
67-
* Create a [[LoggerTakingImplicit]] wrapping the created underlying `org.slf4j.Logger`.
68-
*/
67+
* Create a [[LoggerTakingImplicit]] wrapping the created underlying `org.slf4j.Logger`.
68+
*/
6969
def takingImplicit[A](clazz: Class[_])(implicit ev: CanLog[A]): LoggerTakingImplicit[A] =
7070
new LoggerTakingImplicit[A](LoggerFactory.getLogger(clazz.getName))
7171

@@ -81,13 +81,13 @@ object Logger {
8181
new Logger(LoggerFactory.getLogger(ct.runtimeClass.getName.stripSuffix("$")))
8282

8383
/**
84-
* Create a [[LoggerTakingImplicit]] for the runtime class wrapped by the implicit class
85-
* tag parameter.
86-
* Example:
87-
* {{{
88-
* val logger = Logger.takingImplicit[MyClass]
89-
* }}}
90-
*/
84+
* Create a [[LoggerTakingImplicit]] for the runtime class wrapped by the implicit class
85+
* tag parameter.
86+
* Example:
87+
* {{{
88+
* val logger = Logger.takingImplicit[MyClass, CorrelationId]
89+
* }}}
90+
*/
9191
def takingImplicit[T, A](implicit ct: ClassTag[T], ev: CanLog[A]): LoggerTakingImplicit[A] =
9292
new LoggerTakingImplicit[A](LoggerFactory.getLogger(ct.runtimeClass.getName.stripSuffix("$")))
9393
}

src/main/scala/com/typesafe/scalalogging/LoggerTakingImplicit.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ trait CanLog[A] { this: Serializable =>
88
}
99

1010
@SerialVersionUID(957385465L)
11-
final class LoggerTakingImplicit[A] private[scalalogging] (val underlying: Underlying)
12-
(implicit val canLogEv: CanLog[A]) extends Serializable {
11+
final class LoggerTakingImplicit[A] private[scalalogging] (val underlying: Underlying)(implicit val canLogEv: CanLog[A]) extends Serializable {
1312

1413
// Error
1514

src/test/scala/com/typesafe/scalalogging/LoggerSpec.scala

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,74 @@ class LoggerSpec extends WordSpec with Matchers with MockitoSugar {
374374
}
375375
}
376376

377+
"Serializing LoggerTakingImplicit" should {
378+
case class CorrelationId(value: String)
379+
implicit val correlationId = CorrelationId(value = "correlationId")
380+
381+
implicit case object canLogCorrelationId extends CanLog[CorrelationId] {
382+
override def logMessage(originalMsg: String, a: CorrelationId): String = s"${a.value} msg"
383+
}
384+
385+
def serialize[A](logger: LoggerTakingImplicit[A]): Array[Byte] = {
386+
val byteArrayStream = new ByteArrayOutputStream
387+
val out = new ObjectOutputStream(byteArrayStream)
388+
389+
out.writeObject(logger)
390+
out.close()
391+
byteArrayStream.close()
392+
393+
byteArrayStream.toByteArray
394+
}
395+
396+
def deserialize[A](array: Array[Byte]): LoggerTakingImplicit[A] = {
397+
val byteArrayStream = new ByteArrayInputStream(array)
398+
val in = new ObjectInputStream(byteArrayStream)
399+
400+
val logger = in.readObject.asInstanceOf[LoggerTakingImplicit[A]]
401+
in.close()
402+
byteArrayStream.close()
403+
404+
logger
405+
}
406+
407+
"be usable after deserialization" in {
408+
val logger =
409+
deserialize[CorrelationId](
410+
serialize[CorrelationId](
411+
Logger.takingImplicit[CorrelationId](
412+
org.slf4j.LoggerFactory.getLogger("test")
413+
)
414+
)
415+
)
416+
417+
logger.trace("Back from deserialization")
418+
}
419+
420+
"constructed by explicit class and be usable after deserialization" in {
421+
val logger =
422+
deserialize[CorrelationId](
423+
serialize[CorrelationId](
424+
Logger.takingImplicit[CorrelationId](
425+
classOf[LoggerSpec]
426+
)
427+
)
428+
)
429+
430+
logger.trace("Back from deserialization")
431+
}
432+
433+
"constructed by implicit class tag and be usable after deserialization" in {
434+
val logger =
435+
deserialize[CorrelationId](
436+
serialize[CorrelationId](
437+
Logger.takingImplicit[LoggerSpec, CorrelationId]
438+
)
439+
)
440+
441+
logger.trace("Back from deserialization")
442+
}
443+
}
444+
377445
def fixture(p: Underlying => Boolean, isEnabled: Boolean) =
378446
new {
379447
val msg = "msg"

src/test/scala/com/typesafe/scalalogging/LoggerTakingImplicitSpec.scala

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package com.typesafe.scalalogging
22

3-
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream}
4-
53
import org.mockito.Matchers._
64
import org.mockito.Mockito._
75
import org.scalatest.mockito.MockitoSugar
8-
import org.scalatest.{Matchers, WordSpec}
9-
import org.slf4j.{Logger => Underlying}
6+
import org.scalatest.{ Matchers, WordSpec }
7+
import org.slf4j.{ Logger => Underlying }
108

119
class LoggerTakingImplicitSpec extends WordSpec with Matchers with MockitoSugar {
1210

@@ -377,69 +375,10 @@ class LoggerTakingImplicitSpec extends WordSpec with Matchers with MockitoSugar
377375
}
378376
}
379377

380-
"Serializing Logger" should {
381-
implicit val correlationId = CorrelationId(value = "correlationId")
382-
383-
implicit case object canLogCorrelationId extends CanLog[CorrelationId] {
384-
override def logMessage(originalMsg: String, a: CorrelationId): String = s"${a.value} msg"
385-
}
386-
387-
def serialize[A](logger: LoggerTakingImplicit[A]): Array[Byte] = {
388-
val byteArrayStream = new ByteArrayOutputStream
389-
val out = new ObjectOutputStream(byteArrayStream)
390-
391-
out.writeObject(logger)
392-
out.close()
393-
byteArrayStream.close()
394-
395-
byteArrayStream.toByteArray
396-
}
397-
398-
def deserialize[A](array: Array[Byte]): LoggerTakingImplicit[A] = {
399-
val byteArrayStream = new ByteArrayInputStream(array)
400-
val in = new ObjectInputStream(byteArrayStream)
401-
402-
val logger = in.readObject.asInstanceOf[LoggerTakingImplicit[A]]
403-
in.close()
404-
byteArrayStream.close()
405-
406-
logger
407-
}
408-
409-
"be usable after deserialization" in {
410-
val logger =
411-
deserialize[CorrelationId](
412-
serialize[CorrelationId](
413-
Logger.takingImplicit[CorrelationId](
414-
org.slf4j.LoggerFactory.getLogger("test"))))
415-
416-
logger.trace("Back from deserialization")
417-
}
418-
419-
"constructed by explicit class and be usable after deserialization" in {
420-
val logger =
421-
deserialize[CorrelationId](
422-
serialize[CorrelationId](
423-
Logger.takingImplicit[CorrelationId](
424-
classOf[LoggerSpec])))
425-
426-
logger.trace("Back from deserialization")
427-
}
428-
429-
"constructed by implicit class tag and be usable after deserialization" in {
430-
val logger =
431-
deserialize[CorrelationId](
432-
serialize[CorrelationId](
433-
Logger.takingImplicit[LoggerSpec, CorrelationId]))
434-
435-
logger.trace("Back from deserialization")
436-
}
437-
}
438-
439378
def fixture(p: Underlying => Boolean, isEnabled: Boolean) =
440379
new {
441380
implicit val correlationId = CorrelationId("corrId")
442-
implicit val canLogCorrelationId = mock[CanLog[CorrelationId]]
381+
implicit val canLogCorrelationId = mock[CanLog[CorrelationId]]
443382
val msg = "msg"
444383
val cause = new RuntimeException("cause")
445384
val arg1 = "arg1"

0 commit comments

Comments
 (0)