diff --git a/core/src/main/java/hudson/util/CopyOnWriteMap.java b/core/src/main/java/hudson/util/CopyOnWriteMap.java index a7fc3c347a..9bbb7e0b91 100644 --- a/core/src/main/java/hudson/util/CopyOnWriteMap.java +++ b/core/src/main/java/hudson/util/CopyOnWriteMap.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -56,7 +56,7 @@ public abstract class CopyOnWriteMap implements Map { */ private volatile Map view; - protected CopyOnWriteMap(Map core) { + protected CopyOnWriteMap(Map core) { update(core); } @@ -64,7 +64,7 @@ public abstract class CopyOnWriteMap implements Map { update(Collections.emptyMap()); } - private void update(Map m) { + protected void update(Map m) { core = m; view = Collections.unmodifiableMap(core); } @@ -142,7 +142,7 @@ public abstract class CopyOnWriteMap implements Map { * {@link CopyOnWriteMap} backed by {@link HashMap}. */ public static final class Hash extends CopyOnWriteMap { - public Hash(Map core) { + public Hash(Map core) { super(new LinkedHashMap(core)); } @@ -187,6 +187,7 @@ public abstract class CopyOnWriteMap implements Map { } public Tree(Comparator comparator) { + super(new TreeMap(comparator)); this.comparator = comparator; } @@ -195,11 +196,16 @@ public abstract class CopyOnWriteMap implements Map { } protected Map copy() { - TreeMap m = new TreeMap(comparator); + TreeMap m = new TreeMap(comparator); m.putAll(core); return m; } + @Override + public synchronized void clear() { + update(new TreeMap(comparator)); + } + public static class ConverterImpl extends TreeMapConverter { public ConverterImpl(Mapper mapper) { super(mapper); diff --git a/core/src/test/java/hudson/util/CopyOnWriteListTest.java b/core/src/test/java/hudson/util/CopyOnWriteListTest.java index bdb50ad3e6..3ba6cd1ff7 100644 --- a/core/src/test/java/hudson/util/CopyOnWriteListTest.java +++ b/core/src/test/java/hudson/util/CopyOnWriteListTest.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -27,9 +27,10 @@ import junit.framework.TestCase; import java.util.ArrayList; import java.util.List; +import java.io.StringWriter; /** - * @author Kohsuke Kawaguchi + * @author Kohsuke Kawaguchi, Alan Harder */ public class CopyOnWriteListTest extends TestCase { public static final class TestData { @@ -41,12 +42,27 @@ public class CopyOnWriteListTest extends TestCase { * Verify that the serialization form of List and CopyOnWriteList are the same. */ public void testSerialization() throws Exception { - XStream2 xs = new XStream2(); TestData td = new TestData(); + StringWriter out = new StringWriter(); + + xs.toXML(td, out); + assertEquals("empty lists", "" + + "", + out.toString().replaceAll("\\s+", "")); + TestData td2 = (TestData)xs.fromXML(out.toString()); + assertTrue(td2.list1.isEmpty()); + assertTrue(td2.list2.isEmpty()); + td.list1.add("foobar1"); td.list2.add("foobar2"); - - xs.toXML(td,System.out); + xs.toXML(td, out = new StringWriter()); + assertEquals("lists", "" + + "foobar1foobar2" + + "", + out.toString().replaceAll("\\s+", "")); + td2 = (TestData)xs.fromXML(out.toString()); + assertEquals("foobar1", td2.list1.getView().get(0)); + assertEquals("foobar2", td2.list2.get(0)); } } diff --git a/core/src/test/java/hudson/util/CopyOnWriteMapTest.java b/core/src/test/java/hudson/util/CopyOnWriteMapTest.java new file mode 100644 index 0000000000..00a42ee46c --- /dev/null +++ b/core/src/test/java/hudson/util/CopyOnWriteMapTest.java @@ -0,0 +1,115 @@ +/* + * The MIT License + * + * Copyright (c) 2010, Yahoo!, Inc., Alan Harder + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package hudson.util; + +import java.io.StringWriter; +import java.util.Comparator; +import java.util.HashMap; +import java.util.TreeMap; +import junit.framework.TestCase; + +/** + * @author Mike Dillon, Alan Harder + */ +public class CopyOnWriteMapTest extends TestCase { + public static final class HashData { + CopyOnWriteMap.Hash map1 = new CopyOnWriteMap.Hash(); + HashMap map2 = new HashMap(); + } + + /** + * Verify that serialization form of CopyOnWriteMap.Hash and HashMap are the same. + */ + public void testHashSerialization() throws Exception { + HashData td = new HashData(); + XStream2 xs = new XStream2(); + StringWriter out = new StringWriter(); + xs.toXML(td, out); + assertEquals("empty maps", "" + + "", + out.toString().replaceAll("\\s+", "")); + HashData td2 = (HashData)xs.fromXML(out.toString()); + assertTrue(td2.map1.isEmpty()); + assertTrue(td2.map2.isEmpty()); + + td.map1.put("foo1", "bar1"); + td.map2.put("foo2", "bar2"); + xs.toXML(td, out = new StringWriter()); + assertEquals("maps", "" + + "foo1bar1" + + "foo2bar2" + + "", + out.toString().replaceAll("\\s+", "")); + td2 = (HashData)xs.fromXML(out.toString()); + assertEquals("bar1", td2.map1.get("foo1")); + assertEquals("bar2", td2.map2.get("foo2")); + } + + public static final class TreeData { + CopyOnWriteMap.Tree map1; + TreeMap map2; + TreeData() { + map1 = new CopyOnWriteMap.Tree(); + map2 = new TreeMap(); + } + TreeData(Comparator comparator) { + map1 = new CopyOnWriteMap.Tree(comparator); + map2 = new TreeMap(comparator); + } + } + + /** + * Verify that an empty CopyOnWriteMap.Tree can be serialized, + * and that serialization form is the same as a standard TreeMap. + */ + public void testTreeSerialization() throws Exception { + TreeData td = new TreeData(); + XStream2 xs = new XStream2(); + StringWriter out = new StringWriter(); + xs.toXML(td, out); + assertEquals("empty maps", "" + + "" + + "", + out.toString().replaceAll("\\s+", "")); + TreeData td2 = (TreeData)xs.fromXML(out.toString()); + assertTrue(td2.map1.isEmpty()); + assertTrue(td2.map2.isEmpty()); + + td = new TreeData(String.CASE_INSENSITIVE_ORDER); + td.map1.put("foo1", "bar1"); + td.map2.put("foo2", "bar2"); + xs.toXML(td, out = new StringWriter()); + assertEquals("maps", "" + + "" + + "foo1bar1" + + "" + + "foo2bar2" + + "", + out.toString().replaceAll(">\\s+<", "><")); + td2 = (TreeData)xs.fromXML(out.toString()); + assertEquals("bar1", td2.map1.get("foo1")); + assertEquals("bar2", td2.map2.get("foo2")); + } +}