Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jave Map wrapper respects put/remove contract
If map contains key, always return nonEmpty. If binding to null, return Some(null) instead of None.
- Loading branch information
Showing
2 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package scala.jdk | ||
|
||
import java.util.{ | ||
Dictionary, | ||
HashMap => JMap, | ||
Hashtable => JTable, | ||
Properties => JProperties, | ||
} | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
|
||
import scala.tools.testkit.AssertUtil.assertThrows | ||
|
||
import CollectionConverters._ | ||
|
||
class CollectionConvertersTest { | ||
@Test def `t11894 Map wrapper respects put contract`(): Unit = { | ||
val sut = new JMap[String, String].asScala | ||
assertEquals(None, sut.put("one", null)) | ||
assertEquals(Some(null), sut.put("one", "eins")) | ||
assertEquals(Some("eins"), sut.put("one", "uno")) | ||
assertEquals(Some("uno"), sut.remove("one")) | ||
assertEquals(None, sut.remove("one")) | ||
assertEquals(None, sut.put("one", null)) | ||
assertEquals(Some(null), sut.remove("one")) | ||
assertEquals(None, sut.remove("one")) | ||
} | ||
@Test def `t11894 Dictionary wrapper disallows nulls`(): Unit = { | ||
val tbl = new JTable[String, String] | ||
val sut = tbl.asInstanceOf[Dictionary[String, String]].asScala | ||
assertThrows[NullPointerException](sut.put("any", null)) | ||
} | ||
@Test def `t11894 Properties wrapper enforces Strings`(): Unit = { | ||
val ps = new JProperties() | ||
val sut = ps.asScala | ||
assertThrows[NullPointerException](sut.put("any", null)) | ||
assertEquals(None, sut.put("one", "eins")) | ||
assertEquals("eins", sut("one")) | ||
ps.put("one", new Object()) | ||
assertThrows[ClassCastException](sut.put("one", "uno")) | ||
} | ||
} |