⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 translatorreadertest.java

📁 JSPWiki,100%Java开发的一套完整WIKI程序
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
package com.ecyrd.jspwiki;import com.ecyrd.jspwiki.attachment.Attachment;import com.ecyrd.jspwiki.providers.*;import junit.framework.*;import java.io.*;import java.util.*;import javax.servlet.*;import com.ecyrd.jspwiki.acl.*;import com.ecyrd.jspwiki.auth.permissions.*;import com.ecyrd.jspwiki.auth.*;public class TranslatorReaderTest extends TestCase{    Properties props = new Properties();    Vector     created = new Vector();    static final String PAGE_NAME = "testpage";    TestEngine testEngine;    public TranslatorReaderTest( String s )    {        super( s );    }    public void setUp()        throws Exception    {        props.load( TestEngine.findTestProperties() );        props.setProperty( "jspwiki.translatorReader.matchEnglishPlurals", "true" );        testEngine = new TestEngine( props );    }    public void tearDown()    {        deleteCreatedPages();    }    private void newPage( String name )    {        testEngine.saveText( name, "<test>" );        created.addElement( name );    }    private void deleteCreatedPages()    {        for( Iterator i = created.iterator(); i.hasNext(); )        {            String name = (String) i.next();            testEngine.deletePage(name);        }        created.clear();    }    private String translate( String src )        throws IOException,               NoRequiredPropertyException,               ServletException    {        WikiContext context = new WikiContext( testEngine,                                               new WikiPage(PAGE_NAME) );        Reader r = new TranslatorReader( context,                                          new BufferedReader( new StringReader(src)) );        StringWriter out = new StringWriter();        int c;        while( ( c=r.read()) != -1 )        {            out.write( c );        }        return out.toString();    }    private String translate( WikiPage p, String src )        throws IOException,               NoRequiredPropertyException,               ServletException    {        WikiContext context = new WikiContext( testEngine,                                               p );        Reader r = new TranslatorReader( context,                                          new BufferedReader( new StringReader(src)) );        StringWriter out = new StringWriter();        int c;        while( ( c=r.read()) != -1 )        {            out.write( c );        }        return out.toString();    }    public void testHyperlinks2()        throws Exception    {        newPage("Hyperlink");        String src = "This should be a [hyperlink]";        assertEquals( "This should be a <a class=\"wikipage\" href=\"Wiki.jsp?page=Hyperlink\">hyperlink</a>",                      translate(src) );    }    public void testHyperlinks3()        throws Exception    {        newPage("HyperlinkToo");        String src = "This should be a [hyperlink too]";        assertEquals( "This should be a <a class=\"wikipage\" href=\"Wiki.jsp?page=HyperlinkToo\">hyperlink too</a>",                      translate(src) );    }    public void testHyperlinks4()        throws Exception    {        newPage("HyperLink");        String src = "This should be a [HyperLink]";        assertEquals( "This should be a <a class=\"wikipage\" href=\"Wiki.jsp?page=HyperLink\">HyperLink</a>",                      translate(src) );    }    public void testHyperlinks5()        throws Exception    {        newPage("HyperLink");        String src = "This should be a [here|HyperLink]";        assertEquals( "This should be a <a class=\"wikipage\" href=\"Wiki.jsp?page=HyperLink\">here</a>",                      translate(src) );    }    public void testHyperlinksNamed1()        throws Exception    {        newPage("HyperLink");        String src = "This should be a [here|HyperLink#heading]";        assertEquals( "This should be a <a class=\"wikipage\" href=\"Wiki.jsp?page=HyperLink#section-HyperLink-Heading\">here</a>",                      translate(src) );    }    public void testHyperlinksNamed2()        throws Exception    {        newPage("HyperLink");        String src = "This should be a [HyperLink#heading]";        assertEquals( "This should be a <a class=\"wikipage\" href=\"Wiki.jsp?page=HyperLink#section-HyperLink-Heading\">HyperLink#heading</a>",                      translate(src) );    }    //    //  Testing CamelCase hyperlinks    //    public void testHyperLinks6()        throws Exception    {        newPage("DiscussionAboutWiki");        newPage("WikiMarkupDevelopment");        String src = "[DiscussionAboutWiki] [WikiMarkupDevelopment].";        assertEquals( "<a class=\"wikipage\" href=\"Wiki.jsp?page=DiscussionAboutWiki\">DiscussionAboutWiki</a> <a class=\"wikipage\" href=\"Wiki.jsp?page=WikiMarkupDevelopment\">WikiMarkupDevelopment</a>.",                      translate(src) );           }    public void testHyperlinksCC()        throws Exception    {        newPage("HyperLink");        String src = "This should be a HyperLink.";        assertEquals( "This should be a <a class=\"wikipage\" href=\"Wiki.jsp?page=HyperLink\">HyperLink</a>.",                      translate(src) );    }    public void testHyperlinksCCNonExistant()        throws Exception    {        String src = "This should be a HyperLink.";        assertEquals( "This should be a <u>HyperLink</u><a href=\"Edit.jsp?page=HyperLink\">?</a>.",                      translate(src) );    }    /**     *  Check if the CC hyperlink translator gets confused with     *  unorthodox bracketed links.     */    public void testHyperlinksCC2()        throws Exception    {        newPage("HyperLink");        String src = "This should be a [  HyperLink  ].";        assertEquals( "This should be a <a class=\"wikipage\" href=\"Wiki.jsp?page=HyperLink\">  HyperLink  </a>.",                      translate(src) );    }    public void testHyperlinksCC3()        throws Exception    {        String src = "This should be a nonHyperLink.";        assertEquals( "This should be a nonHyperLink.",                      translate(src) );    }    /** Two links on same line. */    public void testHyperlinksCC4()        throws Exception    {        newPage("HyperLink");        newPage("ThisToo");        String src = "This should be a HyperLink, and ThisToo.";        assertEquals( "This should be a <a class=\"wikipage\" href=\"Wiki.jsp?page=HyperLink\">HyperLink</a>, and <a class=\"wikipage\" href=\"Wiki.jsp?page=ThisToo\">ThisToo</a>.",                      translate(src) );    }    /** Two mixed links on same line. */    public void testHyperlinksCC5()        throws Exception    {        newPage("HyperLink");        newPage("ThisToo");        String src = "This should be a [HyperLink], and ThisToo.";        assertEquals( "This should be a <a class=\"wikipage\" href=\"Wiki.jsp?page=HyperLink\">HyperLink</a>, and <a class=\"wikipage\" href=\"Wiki.jsp?page=ThisToo\">ThisToo</a>.",                      translate(src) );    }    /** Closing tags only. */    public void testHyperlinksCC6()        throws Exception    {        newPage("HyperLink");        newPage("ThisToo");        String src = "] This ] should be a HyperLink], and ThisToo.";        assertEquals( "] This ] should be a <a class=\"wikipage\" href=\"Wiki.jsp?page=HyperLink\">HyperLink</a>], and <a class=\"wikipage\" href=\"Wiki.jsp?page=ThisToo\">ThisToo</a>.",                      translate(src) );    }    /** First and last words on line. */    public void testHyperlinksCCFirstAndLast()        throws Exception    {        newPage("HyperLink");        newPage("ThisToo");        String src = "HyperLink, and ThisToo";        assertEquals( "<a class=\"wikipage\" href=\"Wiki.jsp?page=HyperLink\">HyperLink</a>, and <a class=\"wikipage\" href=\"Wiki.jsp?page=ThisToo\">ThisToo</a>",                      translate(src) );    }    /** Hyperlinks inside URIs. */    public void testHyperlinksCCURLs()        throws Exception    {        String src = "http://www.foo.bar/ANewHope/";        // System.out.println( "EX:"+translate(src) );        assertEquals( "<a class=\"external\" href=\"http://www.foo.bar/ANewHope/\">http://www.foo.bar/ANewHope/</a>",                      translate(src) );    }    /** Hyperlinks inside URIs. */    public void testHyperlinksCCURLs2()        throws Exception    {        String src = "mailto:foo@bar.com";        // System.out.println( "EX:"+translate(src) );        assertEquals( "<a class=\"external\" href=\"mailto:foo@bar.com\">mailto:foo@bar.com</a>",                      translate(src) );    }    /** Hyperlinks inside URIs. */    public void testHyperlinksCCURLs3()        throws Exception    {        String src = "This should be a link: http://www.foo.bar/ANewHope/.  Is it?";        // System.out.println( "EX:"+translate(src) );        assertEquals( "This should be a link: <a class=\"external\" href=\"http://www.foo.bar/ANewHope/\">http://www.foo.bar/ANewHope/</a>.  Is it?",                      translate(src) );    }    /** Hyperlinks in brackets. */    public void testHyperlinksCCURLs4()        throws Exception    {        String src = "This should be a link: (http://www.foo.bar/ANewHope/)  Is it?";        // System.out.println( "EX:"+translate(src) );        assertEquals( "This should be a link: (<a class=\"external\" href=\"http://www.foo.bar/ANewHope/\">http://www.foo.bar/ANewHope/</a>)  Is it?",                      translate(src) );    }    /** Hyperlinks end line. */    public void testHyperlinksCCURLs5()        throws Exception    {        String src = "This should be a link: http://www.foo.bar/ANewHope/\nIs it?";        // System.out.println( "EX:"+translate(src) );        assertEquals( "This should be a link: <a class=\"external\" href=\"http://www.foo.bar/ANewHope/\">http://www.foo.bar/ANewHope/</a>\nIs it?",                      translate(src) );    }    /** Hyperlinks with odd chars. */    public void testHyperlinksCCURLs6()        throws Exception    {        String src = "This should not be a link: http://''some.server''/wiki/Wiki.jsp\nIs it?";        // System.out.println( "EX:"+translate(src) );        assertEquals( "This should not be a link: http://<i>some.server</i>/wiki/Wiki.jsp\nIs it?",                      translate(src) );    }    public void testHyperlinksCCNegated()        throws Exception    {        String src = "This should not be a ~HyperLink.";        assertEquals( "This should not be a HyperLink.",                      translate(src) );    }    public void testHyperlinksCCNegated2()        throws Exception    {        String src = "~HyperLinks should not be matched.";        assertEquals( "HyperLinks should not be matched.",                      translate(src) );    }    public void testCCLinkInList()        throws Exception    {        newPage("HyperLink");        String src = "*HyperLink";        assertEquals( "<ul>\n<li><a class=\"wikipage\" href=\"Wiki.jsp?page=HyperLink\">HyperLink</a></li>\n</ul>\n",                      translate(src) );    }    public void testCCLinkBold()        throws Exception    {        newPage("BoldHyperLink");        String src = "__BoldHyperLink__";        assertEquals( "<b><a class=\"wikipage\" href=\"Wiki.jsp?page=BoldHyperLink\">BoldHyperLink</a></b>",                      translate(src) );    }    public void testCCLinkBold2()        throws Exception    {        newPage("HyperLink");        String src = "Let's see, if a bold __HyperLink__ is correct?";        assertEquals( "Let's see, if a bold <b><a class=\"wikipage\" href=\"Wiki.jsp?page=HyperLink\">HyperLink</a></b> is correct?",                      translate(src) );    }    public void testCCLinkItalic()        throws Exception    {        newPage("ItalicHyperLink");        String src = "''ItalicHyperLink''";        assertEquals( "<i><a class=\"wikipage\" href=\"Wiki.jsp?page=ItalicHyperLink\">ItalicHyperLink</a></i>",                      translate(src) );    }    public void testCCLinkWithPunctuation()        throws Exception    {        newPage("HyperLink");        String src = "Test. Punctuation. HyperLink.";        assertEquals( "Test. Punctuation. <a class=\"wikipage\" href=\"Wiki.jsp?page=HyperLink\">HyperLink</a>.",                      translate(src) );    }    public void testCCLinkWithPunctuation2()        throws Exception    {        newPage("HyperLink");        newPage("ThisToo");        String src = "Punctuations: HyperLink,ThisToo.";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -