[FIXED HUDSON-5776] fix serialization problem with empty CopyOnWriteMap.Tree,

also add/update tests for CopyOnWriteMap and CopyOnWriteList.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@28026 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
mindless committed 2010-02-27 20:12:25 +00:00
1 parent 33b63f8a06
commit b0fbd644c8
3 files changed
+147 -10

No files matched your search

@@ -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", "<hudson.util.CopyOnWriteListTest_-TestData>"
+ "<list1/><list2/></hudson.util.CopyOnWriteListTest_-TestData>",
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", "<hudson.util.CopyOnWriteListTest_-TestData>"
+ "<list1><string>foobar1</string></list1><list2><string>foobar2"
+ "</string></list2></hudson.util.CopyOnWriteListTest_-TestData>",
out.toString().replaceAll("\\s+", ""));
td2 = (TestData)xs.fromXML(out.toString());
assertEquals("foobar1", td2.list1.getView().get(0));
assertEquals("foobar2", td2.list2.get(0));
}
}
@@ -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<String,String> map1 = new CopyOnWriteMap.Hash<String,String>();
HashMap<String,String> map2 = new HashMap<String,String>();
}
/**
* 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", "<hudson.util.CopyOnWriteMapTest_-HashData>"
+ "<map1/><map2/></hudson.util.CopyOnWriteMapTest_-HashData>",
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", "<hudson.util.CopyOnWriteMapTest_-HashData><map1>"
+ "<entry><string>foo1</string><string>bar1</string></entry></map1>"
+ "<map2><entry><string>foo2</string><string>bar2</string></entry>"
+ "</map2></hudson.util.CopyOnWriteMapTest_-HashData>",
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<String,String> map1;
TreeMap<String,String> map2;
TreeData() {
map1 = new CopyOnWriteMap.Tree<String,String>();
map2 = new TreeMap<String,String>();
}
TreeData(Comparator<String> comparator) {
map1 = new CopyOnWriteMap.Tree<String,String>(comparator);
map2 = new TreeMap<String,String>(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", "<hudson.util.CopyOnWriteMapTest_-TreeData>"
+ "<map1><no-comparator/></map1><map2><no-comparator/></map2>"
+ "</hudson.util.CopyOnWriteMapTest_-TreeData>",
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", "<hudson.util.CopyOnWriteMapTest_-TreeData><map1>"
+ "<comparator class=\"java.lang.String$CaseInsensitiveComparator\"/>"
+ "<entry><string>foo1</string><string>bar1</string></entry></map1>"
+ "<map2><comparator class=\"java.lang.String$CaseInsensitiveComparator\""
+ " reference=\"../../map1/comparator\"/>"
+ "<entry><string>foo2</string><string>bar2</string></entry></map2>"
+ "</hudson.util.CopyOnWriteMapTest_-TreeData>",
out.toString().replaceAll(">\\s+<", "><"));
td2 = (TreeData)xs.fromXML(out.toString());
assertEquals("bar1", td2.map1.get("foo1"));
assertEquals("bar2", td2.map2.get("foo2"));
}
}