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

📄 attributetests.java

📁 html to xml convertor
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        assertEquals ("Attribute has wrong value", "va", ((Attribute)(attributes.elementAt (2))).getValue ());        for (int i = 0; i < attributes.size (); i++)            assertTrue ("No attribute should be called va\"lue", !((Attribute)(attributes.elementAt (2))).getName ().equals ("va\"lue"));        assertStringEquals("Attribute missing","att",((Attribute)(attributes.elementAt (2))).getName ());        assertStringEquals("Attribute not parsed","lue\"",((Attribute)(attributes.elementAt (3))).getName ());        assertNull ("Attribute has wrong value", ((Attribute)(attributes.elementAt (3))).getValue ());        assertStringEquals("Empty attribute not parsed","other",((Attribute)(attributes.elementAt (5))).getName ());        assertEquals ("Attribute has wrong value", "fred", ((Attribute)(attributes.elementAt (5))).getValue ());    }    /**     * Test Rule 5.     * See discussion in Bug#891058 Bug in lexer. regarding alternate interpretations.     */    public void testRule5 ()    {        getParameterTableFor ("tag att='va'lue' other=fred");        assertStringEquals("Attribute not parsed","att",((Attribute)(attributes.elementAt (2))).getName ());        assertEquals ("Attribute has wrong value", "va", ((Attribute)(attributes.elementAt (2))).getValue ());        for (int i = 0; i < attributes.size (); i++)            assertTrue ("No attribute should be called va'lue", !((Attribute)(attributes.elementAt (2))).getName ().equals ("va'lue"));        assertStringEquals("Attribute not parsed","lue'",((Attribute)(attributes.elementAt (3))).getName ());        assertNull ("Attribute has wrong value", ((Attribute)(attributes.elementAt (3))).getValue ());        assertStringEquals("Empty attribute not parsed","other",((Attribute)(attributes.elementAt (5))).getName ());        assertEquals ("Attribute has wrong value", "fred", ((Attribute)(attributes.elementAt (5))).getValue ());    }        /**     * Test for lost attributes.     * see bug #778781 SRC-attribute suppression in IMG-tags     * & #753012 IMG SRC not parsed v1.3 & v1.4     * & #755929 Empty string attr. value causes attr parsing to be stopped     * & #778781 SRC-attribute suppression in IMG-tags     * & #832530 empty attribute causes parser to fail     * & #851882 zero length alt tag causes bug in ImageScanner     *     *    HTML before parse:     *    <img src="images/first" alt="first">"     *    <img src="images/second" alt="">     *    <img alt="third" src="images/third">     *    <img alt="" src="images/fourth">     *     *    HTML after parse:     *    <IMG ALT="first" SRC="images/first">     *    <IMG ALT="" SRC="images/second">     *    <IMG ALT="third" SRC="images/third">     *    <IMG ALT="">     */    public void testSrcAndAlt () throws ParserException    {        String html = "<img src=\"images/first\" alt=\"first\">";        createParser (html);        parseAndAssertNodeCount (1);        assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag);        ImageTag img = (ImageTag)node[0];        assertTrue ("bad source", "images/first".equals (img.getImageURL ()));        assertTrue ("bad alt", "first".equals (img.getAttribute ("alt")));        assertStringEquals ("toHtml()", html, img.toHtml ());    }    /**     * see bug #778781 SRC-attribute suppression in IMG-tags     */    public void testSrcAndEmptyAlt () throws ParserException    {        String html = "<img src=\"images/second\" alt=\"\">";        createParser (html);        parseAndAssertNodeCount (1);        assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag);        ImageTag img = (ImageTag)node[0];        assertTrue ("bad source", "images/second".equals (img.getImageURL ()));        assertTrue ("bad alt", "".equals (img.getAttribute ("alt")));        assertStringEquals ("toHtml()", html, img.toHtml ());    }    /**     * see bug #778781 SRC-attribute suppression in IMG-tags     */    public void testAltAndSrc () throws ParserException    {        String html = "<img alt=\"third\" src=\"images/third\">";        createParser (html);        parseAndAssertNodeCount (1);        assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag);        ImageTag img = (ImageTag)node[0];        assertTrue ("bad source", "images/third".equals (img.getImageURL ()));        assertTrue ("bad alt", "third".equals (img.getAttribute ("alt")));        assertStringEquals ("toHtml()", html, img.toHtml ());    }    /**     * see bug #778781 SRC-attribute suppression in IMG-tags     */    public void testEmptyAltAndSrc () throws ParserException    {        String html = "<img alt=\"\" src=\"images/third\">";        createParser (html);        parseAndAssertNodeCount (1);        assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag);        ImageTag img = (ImageTag)node[0];        assertTrue ("bad source", "images/third".equals (img.getImageURL ()));        assertTrue ("bad alt", "".equals (img.getAttribute ("alt")));        assertStringEquals ("toHtml()", html, img.toHtml ());    }    /**     * see bug #911565 isValued() and isNull() don't work     */    public void testPredicates () throws ParserException    {        String html1 = "<img alt=\"\" src=\"images/third\" readonly>";        String html2 = "<img src=\"images/third\" readonly alt=\"\">";        String html3 = "<img readonly alt=\"\" src=\"images/third\">";        String htmls[] = { html1, html2, html3 };        for (int i = 0; i < htmls.length; i++)        {            createParser (htmls[i]);            parseAndAssertNodeCount (1);            assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag);            ImageTag img = (ImageTag)node[0];            Attribute src = img.getAttributeEx ("src");            Attribute alt = img.getAttributeEx ("alt");            Attribute readonly = img.getAttributeEx ("readonly");            assertTrue ("src whitespace", !src.isWhitespace ());            assertTrue ("src not valued", src.isValued ());            assertTrue ("src empty", !src.isEmpty ());            assertTrue ("src standalone", !src.isStandAlone ());            assertTrue ("alt whitespace", !alt.isWhitespace ());            assertTrue ("alt valued", !alt.isValued ());            assertTrue ("alt empty", !alt.isEmpty ());            assertTrue ("alt standalone", !alt.isStandAlone ());            assertTrue ("readonly whitespace", !readonly.isWhitespace ());            assertTrue ("readonly valued", !readonly.isValued ());            assertTrue ("readonly empty", !readonly.isEmpty ());            assertTrue ("readonly not standalone", readonly.isStandAlone ());            // try assigning the name and checking again            src.setName ("SRC");            assertTrue ("setName() failed", "SRC=\"images/third\"".equals (src.toString ()));            assertTrue ("src whitespace", !src.isWhitespace ());            assertTrue ("src not valued", src.isValued ());            assertTrue ("src empty", !src.isEmpty ());            assertTrue ("src standalone", !src.isStandAlone ());            alt.setName ("ALT");            assertTrue ("setName() failed", "ALT=\"\"".equals (alt.toString ()));            assertTrue ("alt whitespace", !alt.isWhitespace ());            assertTrue ("alt valued", !alt.isValued ());            assertTrue ("alt empty", !alt.isEmpty ());            assertTrue ("alt standalone", !alt.isStandAlone ());            readonly.setName ("READONLY");            assertTrue ("setName() failed", "READONLY".equals (readonly.toString ()));            assertTrue ("readonly whitespace", !readonly.isWhitespace ());            assertTrue ("readonly valued", !readonly.isValued ());            assertTrue ("readonly empty", !readonly.isEmpty ());            assertTrue ("readonly not standalone", readonly.isStandAlone ());            // try assigning the assignment and checking again            src.setAssignment (" = ");            assertTrue ("setAssignment() failed", "SRC = \"images/third\"".equals (src.toString ()));            assertTrue ("src whitespace", !src.isWhitespace ());            assertTrue ("src not valued", src.isValued ());            assertTrue ("src empty", !src.isEmpty ());            assertTrue ("src standalone", !src.isStandAlone ());            alt.setAssignment (" = ");            assertTrue ("setAssignment() failed", "ALT = \"\"".equals (alt.toString ()));            assertTrue ("alt whitespace", !alt.isWhitespace ());            assertTrue ("alt valued", !alt.isValued ());            assertTrue ("alt empty", !alt.isEmpty ());            assertTrue ("alt standalone", !alt.isStandAlone ());            readonly.setAssignment ("=");            assertTrue ("setAssignment() failed", "READONLY=".equals (readonly.toString ()));            assertTrue ("readonly whitespace", !readonly.isWhitespace ());            assertTrue ("readonly valued", !readonly.isValued ());            assertTrue ("readonly not empty", readonly.isEmpty ());            assertTrue ("readonly standalone", !readonly.isStandAlone ());            // try assigning the value and checking again            createParser (htmls[i]);            parseAndAssertNodeCount (1);            assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag);            img = (ImageTag)node[0];            src = img.getAttributeEx ("src");            alt = img.getAttributeEx ("alt");            readonly = img.getAttributeEx ("readonly");            src.setValue ("cgi-bin/redirect");            assertTrue ("setValue() failed", "src=\"cgi-bin/redirect\"".equals (src.toString ()));            assertTrue ("src whitespace", !src.isWhitespace ());            assertTrue ("src not valued", src.isValued ());            assertTrue ("src empty", !src.isEmpty ());            assertTrue ("src standalone", !src.isStandAlone ());            alt.setValue ("no image");            assertTrue ("setValue() failed", "alt=\"no image\"".equals (alt.toString ()));            assertTrue ("alt whitespace", !alt.isWhitespace ());            assertTrue ("alt not valued", alt.isValued ());            assertTrue ("alt empty", !alt.isEmpty ());            assertTrue ("alt standalone", !alt.isStandAlone ());            readonly.setValue ("true"); // this may be bogus, really need to set assignment too, see below            assertTrue ("setValue() failed", "readonlytrue".equals (readonly.toString ()));            assertTrue ("readonly whitespace", !readonly.isWhitespace ());            assertTrue ("readonly not valued", readonly.isValued ());            assertTrue ("readonly empty", !readonly.isEmpty ());            assertTrue ("readonly standalone", !readonly.isStandAlone ());            readonly.setAssignment ("=");            assertTrue ("setAssignment() failed", "readonly=true".equals (readonly.toString ()));            assertTrue ("readonly whitespace", !readonly.isWhitespace ());            assertTrue ("readonly not valued", readonly.isValued ());            assertTrue ("readonly empty", !readonly.isEmpty ());            assertTrue ("readonly standalone", !readonly.isStandAlone ());        }    }    /**     * see bug #911565 isValued() and isNull() don't work     */    public void testSetQuote () throws ParserException    {        String html = "<img alt=\"\" src=\"images/third\" toast>";        createParser (html);        parseAndAssertNodeCount (1);        assertTrue ("Node should be an ImageTag", node[0] instanceof ImageTag);        ImageTag img = (ImageTag)node[0];        Attribute src = img.getAttributeEx ("src");        src.setQuote ('\0');        assertTrue ("setQuote('\\0') failed", "src=images/third".equals (src.toString ()));        src.setQuote ('\'');        assertTrue ("setQuote('\\'') failed", "src='images/third'".equals (src.toString ()));    }        /**     * see bug #979893 Not Parsing all Attributes     */    public void testNoSpace () throws ParserException    {        String id = "A19012_00002";        String rawid = "\"" + id + "\"";        String cls = "BuyLink";        String rawcls = "\"" + cls + "\"";        String href = "http://www.someplace.com/buyme.html";        String rawhref = "\"" + href + "\"";        String html = "<a id=" + rawid + /* no space */ "class=" + rawcls + " href=" + rawhref + ">Pick me.</a>";        createParser (html);        parseAndAssertNodeCount (1);        assertTrue ("Node should be an LinkTag", node[0] instanceof LinkTag);        LinkTag link = (LinkTag)node[0];        Vector attributes = link.getAttributesEx ();        assertEquals ("Incorrect number of attributes", 6, attributes.size ());        assertStringEquals ("id wrong", rawid, link.getAttributeEx ("id").getRawValue ());        assertStringEquals ("class wrong", rawcls, link.getAttributeEx ("class").getRawValue ());        assertStringEquals ("href wrong", rawhref, link.getAttributeEx ("href").getRawValue ());        assertStringEquals ("id wrong", id, link.getAttributeEx ("id").getValue ());        assertStringEquals ("class wrong", cls, link.getAttributeEx ("class").getValue ());        assertStringEquals ("href wrong", href, link.getAttributeEx ("href").getValue ());    }}

⌨️ 快捷键说明

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