From 28b429ad56d2c595141e66a69c66e83f6739f143 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Tue, 17 May 2011 11:21:31 -0700 Subject: [PATCH] Fix some failing XML tests. Change-Id: I7504e849b9fe67900248a2e223d189c8fcef224d --- expectations/icebox.txt | 4 +- expectations/knownfailures.txt | 5 + .../java/libcore/xml/ExpatSaxParserTest.java | 104 ++++-------------- 3 files changed, 31 insertions(+), 82 deletions(-) diff --git a/expectations/icebox.txt b/expectations/icebox.txt index 4d664a6e1..d85efc1b2 100644 --- a/expectations/icebox.txt +++ b/expectations/icebox.txt @@ -42,13 +42,13 @@ description: "Dalvik doesn't support XML Schemas, DTDs or validation", bug: 3268630, name: "libcore.xml.DeclarationTest#testGetXmlEncoding", - substring: "This implementation doesn't parse the encoding from the XML declaration expected: but was:" + substring: "This implementation doesn't parse the encoding from the XML declaration" }, { description: "Dalvik doesn't support XML Schemas, DTDs or validation", bug: 3268630, name: "libcore.xml.DeclarationTest#testGetXmlStandalone", - substring: "This implementation doesn't parse standalone from the XML declaration expected: but was:" + substring: "This implementation doesn't parse standalone from the XML declaration" }, { description: "Dalvik doesn't support XML Schemas, DTDs or validation", diff --git a/expectations/knownfailures.txt b/expectations/knownfailures.txt index 3d2eb5f02..f7843947e 100644 --- a/expectations/knownfailures.txt +++ b/expectations/knownfailures.txt @@ -10,6 +10,11 @@ ], bug: 3483365 }, +{ + description: "Expat uses an unbounded number of global references", + name: "libcore.xml.ExpatSaxParserTest#testGlobalReferenceTableOverflow", + bug: 2772628 +}, { description: "Test fails, Intermediate certificate lacks BasicConstraints", name: "com.android.org.bouncycastle.jce.provider.PKIXCertPathValidatorSpiTest#testTrustAndRemoteCertificatesWithDifferentEncodings", diff --git a/luni/src/test/java/libcore/xml/ExpatSaxParserTest.java b/luni/src/test/java/libcore/xml/ExpatSaxParserTest.java index 2db8e82e8..84b794258 100644 --- a/luni/src/test/java/libcore/xml/ExpatSaxParserTest.java +++ b/luni/src/test/java/libcore/xml/ExpatSaxParserTest.java @@ -16,6 +16,16 @@ package libcore.xml; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import junit.framework.Assert; import junit.framework.TestCase; import org.apache.harmony.xml.ExpatReader; @@ -27,20 +37,8 @@ import org.xml.sax.XMLReader; import org.xml.sax.ext.DefaultHandler2; import org.xml.sax.helpers.DefaultHandler; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Reader; -import java.io.StringReader; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.net.ServerSocket; -import java.net.Socket; +import tests.http.MockResponse; +import tests.http.MockWebServer; public class ExpatSaxParserTest extends TestCase { @@ -586,98 +584,44 @@ public void characters(char ch[], int start, int length) } public void testExternalEntityDownload() throws IOException, SAXException { - class Server implements Runnable { - - private final ServerSocket serverSocket; - - Server() throws IOException { - serverSocket = new ServerSocket(8080); - } - - public void run() { - try { - Socket socket = serverSocket.accept(); - - final InputStream in = socket.getInputStream(); - Thread inputThread = new Thread() { - public void run() { - try { - byte[] buffer = new byte[1024]; - while (in.read(buffer) > -1) { /* ignore */ } - } catch (IOException e) { - e.printStackTrace(); - } - } - }; - inputThread.setDaemon(true); - inputThread.start(); - - OutputStream out = socket.getOutputStream(); - - String body = ""; - String response = "HTTP/1.0 200 OK\n" - + "Content-Length: " + body.length() + "\n" - + "\n" - + body; - - out.write(response.getBytes("UTF-8")); - out.close(); - serverSocket.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } + final MockWebServer server = new MockWebServer(); + server.enqueue(new MockResponse().setBody("")); + server.play(); class Handler extends DefaultHandler { + final List elementNames = new ArrayList(); - List elementNames = new ArrayList(); - - public InputSource resolveEntity(String publicId, String systemId) - throws IOException, SAXException { + @Override public InputSource resolveEntity(String publicId, String systemId) + throws IOException { // The parser should have resolved the systemId. - assertEquals("http://localhost:8080/systemBar", systemId); + assertEquals(server.getUrl("/systemBar").toString(), systemId); return new InputSource(systemId); } - @Override - public void startElement(String uri, String localName, String qName, - Attributes attributes) throws SAXException { + @Override public void startElement(String uri, String localName, String qName, + Attributes attributes) { elementNames.add(localName); } - @Override - public void endElement(String uri, String localName, String qName) - throws SAXException { + @Override public void endElement(String uri, String localName, String qName) { elementNames.add("/" + localName); } } - // Start server to serve up the XML for 'systemBar'. - Thread serverThread = new Thread(new Server()); - serverThread.setDaemon(true); - serverThread.start(); - // 'systemBar', the external entity, is relative to 'systemFoo': Reader in = new StringReader("\n" + "\n" + "]>\n" + "&bar;"); - ExpatReader reader = new ExpatReader(); - Handler handler = new Handler(); - reader.setContentHandler(handler); reader.setEntityResolver(handler); - InputSource source = new InputSource(in); - source.setSystemId("http://localhost:8080/systemFoo"); + source.setSystemId(server.getUrl("/systemFoo").toString()); reader.parse(source); - - assertEquals(Arrays.asList("foo", "bar", "/bar", "/foo"), - handler.elementNames); + assertEquals(Arrays.asList("foo", "bar", "/bar", "/foo"), handler.elementNames); } /**