[gxml] TwDocument.save() bytes writting monitoring. Unit test for sub-nodes.



commit c320375f36c1701797bf27f25d20ca0aec540acd
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue May 5 16:26:34 2015 -0500

    TwDocument.save() bytes writting monitoring. Unit test for sub-nodes.
    
    * Implemented TwDocument.save() bites written monitoring to flush
      when 1500 bytes have been done.
    * Added test to create sub-nodes

 gxml/TwDocument.vala     |   15 ++++++++---
 test/TwDocumentTest.vala |   63 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+), 4 deletions(-)
---
diff --git a/gxml/TwDocument.vala b/gxml/TwDocument.vala
index bdead94..fe78a30 100644
--- a/gxml/TwDocument.vala
+++ b/gxml/TwDocument.vala
@@ -108,6 +108,7 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
   }
   public void start_node (Xml.TextWriter tw, GXml.Node node)
   {
+    int size = 0;
 #if DEBUG
     GLib.message (@"Starting Node: start Node: '$(node.name)'");
 #endif
@@ -136,14 +137,16 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
 #if DEBUG
     GLib.message ("Starting Element: write attribute with NS");
 #endif
-          tw.write_attribute_ns (attr.ns_prefix (), attr.name, attr.ns_uri (), attr.value);
+          size += tw.write_attribute_ns (attr.ns_prefix (), attr.name, attr.ns_uri (), attr.value);
         }
         else {
 #if DEBUG
     GLib.message ("Starting Element: write attribute no NS");
 #endif
-          tw.write_attribute (attr.name, attr.value);
+          size += tw.write_attribute (attr.name, attr.value);
         }
+        if (size > 1500)
+          tw.flush ();
       }
 #if DEBUG
     GLib.message (@"Starting Element: writting Node '$(node.name)' childs");
@@ -156,12 +159,16 @@ public class GXml.TwDocument : GXml.TwNode, GXml.Document
           start_node (tw, n);
           if (n.value != null)
             tw.write_string (n.value);
-          tw.end_element ();
+          size += tw.end_element ();
+          if (size > 1500)
+            tw.flush ();
         }
       }
     }
     if (node is GXml.Comment) {
-      tw.write_comment (node.value);
+      size += tw.write_comment (node.value);
+      if (size > 1500)
+        tw.flush ();
     }
   }
 }
diff --git a/test/TwDocumentTest.vala b/test/TwDocumentTest.vala
index 0c83e05..463cfed 100644
--- a/test/TwDocumentTest.vala
+++ b/test/TwDocumentTest.vala
@@ -173,5 +173,68 @@ class TwDocumentTest : GXmlTest {
                                        assert_not_reached ();
                                }
                        });
+               Test.add_func ("/gxml/tw-document/root/childs-childs", () => {
+#if DEBUG
+                               GLib.message (@"TwDocument root childs/childs...");
+#endif
+                       try {
+#if DEBUG
+                               GLib.message (@"Checking file to save to...");
+#endif
+                               var f = GLib.File.new_for_path (GXmlTestConfig.TEST_SAVE_DIR+"/tw-large.xml");
+                               if (f.query_exists ()) f.delete ();
+#if DEBUG
+                               GLib.message (@"Creating Document...");
+#endif
+                               var d = new TwDocument (GXmlTestConfig.TEST_SAVE_DIR+"/tw-large.xml");
+                               var e = d.create_element ("bookstore");
+                               d.childs.add (e);
+                               assert (d.childs.size == 1);
+                               assert (d.root != null);
+                               assert (d.root.name == "bookstore");
+                               assert (d.root.value == null);
+                               var r = (GXml.Element) d.root;
+                               r.set_attr ("name","The Great Book");
+#if DEBUG
+                               GLib.message (@"Creating chidls...");
+#endif
+                               for (int i = 0; i < 5000; i++){
+                                       var b = (GXml.Element) d.create_element ("book");
+                                       r.childs.add (b);
+                                       var aths = (GXml.Element) d.create_element ("Authors");
+                                       b.childs.add (aths);
+                                       var ath1 = (GXml.Element) d.create_element ("Author");
+                                       aths.childs.add (ath1);
+                                       var name1 = (GXml.Element) d.create_element ("Name");
+                                       name1.content = "Fred";
+                                       ath1.childs.add (name1);
+                                       var email1 = (GXml.Element) d.create_element ("Email");
+                                       email1.content = "fweasley hogwarts co uk";
+                                       ath1.childs.add (email1);
+                                       var ath2 = (GXml.Element) d.create_element ("Author");
+                                       aths.childs.add (ath2);
+                                       var name2 = (GXml.Element) d.create_element ("Name");
+                                       name2.content = "Greoge";
+                                       ath2.childs.add (name2);
+                                       var email2 = (GXml.Element) d.create_element ("Email");
+                                       email2.content = "gweasley hogwarts co uk";
+                                       ath2.childs.add (email2);
+                               }
+                               assert (d.root.childs.size == 5000);
+                               foreach (GXml.Node n in d.root.childs) {
+                                       assert (n.childs.size == 1);
+                                       foreach (GXml.Node cn in n.childs) {
+                                               assert (cn.childs.size == 2);
+                                               foreach (GXml.Node ccn in cn.childs) {
+                                                       assert (ccn.childs.size == 2);
+                                               }
+                                       }
+                               }
+                       }
+                       catch (GLib.Error e) {
+                               GLib.message (@"ERROR: $(e.message)");
+                               assert_not_reached ();
+                       }
+               });
        }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]