harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zhouke...@apache.org
Subject svn commit: r931467 - in /harmony/enhanced/java/trunk/classlib/modules/beans/src: main/java/java/beans/XMLEncoder.java test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java test/resources/xml/ChangedObject.xml
Date Wed, 07 Apr 2010 08:47:39 GMT
Author: zhoukevin
Date: Wed Apr  7 08:47:39 2010
New Revision: 931467

URL: http://svn.apache.org/viewvc?rev=931467&view=rev
Log:
Fix another problem of java.beans.XMLEncoder. Regard the test case, previous HY outputs like:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.6.0" class="java.beans.XMLDecoder">
 <object class="org.apache.harmony.beans.tests.java.beans.XMLEncoderTest$Sample">
  <void property="name">
   <string>oldName</string>
  </void>
  <void property="name">
   <string>newName</string>
  </void>
 </object>
</java>

HY puts an additional property "name", and it doesn't encode the second object.

Added:
    harmony/enhanced/java/trunk/classlib/modules/beans/src/test/resources/xml/ChangedObject.xml
Modified:
    harmony/enhanced/java/trunk/classlib/modules/beans/src/main/java/java/beans/XMLEncoder.java
    harmony/enhanced/java/trunk/classlib/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java

Modified: harmony/enhanced/java/trunk/classlib/modules/beans/src/main/java/java/beans/XMLEncoder.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/beans/src/main/java/java/beans/XMLEncoder.java?rev=931467&r1=931466&r2=931467&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/beans/src/main/java/java/beans/XMLEncoder.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/beans/src/main/java/java/beans/XMLEncoder.java
Wed Apr  7 08:47:39 2010
@@ -920,27 +920,28 @@ public class XMLEncoder extends Encoder 
     @Override
     public void writeObject(Object o) {
         synchronized (this) {
-            boolean oldWritingObject = writingObject;
-            writingObject = true;
-            try {
-                super.writeObject(o);
-            } finally {
-                writingObject = oldWritingObject;
+            ArrayList<Object> prePending = (ArrayList<Object>) cache.get(o);
+            if (prePending == null) {
+                boolean oldWritingObject = writingObject;
+                writingObject = true;
+                try {
+                    super.writeObject(o);
+                } finally {
+                    writingObject = oldWritingObject;
+                }
+            } else {
+                flushPrePending.clear();
+                flushPrePending.addAll(prePending);
             }
 
-            // root object?
+            // root object
             if (!writingObject) {
-                boolean isCached;
-                ArrayList<Object> pending = (ArrayList<Object>) cache.get(o);
-                if (isCached = (pending != null)) {
-                    flushPrePending.clear();
-                    flushPrePending.addAll(pending);
-                } else {
-                    if (o != null) {
-                        pending = new ArrayList<Object>();
-                        pending.addAll(flushPrePending);
-                        cache.put(o, pending);
-                    }
+                boolean isNotCached = prePending == null;
+                // is not cached, add to cache
+                if (isNotCached && o != null) {
+                    prePending = new ArrayList<Object>();
+                    prePending.addAll(flushPrePending);
+                    cache.put(o, prePending);
                 }
 
                 // add to pending
@@ -948,7 +949,7 @@ public class XMLEncoder extends Encoder 
                 flushPendingStat.addAll(flushPrePending);
                 flushPrePending.clear();
 
-                if (!isCached && flushPending.contains(o)) {
+                if (isNotCached && flushPending.contains(o)) {
                     flushPendingStat.remove(o);
                 } else {
                     flushPending.add(o);

Modified: harmony/enhanced/java/trunk/classlib/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java?rev=931467&r1=931466&r2=931467&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java
Wed Apr  7 08:47:39 2010
@@ -871,4 +871,36 @@ public class XMLEncoderTest extends Test
             return exp;
         }
     }
+
+    public void testWriteObject_ChangedObject() throws Exception {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        XMLEncoder xmlEncoder = new XMLEncoder(bos);
+        Sample sample = new Sample("oldName");
+        xmlEncoder.writeObject(sample);
+        sample.setName("newName");
+        xmlEncoder.writeObject(sample);
+        xmlEncoder.close();
+        assertXMLContent(null, bos.toByteArray(), "/xml/ChangedObject.xml");
+    }
+
+    public static class Sample {
+
+        String name;
+
+        public Sample() {
+            name = null;
+        }
+
+        public Sample(String n) {
+            name = n;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+    }
 }
\ No newline at end of file

Added: harmony/enhanced/java/trunk/classlib/modules/beans/src/test/resources/xml/ChangedObject.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/beans/src/test/resources/xml/ChangedObject.xml?rev=931467&view=auto
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/beans/src/test/resources/xml/ChangedObject.xml
(added)
+++ harmony/enhanced/java/trunk/classlib/modules/beans/src/test/resources/xml/ChangedObject.xml
Wed Apr  7 08:47:39 2010
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?> 
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<java version="${version}" class="java.beans.XMLDecoder"> 
+ <object id="XMLEncoderTest$Sample0" class="org.apache.harmony.beans.tests.java.beans.XMLEncoderTest$Sample">

+  <void property="name"> 
+   <string>oldName</string> 
+  </void> 
+ </object> 
+ <object idref="XMLEncoderTest$Sample0"/> 
+</java> 
\ No newline at end of file



Mime
View raw message