This is an automated email from the ASF dual-hosted git repository. juanpablo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/jspwiki.git
commit 1a01c8153fd48630d5ca8c190524fde8a6464bad Author: juanpablo <[email protected]> AuthorDate: Wed Jan 1 21:02:50 2020 +0100 JSPWIKI-120: scanWikiLinks(..) and updateReferences(..) methods from WikiEngine moved to ReferenceManager --- .../java/org/apache/wiki/ReferenceManager.java | 35 +++- .../src/main/java/org/apache/wiki/WikiEngine.java | 48 +---- .../apache/wiki/attachment/AttachmentManager.java | 60 ++---- .../org/apache/wiki/content/WikiPageRenamer.java | 6 +- .../org/apache/wiki/pages/DefaultPageManager.java | 35 +--- .../java/org/apache/wiki/ReferenceManagerTest.java | 216 ++++++++------------- .../test/java/org/apache/wiki/WikiEngineTest.java | 12 -- 7 files changed, 146 insertions(+), 266 deletions(-) diff --git a/jspwiki-main/src/main/java/org/apache/wiki/ReferenceManager.java b/jspwiki-main/src/main/java/org/apache/wiki/ReferenceManager.java index cc46816..ea09f2d 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/ReferenceManager.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/ReferenceManager.java @@ -160,7 +160,7 @@ public class ReferenceManager extends BasicPageFilter implements InternalModule, */ private void updatePageReferences( final WikiPage page ) throws ProviderException { final String content = m_engine.getPageManager().getPageText( page.getName(), WikiPageProvider.LATEST_VERSION ); - final Collection< String > links = m_engine.scanWikiLinks( page, content ); + final Collection< String > links = scanWikiLinks( page, content ); final TreeSet< String > res = new TreeSet<>( links ); final List< Attachment > attachments = m_engine.getAttachmentManager().listAttachments( page ); for( final Attachment att : attachments ) { @@ -411,11 +411,32 @@ public class ReferenceManager extends BasicPageFilter implements InternalModule, @Override public void postSave( final WikiContext context, final String content ) { final WikiPage page = context.getPage(); - updateReferences( page.getName(), context.getEngine().scanWikiLinks( page, content ) ); + updateReferences( page.getName(), scanWikiLinks( page, content ) ); serializeAttrsToDisk( page ); } /** + * Reads a WikiPageful of data from a String and returns all links internal to this Wiki in a Collection. + * + * @param page The WikiPage to scan + * @param pagedata The page contents + * @return a Collection of Strings + */ + public Collection< String > scanWikiLinks( final WikiPage page, final String pagedata ) { + final LinkCollector localCollector = new LinkCollector(); + + m_engine.textToHTML( new WikiContext( m_engine, page ), + pagedata, + localCollector, + null, + localCollector, + false, + true ); + + return localCollector.getLinks(); + } + + /** * Updates the m_referedTo and m_referredBy hashmaps when a page has been deleted. * <P> * Within the m_refersTo map the pagename is a key. The whole key-value-set has to be removed to keep the map clean. @@ -471,6 +492,16 @@ public class ReferenceManager extends BasicPageFilter implements InternalModule, } /** + * Updates all references for the given page. + * + * @param page wiki page for which references should be updated + */ + public void updateReferences( final WikiPage page ) { + final String pageData = m_engine.getPureText( page.getName(), WikiProvider.LATEST_VERSION ); + updateReferences( page.getName(), scanWikiLinks( page, pageData ) ); + } + + /** * Updates the referred pages of a new or edited WikiPage. If a refersTo entry for this page already exists, it is removed * and a new one is built from scratch. Also calls updateReferredBy() for each referenced page. * <P> diff --git a/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java b/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java index 8d32d26..5d0a7d2 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java @@ -1380,27 +1380,6 @@ public class WikiEngine { } /** - * Reads a WikiPageful of data from a String and returns all links internal to this Wiki in a Collection. - * - * @param page The WikiPage to scan - * @param pagedata The page contents - * @return a Collection of Strings - */ - public Collection< String > scanWikiLinks( final WikiPage page, final String pagedata ) { - final LinkCollector localCollector = new LinkCollector(); - - textToHTML( new WikiContext( this, page ), - pagedata, - localCollector, - null, - localCollector, - false, - true ); - - return localCollector.getLinks(); - } - - /** * Just convert WikiText to HTML. * * @param context The WikiContext in which to do the conversion @@ -1448,13 +1427,13 @@ public class WikiEngine { * @param justParse Just parses the pagedata, does not actually render. In this case, this methods an empty string. * @return HTML-rendered page text. */ - private String textToHTML( final WikiContext context, - String pagedata, - final StringTransmutator localLinkHook, - final StringTransmutator extLinkHook, - final StringTransmutator attLinkHook, - final boolean parseAccessRules, - final boolean justParse ) { + public String textToHTML( final WikiContext context, + String pagedata, + final StringTransmutator localLinkHook, + final StringTransmutator extLinkHook, + final StringTransmutator attLinkHook, + final boolean parseAccessRules, + final boolean justParse ) { String result = ""; if( pagedata == null ) { @@ -1507,19 +1486,6 @@ public class WikiEngine { return result; } - /** - * Updates all references for the given page. - * - * @param page wiki page for which references should be updated - */ - public void updateReferences( WikiPage page ) - { - String pageData = getPureText( page.getName(), WikiProvider.LATEST_VERSION ); - - m_referenceManager.updateReferences( page.getName(), - scanWikiLinks( page, pageData ) ); - } - /** * Writes the WikiText of a page into the page repository. If the <code>jspwiki.properties</code> file contains diff --git a/jspwiki-main/src/main/java/org/apache/wiki/attachment/AttachmentManager.java b/jspwiki-main/src/main/java/org/apache/wiki/attachment/AttachmentManager.java index 32036c9..ab819ab 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/attachment/AttachmentManager.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/attachment/AttachmentManager.java @@ -514,9 +514,8 @@ public class AttachmentManager } /** - * Stores an attachment that lives in the given file. - * If the attachment did not exist previously, this method - * will create it. If it did exist, it stores a new version. + * Stores an attachment that lives in the given file. If the attachment did not exist previously, this method will create it. + * If it did exist, it stores a new version. * * @param att Attachment to store this under. * @param source A file to read from. @@ -524,27 +523,15 @@ public class AttachmentManager * @throws IOException If writing the attachment failed. * @throws ProviderException If something else went wrong. */ - public void storeAttachment( Attachment att, File source ) - throws IOException, - ProviderException - { - FileInputStream in = null; - - try - { - in = new FileInputStream( source ); + public void storeAttachment( final Attachment att, final File source ) throws IOException, ProviderException { + try( final FileInputStream in = new FileInputStream( source ) ) { storeAttachment( att, in ); } - finally - { - if( in != null ) in.close(); - } } /** - * Stores an attachment directly from a stream. - * If the attachment did not exist previously, this method - * will create it. If it did exist, it stores a new version. + * Stores an attachment directly from a stream. If the attachment did not exist previously, this method will create it. + * If it did exist, it stores a new version. * * @param att Attachment to store this under. * @param in InputStream from which the attachment contents will be read. @@ -552,32 +539,23 @@ public class AttachmentManager * @throws IOException If writing the attachment failed. * @throws ProviderException If something else went wrong. */ - public void storeAttachment( Attachment att, InputStream in ) - throws IOException, - ProviderException - { - if( m_provider == null ) - { + public void storeAttachment( final Attachment att, final InputStream in ) throws IOException, ProviderException { + if( m_provider == null ) { return; } - // // Checks if the actual, real page exists without any modifications // or aliases. We cannot store an attachment to a non-existent page. - // - if( !m_engine.getPageManager().pageExists( att.getParentName() ) ) - { + if( !m_engine.getPageManager().pageExists( att.getParentName() ) ) { // the caller should catch the exception and use the exception text as an i18n key - throw new ProviderException( "attach.parent.not.exist" ); + throw new ProviderException( "attach.parent.not.exist" ); } m_provider.putAttachmentData( att, in ); + m_engine.getReferenceManager().updateReferences( att.getName(), new ArrayList<>() ); - m_engine.getReferenceManager().updateReferences( att.getName(), new ArrayList< String >() ); - - WikiPage parent = new WikiPage( m_engine, att.getParentName() ); - m_engine.updateReferences( parent ); - + final WikiPage parent = new WikiPage( m_engine, att.getParentName() ); + m_engine.getReferenceManager().updateReferences( parent ); m_engine.getSearchManager().reindexPage( att ); } @@ -590,18 +568,14 @@ public class AttachmentManager * disabled. * @throws ProviderException If the provider fails for some reason. */ - public List<Attachment> getVersionHistory( String attachmentName ) - throws ProviderException - { - if( m_provider == null ) - { + public List< Attachment > getVersionHistory( final String attachmentName ) throws ProviderException { + if( m_provider == null ) { return null; } - Attachment att = getAttachmentInfo( (WikiContext)null, attachmentName ); + final Attachment att = getAttachmentInfo( null, attachmentName ); - if( att != null ) - { + if( att != null ) { return m_provider.getVersionHistory( att ); } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/content/WikiPageRenamer.java b/jspwiki-main/src/main/java/org/apache/wiki/content/WikiPageRenamer.java index f4da6b0..5eed7be 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/content/WikiPageRenamer.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/content/WikiPageRenamer.java @@ -114,7 +114,7 @@ public class WikiPageRenamer implements PageRenamer { // Update the references engine.getReferenceManager().pageRemoved( fromPage ); - engine.updateReferences( toPage ); + engine.getReferenceManager().updateReferences( toPage ); // Update referrers if( changeReferrers ) { @@ -128,7 +128,7 @@ public class WikiPageRenamer implements PageRenamer { for( final Attachment att:attachmentsNewName ) { final WikiPage toAttPage = engine.getPage( att.getName() ); // add reference to attachment under new page name - engine.updateReferences( toAttPage ); + engine.getReferenceManager().updateReferences( toAttPage ); engine.getSearchManager().reindexPage( att ); } @@ -187,7 +187,7 @@ public class WikiPageRenamer implements PageRenamer { try { engine.getPageManager().putPageText( p, newText ); - engine.updateReferences( p ); + engine.getReferenceManager().updateReferences( p ); } catch( final ProviderException e ) { // We fail with an error, but we will try to continue to rename other referrers as well. log.error("Unable to perform rename.",e); diff --git a/jspwiki-main/src/main/java/org/apache/wiki/pages/DefaultPageManager.java b/jspwiki-main/src/main/java/org/apache/wiki/pages/DefaultPageManager.java index 0f508c5..3d5245a 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/pages/DefaultPageManager.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/pages/DefaultPageManager.java @@ -160,7 +160,7 @@ public class DefaultPageManager extends ModuleManager implements PageManager { * @see org.apache.wiki.pages.PageManager#getPageText(java.lang.String, int) */ @Override - public String getPageText(String pageName, int version) throws ProviderException { + public String getPageText( final String pageName, final int version) throws ProviderException { if (pageName == null || pageName.length() == 0) { throw new ProviderException("Illegal page name"); } @@ -168,19 +168,14 @@ public class DefaultPageManager extends ModuleManager implements PageManager { try { text = m_provider.getPageText(pageName, version); - } catch (RepositoryModifiedException e) { - // + } catch (final RepositoryModifiedException e) { // This only occurs with the latest version. - // LOG.info("Repository has been modified externally while fetching page " + pageName); - // // Empty the references and yay, it shall be recalculated - // - //WikiPage p = new WikiPage( pageName ); - WikiPage p = m_provider.getPageInfo(pageName, version); + final WikiPage p = m_provider.getPageInfo(pageName, version); - m_engine.updateReferences(p); + m_engine.getReferenceManager().updateReferences( p ); if (p != null) { m_engine.getSearchManager().reindexPage(p); @@ -189,7 +184,7 @@ public class DefaultPageManager extends ModuleManager implements PageManager { // // Make sure that it no longer exists in internal data structures either. // - WikiPage dummy = new WikiPage(m_engine, pageName); + final WikiPage dummy = new WikiPage(m_engine, pageName); m_engine.getSearchManager().pageRemoved(dummy); m_engine.getReferenceManager().pageRemoved(dummy); } @@ -294,38 +289,26 @@ public class DefaultPageManager extends ModuleManager implements PageManager { * @see org.apache.wiki.pages.PageManager#getPageInfo(java.lang.String, int) */ @Override - public WikiPage getPageInfo(String pageName, int version) throws ProviderException { + public WikiPage getPageInfo( final String pageName, final int version) throws ProviderException { if (pageName == null || pageName.length() == 0) { throw new ProviderException("Illegal page name '" + pageName + "'"); } - WikiPage page = null; + WikiPage page; try { page = m_provider.getPageInfo(pageName, version); - } catch (RepositoryModifiedException e) { - // + } catch ( final RepositoryModifiedException e) { // This only occurs with the latest version. - // LOG.info("Repository has been modified externally while fetching info for " + pageName); page = m_provider.getPageInfo(pageName, version); if (page != null) { - m_engine.updateReferences(page); + m_engine.getReferenceManager().updateReferences(page); } else { m_engine.getReferenceManager().pageRemoved(new WikiPage(m_engine, pageName)); } } - // - // Should update the metadata. - // - /* - if( page != null && !page.hasMetadata() ) - { - WikiContext ctx = new WikiContext(m_engine,page); - m_engine.textToHTML( ctx, getPageText(pageName,version) ); - } - */ return page; } diff --git a/jspwiki-main/src/test/java/org/apache/wiki/ReferenceManagerTest.java b/jspwiki-main/src/test/java/org/apache/wiki/ReferenceManagerTest.java index 71afe02..80d7e6a 100644 --- a/jspwiki-main/src/test/java/org/apache/wiki/ReferenceManagerTest.java +++ b/jspwiki-main/src/test/java/org/apache/wiki/ReferenceManagerTest.java @@ -12,37 +12,31 @@ * limitations under the License. */ package org.apache.wiki; -import java.util.Collection; -import java.util.Iterator; -import java.util.Properties; -import java.util.Set; - +import net.sf.ehcache.CacheManager; import org.apache.wiki.api.exceptions.WikiException; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import net.sf.ehcache.CacheManager; +import java.util.Collection; +import java.util.Iterator; +import java.util.Properties; +import java.util.Set; /** * The ReferenceManager maintains all hyperlinks between wiki pages. */ -public class ReferenceManagerTest -{ +public class ReferenceManagerTest { + Properties props = TestEngine.getTestProperties(); TestEngine engine; ReferenceManager mgr; @BeforeEach - public void setUp() - throws Exception - { + public void setUp() throws Exception { props.setProperty( "jspwiki.translatorReader.matchEnglishPlurals", "true"); - // make sure that the reference manager cache is cleaned first - TestEngine.emptyWorkDir(null); - engine = new TestEngine(props); // create two handy wiki pages used in most test cases @@ -54,53 +48,42 @@ public class ReferenceManagerTest } @AfterEach - public void tearDown() - throws Exception - { + public void tearDown() { // any wiki page that was created must be deleted! TestEngine.emptyWikiDir(); - // jspwiki always uses a singleton CacheManager, so - // clear the cache at the end of every test case to avoid - // polluting another test case + // jspwiki always uses a singleton CacheManager, so clear the cache at the end of every test case to avoid polluting another test case CacheManager.getInstance().removeAllCaches(); + + // make sure that the reference manager cache is cleaned + TestEngine.emptyWorkDir(null); } @Test - public void testNonExistant1() - throws Exception - { - Collection< String > c = mgr.findReferrers("Foobar2"); + public void testNonExistant1() { + final Collection< String > c = mgr.findReferrers("Foobar2"); Assertions.assertNotNull( c, "referrers expected" ); Assertions.assertTrue( c.size() == 1 && c.contains("Foobar") ); } @Test - public void testNonExistant2() - { - Collection< String > c = mgr.findReferrers("TestBug"); - + public void testNonExistant2() { + final Collection< String > c = mgr.findReferrers("TestBug"); Assertions.assertNull( c ); } @Test - public void testRemove() - throws Exception - { + public void testRemove() throws Exception { Collection< String > c = mgr.findReferrers("Foobar2"); - Assertions.assertNotNull( c, "referrers expected" ); Assertions.assertTrue( c.size() == 1 && c.contains("Foobar") ); engine.deletePage( "Foobar" ); - c = mgr.findReferrers("Foobar2"); - Assertions.assertNull( c ); engine.saveText( "Foobar", "[Foobar2]"); - c = mgr.findReferrers("Foobar2"); Assertions.assertNotNull( c, "referrers expected" ); @@ -108,18 +91,14 @@ public class ReferenceManagerTest } @Test - public void testUnreferenced() - throws Exception - { - Collection< String > c = mgr.findUnreferenced(); + public void testUnreferenced() { + final Collection< String > c = mgr.findUnreferenced(); Assertions.assertTrue( Util.collectionContains( c, "TestPage" ), "Unreferenced page not found by ReferenceManager" ); } @Test - public void testBecomesUnreferenced() - throws Exception - { + public void testBecomesUnreferenced() throws Exception { engine.saveText( "Foobar2", "[TestPage]" ); Collection< String > c = mgr.findUnreferenced(); @@ -129,65 +108,52 @@ public class ReferenceManagerTest c = mgr.findUnreferenced(); Assertions.assertEquals( 1, c.size(), "Wrong # of orphan pages" ); - Iterator< String > i = c.iterator(); - String first = i.next(); + final Iterator< String > i = c.iterator(); + final String first = i.next(); Assertions.assertEquals( "TestPage", first, "Not correct referrers" ); } @Test - public void testUncreated() - throws Exception - { - Collection< String > c = mgr.findUncreated(); - - Assertions.assertTrue( c.size()==1 && ((String) c.iterator().next()).equals("Foobar2") ); + public void testUncreated() { + final Collection< String > c = mgr.findUncreated(); + Assertions.assertTrue( c.size()==1 && ( c.iterator().next() ).equals("Foobar2") ); } @Test - public void testReferrers() - throws Exception - { + public void testReferrers() { Collection< String > c = mgr.findReferrers( "TestPage" ); Assertions.assertNull( c, "TestPage referrers" ); c = mgr.findReferrers( "Foobar" ); Assertions.assertNotNull( c, "referrers expected" ); - Assertions.assertTrue( c.size()==2, "Foobar referrers" ); + Assertions.assertEquals( 2, c.size(), "Foobar referrers" ); c = mgr.findReferrers( "Foobar2" ); Assertions.assertNotNull( c, "referrers expected" ); - Assertions.assertTrue( c.size()==1 && ((String) c.iterator().next()).equals("Foobar"), "Foobar2 referrers" ); + Assertions.assertTrue( c.size() == 1 && ( c.iterator().next() ).equals("Foobar"), "Foobar2 referrers" ); c = mgr.findReferrers( "Foobars" ); Assertions.assertNotNull( c, "referrers expected" ); Assertions.assertEquals( 2, c.size(), "Foobars referrers" ); - //Assertions.assertEquals( "Foobars referrer 'TestPage'", "TestPage", (String) c.iterator().next() ); } @Test - public void testRefersTo() - throws Exception - { - Collection< String > s = mgr.findRefersTo( "Foobar" ); + public void testRefersTo() { + final Collection< String > s = mgr.findRefersTo( "Foobar" ); Assertions.assertTrue( s.contains("Foobar"), "does not have Foobar" ); - // Assertions.assertTrue( "does not have Foobars", s.contains("Foobars") ); Assertions.assertTrue( s.contains("Foobar2"), "does not have Foobar2" ); } /** * Should Assertions.fail in 2.2.14-beta - * @throws Exception */ @Test - public void testSingularReferences() - throws Exception - { + public void testSingularReferences() throws Exception { engine.saveText( "RandomPage", "FatalBugs" ); engine.saveText( "FatalBugs", "<foo>" ); engine.saveText( "BugCommentPreviewDeletesAllComments", "FatalBug" ); - - Collection< String > c = mgr.findReferrers( "FatalBugs" ); + final Collection< String > c = mgr.findReferrers( "FatalBugs" ); Assertions.assertNotNull( c, "referrers expected" ); Assertions.assertEquals( 2, c.size(), "FatalBugs referrers number" ); @@ -201,42 +167,34 @@ public class ReferenceManagerTest // a plural and a singular form of the page becomes nigh impossible, so we // just don't do it. @Test - public void testUpdatePluralOnlyRef() - throws Exception - { + public void testUpdatePluralOnlyRef() throws Exception { engine.saveText( "TestPage", "Reference to [Foobars]." ); Collection< String > c = mgr.findUnreferenced(); - Assertions.assertTrue( c.size()==1 && ((String) c.iterator().next()).equals("TestPage"), "Foobar unreferenced" ); + Assertions.assertTrue( c.size()==1 && ( c.iterator().next() ).equals("TestPage"), "Foobar unreferenced" ); c = mgr.findReferrers( "Foobar" ); Assertions.assertNotNull( c, "referrers expected" ); - Assertions.assertTrue( c.size()==2, "Foobar referrers" ); + Assertions.assertEquals( 2, c.size(), "Foobar referrers" ); } /** - * Opposite to testUpdatePluralOnlyRef(). Is a page with plural form recognized as - * the page referenced by a singular link. + * Opposite to testUpdatePluralOnlyRef(). Is a page with plural form recognized as the page referenced by a singular link. */ - @Test - public void testUpdateFoobar2s() - throws Exception - { + public void testUpdateFoobar2s() throws Exception { engine.saveText( "Foobar2s", "qwertz" ); - Assertions.assertTrue( mgr.findUncreated().size()==0, "no uncreated" ); + Assertions.assertEquals( 0, mgr.findUncreated().size(), "no uncreated" ); - Collection< String > c = mgr.findReferrers( "Foobar2s" ); + final Collection< String > c = mgr.findReferrers( "Foobar2s" ); Assertions.assertNotNull( c, "referrers expected" ); - Assertions.assertTrue( c!=null && c.size()==1 && ((String) c.iterator().next()).equals("Foobar"), "referrers" ); + Assertions.assertTrue( c.size()==1 && ( c.iterator().next() ).equals("Foobar"), "referrers" ); } @Test - public void testUpdateBothExist() - throws Exception - { + public void testUpdateBothExist() throws Exception { engine.saveText( "Foobars", "qwertz" ); - Collection< String > c = mgr.findReferrers( "Foobars" ); + final Collection< String > c = mgr.findReferrers( "Foobars" ); Assertions.assertNotNull( c, "referrers expected" ); Assertions.assertEquals( 2, c.size(), "Foobars referrers" ); Assertions.assertTrue( c.contains( "TestPage" ) && c.contains("Foobar"), "Foobars referrer is not TestPage" ); @@ -249,30 +207,25 @@ public class ReferenceManagerTest engine.saveText( "Foobars", "qwertz" ); engine.saveText( "TestPage", "Reference to [Foobar], [Foobars]." ); - Collection< String > c = mgr.findReferrers( "Foobars" ); + final Collection< String > c = mgr.findReferrers( "Foobars" ); Assertions.assertNotNull( c, "referrers expected" ); Assertions.assertEquals( 2, c.size(), "Foobars referrers count" ); Assertions.assertTrue( c.contains("TestPage") && c.contains("Foobar"), "Foobars referrers" ); } @Test - public void testCircularRefs() - throws Exception - { + public void testCircularRefs() throws Exception { engine.saveText( "Foobar2", "ref to [TestPage]" ); - Assertions.assertTrue( mgr.findUncreated().size()==0, "no uncreated" ); - Assertions.assertTrue( mgr.findUnreferenced().size()==0, "no unreferenced" ); + Assertions.assertEquals( 0, mgr.findUncreated().size(), "no uncreated" ); + Assertions.assertEquals( 0, mgr.findUnreferenced().size(), "no unreferenced" ); } @Test - public void testPluralSingularUpdate1() - throws Exception - { + public void testPluralSingularUpdate1() throws Exception { engine.saveText( "BugOne", "NewBug" ); engine.saveText( "NewBugs", "foo" ); engine.saveText( "OpenBugs", "bar" ); - engine.saveText( "BugOne", "OpenBug" ); Collection< String > ref = mgr.findReferrers( "NewBugs" ); @@ -294,9 +247,7 @@ public class ReferenceManagerTest } @Test - public void testPluralSingularUpdate2() - throws Exception - { + public void testPluralSingularUpdate2() throws Exception { engine.saveText( "BugOne", "NewBug" ); engine.saveText( "NewBug", "foo" ); engine.saveText( "OpenBug", "bar" ); @@ -322,14 +273,11 @@ public class ReferenceManagerTest } @Test - public void testPluralSingularUpdate3() - throws Exception - { + public void testPluralSingularUpdate3() throws Exception { engine.saveText( "BugOne", "NewBug" ); engine.saveText( "BugTwo", "NewBug" ); engine.saveText( "NewBugs", "foo" ); engine.saveText( "OpenBugs", "bar" ); - engine.saveText( "BugOne", "OpenBug" ); Collection< String > ref = mgr.findReferrers( "NewBugs" ); @@ -351,43 +299,42 @@ public class ReferenceManagerTest Assertions.assertNotNull( ref, "referrers expected" ); Assertions.assertEquals( 1,ref.size(), "openbug" ); Assertions.assertEquals( "BugOne",ref.iterator().next(), "openbug2" ); - } @Test - public void testSelf() throws WikiException - { + public void testSelf() throws WikiException { engine.saveText( "BugOne", "BugOne" ); - Collection< String > ref = mgr.findReferrers( "BugOne" ); + final Collection< String > ref = mgr.findReferrers( "BugOne" ); Assertions.assertNotNull( ref, "referrers expected" ); Assertions.assertEquals( 1, ref.size(), "wrong size" ); Assertions.assertEquals( "BugOne", ref.iterator().next(), "ref"); } + @Test + public void testReadLinks() { + final String src="Foobar. [Foobar]. Frobozz. [This is a link]."; + final Object[] result = mgr.scanWikiLinks( new WikiPage( engine, "Test"), src ).toArray(); + + Assertions.assertEquals( "Foobar", result[0], "item 0" ); + Assertions.assertEquals( "This is a link", result[1], "item 1" ); + } + /** * Test method: dumps the contents of ReferenceManager link lists to stdout. * This method is NOT synchronized, and should be used in testing * with one user, one WikiEngine only. */ - public static String dumpReferenceManager( ReferenceManager rm ) - { - StringBuilder buf = new StringBuilder(); - try - { + public static String dumpReferenceManager( final ReferenceManager rm ) { + final StringBuilder buf = new StringBuilder(); + try { buf.append( "================================================================\n" ); buf.append( "Referred By list:\n" ); Set< String > keys = rm.getReferredBy().keySet(); - Iterator< String > it = keys.iterator(); - while( it.hasNext() ) - { - String key = it.next(); - buf.append( key + " referred by: " ); - Set< String > refs = rm.getReferredBy().get( key ); - Iterator< String > rit = refs.iterator(); - while( rit.hasNext() ) - { - String aRef = rit.next(); - buf.append( aRef + " " ); + for( final String key : keys ) { + buf.append( key ).append( " referred by: " ); + final Set< String > refs = rm.getReferredBy().get( key ); + for( final String aRef : refs ) { + buf.append( aRef ).append( " " ); } buf.append( "\n" ); } @@ -396,19 +343,12 @@ public class ReferenceManagerTest buf.append( "----------------------------------------------------------------\n" ); buf.append( "Refers To list:\n" ); keys = rm.getRefersTo().keySet(); - it = keys.iterator(); - while( it.hasNext() ) - { - String key = (String) it.next(); - buf.append( key + " refers to: " ); - Collection< String > refs = rm.getRefersTo().get( key ); - if(refs != null) - { - Iterator< String > rit = refs.iterator(); - while( rit.hasNext() ) - { - String aRef = rit.next(); - buf.append( aRef + " " ); + for( final String key : keys ) { + buf.append( key ).append( " refers to: " ); + final Collection< String > refs = rm.getRefersTo().get( key ); + if(refs != null) { + for( final String aRef : refs ) { + buf.append( aRef ).append( " " ); } buf.append( "\n" ); } else { @@ -416,10 +356,8 @@ public class ReferenceManagerTest } } buf.append( "================================================================\n" ); - } - catch(Exception e) - { - buf.append("Problem in dump(): " + e + "\n" ); + } catch( final Exception e ) { + buf.append("Problem in dump(): " ).append( e ).append( "\n" ); } return( buf.toString() ); diff --git a/jspwiki-main/src/test/java/org/apache/wiki/WikiEngineTest.java b/jspwiki-main/src/test/java/org/apache/wiki/WikiEngineTest.java index aa6ab6b..6f7f9c8 100644 --- a/jspwiki-main/src/test/java/org/apache/wiki/WikiEngineTest.java +++ b/jspwiki-main/src/test/java/org/apache/wiki/WikiEngineTest.java @@ -226,18 +226,6 @@ public class WikiEngineTest } @Test - public void testReadLinks() - throws Exception - { - String src="Foobar. [Foobar]. Frobozz. [This is a link]."; - - Object[] result = m_engine.scanWikiLinks( new WikiPage(m_engine, "Test"), src ).toArray(); - - Assertions.assertEquals( "Foobar", result[0], "item 0" ); - Assertions.assertEquals( "This is a link", result[1], "item 1" ); - } - - @Test public void testBeautifyTitle() { String src = "WikiNameThingy";
