From 27a69c2fb03f044889356d8a689e5989c6c0c3d4 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 4 Jan 2024 13:47:00 +0100 Subject: [PATCH] fix ByteStringBuilder.addAll (#903) (#908) * fix ByteStringBuilder.addAll * sort out tests for different scala versions * rework tests * rework unmanaged source dirs --- .../ByteStringBuilderScala213PlusSpec.scala | 41 +++++++++++++++++++ .../pekko/util/ByteStringBuilderSpec.scala | 38 +++++++++++++++++ .../org/apache/pekko/util/ByteString.scala | 2 +- .../org/apache/pekko/util/ByteString.scala | 2 +- project/PekkoBuild.scala | 10 +++++ 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 actor-tests/src/test/scala-2.13+/org/apache/pekko/util/ByteStringBuilderScala213PlusSpec.scala create mode 100644 actor-tests/src/test/scala/org/apache/pekko/util/ByteStringBuilderSpec.scala diff --git a/actor-tests/src/test/scala-2.13+/org/apache/pekko/util/ByteStringBuilderScala213PlusSpec.scala b/actor-tests/src/test/scala-2.13+/org/apache/pekko/util/ByteStringBuilderScala213PlusSpec.scala new file mode 100644 index 00000000000..7c7777d3a09 --- /dev/null +++ b/actor-tests/src/test/scala-2.13+/org/apache/pekko/util/ByteStringBuilderScala213PlusSpec.scala @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pekko.util + +import org.scalatest.matchers.should.Matchers +import org.scalatest.wordspec.AnyWordSpec + +/** + * Extra tests to run for ByteStringBuilder when building with Scala 2.13+ + */ +class ByteStringBuilderScala213PlusSpec extends AnyWordSpec with Matchers { + "ByteStringBuilder" should { + "handle addAll with LinearSeq" in { + val result: ByteString = ByteString.newBuilder.addAll(List[Byte]('a')).result() + result shouldEqual ByteString("a") + } + "handle addAll with IndexedSeq" in { + val result: ByteString = ByteString.newBuilder.addAll(Vector[Byte]('a')).result() + result shouldEqual ByteString("a") + } + "handle addAll with ByteString" in { + val result: ByteString = ByteString.newBuilder.addAll(ByteString("a")).result() + result shouldEqual ByteString("a") + } + } +} diff --git a/actor-tests/src/test/scala/org/apache/pekko/util/ByteStringBuilderSpec.scala b/actor-tests/src/test/scala/org/apache/pekko/util/ByteStringBuilderSpec.scala new file mode 100644 index 00000000000..c878e59c5ba --- /dev/null +++ b/actor-tests/src/test/scala/org/apache/pekko/util/ByteStringBuilderSpec.scala @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pekko.util + +import org.scalatest.matchers.should.Matchers +import org.scalatest.wordspec.AnyWordSpec + +class ByteStringBuilderSpec extends AnyWordSpec with Matchers { + "ByteStringBuilder" should { + "handle ++= with LinearSeq" in { + val result: ByteString = ByteString.newBuilder.++=(List[Byte]('a')).result() + result shouldEqual ByteString("a") + } + "handle ++= with IndexedSeq" in { + val result: ByteString = ByteString.newBuilder.++=(Vector[Byte]('a')).result() + result shouldEqual ByteString("a") + } + "handle ++= with ByteString" in { + val result: ByteString = ByteString.newBuilder.++=(ByteString("a")).result() + result shouldEqual ByteString("a") + } + } +} diff --git a/actor/src/main/scala-2.13/org/apache/pekko/util/ByteString.scala b/actor/src/main/scala-2.13/org/apache/pekko/util/ByteString.scala index 41696881436..5fa71412ae1 100644 --- a/actor/src/main/scala-2.13/org/apache/pekko/util/ByteString.scala +++ b/actor/src/main/scala-2.13/org/apache/pekko/util/ByteString.scala @@ -1190,7 +1190,7 @@ final class ByteStringBuilder extends Builder[Byte, ByteString] { _length += seq.length } case _ => - super.++=(xs) + super.addAll(xs) } this } diff --git a/actor/src/main/scala-3/org/apache/pekko/util/ByteString.scala b/actor/src/main/scala-3/org/apache/pekko/util/ByteString.scala index 077a69770e3..05a0eca2dd1 100644 --- a/actor/src/main/scala-3/org/apache/pekko/util/ByteString.scala +++ b/actor/src/main/scala-3/org/apache/pekko/util/ByteString.scala @@ -1190,7 +1190,7 @@ final class ByteStringBuilder extends Builder[Byte, ByteString] { _length += seq.length } case _ => - super.++=(xs) + super.addAll(xs) } this } diff --git a/project/PekkoBuild.scala b/project/PekkoBuild.scala index b637f8c647e..96ff13cf2ae 100644 --- a/project/PekkoBuild.scala +++ b/project/PekkoBuild.scala @@ -166,6 +166,16 @@ object PekkoBuild { case _ => Nil } }, + // Adds a `src/test/scala-2.13+` source directory for code shared + // between Scala 2.13 and Scala 3 + Test / unmanagedSourceDirectories ++= { + val sourceDir = (Test / sourceDirectory).value + CrossVersion.partialVersion(scalaVersion.value) match { + case Some((3, n)) => Seq(sourceDir / "scala-2.13+") + case Some((2, n)) if n >= 13 => Seq(sourceDir / "scala-2.13+") + case _ => Nil + } + }, ThisBuild / ivyLoggingLevel := UpdateLogging.Quiet, licenses := Seq(("Apache-2.0", url("https://www.apache.org/licenses/LICENSE-2.0.html"))), homepage := Some(url("https://pekko.apache.org/")),