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 org.openjdk.testlib.java.util.stream.LambdaTestHelpers;
27 
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.Map;
31 import java.util.Set;
32 
33 import org.testng.annotations.AfterClass;
34 import org.testng.annotations.AfterMethod;
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.BeforeMethod;
37 import org.testng.annotations.Test;
38 
39 import static org.testng.Assert.assertEquals;
40 
41 /**
42  * Unit tests for extension methods on Map
43  */
44 public class MapTest {
45 
46     private static final Map<Integer, String> EXPECTED = new HashMap<>();
47 
48     private Map<Integer, String> map;
49 
50     @BeforeClass
setUpClass()51     public void setUpClass() {
52         EXPECTED.put(0, "zero");
53         EXPECTED.put(1, "one");
54         EXPECTED.put(2, "two");
55         EXPECTED.put(3, "three");
56         EXPECTED.put(4, "four");
57         EXPECTED.put(5, "five");
58         EXPECTED.put(6, "six");
59         EXPECTED.put(7, "seven");
60         EXPECTED.put(8, "eight");
61         EXPECTED.put(9, "nine");
62     }
63 
64     @AfterClass
tearDownClass()65     public void tearDownClass() {
66         EXPECTED.clear();
67     }
68 
69     @BeforeMethod
setUp()70     public void setUp() {
71         map = new HashMap<>(EXPECTED);
72     }
73 
74     @AfterMethod
tearDown()75     public void tearDown() {
76         map.clear();
77         map = null;
78     }
79 
80     @Test(groups = { "serialization-hostile" })
testForEach()81     public void testForEach() {
82         final Set<String> values = new HashSet<>(EXPECTED.size());
83         map.forEach((k, v) -> {values.add(v);});
84         LambdaTestHelpers.assertContentsUnordered(values, EXPECTED.values());
85     }
86 
87     @Test
testReplaceAll()88     public void testReplaceAll() {
89         map.replaceAll((k, v) -> {return v.toUpperCase();});
90         for (final Map.Entry<Integer, String> entry : map.entrySet()) {
91             assertEquals(entry.getValue(), EXPECTED.get(entry.getKey()).toUpperCase());
92         }
93     }
94 }
95