alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

// $Header: /home/cvs/jakarta-jmeter/src/htmlparser/org/htmlparser/tests/parserHelperTests/TagParserTest.java,v 1.2 2004/02/10 13:41:10 woolfel Exp $
/*
 * ====================================================================
 * Copyright 2002-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

// The developers of JMeter and Apache are greatful to the developers
// of HTMLParser for giving Apache Software Foundation a non-exclusive
// license. The performance benefits of HTMLParser are clear and the
// users of JMeter will benefit from the hard work the HTMLParser
// team. For detailed information about HTMLParser, the project is
// hosted on sourceforge at http://htmlparser.sourceforge.net/.
//
// HTMLParser was originally created by Somik Raha in 2000. Since then
// a healthy community of users has formed and helped refine the
// design so that it is able to tackle the difficult task of parsing
// dirty HTML. Derrick Oswald is the current lead developer and was kind
// enough to assist JMeter.

package org.htmlparser.tests.parserHelperTests;
import java.util.HashMap;
import java.util.Map;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.Tag;
import org.htmlparser.tests.ParserTestCase;
import org.htmlparser.util.ParserException;

public class TagParserTest extends ParserTestCase
{
    private static final String TEST_HTML =
        ""
            + ""
            + ""
            + "  "
            + "	"
            + "    SourceForge.net: Modify: 711073 - HTMLTagParser not threadsafe as a static variable in Tag"
            + "	"
            + "		"
            + ""
            + ""
            + ""
            + ""
            + ""
            + "";
    private Map results;
    private int testProgress;

    public TagParserTest(String name)
    {
        super(name);
    }

    public void testTagWithQuotes() throws Exception
    {
        String testHtml =
            "\"Marshall";

        createParser(testHtml);
        parseAndAssertNodeCount(1);
        assertType("should be Tag", Tag.class, node[0]);
        Tag tag = (Tag) node[0];
        assertStringEquals("alt", "Marshall Field's", tag.getAttribute("ALT"));
        assertStringEquals(
            "html",
            "\"Marshall",
            tag.toHtml());
    }

    public void testEmptyTag() throws Exception
    {
        createParser("");
        parseAndAssertNodeCount(1);
        assertType("should be Tag", Tag.class, node[0]);
        Tag tag = (Tag) node[0];
        assertStringEquals("tag name", "CUSTOM", tag.getTagName());
        assertTrue("empty tag", tag.isEmptyXmlTag());
        assertStringEquals("html", "", tag.toHtml());
    }

    public void testTagWithCloseTagSymbolInAttribute() throws ParserException
    {
        createParser("b\">");
        parseAndAssertNodeCount(1);
        assertType("should be Tag", Tag.class, node[0]);
        Tag tag = (Tag) node[0];
        assertStringEquals("attribute", "a>b", tag.getAttribute("att"));
    }

    public void testTagWithOpenTagSymbolInAttribute() throws ParserException
    {
        createParser("");
        parseAndAssertNodeCount(1);
        assertType("should be Tag", Tag.class, node[0]);
        Tag tag = (Tag) node[0];
        assertStringEquals("attribute", "a");
        parseAndAssertNodeCount(1);
        assertType("should be Tag", Tag.class, node[0]);
        Tag tag = (Tag) node[0];
        assertStringEquals("html", "", tag.toHtml());
        assertStringEquals("attribute", "a in multi-line attributes
     * submitted by Joe Robins (zorblak)
     */

    public void testMultiLine1() throws ParserException
    {
        createParser("\">");
        parseAndAssertNodeCount(1);
        assertType("should be Tag", Tag.class, node[0]);
        Tag tag = (Tag) node[0];
        String html = tag.toHtml();
        assertStringEquals(
            "html",
            "\" NAME=\"foo\">",
            html);
        String attribute1 = tag.getAttribute("NAME");
        assertStringEquals("attribute 1", "foo", attribute1);
        String attribute2 = tag.getAttribute("CONTENT");
        assertStringEquals("attribute 2", "foo", attribute2);
    }

    public void testMultiLine2() throws ParserException
    {
        createParser("");
        parseAndAssertNodeCount(1);
        assertType("should be Tag", Tag.class, node[0]);
        Tag tag = (Tag) node[0];
        String html = tag.toHtml();
        assertStringEquals(
            "html",
            "",
            html);
        String attribute1 = tag.getAttribute("NAME");
        assertStringEquals("attribute 1", "foo", attribute1);
        String attribute2 = tag.getAttribute("CONTENT");
        assertStringEquals("attribute 2", "foo\">");
        parseAndAssertNodeCount(1);
        assertType("should be Tag", Tag.class, node[0]);
        Tag tag = (Tag) node[0];
        String html = tag.toHtml();
        assertStringEquals(
            "html",
            "\" NAME=\"foo\">",
            html);
        String attribute1 = tag.getAttribute("NAME");
        assertStringEquals("attribute 1", "foo", attribute1);
        String attribute2 = tag.getAttribute("CONTENT");
        assertStringEquals("attribute 2", "foobar>", attribute2);
    }

    public void testMultiLine4() throws ParserException
    {
        createParser("\">");
        parseAndAssertNodeCount(1);
        assertType("should be Tag", Tag.class, node[0]);
        Tag tag = (Tag) node[0];
        String html = tag.toHtml();
        assertStringEquals(
            "html",
            "\" NAME=\"foo\">",
            html);
        String attribute1 = tag.getAttribute("NAME");
        assertStringEquals("attribute 1", "foo", attribute1);
        String attribute2 = tag.getAttribute("CONTENT");
        assertStringEquals("attribute 2", "foo\r\nbar>", attribute2);
    }

    /**
     * Test multiline tag like attribute.
     * See feature request #725749 Handle < and > in multi-line attributes.
     * Only perform this test if it's version 1.4 or higher.
     */
    public void testMultiLine5() throws ParserException
    {
        // 
        createParser("\nbar\">");
        if (1.4 <= Parser.getVersionNumber())
        {
            parseAndAssertNodeCount(1);
            assertType("should be Tag", Tag.class, node[0]);
            Tag tag = (Tag) node[0];
            String html = tag.toHtml();
            assertStringEquals(
                "html",
                "\r\nbar\" NAME=\"foo\">",
                html);
            String attribute1 = tag.getAttribute("NAME");
            assertStringEquals("attribute 1", "foo", attribute1);
            String attribute2 = tag.getAttribute("CONTENT");
            assertStringEquals("attribute 2", "\r\nbar", attribute2);
        }
    }

    /**
     * Test multiline broken tag like attribute.
     * See feature request #725749 Handle < and > in multi-line attributes.
     * Only perform this test if it's version 1.4 or higher.
     */
    public void testMultiLine6() throws ParserException
    {
        // 
        createParser("\nbar\">");
        if (1.4 <= Parser.getVersionNumber())
        {
            parseAndAssertNodeCount(1);
            assertType("should be Tag", Tag.class, node[0]);
            Tag tag = (Tag) node[0];
            String html = tag.toHtml();
            assertStringEquals(
                "html",
                "\r\nbar\" NAME=\"foo\">",
                html);
            String attribute1 = tag.getAttribute("NAME");
            assertStringEquals("attribute 1", "foo", attribute1);
            String attribute2 = tag.getAttribute("CONTENT");
            assertStringEquals("attribute 2", "foo>\r\nbar", attribute2);
        }
    }

    /**
     * Test multiline split tag like attribute.
     * See feature request #725749 Handle < and > in multi-line attributes.
     * Only perform this test if it's version 1.4 or higher.
     */
    public void testMultiLine7() throws ParserException
    {
        // 
        createParser("",
                html);
            String attribute1 = tag.getAttribute("NAME");
            assertStringEquals("attribute 1", "foo", attribute1);
            String attribute2 = tag.getAttribute("CONTENT");
            assertStringEquals("attribute 2", "");
        if (1.4 <= Parser.getVersionNumber())
        {
            String testHtml1 =
                "20020701&txt_clr=White&bg_clr=Red&url=http://localhost/Testing/Report1.html\">20020702 Report 1"
                    + TEST_HTML;

            String testHtml2 =
                "" + TEST_HTML;
            ParsingThread parsingThread[] = new ParsingThread[100];
            results = new HashMap();
            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(50);
                }
                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 parser;
        int id;
        LinkTag link1, link2;
        boolean result;
        int max;

        ParsingThread(int id, String testHtml, int max)
        {
            this.id = id;
            this.max = max;
            this.parser = Parser.createParser(testHtml);
            parser.registerScanners();
        }

        public void run()
        {
            try
            {
                result = false;
                Node linkTag[] = parser.extractAllNodesThatAre(LinkTag.class);
                link1 = (LinkTag) linkTag[0];
                link2 = (LinkTag) linkTag[1];
                if (id < max / 2)
                {
                    if (link1
                        .getLink()
                        .equals("/cgi-bin/view_search?query_text=postdate>20020701&txt_clr=White&bg_clr=Red&url=http://localhost/Testing/Report1.html")
                        && link2.getLink().equals(
                            "http://normallink.com/sometext.html"))
                        result = true;
                }
                else
                {
                    if (link1
                        .getLink()
                        .equals("http://normallink.com/sometext.html")
                        && link2.getLink().equals(
                            "http://normallink.com/sometext.html"))
                        result = true;
                }
            }
            catch (ParserException e)
            {
                System.err.println("Parser Exception");
                e.printStackTrace();
            }
            finally
            {
                testProgress += id;
            }
        }

        public LinkTag getLink1()
        {
            return link1;
        }

        public LinkTag getLink2()
        {
            return link2;
        }

        public boolean passed()
        {
            return result;
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.