KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > publishers > rss > ItemTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2005, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.publishers.rss;
38
39 import java.util.Date JavaDoc;
40 import java.io.StringReader JavaDoc;
41 import java.text.SimpleDateFormat JavaDoc;
42
43 import org.jdom.Element;
44 import org.jdom.Document;
45 import org.jdom.input.SAXBuilder;
46 import junit.framework.TestCase;
47
48 /*
49  * Copyright (c) 2005 Hewlett-Packard Development Company, L.P.
50  */

51 public class ItemTest extends TestCase {
52
53     private Element itemElement;
54
55     public void setUp() throws Exception JavaDoc {
56
57         itemElement = new Element(RSS.NODE_ITEM);
58
59         Element titleElement = new Element(RSS.NODE_ITEM_TITLE);
60         titleElement.addContent("title");
61         itemElement.addContent(titleElement);
62
63         Element linkElement = new Element(RSS.NODE_ITEM_LINK);
64         linkElement.addContent("link");
65         itemElement.addContent(linkElement);
66
67         Element descriptionElement = new Element(RSS.NODE_ITEM_DESCRIPTION);
68         descriptionElement.addContent("description");
69         itemElement.addContent(descriptionElement);
70
71         Element dateElement = new Element(RSS.NODE_ITEM_PUBLISH_DATE);
72         dateElement.addContent(new SimpleDateFormat JavaDoc(RSS.DATE_FORMAT).format(new Date JavaDoc()));
73         itemElement.addContent(dateElement);
74     }
75
76     public void testConstructors() throws Exception JavaDoc {
77         Item item = new Item();
78         assertNull("default constructor should have null title.", item.getTitle());
79         assertNull("default constructor should have null description.", item.getDescription());
80         assertNull("default constructor should have null link.", item.getLink());
81         assertNull("default constructor should have null publish date.", item.getPublishDate());
82
83
84         item = new Item(itemElement);
85         assertEquals("title", item.getTitle());
86         assertEquals("link", item.getLink());
87         assertEquals("description", item.getDescription());
88         assertEquals(
89             new SimpleDateFormat JavaDoc(RSS.DATE_FORMAT).parse(itemElement.getChild(RSS.NODE_ITEM_PUBLISH_DATE).getText()),
90             item.getPublishDate());
91     }
92
93     public void testToXml() throws Exception JavaDoc {
94
95         Item item = new Item(itemElement);
96         String JavaDoc output = item.toXml();
97
98         //ensure it's valid XML...
99
SAXBuilder builder = new SAXBuilder();
100         Document doc = builder.build(new StringReader JavaDoc(output));
101
102         Element root = doc.getRootElement();
103         assertEquals(root.getChild(RSS.NODE_ITEM_TITLE).getText(), "title");
104         assertEquals(root.getChild(RSS.NODE_ITEM_LINK).getText(), "link");
105         assertEquals(root.getChild(RSS.NODE_ITEM_DESCRIPTION).getText(), "description");
106         assertEquals(
107             root.getChild(RSS.NODE_ITEM_PUBLISH_DATE).getText(),
108             itemElement.getChild(RSS.NODE_ITEM_PUBLISH_DATE).getText());
109     }
110 }
Popular Tags