FileDocCategorySizeDatePackage
SafeSaxTest.javaAPI DocAndroid 5.1 API19428Thu Mar 12 22:22:42 GMT 2015android.sax

SafeSaxTest

public class SafeSaxTest extends android.test.AndroidTestCase

Fields Summary
private static final String
TAG
private static final String
ATOM_NAMESPACE
private static final String
MEDIA_NAMESPACE
private static final String
YOUTUBE_NAMESPACE
private static final String
GDATA_NAMESPACE
Constructors Summary
Methods Summary
private static org.xml.sax.ContentHandlernewContentHandler(android.sax.SafeSaxTest$VideoAdapter videoAdapter)

        return new HandlerFactory().newContentHandler(videoAdapter);
    
private static voidpureSaxTest(java.io.InputStream inputStream)

        long start = System.currentTimeMillis();
        VideoAdapter videoAdapter = new VideoAdapter();
        Xml.parse(inputStream, Xml.Encoding.UTF_8, new YouTubeContentHandler(videoAdapter));
        long elapsed = System.currentTimeMillis() - start;
        Log.i(TAG, "pure SAX: " + elapsed + "ms");
    
private static voidsaxyModelTest(java.io.InputStream inputStream)

        long start = System.currentTimeMillis();
        VideoAdapter videoAdapter = new VideoAdapter();
        Xml.parse(inputStream, Xml.Encoding.UTF_8, newContentHandler(videoAdapter));
        long elapsed = System.currentTimeMillis() - start;
        Log.i(TAG, "Saxy Model: " + elapsed + "ms");
    
private static voidsaxyModelTest(java.io.InputStream inputStream, org.xml.sax.ContentHandler contentHandler)

        long start = System.currentTimeMillis();
        Xml.parse(inputStream, Xml.Encoding.UTF_8, contentHandler);
        long elapsed = System.currentTimeMillis() - start;
        Log.i(TAG, "Saxy Model (preloaded): " + elapsed + "ms");
    
public voidtestListener()

        String xml = "<feed xmlns='http://www.w3.org/2005/Atom'>\n"
                + "<entry>\n"
                + "<id>a</id>\n"
                + "</entry>\n"
                + "<entry>\n"
                + "<id>b</id>\n"
                + "</entry>\n"
                + "</feed>\n";

        RootElement root = new RootElement(ATOM_NAMESPACE, "feed");
        Element entry = root.requireChild(ATOM_NAMESPACE, "entry");
        Element id = entry.requireChild(ATOM_NAMESPACE, "id");

        ElementCounter rootCounter = new ElementCounter();
        ElementCounter entryCounter = new ElementCounter();
        TextElementCounter idCounter = new TextElementCounter();

        root.setElementListener(rootCounter);
        entry.setElementListener(entryCounter);
        id.setTextElementListener(idCounter);

        Xml.parse(xml, root.getContentHandler());

        assertEquals(1, rootCounter.starts);
        assertEquals(1, rootCounter.ends);
        assertEquals(2, entryCounter.starts);
        assertEquals(2, entryCounter.ends);
        assertEquals(2, idCounter.starts);
        assertEquals("ab", idCounter.bodies);
    
public voidtestMissingRequiredChild()

        String xml = "<feed></feed>";
        RootElement root = new RootElement("feed");
        root.requireChild("entry");

        try {
            Xml.parse(xml, root.getContentHandler());
            fail("expected exception not thrown");
        } catch (SAXException e) {
            // Expected.
        }
    
public voidtestMixedContent()

        String xml = "<feed><entry></entry></feed>";

        RootElement root = new RootElement("feed");
        root.setEndTextElementListener(new EndTextElementListener() {
            public void end(String body) {
            }
        });

        try {
            Xml.parse(xml, root.getContentHandler());
            fail("expected exception not thrown");
        } catch (SAXException e) {
            // Expected.
        }
    
public voidtestPerformance()

        InputStream in = mContext.getResources().openRawResource(R.raw.youtube);
        byte[] xmlBytes;
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) != -1) {
                out.write(buffer, 0, length);
            }
            xmlBytes = out.toByteArray();
        } finally {
            in.close();
        }

        Log.i("***", "File size: " + (xmlBytes.length / 1024) + "k");

        VideoAdapter videoAdapter = new VideoAdapter();
        ContentHandler handler = newContentHandler(videoAdapter);
        for (int i = 0; i < 2; i++) {
            pureSaxTest(new ByteArrayInputStream(xmlBytes));
            saxyModelTest(new ByteArrayInputStream(xmlBytes));
            saxyModelTest(new ByteArrayInputStream(xmlBytes), handler);
        }