1 37 package net.sourceforge.cruisecontrol.publishers.rss; 38 39 import java.util.Date ; 40 import java.io.StringReader ; 41 import java.text.SimpleDateFormat ; 42 43 import org.jdom.Element; 44 import org.jdom.Document; 45 import org.jdom.input.SAXBuilder; 46 import junit.framework.TestCase; 47 48 51 public class ItemTest extends TestCase { 52 53 private Element itemElement; 54 55 public void setUp() throws Exception { 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 (RSS.DATE_FORMAT).format(new Date ())); 73 itemElement.addContent(dateElement); 74 } 75 76 public void testConstructors() throws Exception { 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 (RSS.DATE_FORMAT).parse(itemElement.getChild(RSS.NODE_ITEM_PUBLISH_DATE).getText()), 90 item.getPublishDate()); 91 } 92 93 public void testToXml() throws Exception { 94 95 Item item = new Item(itemElement); 96 String output = item.toXml(); 97 98 SAXBuilder builder = new SAXBuilder(); 100 Document doc = builder.build(new StringReader (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 |