Title: [2067] trunk: Implicit elements that match multiple defined implicit collections will be assigned to the map with the nearest matching element type (XSTR-733).

Diff

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/mapper/ImplicitCollectionMapper.java (2066 => 2067)


--- trunk/xstream/src/java/com/thoughtworks/xstream/mapper/ImplicitCollectionMapper.java	2013-05-29 18:12:07 UTC (rev 2066)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/mapper/ImplicitCollectionMapper.java	2013-06-01 12:38:45 UTC (rev 2067)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2005 Joe Walnes.
- * Copyright (C) 2006, 2007, 2009, 2011, 2012 XStream Committers.
+ * Copyright (C) 2006, 2007, 2009, 2011, 2012, 2013 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -165,9 +165,11 @@
                             return def.getFieldName();
                         }
                     } else {
-                        unnamed = def;
-                        if (itemFieldName == null) {
-                            break;
+                        if (unnamed == null
+                            || unnamed.getItemType() == null
+                            || (def.getItemType() != null 
+                                && unnamed.getItemType().isAssignableFrom(def.getItemType()))) {
+                            unnamed = def;
                         }
                     }
                 }

Modified: trunk/xstream/src/test/com/thoughtworks/acceptance/ImplicitArrayTest.java (2066 => 2067)


--- trunk/xstream/src/test/com/thoughtworks/acceptance/ImplicitArrayTest.java	2013-05-29 18:12:07 UTC (rev 2066)
+++ trunk/xstream/src/test/com/thoughtworks/acceptance/ImplicitArrayTest.java	2013-06-01 12:38:45 UTC (rev 2067)
@@ -69,6 +69,7 @@
     }
 
     public static class MegaFarm extends Farm {
+        String separator = "---";
         String[] names;
     }
 
@@ -87,6 +88,7 @@
                 "  <animal>\n" +
                 "    <name>Sheep</name>\n" +
                 "  </animal>\n" +
+                "  <separator>---</separator>\n" +
                 "</MEGA-farm>";
 
         xstream.addImplicitCollection(Farm.class, "animals");
@@ -112,6 +114,7 @@
                 "  <animal>\n" +
                 "    <name>Sheep</name>\n" +
                 "  </animal>\n" +
+                "  <separator>---</separator>\n" +
                 "  <name>McDonald</name>\n" +
                 "  <name>Ponte Rosa</name>\n" +
                 "</MEGA-farm>";
@@ -140,6 +143,7 @@
                 "  <animal>\n" +
                 "    <name>Sheep</name>\n" +
                 "  </animal>\n" +
+                "  <separator>---</separator>\n" +
                 "  <name>McDonald</name>\n" +
                 "  <name>Ponte Rosa</name>\n" +
                 "</MEGA-farm>";
@@ -164,6 +168,7 @@
                 "  <animal>\n" +
                 "    <name>Sheep</name>\n" +
                 "  </animal>\n" +
+                "  <separator>---</separator>\n" +
                 "</MEGA-farm>";
 
         xstream.addImplicitCollection(MegaFarm.class, "animals");
@@ -206,6 +211,7 @@
                 "    <animal>\n" +
                 "      <name>Sheep</name>\n" +
                 "    </animal>\n" +
+                "    <separator>---</separator>\n" +
                 "    <name>McDonald</name>\n" +
                 "    <name>Ponte Rosa</name>\n" +
                 "  </MEGA-farm>\n" +
@@ -219,6 +225,7 @@
 
     public static class House extends StandardObject {
         private Room[] rooms;
+        private String separator = "---";
         private Person[] people;
         
         public List getPeople() {
@@ -277,6 +284,7 @@
                 + "  <room>\n"
                 + "    <name>bathroom</name>\n"
                 + "  </room>\n"
+                + "  <separator>---</separator>\n"
                 + "  <person>\n"
                 + "    <name>joe</name>\n"
                 + "    <email>[email protected]</email>\n"
@@ -326,6 +334,7 @@
                 + "  <room class=\"chamber\" number=\"13\">\n"
                 + "    <name>room</name>\n"
                 + "  </room>\n"
+                + "  <separator>---</separator>\n"
                 + "</house>";
 
         xstream.alias("house", House.class);

Modified: trunk/xstream/src/test/com/thoughtworks/acceptance/ImplicitCollectionTest.java (2066 => 2067)


--- trunk/xstream/src/test/com/thoughtworks/acceptance/ImplicitCollectionTest.java	2013-05-29 18:12:07 UTC (rev 2066)
+++ trunk/xstream/src/test/com/thoughtworks/acceptance/ImplicitCollectionTest.java	2013-06-01 12:38:45 UTC (rev 2067)
@@ -102,6 +102,7 @@
     }
 
     public static class MegaFarm extends Farm {
+        String separator = "---";
         List names;
         public MegaFarm(int size) {
             super(size);
@@ -124,6 +125,7 @@
                 "  <animal>\n" +
                 "    <name>Sheep</name>\n" +
                 "  </animal>\n" +
+                "  <separator>---</separator>\n" +
                 "</MEGA-farm>";
 
         xstream.addImplicitCollection(Farm.class, "animals");
@@ -149,6 +151,7 @@
                 "  <animal>\n" +
                 "    <name>Sheep</name>\n" +
                 "  </animal>\n" +
+                "  <separator>---</separator>\n" +
                 "  <name>McDonald</name>\n" +
                 "  <name>Ponte Rosa</name>\n" +
                 "</MEGA-farm>";
@@ -177,6 +180,7 @@
                 "  <animal>\n" +
                 "    <name>Sheep</name>\n" +
                 "  </animal>\n" +
+                "  <separator>---</separator>\n" +
                 "  <name>McDonald</name>\n" +
                 "  <name>Ponte Rosa</name>\n" +
                 "</MEGA-farm>";
@@ -202,6 +206,7 @@
                 "  <animal>\n" +
                 "    <name>Sheep</name>\n" +
                 "  </animal>\n" +
+                "  <separator>---</separator>\n" +
                 "</MEGA-farm>";
 
         xstream.addImplicitCollection(MegaFarm.class, "animals");
@@ -243,6 +248,7 @@
                 "    <animal>\n" +
                 "      <name>Sheep</name>\n" +
                 "    </animal>\n" +
+                "    <separator>---</separator>\n" +
                 "    <name>McDonald</name>\n" +
                 "    <name>Ponte Rosa</name>\n" +
                 "  </MEGA-farm>\n" +
@@ -256,6 +262,7 @@
 
     public static class House extends StandardObject {
         private List rooms = new ArrayList();
+        private String separator = "---";
         private List people = new ArrayList();
 
         public void add(Room room) {
@@ -318,6 +325,7 @@
                 + "  <room>\n"
                 + "    <name>bathroom</name>\n"
                 + "  </room>\n"
+                + "  <separator>---</separator>\n"
                 + "  <person>\n"
                 + "    <name>joe</name>\n"
                 + "    <email>[email protected]</email>\n"
@@ -339,14 +347,18 @@
         assertEquals(house.getPeople(), serializedHouse.getPeople());
         assertEquals(house.getRooms(), serializedHouse.getRooms());
     }
-    
+
     public void testWithEMPTY_LIST() {
         House house = new House();
         house.people = Collections.EMPTY_LIST;
         house.rooms = Collections.EMPTY_LIST;
         xstream.addImplicitCollection(House.class, "rooms", Room.class);
         xstream.addImplicitCollection(House.class, "people", Person.class);
-        assertEquals("<house/>", xstream.toXML(house));
+        String expected = "" 
+                + "<house>\n" 
+                + "  <separator>---</separator>\n" 
+                + "</house>";
+        assertEquals(expected, xstream.toXML(house));
     }
 
     public static class Zoo extends StandardObject {

Modified: trunk/xstream/src/test/com/thoughtworks/acceptance/ImplicitMapTest.java (2066 => 2067)


--- trunk/xstream/src/test/com/thoughtworks/acceptance/ImplicitMapTest.java	2013-05-29 18:12:07 UTC (rev 2066)
+++ trunk/xstream/src/test/com/thoughtworks/acceptance/ImplicitMapTest.java	2013-06-01 12:38:45 UTC (rev 2067)
@@ -120,6 +120,7 @@
     }
 
     public static class MegaSampleMaps extends SampleMaps {
+        String separator = "---";
         Map other = new OrderRetainingMap();
         {
             good = new OrderRetainingMap();
@@ -145,6 +146,7 @@
                 "    <name>Linux</name>\n" +
                 "  </software>\n" +
                 "  <bad/>\n" +
+                "  <separator>---</separator>\n" +
                 "  <other/>\n" +
                 "</MEGA-sample>";
 
@@ -171,6 +173,7 @@
                 "    <name>Linux</name>\n" +
                 "  </software>\n" +
                 "  <bad/>\n" +
+                "  <separator>---</separator>\n" +
                 "  <hardware>\n" +
                 "    <arch>i386</arch>\n" +
                 "    <name>Intel</name>\n" +
@@ -201,6 +204,7 @@
                 "    <name>Linux</name>\n" +
                 "  </software>\n" +
                 "  <bad/>\n" +
+                "  <separator>---</separator>\n" +
                 "  <hardware>\n" +
                 "    <arch>i386</arch>\n" +
                 "    <name>Intel</name>\n" +
@@ -230,6 +234,7 @@
                 "    <name>Linux</name>\n" +
                 "  </software>\n" +
                 "  <bad/>\n" +
+                "  <separator>---</separator>\n" +
                 "  <other/>\n" +
                 "</MEGA-sample>";
 
@@ -269,6 +274,7 @@
                 "      <name>Linux</name>\n" +
                 "    </software>\n" +
                 "    <bad/>\n" +
+                "    <separator>---</separator>\n" +
                 "    <hardware>\n" +
                 "      <arch>i386</arch>\n" +
                 "      <name>Intel</name>\n" +
@@ -312,6 +318,7 @@
                 "    <id>i</id>\n" +
                 "    <price>399.99</price>\n" +
                 "  </product>\n" +
+                "  <separator>---</separator>\n" +
                 "  <hardware>\n" +
                 "    <arch>i386</arch>\n" +
                 "    <name>Intel</name>\n" +

Modified: trunk/xstream/src/test/com/thoughtworks/acceptance/ImplicitTest.java (2066 => 2067)


--- trunk/xstream/src/test/com/thoughtworks/acceptance/ImplicitTest.java	2013-05-29 18:12:07 UTC (rev 2066)
+++ trunk/xstream/src/test/com/thoughtworks/acceptance/ImplicitTest.java	2013-06-01 12:38:45 UTC (rev 2067)
@@ -33,11 +33,13 @@
         }
         
         public A[] aArray = new A[2];
+        public String separator1 = "--1--";
         public List bList = new ArrayList();
+        public String separator2 = "--2--";
         public Map cMap = new OrderRetainingMap();
     }
     
-    public void testAllImplicitTypesAtOnce()
+    public void testAllImplicitTypesAtOnceWithImplicitElementTypes()
     {
         xstream.alias("implicits", AllImplicitTypes.class);
         xstream.alias("a", AllImplicitTypes.A.class);
@@ -55,12 +57,14 @@
             + "  <a>\n"
             + "    <val>2</val>\n"
             + "  </a>\n"
+            + "  <separator1>--1--</separator1>\n"
             + "  <b>\n"
             + "    <val>3</val>\n"
             + "  </b>\n"
             + "  <b>\n"
             + "    <val>4</val>\n"
             + "  </b>\n"
+            + "  <separator2>--2--</separator2>\n"
             + "  <c>\n"
             + "    <val>5</val>\n"
             + "  </c>\n"
@@ -86,4 +90,105 @@
         implicits.cMap.put(c.val, c);
         assertBothWays(implicits, expected);
     }
+    
+    public void testAllImplicitTypesAtOnceWithExplicitElementTypes()
+    {
+        xstream.alias("implicits", AllImplicitTypes.class);
+        xstream.alias("a", AllImplicitTypes.A.class);
+        xstream.alias("b", AllImplicitTypes.B.class);
+        xstream.alias("c", AllImplicitTypes.C.class);
+        xstream.addDefaultImplementation(OrderRetainingMap.class, Map.class);
+        xstream.addImplicitArray(AllImplicitTypes.class, "aArray");
+        xstream.addImplicitCollection(AllImplicitTypes.class, "bList", AllImplicitTypes.B.class);
+        xstream.addImplicitMap(AllImplicitTypes.class, "cMap", AllImplicitTypes.C.class, "val");
+        String expected = ""
+            + "<implicits>\n"
+            + "  <a>\n"
+            + "    <val>1</val>\n"
+            + "  </a>\n"
+            + "  <a>\n"
+            + "    <val>2</val>\n"
+            + "  </a>\n"
+            + "  <separator1>--1--</separator1>\n"
+            + "  <b>\n"
+            + "    <val>3</val>\n"
+            + "  </b>\n"
+            + "  <b>\n"
+            + "    <val>4</val>\n"
+            + "  </b>\n"
+            + "  <separator2>--2--</separator2>\n"
+            + "  <c>\n"
+            + "    <val>5</val>\n"
+            + "  </c>\n"
+            + "  <c>\n"
+            + "    <val>6</val>\n"
+            + "  </c>\n"
+            + "</implicits>";
+
+        AllImplicitTypes implicits = new AllImplicitTypes();
+        implicits.aArray[0] = new AllImplicitTypes.A();
+        implicits.aArray[0].val = 1;
+        implicits.aArray[1] = new AllImplicitTypes.A();
+        implicits.aArray[1].val = 2;
+        implicits.bList.add(new AllImplicitTypes.B());
+        ((AllImplicitTypes.B)implicits.bList.get(0)).val = 3;
+        implicits.bList.add(new AllImplicitTypes.B());
+        ((AllImplicitTypes.B)implicits.bList.get(1)).val = 4;
+        AllImplicitTypes.C c = new AllImplicitTypes.C();
+        c.val = new Integer(5);
+        implicits.cMap.put(c.val, c);
+        c = new AllImplicitTypes.C();
+        c.val = new Integer(6);
+        implicits.cMap.put(c.val, c);
+        assertBothWays(implicits, expected);
+    }
+    
+    public void testAllImplicitTypesAtOnceWithExplicitElementNames()
+    {
+        xstream.alias("implicits", AllImplicitTypes.class);
+        xstream.addDefaultImplementation(OrderRetainingMap.class, Map.class);
+        xstream.addImplicitArray(AllImplicitTypes.class, "aArray", "a");
+        xstream.addImplicitCollection(AllImplicitTypes.class, "bList", "b", AllImplicitTypes.B.class);
+        xstream.addImplicitMap(AllImplicitTypes.class, "cMap", "c", AllImplicitTypes.C.class, "val");
+        String expected = ""
+            + "<implicits>\n"
+            + "  <a>\n"
+            + "    <val>1</val>\n"
+            + "  </a>\n"
+            + "  <a>\n"
+            + "    <val>2</val>\n"
+            + "  </a>\n"
+            + "  <separator1>--1--</separator1>\n"
+            + "  <b>\n"
+            + "    <val>3</val>\n"
+            + "  </b>\n"
+            + "  <b>\n"
+            + "    <val>4</val>\n"
+            + "  </b>\n"
+            + "  <separator2>--2--</separator2>\n"
+            + "  <c>\n"
+            + "    <val>5</val>\n"
+            + "  </c>\n"
+            + "  <c>\n"
+            + "    <val>6</val>\n"
+            + "  </c>\n"
+            + "</implicits>";
+
+        AllImplicitTypes implicits = new AllImplicitTypes();
+        implicits.aArray[0] = new AllImplicitTypes.A();
+        implicits.aArray[0].val = 1;
+        implicits.aArray[1] = new AllImplicitTypes.A();
+        implicits.aArray[1].val = 2;
+        implicits.bList.add(new AllImplicitTypes.B());
+        ((AllImplicitTypes.B)implicits.bList.get(0)).val = 3;
+        implicits.bList.add(new AllImplicitTypes.B());
+        ((AllImplicitTypes.B)implicits.bList.get(1)).val = 4;
+        AllImplicitTypes.C c = new AllImplicitTypes.C();
+        c.val = new Integer(5);
+        implicits.cMap.put(c.val, c);
+        c = new AllImplicitTypes.C();
+        c.val = new Integer(6);
+        implicits.cMap.put(c.val, c);
+        assertBothWays(implicits, expected);
+    }
 }

Modified: trunk/xstream-distribution/src/content/changes.html (2066 => 2067)


--- trunk/xstream-distribution/src/content/changes.html	2013-05-29 18:12:07 UTC (rev 2066)
+++ trunk/xstream-distribution/src/content/changes.html	2013-06-01 12:38:45 UTC (rev 2067)
@@ -67,6 +67,8 @@
     	<li>JIRA:XSTR-457: Do not write 'defined-in' attribute it not needed.</li>
     	<li>JettisonMappedXmlDriver provides better support to overwrite its create methods.</li>
     	<li>JIRA:XSTR-736: XStream.unmarshal may throw NPE is version info of manifest is missing.</li>
+    	<li>JIRA:XSTR-733: Implicit elements that match multiple defined implicit collections will be assigned to the
+    	map with the nearest matching element type.</li>
     </ul>
 
     <h2>API changes</h2>

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to