harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mloe...@apache.org
Subject svn commit: r417665 - in /incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans: XMLEncoderTest.java auxiliary/TestEventHandler.java
Date Wed, 28 Jun 2006 04:37:37 GMT
Author: mloenko
Date: Tue Jun 27 21:37:36 2006
New Revision: 417665

URL: http://svn.apache.org/viewvc?rev=417665&view=rev
Log:
applied patch from HARMONY-680
[classlib][beans] implementation of XML parser for parsing sample xml files

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/auxiliary/TestEventHandler.java
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java?rev=417665&r1=417664&r2=417665&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java
Tue Jun 27 21:37:36 2006
@@ -22,21 +22,24 @@
 import java.beans.PersistenceDelegate;
 import java.beans.Statement;
 import java.beans.XMLEncoder;
-import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
 
 import junit.framework.TestCase;
+//import junit.framework.TestSuite;
 import org.apache.harmony.beans.tests.java.beans.EncoderTest.SampleBean;
 import org.apache.harmony.beans.tests.java.beans.mock.MockBean4Codec;
 
 import org.apache.harmony.beans.tests.java.beans.auxiliary.StandardBean;
 import org.apache.harmony.beans.tests.java.beans.auxiliary.AType;
+import org.apache.harmony.beans.tests.java.beans.auxiliary.TestEventHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLReaderFactory;
 
 /**
  * Tests for XMLEncoder
@@ -138,6 +141,14 @@
 		}
 	}
 
+    public XMLEncoderTest() {
+        super();
+    }
+    
+    public XMLEncoderTest(String s) {
+        super(s);
+    }
+    
 	public static String ident() {
 		Exception ex = new Exception();
 		int level = ex.getStackTrace().length;
@@ -180,15 +191,15 @@
 		// coverd by testWriteStatement
 	}
 
-	public void testWriteObject_Null() throws IOException {
+	public void testWriteObject_Null() throws Exception {
 		assertCodedXML(null, "/xml/null.xml");
 	}
 
-	public void testWriteObject_Integer() throws IOException {
+	public void testWriteObject_Integer() throws Exception {
 		assertCodedXML(new Integer(3), "/xml/int.xml");
 	}
 
-	public void testWriteObject_StringCodec() throws IOException {
+	public void testWriteObject_StringCodec() throws Exception {
 		SampleBean b = new SampleBean();
 		b.setMyid("<Li Yang> & \"liyang'");
 		SampleBean c = new SampleBean();
@@ -197,22 +208,22 @@
 		assertCodedXML(b, "/xml/SampleBean_StringCodec.xml");
 	}
 
-	public void testWriteObject_IntArray() throws IOException {
+	public void testWriteObject_IntArray() throws Exception {
 		assertCodedXML(new int[] { 1, 2, 3 }, "/xml/IntArray.xml");
 	}
 
-	public void testWriteObject_PropertyDependency() throws IOException {
+	public void testWriteObject_PropertyDependency() throws Exception {
 		DependencyBean b = new DependencyBean();
 		b.getInts()[0] = 888;
 		b.setRef(b.getInts());
 		assertCodedXML(b, "/xml/DependencyBean.xml");
 	}
 
-	public void testWriteObject_NoChange() throws IOException {
+	public void testWriteObject_NoChange() throws Exception {
 		assertCodedXML(new MockBean4Codec(), "/xml/MockBean4Codec_NoChange.xml");
 	}
 
-	public void testWriteObject_BornFriendChange() throws IOException {
+	public void testWriteObject_BornFriendChange() throws Exception {
 		MockBean4Codec b = new MockBean4Codec();
 		b.getBornFriend().getZarr()[0] = 888;
 		b.setNill(b.getBornFriend());
@@ -220,17 +231,17 @@
 		assertCodedXML(b, "/xml/MockBean4Codec_BornFriendChange.xml");
 	}
 
-	public void testWriteObject_ManyChanges() throws IOException {
+	public void testWriteObject_ManyChanges() throws Exception {
 		assertCodedXML(MockBean4Codec.getInstanceOfManyChanges(),
 				"/xml/MockBean4Codec_ManyChanges.xml");
 	}
 
-	public void testWriteObject_ManyChanges_2() throws IOException {
+	public void testWriteObject_ManyChanges_2() throws Exception {
 		assertCodedXML(MockBean4Codec.getInstanceOfManyChanges2(),
 				"/xml/MockBean4Codec_ManyChanges_2.xml");
 	}
 
-	public void testWriteObject_SetOwner() throws IOException {
+	public void testWriteObject_SetOwner() throws Exception {
 		ByteArrayOutputStream temp = new ByteArrayOutputStream();
 		XMLEncoder enc = new XMLEncoder(temp);
 
@@ -243,7 +254,7 @@
 
 	}
 
-	public void testWriteObject_SetOwnerWithWriteStatement() throws IOException {
+	public void testWriteObject_SetOwnerWithWriteStatement() throws Exception {
 		ByteArrayOutputStream temp = new ByteArrayOutputStream();
 		XMLEncoder enc = new XMLEncoder(temp);
 
@@ -259,7 +270,7 @@
 
 	}
 
-	public void testWriteObject_StaticField() throws IOException {
+	public void testWriteObject_StaticField() throws Exception {
 		ByteArrayOutputStream temp = new ByteArrayOutputStream();
 		XMLEncoder enc = new XMLEncoder(temp);
 
@@ -337,7 +348,7 @@
 		assertNull(enc.getOwner());
 	}
 
-	private void assertCodedXML(Object obj, String xmlFile) throws IOException {
+	private void assertCodedXML(Object obj, String xmlFile) throws Exception {
 		ByteArrayOutputStream temp = new ByteArrayOutputStream();
 		XMLEncoder enc = new XMLEncoder(temp);
 
@@ -345,7 +356,18 @@
 	}
 
 	private void assertCodedXML(Object obj, String xmlFile,
-			ByteArrayOutputStream temp, XMLEncoder enc) throws IOException {
+			ByteArrayOutputStream temp, XMLEncoder enc) throws Exception {
+
+        InputStream refIn;
+	    InputStreamReader xml;
+        InputStreamReader refXml;
+
+        XMLReader xmlReader;
+        XMLReader refXmlReader;
+        TestEventHandler handler = new TestEventHandler();
+        TestEventHandler refHandler = new TestEventHandler();
+        String saxParserClassName = System.getProperty("org.xml.sax.driver");
+        
 		if (enc == null || temp == null) {
 			temp = new ByteArrayOutputStream();
 			enc = new XMLEncoder(temp);
@@ -354,63 +376,30 @@
 		enc.close();
 		byte bytes[] = temp.toByteArray();
 
-		InputStream refIn = XMLEncoderTest.class.getResourceAsStream(xmlFile);
+		refIn = XMLEncoderTest.class.getResourceAsStream(xmlFile);
 		if (refIn == null) {
-			FileOutputStream file = new FileOutputStream(xmlFile);
-			file.write(bytes);
-			file.close();
 			throw new Error("resource " + xmlFile + " not exist in "
-					+ XMLEncoderTest.class.getPackage()
-					+ ", write in current dir!");
-		}
-		BufferedReader xml = new BufferedReader(new InputStreamReader(
-				new ByteArrayInputStream(bytes), "UTF-8"));
-		BufferedReader refXml = new BufferedReader(new InputStreamReader(refIn,
-				"UTF-8"));
-
-		String line = null, refLine = null;
-		int lineNum = 0;
-		while (true) {
-			lineNum++;
-			line = xml.readLine();
-			refLine = refXml.readLine();
-			if (line == null && refLine == null) {
-				break;
-			}
-			if (line == null) {
-				throw new RuntimeException("line " + lineNum
-						+ ", xml ends, but ref line is: " + refLine);
-			}
-			if (refLine == null) {
-				throw new RuntimeException("line " + lineNum
-						+ ", ref xml ends, but line is: " + line);
-			}
-			if (lineNum == 2) {
-				String trim = line.trim();
-				assertTrue(trim.startsWith("<java version=\""));
-				assertTrue(trim.endsWith("\" class=\"java.beans.XMLDecoder\">"));
-			} else {
-				String trim = line.trim();
-				String refTrim = refLine.trim();
-				if (trim.endsWith(" />") && refTrim.endsWith("/>")
-						&& trim.length() == refTrim.length() + 1) {
-					trim = trim.substring(0, trim.length() - 3);
-					refTrim = refTrim.substring(0, refTrim.length() - 2);
-				}
-				if (!trim.equals(refTrim)) {
-					System.out.println("---- Bad xml ----");
-					BufferedReader reader = new BufferedReader(
-							new InputStreamReader(new ByteArrayInputStream(
-									bytes), "UTF-8"));
-					String l = null;
-					while ((l = reader.readLine()) != null) {
-						System.out.println(l);
-					}
-					throw new RuntimeException("line " + lineNum
-							+ ", expected: " + refLine + ", but was: " + line);
-				}
-			}
+					+ XMLEncoderTest.class.getPackage());
 		}
+		xml = new InputStreamReader(new ByteArrayInputStream(bytes), "UTF-8");
+		refXml = new InputStreamReader(refIn, "UTF-8");
+
+        if (saxParserClassName == null) {
+            saxParserClassName = "org.apache.xerces.parsers.SAXParser";
+        }
+        
+        xmlReader = XMLReaderFactory.createXMLReader(saxParserClassName);
+        xmlReader.setContentHandler(handler);
+        xmlReader.setErrorHandler(handler);
+        xmlReader.parse(new InputSource(xml));
+        
+        refXmlReader = XMLReaderFactory.createXMLReader(saxParserClassName);
+        refXmlReader.setContentHandler(refHandler);
+        refXmlReader.setErrorHandler(refHandler);
+        refXmlReader.parse(new InputSource(refXml));
+
+        assertEquals("Generated XML differs from the sample,", refHandler.root,
+                handler.root);
 	}
 
     

Added: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/auxiliary/TestEventHandler.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/auxiliary/TestEventHandler.java?rev=417665&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/auxiliary/TestEventHandler.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/auxiliary/TestEventHandler.java
Tue Jun 27 21:37:36 2006
@@ -0,0 +1,227 @@
+/*
+ * Created on 27.06.2006
+ * @author Alexei Y. Zakharov
+ */
+package org.apache.harmony.beans.tests.java.beans.auxiliary;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.LinkedHashMap;
+
+import java.io.FileReader;
+
+import org.xml.sax.helpers.DefaultHandler;
+import org.xml.sax.XMLReader;
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.XMLReaderFactory;
+
+/**
+ * This class is used by XMLEncoderTest for handling SAX parser events. 
+ * @author Alexei Zakharov
+ */
+public class TestEventHandler extends DefaultHandler {
+   
+    public Tag root = null;
+    
+    private Tag currentTag = null;
+
+    
+    public void startDocument() {
+    }
+
+    public void endDocument() {
+    }
+
+    public void startElement(String uri, String name, String qName,
+            Attributes atts)
+    {
+        Tag theTag = new Tag(name, currentTag);
+
+        theTag.fillAttributes(atts);
+        if (currentTag != null) {
+            currentTag.innerTags.add(theTag);
+        }
+        if (root == null) {
+            root = theTag;
+        }
+        currentTag = theTag;
+    }
+
+
+    public void endElement(String uri, String name, String qName)
+            throws SAXException 
+    {
+        if (!name.equals(currentTag.name)) {
+            throw new SAXException("unexpected closing tag: " + name);
+        }
+        currentTag = currentTag.parent;
+    }
+
+
+    public void characters (char ch[], int start, int length) {
+        currentTag.content.append(ch, start, length);
+    }
+
+    public static void main(String argv[]) throws Exception {
+
+        XMLReader xmlReader;
+        TestEventHandler handler = new TestEventHandler();
+        FileReader reader;
+        String saxParserClassName = System.getProperty("org.xml.sax.driver");
+
+        if(saxParserClassName == null) {
+            saxParserClassName = "org.apache.xerces.parsers.SAXParser";
+        }
+        xmlReader = XMLReaderFactory.createXMLReader(saxParserClassName);
+        xmlReader.setContentHandler(handler);
+        xmlReader.setErrorHandler(handler);
+
+        if (argv.length < 1) {
+            throw new Exception("input file should be specified");
+        }
+        reader = new FileReader(argv[0]);
+        xmlReader.parse(new InputSource(reader));
+        System.out.println(handler.root.toString());
+    }
+
+    
+    static class Tag {
+        String name;
+        HashMap<String, String> attributes =
+                new LinkedHashMap<String, String>();
+        HashSet<Tag> innerTags = new LinkedHashSet<Tag>();
+        Tag parent = null;
+        StringBuffer content = new StringBuffer();
+        boolean ignoreJavaVersion = true;
+
+        public Tag(String name, Tag parent) {
+            this.name = name; 
+            this.parent = parent;
+        }
+
+        public void fillAttributes(Attributes attrs) {
+            for (int i = 0; i < attrs.getLength(); i++) {
+                String name = attrs.getLocalName(i);
+                String value = attrs.getValue(i);
+                
+                attributes.put(name, value);
+            }
+        }
+
+        public boolean equals(Object obj) {
+            Iterator<Map.Entry<String, String>> it;
+            Iterator<Tag> itTag;
+            Tag tag;
+            
+            if (!(obj instanceof Tag)) {
+                return false;
+            }
+            tag = (Tag) obj;
+            
+            // name
+            if (!name.equals(tag.name)) {
+                return false;
+            }
+            
+            // attributes
+            if (attributes.entrySet().size() !=
+                    tag.attributes.entrySet().size()) {
+                return false;
+            }
+            it = attributes.entrySet().iterator();
+         
+            while (it.hasNext()) {
+                Map.Entry<String, String> entry = it.next();
+                Iterator<Map.Entry<String, String>> it2 =
+                        tag.attributes.entrySet().iterator();
+                boolean found = false;
+                
+                while (it2.hasNext()) {
+                    Map.Entry<String, String> entry2 = it2.next();
+                    
+                    if (entry2.getKey().equals(entry.getKey())) {
+                        if (ignoreJavaVersion &&
+                            tag.name.equals("java") &&
+                            entry.getKey().equals("version"))
+                        {
+                            // ignore java version
+                            found = true;
+                            break;
+                        }
+                        else if (entry2.getValue().equals(entry.getValue()))
+                        {
+                            // values are the same
+                            found = true;
+                            break;
+                        }
+                    }
+                }
+                if (!found) {
+                    return false;
+                }
+            }
+        
+            // inner tags
+            if (innerTags.size() != tag.innerTags.size()) {
+                return false;
+            }
+            itTag = innerTags.iterator();
+            while (itTag.hasNext()) {
+                Tag innerTag = itTag.next();
+                Iterator<Tag> itTag2 = tag.innerTags.iterator();
+                boolean found = false;
+                
+                while (itTag2.hasNext()) {
+                    Tag innerTag2 = itTag2.next();
+                    
+                    if (innerTag.equals(innerTag2)) {
+                        found = true;
+                        break;
+                    }
+                }
+                if (!found) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        public String toString() {
+            StringBuffer sb = new StringBuffer();
+            Iterator<Map.Entry<String, String>> it;
+            
+            sb.append('<' + name);
+            it = attributes.entrySet().iterator();
+            while (it.hasNext()) {
+                Map.Entry<String, String> entry = it.next();
+                
+                sb.append(" " + entry.getKey() + "=\"" + entry.getValue() + 
+                        "\"");
+            }
+            if (innerTags.isEmpty() && content.length() == 0) {
+                sb.append("/>\n");
+            } else if (innerTags.isEmpty() && content.length() > 0) {
+                sb.append(">");
+                sb.append(content);
+                sb.append("</" + name + ">\n");
+            } else {
+                Iterator<Tag> it2 = innerTags.iterator();
+
+                sb.append(">\n");
+                while (it2.hasNext()) {
+                    Tag child = it2.next();
+
+                    sb.append(child.toString() + "\n");
+                }
+                sb.append("</" + name + ">\n");
+            }
+            return sb.toString();
+        }
+    }
+    
+}



Mime
View raw message