summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorIlya Maykov <ivmaykov@gmail.com>2013-09-30 16:45:48 -0700
committerIlya Maykov <ivmaykov@gmail.com>2013-10-01 16:39:32 -0700
commit55a558a50bf4e020db6de2cd0bd9dbe2e5179eda (patch)
tree28e03f58df49491c39afe2d4a3428eb063e8eb45 /test
parent2bba7797028a19b541b5bd88bd2b732e9a58681c (diff)
downloadscala-55a558a50bf4e020db6de2cd0bd9dbe2e5179eda.tar.gz
scala-55a558a50bf4e020db6de2cd0bd9dbe2e5179eda.tar.bz2
scala-55a558a50bf4e020db6de2cd0bd9dbe2e5179eda.zip
SI-7883 - don't iterate over all keys in MapWrapper.containsKey()
Diffstat (limited to 'test')
-rw-r--r--test/junit/scala/collection/convert/MapWrapperTest.scala49
1 files changed, 49 insertions, 0 deletions
diff --git a/test/junit/scala/collection/convert/MapWrapperTest.scala b/test/junit/scala/collection/convert/MapWrapperTest.scala
new file mode 100644
index 0000000000..060b6b5937
--- /dev/null
+++ b/test/junit/scala/collection/convert/MapWrapperTest.scala
@@ -0,0 +1,49 @@
+package scala.collection.convert
+
+import org.junit.Assert._
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+
+@RunWith(classOf[JUnit4])
+class MapWrapperTest {
+
+ /* Test for SI-7883 */
+ @Test
+ def testContains() {
+ import scala.collection.JavaConverters.mapAsJavaMapConverter
+ import scala.language.reflectiveCalls // for accessing containsCounter
+
+ // A HashMap which throws an exception when the iterator() method is called.
+ // Before the fix for SI-7883, calling MapWrapper.containsKey() used to
+ // iterate through every element of the wrapped Map, and thus would crash
+ // in this case.
+ val scalaMap = new scala.collection.mutable.HashMap[String, String] {
+ var containsCounter = 0 // keep track of how often contains() has been called.
+ override def iterator = throw new UnsupportedOperationException
+
+ override def contains(key: String): Boolean = {
+ containsCounter += 1
+ super.contains(key)
+ }
+ }
+
+ val javaMap = scalaMap.asJava
+
+ scalaMap("hello") = "world"
+ scalaMap(null) = "null's value"
+
+ assertEquals(0, scalaMap.containsCounter)
+ assertTrue(javaMap.containsKey("hello")) // positive test
+ assertTrue(javaMap.containsKey(null)) // positive test, null key
+
+ assertFalse(javaMap.containsKey("goodbye")) // negative test
+ // Note: this case does NOT make it to scalaMap's contains() method because the runtime
+ // cast fails in MapWrapper, so the containsCounter is not incremented in this case.
+ assertFalse(javaMap.containsKey(42)) // negative test, wrong key type
+
+ assertEquals(Some("null's value"), scalaMap.remove(null))
+ assertFalse(javaMap.containsKey(null)) // negative test, null key
+ assertEquals(4, scalaMap.containsCounter)
+ }
+}