File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\test\java\hudson\util\CopyOnWriteMapTest.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, Yahoo!, Inc., Alan Harder
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import java.util.Comparator;
27 import java.util.HashMap;
28 import java.util.TreeMap;
29 import junit.framework.TestCase;
30
31 /**
32 * @author Mike Dillon, Alan Harder
33 */
34 public class CopyOnWriteMapTest extends TestCase {
35 public static final class HashData {
36 CopyOnWriteMap.Hash<String,String> map1 = new CopyOnWriteMap.Hash<String,String>();
37 HashMap<String,String> map2 = new HashMap<String,String>();
38 }
39
40 /**
41 * Verify that serialization form of CopyOnWriteMap.Hash and HashMap are the same.
42 */
43 public void testHashSerialization() throws Exception {
44 HashData td = new HashData();
45 XStream2 xs = new XStream2();
46
47 String out = xs.toXML(td);
48 assertEquals("empty maps", "<hudson.util.CopyOnWriteMapTest_-HashData>"
49 + "<map1/><map2/></hudson.util.CopyOnWriteMapTest_-HashData>",
50 out.replaceAll("\\s+", ""));
51 HashData td2 = (HashData)xs.fromXML(out);
52 assertTrue(td2.map1.isEmpty());
53 assertTrue(td2.map2.isEmpty());
54
55 td.map1.put("foo1", "bar1");
56 td.map2.put("foo2", "bar2");
57 out = xs.toXML(td);
58 assertEquals("maps", "<hudson.util.CopyOnWriteMapTest_-HashData><map1>"
59 + "<entry><string>foo1</string><string>bar1</string></entry></map1>"
60 + "<map2><entry><string>foo2</string><string>bar2</string></entry>"
61 + "</map2></hudson.util.CopyOnWriteMapTest_-HashData>",
62 out.replaceAll("\\s+", ""));
63 td2 = (HashData)xs.fromXML(out);
64 assertEquals("bar1", td2.map1.get("foo1"));
65 assertEquals("bar2", td2.map2.get("foo2"));
66 }
67
68 public static final class TreeData {
69 CopyOnWriteMap.Tree<String,String> map1;
70 TreeMap<String,String> map2;
71 TreeData() {
72 map1 = new CopyOnWriteMap.Tree<String,String>();
73 map2 = new TreeMap<String,String>();
74 }
75 TreeData(Comparator<String> comparator) {
76 map1 = new CopyOnWriteMap.Tree<String,String>(comparator);
77 map2 = new TreeMap<String,String>(comparator);
78 }
79 }
80
81 /**
82 * Verify that an empty CopyOnWriteMap.Tree can be serialized,
83 * and that serialization form is the same as a standard TreeMap.
84 */
85 public void testTreeSerialization() throws Exception {
86 TreeData td = new TreeData();
87 XStream2 xs = new XStream2();
88
89 String out = xs.toXML(td);
90 assertEquals("empty maps", "<hudson.util.CopyOnWriteMapTest_-TreeData>"
91 + "<map1><no-comparator/></map1><map2><no-comparator/></map2>"
92 + "</hudson.util.CopyOnWriteMapTest_-TreeData>",
93 out.replaceAll("\\s+", ""));
94 TreeData td2 = (TreeData)xs.fromXML(out);
95 assertTrue(td2.map1.isEmpty());
96 assertTrue(td2.map2.isEmpty());
97
98 td = new TreeData(String.CASE_INSENSITIVE_ORDER);
99 td.map1.put("foo1", "bar1");
100 td.map2.put("foo2", "bar2");
101 out = xs.toXML(td);
102 assertEquals("maps", "<hudson.util.CopyOnWriteMapTest_-TreeData><map1>"
103 + "<comparator class=\"java.lang.String$CaseInsensitiveComparator\"/>"
104 + "<entry><string>foo1</string><string>bar1</string></entry></map1>"
105 + "<map2><comparator class=\"java.lang.String$CaseInsensitiveComparator\""
106 + " reference=\"../../map1/comparator\"/>"
107 + "<entry><string>foo2</string><string>bar2</string></entry></map2>"
108 + "</hudson.util.CopyOnWriteMapTest_-TreeData>",
109 out.replaceAll(">\\s+<", "><"));
110 td2 = (TreeData)xs.fromXML(out);
111 assertEquals("bar1", td2.map1.get("foo1"));
112 assertEquals("bar2", td2.map2.get("foo2"));
113 }
114 }
115