// BlogBridge -- RSS feed reader, manager, and web based service // Copyright (C) 2002-2006 by R. Pito Salas // // This program is free software; you can redistribute it and/or modify it under // the terms of the GNU General Public License as published by the Free Software Foundation; // either version 2 of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. // See the GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along with this program; // if not, write to the Free Software Foundation, Inc., 59 Temple Place, // Suite 330, Boston, MA 02111-1307 USA // // Contact: R. Pito Salas // mailto:pitosalas@users.sourceforge.net // More information: about BlogBridge // http://www.blogbridge.com // http://sourceforge.net/projects/blogbridge // // $Id: TestXMLFormatDetector.java,v 1.2 2006/11/27 17:00:02 spyromus Exp $ // package com.salas.bb.utils.discovery.detector; import junit.framework.TestCase; import java.io.InputStream; import java.io.ByteArrayInputStream; import java.io.IOException; /** * Tests detection of various formats. */ public class TestXMLFormatDetector extends TestCase { private XMLFormatDetector detector; /** * Initialization. * * @throws Exception in case of error. */ protected void setUp() throws Exception { super.setUp(); detector = new XMLFormatDetector(); } /** Tests unknown format. */ public void testUnknown() { assertFormat(null, "Test"); assertFormat(null, ""); } /** Tests Atom detection. */ public void testAtom() { assertFormat(XMLFormat.ATOM, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<feed xmlns=\"http://www.w3.org/2005/Atom\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" " + "xmlns:thr=\"http://purl.org/syndication/thread/1.0\">\n" + " <title>Charlene Li's Blog</title>"); assertFormat(XMLFormat.ATOM, "\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n" + "<feed version=\"0.3\" xmlns=\"http://purl.org/atom/ns#\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"); } /** Tests OPML detection. */ public void testOPML() { assertFormat(XMLFormat.OPML, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<opml version=\"1.0\">\n" + "<head>\n" + " <title>Oxford Journals</title>\n" + " <dateCreated>2005-12-08</dateCreated>\n" + "</head>"); assertFormat(XMLFormat.OPML, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!-- Generated by BlogBridge Service (http://www.blogbridge.com) on Mon, 21 Aug 2006 15:26:02 CDT -->\n" + "<opml version=\"1.1\" xmlns:bb=\"http://blogbridge.com/ns/2006/opml\"><head><title>NASA</title>"); } /** Tests RSS detection. */ public void testRSS() { assertFormat(XMLFormat.RSS, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<rss version=\"1.0\"><channel><title>Physics Today magazine</title>"); assertFormat(XMLFormat.RSS, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<rss version=\"2.0\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n" + "\n" + "<channel>"); assertFormat(XMLFormat.RSS, "<?xml version=\"1.0\"?>\n" + "<!DOCTYPE rss PUBLIC \"-//Netscape Communications//DTD RSS 0.91//EN\"\n" + " \"http://my.netscape.com/publish/formats/rss-0.91.dtd\">\n\n" + "<rss version=\"0.91\">"); assertFormat(XMLFormat.RSS, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<?xml-stylesheet href=\"http://feeds.feedburner.com/~d/styles/rss2full.xsl\" " + "type=\"text/xsl\" media=\"screen\"?>" + "<?xml-stylesheet href=\"http://feeds.feedburner.com/~d/styles/itemcontent.css\" " + "type=\"text/css\" media=\"screen\"?><!-- generator=\"wordpress/1.0-alpha-1\" -->" + "<rss xmlns:content=\"http://purl.org/rss/1.0/modules/content/\" " + "xmlns:feedburner=\"http://rssnamespace.org/feedburner/ext/1.0\" version=\"2.0\">"); } /** * Checks RDF detection with arbitrary namespace. */ public void testRDF() { assertFormat(XMLFormat.RDF, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>" + "<rqwertyqwrety:RDF xmlns:rqwertyqwrety=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" " + "xmlns=\"http://purl.org/rss/1.0/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"); } private static InputStream str(String s) { return new ByteArrayInputStream(s.getBytes()); } private void assertFormat(XMLFormat fmt, String s) { InputStream in = str(s); XMLFormat f = null; try { f = detector.detect(in); } catch (IOException e) { // Impossible } if (fmt == null) assertNull(f); else assertEquals(fmt, f); } }