Skip to content

Commit d53078a

Browse files
authored
Merge branch 'Baeldung:master' into option-either-conversion
2 parents ea61322 + b57fc5f commit d53078a

File tree

141 files changed

+368
-171
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+368
-171
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ lazy val spark_scala = (project in file("spark-scala"))
715715

716716
addCommandAlias(
717717
"ci",
718-
";compile;test:compile;it:compile;scalafmtCheckAll;test"
718+
";compile;test:compile;it:compile;scalafmtCheckAll;validateUnitTestNames;test"
719719
)
720720

721721
addCommandAlias(

cats-effects/src/test/scala/com/baeldung/scala/catseffects/CancellationSpec.scala renamed to cats-effects/src/test/scala/com/baeldung/scala/catseffects/CancellationUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import java.util.concurrent.atomic.AtomicBoolean
1010

1111
import scala.concurrent.duration.DurationInt
1212

13-
class CancellationSpec extends AnyWordSpec with Matchers {
13+
class CancellationUnitTest extends AnyWordSpec with Matchers {
1414

1515
"Cancellation" should {
1616
"cancel the fiber directly and execute the action on cancellation" in {

cats-effects/src/test/scala/com/baeldung/scala/catseffects/ErrorHandlingSpec.scala renamed to cats-effects/src/test/scala/com/baeldung/scala/catseffects/ErrorHandlingUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.scalatest.wordspec.AnyWordSpec
1010
import scala.util.Try
1111
import scala.util.control.NoStackTrace;
1212

13-
class ErrorHandlingSpec extends AnyWordSpec with Matchers {
13+
class ErrorHandlingUnitTest extends AnyWordSpec with Matchers {
1414

1515
"Calculator" should {
1616
"return left value using attempt in case of division by zero" in {

internal-scripts/ClassRenamer.scala

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/** This is an internal scala-cli script written just to reduce manual work of
2+
* renaming files and class names to follow naming standards. This doesn't
3+
* fully correct all the class names, however reduces the manual effort by 90%.
4+
*/
5+
//> using toolkit default
6+
import os._
7+
8+
object RenameClassNames {
9+
def main(args: Array[String]): Unit = {
10+
val directory = os.pwd / os.up
11+
12+
os.walk(directory)
13+
.filter(_.ext == "scala")
14+
.filter(_.toString.contains("src/test/scala"))
15+
.filterNot(_.toString.endsWith("UnitTest.scala"))
16+
.filter(f =>
17+
f.toString.endsWith("Test.scala") || f.toString.endsWith(
18+
"Spec.scala"
19+
) || f.toString.endsWith("Tests.scala") || f.toString
20+
.endsWith("Suite.scala")
21+
)
22+
.foreach { _filePath =>
23+
val _fileName = _filePath.last
24+
25+
val newFileName = _filePath.toString
26+
.replace("Test.scala", "UnitTest.scala")
27+
.replace("Spec.scala", "UnitTest.scala")
28+
.replace("Suite.scala", "UnitTest.scala")
29+
.replace("Tests.scala", "UnitTest.scala")
30+
31+
// rename file
32+
val newFilePath = os.Path(newFileName)
33+
os.move(_filePath, newFilePath)
34+
35+
val content = os.read(newFilePath)
36+
val existingClassName = getClassFromContent(content)
37+
38+
val fileNameWithoutExtension = newFilePath.last.dropRight(6)
39+
40+
def isTestClass(existingClassName: String): Boolean = {
41+
existingClassName
42+
.endsWith("Spec") || existingClassName.endsWith("Test")
43+
}
44+
45+
// Rename the class if it doesn't match the filename
46+
if (
47+
isTestClass(
48+
existingClassName
49+
) && existingClassName.toLowerCase != fileNameWithoutExtension.toLowerCase
50+
) {
51+
// Update the content with the new class name
52+
val updatedContent = content.replaceAll(
53+
s"class\\s+$existingClassName",
54+
s"class $fileNameWithoutExtension"
55+
)
56+
57+
// Now, rename the class
58+
os.write.over(newFilePath, updatedContent)
59+
println(
60+
s"Renamed class in ${newFilePath.last} to $fileNameWithoutExtension"
61+
)
62+
}
63+
}
64+
}
65+
66+
// Extract class name from file content
67+
def getClassFromContent(content: String): String = {
68+
val classNameRegex = """class\s+(\w+)""".r
69+
val matchResult = classNameRegex.findFirstMatchIn(content)
70+
matchResult match {
71+
case Some(m) => m.group(1)
72+
case None => ""
73+
}
74+
}
75+
}

play-scala/application-tests/test/com/baeldung/arrival/actions/SourceActionsTest.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/actions/SourceActionsUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import play.api.mvc.Headers
88
import play.api.mvc.Results.NoContent
99
import play.api.test.{FakeRequest, Helpers}
1010

11-
class SourceActionsTest
11+
class SourceActionsUnitTest
1212
extends AnyWordSpec
1313
with SourceActions
1414
with ScalaFutures {

play-scala/application-tests/test/com/baeldung/arrival/controller/ArrivalControllerH2Test.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/controller/ArrivalControllerH2UnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import play.api.libs.ws.{WSClient, WSResponse}
1111

1212
import scala.concurrent.Future
1313

14-
class ArrivalControllerH2Test
14+
class ArrivalControllerH2UnitTest
1515
extends AnyWordSpec
1616
with WsScalaTestClient
1717
with GuiceOneServerPerTest

play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalDecoratorServiceTest.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalDecoratorServiceUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import org.scalatestplus.play.MixedPlaySpec
55
import play.api.Configuration
66
import play.api.inject.guice.GuiceApplicationBuilder
77

8-
class ArrivalDecoratorServiceTest extends MixedPlaySpec {
8+
class ArrivalDecoratorServiceUnitTest extends MixedPlaySpec {
99

1010
"ArrivalDecoratorService#decorate" should {
1111
"mark as short an arrival with plane name length = 5" in new App(

play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceH2Test.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceH2UnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.scalatestplus.play.guice.GuiceOneAppPerTest
88

99
import scala.language.postfixOps
1010

11-
class ArrivalServiceH2Test
11+
class ArrivalServiceH2UnitTest
1212
extends AnyWordSpec
1313
with GuiceOneAppPerTest
1414
with ScalaFutures

play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceIsolatedTest.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceIsolatedUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.scalatestplus.play.guice.GuiceOneAppPerTest
1313
import play.api.inject.guice.GuiceApplicationBuilder
1414
import play.api.{Application, Configuration, inject}
1515

16-
class ArrivalServiceIsolatedTest
16+
class ArrivalServiceIsolatedUnitTest
1717
extends AnyWordSpec
1818
with GuiceOneAppPerTest
1919
with ScalaFutures {

play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceMocksTest.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceMocksUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import slick.dbio.{DBIO, SuccessAction}
1515

1616
import scala.concurrent.Future
1717

18-
class ArrivalServiceMocksTest
18+
class ArrivalServiceMocksUnitTest
1919
extends AnyWordSpec
2020
with GuiceOneAppPerTest
2121
with ScalaFutures

play-scala/dependency-injection/test/guice/service/ServiceWithRemoteCallTest.scala renamed to play-scala/dependency-injection/test/guice/service/ServiceWithRemoteCallUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.scalatest.wordspec.FixtureAnyWordSpecLike
1010
import org.scalatestplus.play.MixedFixtures
1111
import play.api.inject.guice.GuiceApplicationBuilder
1212

13-
class ServiceWithRemoteCallTest
13+
class ServiceWithRemoteCallUnitTest
1414
extends MixedFixtures
1515
with FixtureAnyWordSpecLike {
1616

play-scala/dependency-injection/test/macwire/service/ServiceWithRemoteCallTest.scala renamed to play-scala/dependency-injection/test/macwire/service/ServiceWithRemoteCallUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package macwire.service
33
import macwire.components.{ApiComponents, MockApiComponents, ServiceComponents}
44
import org.scalatest.wordspec.AnyWordSpec
55

6-
class ServiceWithRemoteCallTest extends AnyWordSpec {
6+
class ServiceWithRemoteCallUnitTest extends AnyWordSpec {
77

88
"ServiceWithRemoteCall call" should {
99

reflection/src/test/scala-2/com/baeldung/reflection/JavaReflectionTest.scala renamed to reflection/src/test/scala-2/com/baeldung/reflection/JavaReflectionUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.baeldung.reflection
22

33
import org.junit.Test
44

5-
class JavaReflectionTest {
5+
class JavaReflectionUnitTest {
66
@Test
77
def invoke_method_dynamically(): Unit = {
88
val person = Person("John", 20)

reflection/src/test/scala-2/com/baeldung/reflection/ScalaReflectionTest.scala renamed to reflection/src/test/scala-2/com/baeldung/reflection/ScalaReflectionUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import org.junit.Test
44

55
import scala.reflect.runtime.{universe => ru}
66

7-
class ScalaReflectionTest {
7+
class ScalaReflectionUnitTest {
88
@Test
99
def instantiate_a_class_at_runtime_v1: Unit = {
1010
val mirror: ru.Mirror = ru.runtimeMirror(getClass.getClassLoader)

sbt-standalone/build.sbt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ scalacOptions += "-target:17"
1010
// Setting source and target for Java
1111
javacOptions ++= Seq("-source", "17", "-target", "17")
1212

13-
1413
// Enforcing the minimum JVM version
1514
initialize := {
16-
val _ = initialize.value // Ensure previous initializations are run
17-
18-
val required = VersionNumber("17")
19-
val current = VersionNumber(sys.props("java.specification.version"))
20-
println(current)
21-
assert(CompatibleJavaVersion(current, required), s"Java $required or above is required to run this project.")
15+
val _ = initialize.value // Ensure previous initializations are run
16+
17+
val required = VersionNumber("17")
18+
val current = VersionNumber(sys.props("java.specification.version"))
19+
println(current)
20+
assert(
21+
CompatibleJavaVersion(current, required),
22+
s"Java $required or above is required to run this project."
23+
)
2224
}
23-

sbt-standalone/project/CompatibleJavaVersion.scala

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import sbt._
22

33
// Define a custom object for Java specification version compatibility
44
object CompatibleJavaVersion extends VersionNumberCompatibility {
5-
def name = "Java specification compatibility"
5+
def name = "Java specification compatibility"
66

7-
def isCompatible(current: VersionNumber, required: VersionNumber): Boolean =
8-
current.numbers.zip(required.numbers)
9-
.foldRight(required.numbers.size <= current.numbers.size) {
10-
case ((curr, req), acc) => (curr > req) || (curr == req && acc)
11-
}
7+
def isCompatible(current: VersionNumber, required: VersionNumber): Boolean =
8+
current.numbers
9+
.zip(required.numbers)
10+
.foldRight(required.numbers.size <= current.numbers.size) {
11+
case ((curr, req), acc) => (curr > req) || (curr == req && acc)
12+
}
1213

13-
def apply(current: VersionNumber, required: VersionNumber): Boolean =
14-
isCompatible(current, required)
15-
}
14+
def apply(current: VersionNumber, required: VersionNumber): Boolean =
15+
isCompatible(current, required)
16+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
object Main extends App {
22
println("Hello from Scala!")
3-
}
3+
}

scala-2-modules/scala2-core/src/test/scala-2/com/baeldung/scala/macros/GenericMacrosTest.scala renamed to scala-2-modules/scala2-core/src/test/scala-2/com/baeldung/scala/macros/GenericMacrosUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.baeldung.scala.macros
33
import org.scalatest.matchers.must.Matchers
44
import org.scalatest.wordspec.AnyWordSpec
55

6-
class GenericMacrosTest extends AnyWordSpec with Matchers {
6+
class GenericMacrosUnitTest extends AnyWordSpec with Matchers {
77
"generic macro" should {
88
"return String for string argument" in {
99
GenericMacros.getType("this is a string") mustBe "String"

scala-2-modules/scala2-core/src/test/scala-2/com/baeldung/scala/macros/OddEvenMacrosTest.scala renamed to scala-2-modules/scala2-core/src/test/scala-2/com/baeldung/scala/macros/OddEvenMacrosUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.baeldung.scala.macros
33
import org.scalatest.matchers.must.Matchers
44
import org.scalatest.wordspec.AnyWordSpec
55

6-
class OddEvenMacrosTest extends AnyWordSpec with Matchers {
6+
class OddEvenMacrosUnitTest extends AnyWordSpec with Matchers {
77

88
"def macros" should {
99
"return literal odd for odd number" in {

scala-2-modules/scala2-libraries/src/finch/todo/src/main/scala/com/baeldung/finch/todo/Main.scala

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,16 @@ object Main extends IOApp {
5555
} yield Created(created)
5656
}
5757

58-
val getTodo: Endpoint[IO, Todo] = get(todosPath :: path[Int]) {
59-
id: Int =>
60-
for {
61-
todos <- sql"select * from todo where id = $id"
62-
.query[Todo]
63-
.to[Set]
64-
.transact(xa)
65-
} yield todos.headOption match {
66-
case None => NotFound(new Exception("Record not found"))
67-
case Some(todo) => Ok(todo)
68-
}
58+
val getTodo: Endpoint[IO, Todo] = get(todosPath :: path[Int]) { id: Int =>
59+
for {
60+
todos <- sql"select * from todo where id = $id"
61+
.query[Todo]
62+
.to[Set]
63+
.transact(xa)
64+
} yield todos.headOption match {
65+
case None => NotFound(new Exception("Record not found"))
66+
case Some(todo) => Ok(todo)
67+
}
6968
}
7069

7170
val updateTodo: Endpoint[IO, Todo] =

scala-2-modules/scala2-libraries/src/test/scala-2/com/baeldung/scala/async/ScalaAsyncTest.scala renamed to scala-2-modules/scala2-libraries/src/test/scala-2/com/baeldung/scala/async/ScalaAsyncUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ object ScalaAsyncTest {
5959
} yield r1 + r2
6060
}
6161

62-
class ScalaAsyncTest extends AsyncWordSpec with Matchers with ScalaFutures {
62+
class ScalaAsyncUnitTest extends AsyncWordSpec with Matchers with ScalaFutures {
6363

6464
import ScalaAsyncTest._
6565

scala-2-modules/scala2-libraries/src/test/scala-2/com/baeldung/scala/pureconfig/SampleConfigLoader.scala renamed to scala-2-modules/scala2-libraries/src/test/scala-2/com/baeldung/scala/pureconfig/SampleConfigLoaderUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import pureconfig.generic.ProductHint
1212
import pureconfig.error.ConfigReaderException
1313
import scala.concurrent.duration._
1414

15-
class SampleConfigLoader extends AnyWordSpec with Matchers {
15+
class SampleConfigLoaderUnitTest extends AnyWordSpec with Matchers {
1616

1717
"pureconfig" should {
1818
"load config successfully" in {

scala-akka-2/src/test/scala-2/com/baeldung/scala/akka_2/grpc/MessageExchangeServiceTest.scala renamed to scala-akka-2/src/test/scala-2/com/baeldung/scala/akka_2/grpc/MessageExchangeServiceUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import akka.testkit.TestKit
66
import org.scalatest.BeforeAndAfterAll
77
import org.scalatest.wordspec.AsyncWordSpecLike
88

9-
class MessageExchangeServiceTest
9+
class MessageExchangeServiceUnitTest
1010
extends TestKit(ActorSystem("Akka-gRPC"))
1111
with AsyncWordSpecLike
1212
with BeforeAndAfterAll {

scala-akka-2/src/test/scala-2/com/baeldung/scala/akka_2/persistence/BankAccountTest.scala renamed to scala-akka-2/src/test/scala-2/com/baeldung/scala/akka_2/persistence/BankAccountUnitTest.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import akka.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit
44
import akka.pattern.StatusReply
55
import org.scalatest.wordspec.AnyWordSpecLike
66

7-
class BankAccountTest extends ScalaTestWithActorTestKit with AnyWordSpecLike {
7+
class BankAccountUnitTest
8+
extends ScalaTestWithActorTestKit
9+
with AnyWordSpecLike {
810

911
"A Bank Account" should {
1012
"deposit money to a bank account" in {

scala-akka-2/src/test/scala-2/com/baeldung/scala/akka_2/query_parameters/QueryParametersTest.scala renamed to scala-akka-2/src/test/scala-2/com/baeldung/scala/akka_2/query_parameters/QueryParametersUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import akka.http.scaladsl.testkit.ScalatestRouteTest
77
import org.scalatest.matchers.should.Matchers
88
import org.scalatest.wordspec.AnyWordSpec
99

10-
class QueryParametersTest
10+
class QueryParametersUnitTest
1111
extends AnyWordSpec
1212
with ScalatestRouteTest
1313
with Matchers {

scala-akka-2/src/test/scala/com/baeldung/scala/akka/http/routetesting/AkkaRoutesTest.scala renamed to scala-akka-2/src/test/scala/com/baeldung/scala/akka/http/routetesting/AkkaRoutesUnitTest.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import com.baeldung.scala.akka.http.routetesting.AkkaRoutes.routes
77
import org.scalatest.flatspec.AnyFlatSpec
88
import org.scalatest.matchers.should.Matchers
99

10-
class AkkaRoutesTest extends AnyFlatSpec with Matchers with ScalatestRouteTest {
10+
class AkkaRoutesUnitTest
11+
extends AnyFlatSpec
12+
with Matchers
13+
with ScalatestRouteTest {
1114
"/hello" should "return Hello World!" in {
1215
Get("/hello") ~> routes ~> check {
1316
status shouldEqual StatusCodes.OK

scala-akka-2/src/test/scala/com/baeldung/scala/akka/stream/StreamTest.scala renamed to scala-akka-2/src/test/scala/com/baeldung/scala/akka/stream/StreamUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import org.scalatest.concurrent.ScalaFutures.convertScalaFuture
77
import org.scalatest.flatspec.AnyFlatSpec
88
import org.scalatest.matchers.should.Matchers
99

10-
class StreamTest extends AnyFlatSpec with Matchers {
10+
class StreamUnitTest extends AnyFlatSpec with Matchers {
1111
implicit val system: ActorSystem = ActorSystem("baeldung")
1212

1313
"The parse flow" should "parse pairs of integers" in {

scala-akka-3/src/test/scala/com/baeldung/scala/akka/TestService.scala renamed to scala-akka-3/src/test/scala/com/baeldung/scala/akka/TestServiceUnitTest.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import org.scalatest.matchers.should.Matchers
55
import org.scalatest.wordspec.AnyWordSpec
66
import akka.actor.testkit.typed.scaladsl.ActorTestKit
77

8-
class TestService extends AnyWordSpec with BeforeAndAfterAll with Matchers {
8+
class TestServiceUnitTest
9+
extends AnyWordSpec
10+
with BeforeAndAfterAll
11+
with Matchers {
912
val testKit = ActorTestKit()
1013
implicit val system = testKit.system
1114

0 commit comments

Comments
 (0)