View Javadoc

1   // Copyright (C) 2004, 2005 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.util.html;
23  
24  import junit.framework.TestCase;
25  
26  
27  /**
28   *  Unit tests for {@link HTMLElement}, {@link HTMLText}.
29   *
30   * @author Philip Aston
31   */
32  public class TestHTML extends TestCase {
33  
34    private static final String[] m_strings = {
35      "The difference between me and you",
36      "is that I'm not on fire.",
37      "",
38      "  \n \t",
39    };
40  
41    public void testHTMLText() throws Exception {
42      for (int i = 0; i < m_strings.length; ++i) {
43        final HTMLText text = new HTMLText(m_strings[i]);
44        assertEquals(m_strings[i], text.toText());
45        assertEquals(m_strings[i], text.toHTML());
46        assertEquals(m_strings[i], text.toString());
47      }
48    }
49  
50    public void testHTMLElement() throws Exception {
51      final HTMLElement parent = new HTMLElement("parent");
52      assertEquals("<parent/>", parent.toHTML());
53      assertEquals("", parent.toText());
54  
55      final HTMLElement child = parent.addElement("child");
56      child.addElement("grandChild");
57      parent.addElement("child2");
58      child.addText("Some text");
59      assertEquals(
60        "<parent><child><grandChild/>Some text</child><child2/></parent>",
61        parent.toHTML());
62      assertEquals("Some text", parent.toText());
63  
64      final HTMLElement nullElement = new HTMLElement();
65      assertEquals("", nullElement.toHTML());
66      nullElement.addElement("foo");
67      assertEquals("<foo/>", nullElement.toHTML());
68  
69      final HTMLElement anotherElement = new HTMLElement("body");
70      anotherElement.addElement("P").addText("text");
71      anotherElement.addElement("code").addText("text2");
72      anotherElement.addElement("br");
73      anotherElement.addText("text3");
74  
75      assertEquals("<body><P>text</P><code>text2</code><br/>text3</body>",
76                   anotherElement.toHTML());
77      assertEquals("text\ntext2\ntext3", anotherElement.toText());
78    }
79  
80    public void testHTMLDocument() throws Exception {
81      final HTMLDocument document = new HTMLDocument();
82      document.getHead().addElement("title").addText("Test");
83      document.getBody().addText("The body");
84  
85      assertEquals("<html><head><title>Test</title></head>" +
86                   "<body>The body</body></html>",
87                   document.toHTML());
88    }
89  }