@Test
public void testBuildFeed() {
try {
Generator generator = feedDoc.getLibVersion();
Id id = feedDoc.buildId(null,
"http://www.colorfulsoftware.com/atom.xml");
Updated updated = feedDoc.buildUpdated(null, Calendar.getInstance()
.getTime().toString());
Title title = feedDoc.buildTitle("test feed", null);
List<Contributor> contributors = new LinkedList<Contributor>();
Contributor contributor = feedDoc.buildContributor(new Name(
"Mad Dog"), null, feedDoc.buildEmail("info@maddog.net"),
null, null);
contributors.add(contributor);
Rights rights = feedDoc.buildRights("GPL 1.0", null);
Icon icon = feedDoc.buildIcon(null, "http://host/images/icon.png");
Logo logo = feedDoc.buildLogo(null, "http://host/images/logo.png");
List<Attribute> catAttrs = new LinkedList<Attribute>();
catAttrs.add(feedDoc.buildAttribute("term", "music"));
catAttrs.add(feedDoc.buildAttribute("scheme",
"http://mtv.com/genere"));
catAttrs.add(feedDoc.buildAttribute("label", "music"));
List<Category> categories = new LinkedList<Category>();
Category category = feedDoc.buildCategory(catAttrs, null);
categories.add(category);
List<Attribute> linkAttrs = new LinkedList<Attribute>();
linkAttrs.add(feedDoc
.buildAttribute("href", "http://www.yahoo.com"));
linkAttrs.add(feedDoc.buildAttribute("rel", "self"));
linkAttrs.add(feedDoc.buildAttribute("hreflang", "en-US"));
List<Link> links = new LinkedList<Link>();
Link link = feedDoc.buildLink(linkAttrs, null);
links.add(link);
Attribute extAttr = feedDoc.buildAttribute("xmlns:xhtml",
"http://www.w3.org/1999/xhtml");
List<Attribute> extAttrs = new LinkedList<Attribute>();
extAttrs.add(extAttr);
// the base feed attributes.
List<Attribute> feedAttrs = new LinkedList<Attribute>();
feedAttrs.add(feedDoc.getAtomBase());
feedAttrs.add(feedDoc.getLangEn());
feedAttrs.addAll(extAttrs);
List<Extension> extensions = new LinkedList<Extension>();
Extension extension = feedDoc.buildExtension("xhtml:div", null,
"<span style='color:red;'>hello there</span>");
extensions.add(extension);
List<Author> authors = new LinkedList<Author>();
authors.add(feedDoc.buildAuthor(feedDoc.buildName("Bill Brown"),
null, null, null, null));
Entry entry = feedDoc
.buildEntry(
feedDoc
.buildId(null,
"http://www.colorfulsoftware.com/atom.xml#entry1"),
feedDoc.buildTitle("an example atom entry", null),
feedDoc.buildUpdated(null, Calendar.getInstance()
.getTime().toString()),
null,
feedDoc
.buildContent(
"Hello World. Welcome to the atomsphere feed builder for atom 1.0 builds. I hope it is useful for you.",
null), authors, null, null, null,
null, null, null, null, null);
List<Entry> entries = new LinkedList<Entry>();
entries.add(entry);
Feed feed = feedDoc.buildFeed(id, title, updated, rights, authors,
categories, contributors, links, feedAttrs, extensions,
generator, null, icon, logo, entries);
assertNotNull(feed);
assertNotNull(feed.getAttribute("xml:lang"));
assertNull(feed.getAttribute("bunk"));
// read and write a full feed.
feed = feedDoc.readFeedToBean(mega);
assertNotNull(feedDoc.getLibVersion());
BufferedWriter out = new BufferedWriter(new FileWriter(
"target/mega.xml"));
out.write(feed.toString());
out.flush();
out.close();
feedDoc.writeFeedDoc(new FileOutputStream(new File(
"target/mega2.xml")), feed, feedDoc.getEncoding(), feedDoc
.getXmlVersion());
assertNotNull(feed.getId());
assertNull(feed.getGenerator().getAttribute("notHere"));
assertNotNull(feed.getAuthor("Bill Brown"));
assertNotNull(feed.getContributor("Bill Brown"));
assertNotNull(feed.getLink("self"));
assertNull(feed.getLink("self").getContent());
assertNull(feed.getLink(null));
assertNotNull(feed.getId().getAttribute("local:something"));
assertNull(feed.getId().getAttribute("bunk"));
assertNull(feed.getCategory("math").getAttribute("anythingWrong"));
assertNull(feed.getCategory("math").getContent());
assertNotNull(feed.getIcon().getAttribute("local:testAttr"));
assertNotNull(feed.getLogo().getAttribute("local:testAttr"));
assertNotNull(feed.getRights().getAttribute("xmlns"));
assertNull(feed.getRights().getAttribute("sayWhat"));
assertNotNull(feed.getUpdated().getDateTime());
assertNull(feed.getSubtitle().getAttribute("sayWhat"));
// sort the entries:
feed = feedDoc.sortEntries(feed, Summary.class, "asc");
feed = feedDoc.sortEntries(feed, Title.class, "desc");
feed = feedDoc.sortEntries(feed, Updated.class, "asc");
// this wont sort anything.
try {
feed = feedDoc.sortEntries(feed, Author.class, "asc");
fail("should not get here.");
} catch (AtomSpecException e) {
assertEquals(e.getMessage(),
"The feed entries cannot be sorted by an invalid type 'Author'.");
}
XMLStreamWriter writer = XMLOutputFactory.newInstance()
.createXMLStreamWriter(
new FileOutputStream("target/dump1.xml"));
feedDoc.writeFeedDoc(writer, feed, null, null);
int fileName = 1;
for (Entry ent : feed.getEntries()) {
out = new BufferedWriter(new FileWriter("target/"
+ (fileName++) + ".xml"));
out.write(ent.toString());
out.flush();
out.close();
if (ent
.getId()
.getAtomUri()
.equals(
"http://colorfulsoftware.localhost/colorfulsoftware/projects/atomsphere/atom.xml#About")) {
Author auth = ent.getAuthor("Bill Brown");
assertNotNull(ent.getAttribute("xmlns"));
assertNotNull(auth);
assertNotNull(auth.getAttribute("local:testAttr"));
Attribute attr1 = auth.getAttribute("local:testAttr");
assertFalse(attr1.equals(auth.getAttribute("local:blank")));
assertNull(auth.getAttribute("local:blank"));
assertNotNull(auth.getExtension("test:test"));
assertNull(auth.getExtension("local:bunky"));
assertNull(ent.getAuthor("some other dude"));
assertNotNull(ent.getContributor("Bill Brown"));
assertNull(ent.getContributor("some other dude"));
assertNotNull(ent.getCategory("science"));
assertNull(ent.getCategory("nothing"));
assertNotNull(ent.getLink("alternate"));
assertNull(ent.getLink("http://www.fakeness.net"));
assertNotNull(ent.getExtension("local:element"));
assertNull(ent.getExtension("local:notthere"));
assertNotNull(ent.getPublished().getAttribute("xmlns"));
assertNotNull(ent.getPublished().getDateTime());
assertNull(ent.getSummary().getAttribute("sayWhat"));
Contributor cont = ent.getContributor("Bill Brown");
assertNotNull(cont);
assertNotNull(cont.getExtension("test:test"));
assertNotNull(cont.getAttribute("local:testAttr"));
attr1 = cont.getAttribute("local:testAttr");
assertFalse(attr1.equals(auth.getAttribute("local:blank")));
assertNull(auth.getAttribute("local:blank"));
assertNotNull(auth.getExtension("test:test"));
assertNull(auth.getExtension("local:bunky"));
assertNull(ent.getAuthor("some other dude"));
assertNotNull(ent.getContributor("Bill Brown"));
assertNull(ent.getContributor("some other dude"));
assertNotNull(ent.getCategory("science"));
assertNull(ent.getCategory("nothing"));
assertNotNull(ent.getLink("alternate"));
assertNull(ent.getLink("http://www.fakeness.net"));
assertNotNull(ent.getExtension("local:element"));
assertNull(ent.getExtension("local:notthere"));
}
// test the source attribute
if (ent.getSource() != null) {
Source source = ent.getSource();
assertNotNull(source.getAttribute("xmlns:sort"));
assertNotNull(source.getAuthor("The Minority Directory"));
assertNotNull(source.getContributor("The People"));
assertNotNull(source.getCategory("purpose"));
assertNotNull(source.getLink("self"));
assertNotNull(source.getExtension("sort:asc"));
}
}
// re read the written feed and check the data.
feed = feedDoc.readFeedToBean(new File("target/dump1.xml"));
assertNotNull(feed);
assertNotNull(feed.getCategories());
List<Category> cats = feed.getCategories();
for (Category cat : cats) {
assertNotNull(cat.getTerm());
if (!cat.getTerm().getValue().equals("thing")
&& !cat.getTerm().getValue().equals("undefined")) {
assertNotNull(cat.getLabel());
assertNotNull(cat.getScheme());
}
}
List<Link> lnks = feed.getLinks();
for (Link lnk : lnks) {
assertNotNull(lnk.getHref());
assertNotNull(lnk.getHreflang());
assertNotNull(lnk.getTitle());
assertNotNull(lnk.getType());
assertNotNull(lnk.getRel());
}
} catch (Exception e) {
e.printStackTrace();
fail("Issue building feed doc.");
}
// test a plain attribute
try {
feedDoc.buildAttribute(null, "null");
fail("should not get here.");
} catch (Exception e) {
assertTrue(e instanceof AtomSpecException);
assertEquals(e.getMessage(), "Attribute names SHOULD NOT be blank.");
}
Id id = null;
Title title = null;
Updated updated = null;
List<Entry> entries = null;
try {
id = feedDoc.buildId(null, "http://www.test.com");