cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmen...@apache.org
Subject svn commit: r506833 - in /cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src: cayenne/java/org/apache/cayenne/xml/ tests/java/org/apache/cayenne/xml/
Date Tue, 13 Feb 2007 03:18:52 GMT
Author: kmenard
Date: Mon Feb 12 19:18:51 2007
New Revision: 506833

URL: http://svn.apache.org/viewvc?view=rev&rev=506833
Log:
CAY-597: XML Encoder with Mapping outputs incorrect XML.
Fixed the encoding problem described and another discovered in the decoding process after
the encoder was fixed.  Tests added as well.

Modified:
    cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/cayenne/java/org/apache/cayenne/xml/SerializableEntity.java
    cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/cayenne/java/org/apache/cayenne/xml/XMLDecoder.java
    cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/cayenne/java/org/apache/cayenne/xml/XMLEncoder.java
    cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/tests/java/org/apache/cayenne/xml/TestObject.java
    cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/tests/java/org/apache/cayenne/xml/XMLDecoderTst.java
    cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/tests/java/org/apache/cayenne/xml/XMLEncoderTst.java

Modified: cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/cayenne/java/org/apache/cayenne/xml/SerializableEntity.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/cayenne/java/org/apache/cayenne/xml/SerializableEntity.java?view=diff&rev=506833&r1=506832&r2=506833
==============================================================================
--- cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/cayenne/java/org/apache/cayenne/xml/SerializableEntity.java
(original)
+++ cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/cayenne/java/org/apache/cayenne/xml/SerializableEntity.java
Mon Feb 12 19:18:51 2007
@@ -72,6 +72,9 @@
         }
         else {
             encodeObject(encoder, this.object, true);
+            
+            // Needed for fix to CAY-597.  This makes sure we get back to the appropriate
level in the DOM, rather than constantly re-rooting the tree.
+            encoder.pop();
         }
     }
 

Modified: cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/cayenne/java/org/apache/cayenne/xml/XMLDecoder.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/cayenne/java/org/apache/cayenne/xml/XMLDecoder.java?view=diff&rev=506833&r1=506832&r2=506833
==============================================================================
--- cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/cayenne/java/org/apache/cayenne/xml/XMLDecoder.java
(original)
+++ cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/cayenne/java/org/apache/cayenne/xml/XMLDecoder.java
Mon Feb 12 19:18:51 2007
@@ -226,9 +226,20 @@
             // If the object implements XMLSerializable, delegate decoding to the class's
             // implementation of decodeFromXML().
             if (XMLSerializable.class.isAssignableFrom(objectClass)) {
+                // Fix for decoding 1-to-1 relationships between the same class type, per
CAY-597.
+                // If we don't re-root the tree, the decoder goes into an infinite loop.
 In particular,
+                // if R1 -> R2, when it decodes R1, it will attempt to decode R2, but
without re-rooting,
+                // the decoder tries to decode R1 again, think it's decoding R2, because
R1 is the first
+                // element of that type found in the XML doc with the true root of the doc.
+                Element oldRoot = root;
+                root = child;
+                
                 XMLSerializable ret = (XMLSerializable) objectClass.newInstance();
                 ret.decodeFromXML(this);
 
+                // Restore the root when we're done decoding the child.
+                root = oldRoot;
+                
                 return ret;
             }
 

Modified: cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/cayenne/java/org/apache/cayenne/xml/XMLEncoder.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/cayenne/java/org/apache/cayenne/xml/XMLEncoder.java?view=diff&rev=506833&r1=506832&r2=506833
==============================================================================
--- cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/cayenne/java/org/apache/cayenne/xml/XMLEncoder.java
(original)
+++ cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/cayenne/java/org/apache/cayenne/xml/XMLEncoder.java
Mon Feb 12 19:18:51 2007
@@ -278,7 +278,7 @@
     /**
      * Pops the top element from the encoding stack.
      */
-    private Node pop() {
+    Node pop() {
         Node old = parent;
         parent = parent.getParentNode();
         return old;

Modified: cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/tests/java/org/apache/cayenne/xml/TestObject.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/tests/java/org/apache/cayenne/xml/TestObject.java?view=diff&rev=506833&r1=506832&r2=506833
==============================================================================
--- cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/tests/java/org/apache/cayenne/xml/TestObject.java
(original)
+++ cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/tests/java/org/apache/cayenne/xml/TestObject.java
Mon Feb 12 19:18:51 2007
@@ -35,6 +35,7 @@
     protected int age;
     protected boolean open;
     protected List children = new ArrayList();
+    protected TestObject parent = null;
 
     public TestObject() {
         this("", 0, false);
@@ -69,6 +70,14 @@
     public void setOpen(boolean open) {
         this.open = open;
     }
+    
+    public TestObject getParent() {
+        return parent;
+    }
+    
+    public void setParent(TestObject parent) {
+        this.parent = parent;
+    }
 
     public void setChildren(List children) {
         this.children = children;
@@ -96,12 +105,19 @@
         if (!Util.nullSafeEquals(name, test.getName())) {
             return false;
         }
+        
+        if (!Util.nullSafeEquals(parent, test.getParent())) {
+            return false;
+        }
 
         return ((test.getAge() == age) && (test.isOpen() == open));
     }
 
     public void encodeAsXML(XMLEncoder encoder) {
         encoder.setRoot("Test", this.getClass().getName());
+        
+        // "parent" must come first to fully test 1-to-1 relationships, per CAY-597.
+        encoder.encodeProperty("parent", parent);
         encoder.encodeProperty("name", name);
         encoder.encodeProperty("age", new Integer(age));
         encoder.encodeProperty("open", new Boolean(open));
@@ -109,7 +125,7 @@
     }
 
     public void decodeFromXML(XMLDecoder decoder) {
-
+        
         if (null != decoder.decodeInteger("age")) {
             age = decoder.decodeInteger("age").intValue();
         }
@@ -117,6 +133,11 @@
         if (null != decoder.decodeBoolean("open")) {
             open = decoder.decodeBoolean("open").booleanValue();
         }
+        
+        // "parent" must come first to fully test 1-to-1 relationships, per CAY-597.
+        if (null != decoder.decodeObject("parent")) {
+            parent = (TestObject) decoder.decodeObject("parent");
+        }
 
         name = decoder.decodeString("name");
         children = (List) decoder.decodeObject("children");
@@ -124,6 +145,7 @@
 
     public String toString() {
         return new ToStringBuilder(this)
+                .append("parent", parent)
                 .append("name", name)
                 .append("age", age)
                 .append("open", open)

Modified: cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/tests/java/org/apache/cayenne/xml/XMLDecoderTst.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/tests/java/org/apache/cayenne/xml/XMLDecoderTst.java?view=diff&rev=506833&r1=506832&r2=506833
==============================================================================
--- cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/tests/java/org/apache/cayenne/xml/XMLDecoderTst.java
(original)
+++ cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/tests/java/org/apache/cayenne/xml/XMLDecoderTst.java
Mon Feb 12 19:18:51 2007
@@ -82,6 +82,36 @@
         TestObject george = new TestObject("George", 57, false);
         assertEquals(decoded, george);
     }
+    
+    //  Added test for 1-to-1 relationship mappings, per CAY-597.
+    public void testDecodeMapping1To1() throws Exception {
+        Reader xml = new InputStreamReader(CayenneTestResources.getResource(XML_DATA_DIR
+                + "1to1-mapped.xml"));
+        Object object = decoder.decode(xml, CayenneTestResources.getResourceURL(
+                XML_DATA_DIR + "1to1-mapping.xml").toExternalForm());
+        
+        assertTrue(object instanceof TestObject);
+        TestObject decoded = (TestObject) object;
+
+        TestObject grandParent = new TestObject();
+        grandParent.setAge(117);
+        grandParent.setName("Sue");
+        grandParent.setOpen(false);
+        
+        TestObject parent = new TestObject();
+        parent.setAge(94);
+        parent.setName("Bill");
+        parent.setOpen(true);
+        parent.setParent(grandParent);
+        
+        TestObject child = new TestObject();
+        child.setAge(57);
+        child.setName("George");
+        child.setOpen(false);
+        child.setParent(parent);
+        
+        assertEquals(decoded, child);
+    }
 
     public void testDecodeMappingCollection() throws Exception {
         Reader xml = new InputStreamReader(CayenneTestResources.getResource(XML_DATA_DIR
@@ -240,7 +270,36 @@
         assertEquals(5, test.getAge());
         assertEquals(true, test.isOpen());
     }
+    
+    //  Added test for 1-to-1 relationships, per CAY-597.
+    public void testDecode1To1() throws Exception {
+        Reader xml = new InputStreamReader(CayenneTestResources.getResource(XML_DATA_DIR
+                + "1to1-encoded.xml"));
+        Object object = decoder.decode(xml);
+        
+        assertTrue(object instanceof TestObject);
+        TestObject decoded = (TestObject) object;
 
+        TestObject grandParent = new TestObject();
+        grandParent.setAge(117);
+        grandParent.setName("Sue");
+        grandParent.setOpen(false);
+        
+        TestObject parent = new TestObject();
+        parent.setAge(94);
+        parent.setName("Bill");
+        parent.setOpen(true);
+        parent.setParent(grandParent);
+        
+        TestObject child = new TestObject();
+        child.setAge(57);
+        child.setName("George");
+        child.setOpen(false);
+        child.setParent(parent);
+        
+        assertEquals(decoded, child);
+    }
+    
     public void testDecodeDataObjectsList() throws Exception {
         final List dataObjects = new ArrayList();
 

Modified: cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/tests/java/org/apache/cayenne/xml/XMLEncoderTst.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/tests/java/org/apache/cayenne/xml/XMLEncoderTst.java?view=diff&rev=506833&r1=506832&r2=506833
==============================================================================
--- cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/tests/java/org/apache/cayenne/xml/XMLEncoderTst.java
(original)
+++ cayenne/main/branches/STABLE-2.0/cayenne/cayenne-java/src/tests/java/org/apache/cayenne/xml/XMLEncoderTst.java
Mon Feb 12 19:18:51 2007
@@ -124,6 +124,61 @@
         
         assertEquals(comp.toString(), result);
     }
+    
+    // Added test for 1-to-1 relationship mappings, per CAY-597.
+    public void test1To1Mapping() throws Exception {
+        XMLEncoder encoder = new XMLEncoder(CayenneTestResources.getResourceURL(
+                XML_DATA_DIR + "1to1-mapping.xml").toExternalForm());
+        
+        TestObject grandParent = new TestObject();
+        grandParent.setAge(117);
+        grandParent.setName("Sue");
+        grandParent.setOpen(false);
+        
+        TestObject parent = new TestObject();
+        parent.setAge(94);
+        parent.setName("Bill");
+        parent.setOpen(true);
+        parent.setParent(grandParent);
+        
+        TestObject child = new TestObject();
+        child.setAge(57);
+        child.setName("George");
+        child.setOpen(false);
+        child.setParent(parent);
+        
+        String result = encoder.encode(child);
+
+        String comp = loadTestFileAsString("1to1-mapped.xml");
+        
+        assertEquals(comp.toString(), result);
+    }
+    
+    // Added test for 1-to-1 relationships, per CAY-597.
+    public void testEncode1To1() throws Exception {
+        TestObject grandParent = new TestObject();
+        grandParent.setAge(117);
+        grandParent.setName("Sue");
+        grandParent.setOpen(false);
+        
+        TestObject parent = new TestObject();
+        parent.setAge(94);
+        parent.setName("Bill");
+        parent.setOpen(true);
+        parent.setParent(grandParent);
+        
+        TestObject child = new TestObject();
+        child.setAge(57);
+        child.setName("George");
+        child.setOpen(false);
+        child.setParent(parent);
+        
+        String xml = new XMLEncoder().encode("Test", child);
+
+        String comp = loadTestFileAsString("1to1-encoded.xml");
+        
+        assertEquals(comp.toString(), xml);
+    }
 
     public void testCollectionMapping() throws Exception {
         XMLEncoder encoder = new XMLEncoder(CayenneTestResources.getResourceURL(



Mime
View raw message