⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tagtests.java

📁 html to xml convertor
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        // <meta name="foo" content="<foo>        // bar">        String html = "<meta name=\"foo\" content=\"<foo>\nbar\">";        createParser(html);        parseAndAssertNodeCount (1);        assertType ("should be MetaTag", MetaTag.class, node[0]);        Tag tag = (Tag)node[0];        assertStringEquals ("html",html, tag.toHtml ());        String attribute1 = tag.getAttribute ("NAME");        assertStringEquals ("attribute 1","foo", attribute1);        String attribute2 = tag.getAttribute ("CONTENT");        assertStringEquals ("attribute 2","<foo>\nbar", attribute2);    }    /**     * Test multiline broken tag like attribute.     * See feature request #725749 Handle < and > in multi-line attributes.     */    public void testMultiLine6 () throws ParserException    {        // <meta name="foo" content="foo>        // bar">        String html = "<meta name=\"foo\" content=\"foo>\nbar\">";        createParser(html);        parseAndAssertNodeCount (1);        assertType ("should be MetaTag", MetaTag.class, node[0]);        Tag tag = (Tag)node[0];        assertStringEquals ("html",html, tag.toHtml ());        String attribute1 = tag.getAttribute ("NAME");        assertStringEquals ("attribute 1","foo", attribute1);        String attribute2 = tag.getAttribute ("CONTENT");        assertStringEquals ("attribute 2","foo>\nbar", attribute2);    }    /**     * Test multiline split tag like attribute.     * See feature request #725749 Handle < and > in multi-line attributes.     */    public void testMultiLine7 () throws ParserException    {        // <meta name="foo" content="<foo        // bar">        String html = "<meta name=\"foo\" content=\"<foo\nbar\"";        createParser(html);        parseAndAssertNodeCount (1);        assertType ("should be MetaTag", MetaTag.class, node[0]);        Tag tag = (Tag)node[0];        assertStringEquals ("html",html + ">", tag.toHtml ());        String attribute1 = tag.getAttribute ("NAME");        assertStringEquals ("attribute 1","foo", attribute1);        String attribute2 = tag.getAttribute ("CONTENT");        assertStringEquals ("attribute 2","<foo\nbar", attribute2);    }    /**     * End of multi line test cases.     */    /**     * Test multiple threads running against the parser.     * See feature request #736144 Handle multi-threaded operation.     */    public void testThreadSafety() throws Exception    {        createParser("<html></html>");        parser.setNodeFactory (new PrototypicalNodeFactory (true));        String testHtml1 = "<a HREF=\"/cgi-bin/view_search?query_text=postdate>20020701&txt_clr=White&bg_clr=Red&url=http://localhost/Testing/Report1.html\">20020702 Report 1</A>" +                            TEST_HTML;        String testHtml2 = "<a href=\"http://normallink.com/sometext.html\">" +                            TEST_HTML;        ParsingThread parsingThread [] =            new ParsingThread[100];        testProgress = 0;        for (int i=0;i<parsingThread.length;i++) {            if (i<parsingThread.length/2)                parsingThread[i] =                    new ParsingThread(i,testHtml1,parsingThread.length);                else                    parsingThread[i] =                        new ParsingThread(i,testHtml2,parsingThread.length);            Thread thread = new Thread(parsingThread[i]);            thread.start();        }        int completionValue = computeCompletionValue(parsingThread.length);        do {            try {                Thread.sleep(500);            }            catch (InterruptedException e) {            }        }        while (testProgress!=completionValue);        for (int i=0;i<parsingThread.length;i++)        {            if (!parsingThread[i].passed())            {                assertNotNull("Thread "+i+" link 1",parsingThread[i].getLink1());                assertNotNull("Thread "+i+" link 2",parsingThread[i].getLink2());                if (i<parsingThread.length/2) {                    assertStringEquals(                        "Thread "+i+", link 1:",                        "/cgi-bin/view_search?query_text=postdate>20020701&txt_clr=White&bg_clr=Red&url=http://localhost/Testing/Report1.html",                        parsingThread[i].getLink1().getLink()                    );                    assertStringEquals(                        "Thread "+i+", link 2:",                        "http://normallink.com/sometext.html",                        parsingThread[i].getLink2().getLink()                    );                } else {                    assertStringEquals(                        "Thread "+i+", link 1:",                        "http://normallink.com/sometext.html",                        parsingThread[i].getLink1().getLink()                    );                    assertNotNull("Thread "+i+" link 2",parsingThread[i].getLink2());                    assertStringEquals(                        "Thread "+i+", link 2:",                        "/cgi-bin/view_search?query_text=postdate>20020701&txt_clr=White&bg_clr=Red&url=http://localhost/Testing/Report1.html",                        parsingThread[i].getLink2().getLink()                    );                }            }        }    }    private int computeCompletionValue(int numThreads) {        return numThreads * (numThreads - 1) / 2;    }    class ParsingThread implements Runnable {        Parser mParser;        int mId;        LinkTag mLink1;        LinkTag mLink2;        boolean mResult;        int mMax;        ParsingThread(int id, String testHtml, int max) {            mId = id;            mMax = max;            mParser = Parser.createParser(testHtml, null);        }        public void run() {            try {                mResult = false;                Node linkTag [] = mParser.extractAllNodesThatMatch (new NodeClassFilter (LinkTag.class)).toNodeArray ();                mLink1 = (LinkTag)linkTag[0];                mLink2 = (LinkTag)linkTag[1];                if (mId < mMax / 2) {                    if (mLink1.getLink().equals("/cgi-bin/view_search?query_text=postdate>20020701&txt_clr=White&bg_clr=Red&url=http://localhost/Testing/Report1.html") &&                        mLink2.getLink().equals("http://normallink.com/sometext.html"))                        mResult = true;                } else {                    if (mLink1.getLink().equals("http://normallink.com/sometext.html") &&                        mLink2.getLink().equals("http://normallink.com/sometext.html"))                        mResult = true;                }            }            catch (ParserException e) {                System.err.println("Parser Exception");                e.printStackTrace();            }            finally {                testProgress += mId;            }        }        public LinkTag getLink1() {            return (mLink1);        }        public LinkTag getLink2() {            return (mLink2);        }        public boolean passed() {            return (mResult);        }    }    /**     * Test the toHTML method for a standalone attribute.     */    public void testStandAloneToHTML () throws ParserException    {        String html = "<input disabled>";        createParser(html);        parser.setNodeFactory (new PrototypicalNodeFactory (true));        parseAndAssertNodeCount (1);        assertType ("should be Tag", Tag.class, node[0]);        Tag tag = (Tag)node[0];        assertStringEquals ("html", html, tag.toHtml ());    }    /**     * Test the toHTML method for a missing value attribute.     */    public void testMissingValueToHTML () throws ParserException    {        String html = "<input disabled=>";        createParser(html);        parser.setNodeFactory (new PrototypicalNodeFactory (true));        parseAndAssertNodeCount (1);        assertType ("should be Tag", Tag.class, node[0]);        Tag tag = (Tag)node[0];        assertStringEquals ("html", html, tag.toHtml ());    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -