📄 translatorreadertest.java
字号:
assertEquals( "<hr />Foo", translate(src) ); } public void testShortRuler1() throws Exception { String src="-"; assertEquals( "-", translate(src) ); } public void testShortRuler2() throws Exception { String src="--"; assertEquals( "--", translate(src) ); } public void testShortRuler3() throws Exception { String src="---"; assertEquals( "---", translate(src) ); } public void testLongRuler() throws Exception { String src="------"; assertEquals( "<hr />", translate(src) ); } public void testHeading1() throws Exception { String src="!Hello\nThis is a test"; assertEquals( "<h4><a name=\"section-testpage-Hello\">Hello</a></h4>\nThis is a test", translate(src) ); } public void testHeading2() throws Exception { String src="!!Hello, testing 1, 2, 3"; assertEquals( "<h3><a name=\"section-testpage-HelloTesting123\">Hello, testing 1, 2, 3</a></h3>", translate(src) ); } public void testHeading3() throws Exception { String src="!!!Hello there, how are you doing?"; assertEquals( "<h2><a name=\"section-testpage-HelloThereHowAreYouDoing\">Hello there, how are you doing?</a></h2>", translate(src) ); } public void testHeadingHyperlinks() throws Exception { String src="!!![Hello]"; assertEquals( "<h2><a name=\"section-testpage-Hello\"><u>Hello</u><a href=\"Edit.jsp?page=Hello\">?</a></a></h2>", translate(src) ); } public void testHeadingHyperlinks2() throws Exception { String src="!!![Hello|http://www.google.com/]"; assertEquals( "<h2><a name=\"section-testpage-Hello\"><a class=\"external\" href=\"http://www.google.com/\">Hello</a></a></h2>", translate(src) ); } public void testHeadingHyperlinks3() throws Exception { String src="![Hello|http://www.google.com/?p=a&c=d]"; assertEquals( "<h4><a name=\"section-testpage-Hello\"><a class=\"external\" href=\"http://www.google.com/?p=a&c=d\">Hello</a></a></h4>", translate(src) ); } /** * in 2.0.0, this one throws OutofMemoryError. */ public void testBrokenPageText() throws Exception { String translation = translate( brokenPageText ); assertNotNull( translation ); } /** * Shortened version of the previous one. */ public void testBrokenPageTextShort() throws Exception { String src = "{{{\ncode.}}\n"; assertEquals( "<pre>\ncode.}}\n</pre>\n", translate(src) ); } /** * Shortened version of the previous one. */ public void testBrokenPageTextShort2() throws Exception { String src = "{{{\ncode.}\n"; assertEquals( "<pre>\ncode.}\n</pre>\n", translate(src) ); } /** * ACL tests. */ public void testSimpleACL1() throws Exception { String src = "Foobar.[{ALLOW view JanneJalkanen}]"; WikiPage p = new WikiPage( PAGE_NAME ); String res = translate( p, src ); assertEquals("Page text", "Foobar.", res); AccessControlList acl = p.getAcl(); UserProfile prof = new UserProfile(); prof.setName("JanneJalkanen"); assertTrue( "no read", acl.checkPermission( prof, new ViewPermission() ) ); assertFalse( "has edit", acl.checkPermission( prof, new EditPermission() ) ); } public void testSimpleACL2() throws Exception { String src = "Foobar.[{ALLOW view JanneJalkanen}]\n"+ "[{DENY view ErikBunn, SuloVilen}]\n"+ "[{ALLOW edit JanneJalkanen, SuloVilen}]"; WikiPage p = new WikiPage( PAGE_NAME ); String res = translate( p, src ); assertEquals("Page text", "Foobar.\n\n", res); AccessControlList acl = p.getAcl(); UserProfile prof = new UserProfile(); prof.setName("JanneJalkanen"); assertTrue( "no read for JJ", acl.checkPermission( prof, new ViewPermission() ) ); assertTrue( "no edit for JJ", acl.checkPermission( prof, new EditPermission() ) ); prof.setName("ErikBunn"); assertFalse( "read for EB", acl.checkPermission( prof, new ViewPermission() ) ); assertFalse( "has edit for EB", acl.checkPermission( prof, new EditPermission() ) ); prof.setName("SuloVilen"); assertFalse("read for SV", acl.checkPermission( prof, new ViewPermission() ) ); assertTrue( "no edit for SV", acl.checkPermission( prof, new EditPermission() ) ); } private boolean containsGroup( List l, String name ) { for( Iterator i = l.iterator(); i.hasNext(); ) { String group = (String) i.next(); if( group.equals( name ) ) return true; } return false; } /** * Metadata tests */ public void testSet1() throws Exception { String src = "Foobar.[{SET name=foo}]"; WikiPage p = new WikiPage( PAGE_NAME ); String res = translate( p, src ); assertEquals("Page text", "Foobar.", res); assertEquals( "foo", p.getAttribute("name") ); } public void testSet2() throws Exception { String src = "Foobar.[{SET name = foo}]"; WikiPage p = new WikiPage( PAGE_NAME ); String res = translate( p, src ); assertEquals("Page text", "Foobar.", res); assertEquals( "foo", p.getAttribute("name") ); } public void testSet3() throws Exception { String src = "Foobar.[{SET name= Janne Jalkanen}]"; WikiPage p = new WikiPage( PAGE_NAME ); String res = translate( p, src ); assertEquals("Page text", "Foobar.", res); assertEquals( "Janne Jalkanen", p.getAttribute("name") ); } /** * Test collection of links. */ public void testCollectingLinks() throws Exception { LinkCollector coll = new LinkCollector(); String src = "[Test]"; WikiContext context = new WikiContext( testEngine, new WikiPage(PAGE_NAME) ); TranslatorReader r = new TranslatorReader( context, new BufferedReader( new StringReader(src)) ); r.addLocalLinkHook( coll ); r.addExternalLinkHook( coll ); r.addAttachmentLinkHook( coll ); StringWriter out = new StringWriter(); FileUtil.copyContents( r, out ); Collection links = coll.getLinks(); assertEquals( "no links found", 1, links.size() ); assertEquals( "wrong link", "Test", links.iterator().next() ); } public void testCollectingLinks2() throws Exception { LinkCollector coll = new LinkCollector(); String src = "["+PAGE_NAME+"/Test.txt]"; WikiContext context = new WikiContext( testEngine, new WikiPage(PAGE_NAME) ); TranslatorReader r = new TranslatorReader( context, new BufferedReader( new StringReader(src)) ); r.addLocalLinkHook( coll ); r.addExternalLinkHook( coll ); r.addAttachmentLinkHook( coll ); StringWriter out = new StringWriter(); FileUtil.copyContents( r, out ); Collection links = coll.getLinks(); assertEquals( "no links found", 1, links.size() ); assertEquals( "wrong link", PAGE_NAME+"/Test.txt", links.iterator().next() ); } public void testCollectingLinksAttachment() throws Exception { // First, make an attachment. try { Attachment att = new Attachment( PAGE_NAME, "TestAtt.txt" ); att.setAuthor( "FirstPost" ); testEngine.getAttachmentManager().storeAttachment( att, testEngine.makeAttachmentFile() ); LinkCollector coll = new LinkCollector(); LinkCollector coll_others = new LinkCollector(); String src = "[TestAtt.txt]"; WikiContext context = new WikiContext( testEngine, new WikiPage(PAGE_NAME) ); TranslatorReader r = new TranslatorReader( context, new BufferedReader( new StringReader(src)) ); r.addLocalLinkHook( coll_others ); r.addExternalLinkHook( coll_others ); r.addAttachmentLinkHook( coll ); StringWriter out = new StringWriter(); FileUtil.copyContents( r, out ); Collection links = coll.getLinks(); assertEquals( "no links found", 1, links.size() ); assertEquals( "wrong link", PAGE_NAME+"/TestAtt.txt", links.iterator().next() ); assertEquals( "wrong links found", 0, coll_others.getLinks().size() ); } finally { String files = testEngine.getWikiProperties().getProperty( BasicAttachmentProvider.PROP_STORAGEDIR ); File storagedir = new File( files, PAGE_NAME+BasicAttachmentProvider.DIR_EXTENSION ); if( storagedir.exists() && storagedir.isDirectory() ) testEngine.deleteAll( storagedir ); } } // This is a random find: the following page text caused an eternal loop in V2.0.x. private static final String brokenPageText = "Please ''check [RecentChanges].\n" + "\n" + "Testing. fewfwefe\n" + "\n" + "CHeck [testpage]\n" + "\n" + "More testing.\n" + "dsadsadsa''\n" + "Is this {{truetype}} or not?\n" + "What about {{{This}}}?\n" + "How about {{this?\n" + "\n" + "{{{\n" + "{{text}}\n" + "}}}\n" + "goo\n" + "\n" + "<b>Not bold</b>\n" + "\n" + "motto\n" + "\n" + "* This is a list which we\n" + "shall continue on a other line.\n" + "* There is a list item here.\n" + "* Another item.\n" + "* More stuff, which continues\n" + "on a second line. And on\n" + "a third line as well.\n" + "And a fourth line.\n" + "* Third item.\n" + "\n" + "Foobar.\n" + "\n" + "----\n" + "\n" + "!!!Really big heading\n" + "Text.\n" + "!! Just a normal heading [with a hyperlink|Main]\n" + "More text.\n" + "!Just a small heading.\n" + "\n" + "This should be __bold__ text.\n" + "\n" + "__more bold text continuing\n" + "on the next line.__\n" + "\n" + "__more bold text continuing\n" + "\n" + "on the next paragraph.__\n" + "\n" + "\n" + "This should be normal.\n" + "\n" + "Now, let's try ''italic text''.\n" + "\n" + "Bulleted lists:\n" + "* One\n" + "Or more.\n" + "* Two\n" + "\n" + "** Two.One\n" + "\n" + "*** Two.One.One\n" + "\n" + "* Three\n" + "\n" + "Numbered lists.\n" + "# One\n" + "# Two\n" + "# Three\n" + "## Three.One\n" + "## Three.Two\n" + "## Three.Three\n" + "### Three.Three.One\n" + "# Four\n" + "\n" + "End?\n" + "\n" + "No, let's {{break}} things.\\ {{{ {{{ {{text}} }}} }}}\n" + "\n" + "More breaking.\n" + "\n" + "{{{\n" + "code.}}\n" + "----\n" + "author: [Asser], [Ebu], [JanneJalkanen], [Jarmo|mailto:jarmo@regex.com.au]\n"; public static Test suite() { return new TestSuite( TranslatorReaderTest.class ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -