aboutsummaryrefslogtreecommitdiff
path: root/tests/run/map_java_conversions.scala
blob: b7b39128c8f78ab010feba30d8f28ed37d19bbc4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
object Test {

  def main(args: Array[String]): Unit = {
    import collection.JavaConversions._

    test(new java.util.HashMap[String, String])
    test(new java.util.Properties)
    testConcMap
  }

  def testConcMap: Unit = {
    import collection.JavaConversions._

    val concMap = new java.util.concurrent.ConcurrentHashMap[String, String]

    test(concMap)
    val cmap = mapAsScalaConcurrentMap(concMap)
    cmap.putIfAbsent("absentKey", "absentValue")
    cmap.put("somekey", "somevalue")
    assert(cmap.remove("somekey", "somevalue") == true)
    assert(cmap.replace("absentKey", "newAbsentValue") == Some("absentValue"))
    assert(cmap.replace("absentKey", "newAbsentValue", ".......") == true)
  }

  def test(m: collection.mutable.Map[String, String]): Unit = {
    m.clear
    assert(m.size == 0)

    m.put("key", "value")
    assert(m.size == 1)

    assert(m.put("key", "anotherValue") == Some("value"))
    assert(m.put("key2", "value2") == None)
    assert(m.size == 2)

    m += (("key3", "value3"))
    assert(m.size == 3)

    m -= "key2"
    assert(m.size == 2)
    assert(m.nonEmpty)
    assert(m.remove("key") == Some("anotherValue"))

    m.clear
    for (i <- 0 until 10) m += (("key" + i, "value" + i))
    for ((k, v) <- m) assert(k.startsWith("key"))
  }

}