1 /*
2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package org.openjdk.tests.java.util;
25 
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.stream.LambdaTestHelpers;
31 
32 import org.testng.annotations.AfterClass;
33 import org.testng.annotations.AfterMethod;
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.BeforeMethod;
36 import org.testng.annotations.Test;
37 
38 import static org.testng.Assert.assertEquals;
39 
40 /**
41  * Unit tests for extension methods on Map
42  */
43 public class MapTest {
44 
45     private static final Map<Integer, String> EXPECTED = new HashMap<>();
46 
47     private Map<Integer, String> map;
48 
49     @BeforeClass
setUpClass()50     public void setUpClass() {
51         EXPECTED.put(0, "zero");
52         EXPECTED.put(1, "one");
53         EXPECTED.put(2, "two");
54         EXPECTED.put(3, "three");
55         EXPECTED.put(4, "four");
56         EXPECTED.put(5, "five");
57         EXPECTED.put(6, "six");
58         EXPECTED.put(7, "seven");
59         EXPECTED.put(8, "eight");
60         EXPECTED.put(9, "nine");
61     }
62 
63     @AfterClass
tearDownClass()64     public void tearDownClass() {
65         EXPECTED.clear();
66     }
67 
68     @BeforeMethod
setUp()69     public void setUp() {
70         map = new HashMap<>(EXPECTED);
71     }
72 
73     @AfterMethod
tearDown()74     public void tearDown() {
75         map.clear();
76         map = null;
77     }
78 
79     @Test(groups = { "serialization-hostile" })
testForEach()80     public void testForEach() {
81         final Set<String> values = new HashSet<>(EXPECTED.size());
82         map.forEach((k, v) -> {values.add(v);});
83         LambdaTestHelpers.assertContentsUnordered(values, EXPECTED.values());
84     }
85 
86     @Test
testReplaceAll()87     public void testReplaceAll() {
88         map.replaceAll((k, v) -> {return v.toUpperCase();});
89         for (final Map.Entry<Integer, String> entry : map.entrySet()) {
90             assertEquals(entry.getValue(), EXPECTED.get(entry.getKey()).toUpperCase());
91         }
92     }
93 }
94