On 11/26/2009 04:28 PM, tmortagne (SVN) wrote:
> Author: tmortagne
> Date: 2009-11-26 16:28:25 +0100 (Thu, 26 Nov 2009)
> New Revision: 25288
>
> Added:
>     
> enterprise/trunk/distribution-test/xmlrpc-tests/src/test/it/org/xwiki/xmlrpc/RenderingTest.java
> Modified:
>     
> enterprise/trunk/distribution-test/xmlrpc-tests/src/test/it/com/xpn/xwiki/it/xmlrpc/AllTests.java
>     
> platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcApiImpl.java
>     
> platform/core/trunk/xwiki-xmlrpc/xwiki-xmlrpc-client/src/main/java/org/xwiki/xmlrpc/XWikiXmlRpcClient.java
>     
> platform/core/trunk/xwiki-xmlrpc/xwiki-xmlrpc-model/src/main/java/org/xwiki/xmlrpc/XWikiXmlRpcApi.java
> Log:
> XWIKI-4464: Extend the XmlRpc API with new rendering features
> * Apply patch from Florin Ciubotaru:
>    **  New getRenderedContendMethods added to the api(We still need a few 
> votes on devs for these to get commited). Completed the client with the new 
> additions.
>    ** Added integration tests.
>
> Modified: 
> enterprise/trunk/distribution-test/xmlrpc-tests/src/test/it/com/xpn/xwiki/it/xmlrpc/AllTests.java
> ===================================================================
> --- 
> enterprise/trunk/distribution-test/xmlrpc-tests/src/test/it/com/xpn/xwiki/it/xmlrpc/AllTests.java
>  2009-11-26 11:14:42 UTC (rev 25287)
> +++ 
> enterprise/trunk/distribution-test/xmlrpc-tests/src/test/it/com/xpn/xwiki/it/xmlrpc/AllTests.java
>  2009-11-26 15:28:25 UTC (rev 25288)
> @@ -62,6 +62,7 @@
>           addTestCase(suite, org.xwiki.xmlrpc.XWikiClassesTest.class);
>           addTestCase(suite, org.xwiki.xmlrpc.XWikiObjectsTest.class);
>           addTestCase(suite, org.xwiki.xmlrpc.SearchTest.class);
> +        addTestCase(suite, org.xwiki.xmlrpc.RenderingTest.class);
>
>           return new XWikiTestSetup(suite);
>       }
>
> Added: 
> enterprise/trunk/distribution-test/xmlrpc-tests/src/test/it/org/xwiki/xmlrpc/RenderingTest.java
> ===================================================================
> --- 
> enterprise/trunk/distribution-test/xmlrpc-tests/src/test/it/org/xwiki/xmlrpc/RenderingTest.java
>                            (rev 0)
> +++ 
> enterprise/trunk/distribution-test/xmlrpc-tests/src/test/it/org/xwiki/xmlrpc/RenderingTest.java
>    2009-11-26 15:28:25 UTC (rev 25288)
> @@ -0,0 +1,145 @@
> +/*
> + * See the NOTICE file distributed with this work for additional
> + * information regarding copyright ownership.
> + *
> + * This is free software; you can redistribute it and/or modify it
> + * under the terms of the GNU Lesser General Public License as
> + * published by the Free Software Foundation; either version 2.1 of
> + * the License, or (at your option) any later version.
> + *
> + * This software 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
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with this software; if not, write to the Free
> + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
> + * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
> + *
> + */
> +package org.xwiki.xmlrpc;
> +
> +import java.util.List;
> +
> +import junit.framework.Assert;
> +
> +import org.apache.xmlrpc.XmlRpcException;
> +import org.xwiki.xmlrpc.model.XWikiPage;
> +
> +public class RenderingTest extends AbstractXWikiXmlRpcTest
> +{
> +    public void testGetInputSyntaxes() throws XmlRpcException
> +    {
> +        List<String>  syntaxes = rpc.getInputSyntaxes();
> +
> +        TestUtils.banner("TEST: getInputSyntaxes()");

I'd swap the banner setting and the retrieval method, since if the 
method fails, then the banner won't be set. Also, System.out should not 
be used.

> +        System.out.format("%d conversion input syntaxes found:\n", 
> syntaxes.size());
> +        for (String syntaxId : syntaxes) {
> +            System.out.format("%s\n", syntaxId);
> +        }
> +        Assert.assertTrue(syntaxes.size() != 0);
> +    }
> +
> +    public void testGetOutputSyntaxes() throws XmlRpcException
> +    {
> +        List<String>  syntaxes = rpc.getOutputSyntaxes();
> +
> +        TestUtils.banner("TEST: getOutputSyntaxes()");
> +        System.out.format("%d conversion output syntaxes found:\n", 
> syntaxes.size());
> +        for (String syntaxId : syntaxes) {
> +            System.out.format("%s\n", syntaxId);
> +        }
> +        Assert.assertTrue(syntaxes.size() != 0);
> +    }
> +
> +    public void testConvert() throws XmlRpcException
> +    {
> +        String inputXWiki =
> +                "**BoldText**//ItalicText//" + String.format("%n") + 
> "==h2Text==" + String.format("%n") + "normalText";

Why String.format("%n")? Doesn't a simple \n suffice? Or, if the reak 
system endline must be used, for a reason I can't see now, how about 
org.apache.commons.lang.SystemUtils.LINE_SEPARATOR or the raw 
System.getProperty("line.separator")?

> +        String inputXhtml =
> +                
> "<div><p><strong>boldText</strong><br/><em>ItalicText</em></p>" + 
> String.format("%n")
> +                    + "<h2 
> id=\"h2Text\"><span>h2Text</span></h2><p>normalText</p></div>";
> +
> +        TestUtils.banner("TEST: convert()");
> +        System.out.format("\nTesting xwiki/2.0 ->  xhtml/1.0 conversion\n");
> +        System.out.format("Input text:\n%s", inputXWiki);
> +
> +        String outputXhtml = rpc.convert(inputXhtml, "xwiki/2.0", 
> "xhtml/1.0");
> +
> +        System.out.format("\nOutput text:\n%s", outputXhtml);
> +
> +        Assert.assertNotNull(outputXhtml);
> +        Assert.assertFalse(outputXhtml.equals(""));

You could also test some generated markup here.

> +        Assert.assertTrue(outputXhtml.contains("strong"));
> +        Assert.assertTrue(outputXhtml.contains("normalText"));
> +        Assert.assertTrue(outputXhtml.contains("ItalicText"));
> +
> +        System.out.format("\nTesting xhtml/1.0 ->  xwiki/2.0 conversion\n");
> +        System.out.format("Input text:\n%s", inputXhtml);
> +
> +        String outputXWiki = rpc.convert(inputXhtml, "xhtml/1.0", 
> "xwiki/2.0");
> +
> +        System.out.format("\nOutput text:\n%s\n", outputXWiki);
> +
> +        Assert.assertNotNull(outputXWiki);
> +        Assert.assertFalse(outputXhtml.equals(""));
> +        Assert.assertTrue(outputXhtml.contains("normalText"));
> +        Assert.assertTrue(outputXhtml.contains("ItalicText"));
> +    }
> +
> +    public void testGetRenderedContent() throws XmlRpcException
> +    {
> +        String pageContent = "**Text in 
> Bold**{{velocity}}VelocityCode{{/velocity}}";
> +
> +        XWikiPage page = new XWikiPage();
> +        page.setId(TestConstants.TEST_PAGE);
> +        page.setContent(pageContent);
> +        page.setSyntaxId("xwiki/2.0");
> +
> +        TestUtils.banner("TEST: getRenderedContent()");
> +        System.out.format("\nCalling getRenderedContent for page %s\n", 
> page.getId());
> +        System.out.format("\nWiki content is:\n%s\n", pageContent);
> +
> +        rpc.storePage(page);
> +
> +        try {
> +            String renderedContent = rpc.getRenderedContent(page.getId(), 
> "annotatedxhtml/1.0");
> +
> +            System.out.format("\nObtained rendered content:\n%s\n\n", 
> renderedContent);
> +
> +            Assert.assertTrue(renderedContent.contains("Text in Bold"));
> +            Assert.assertTrue(renderedContent.contains("startmacro"));
> +        } finally {
> +            rpc.removePage(page.getId());
> +        }
> +    }
> +
> +    public void testRenderPageContent() throws XmlRpcException
> +    {
> +        String pageContent = "**Text in 
> Bold**{{velocity}}VelocityCode{{/velocity}}";
> +
> +        XWikiPage page = new XWikiPage();
> +        page.setId(TestConstants.TEST_PAGE);
> +        page.setContent(pageContent);
> +        page.setSyntaxId("xwiki/2.0");
> +
> +        TestUtils.banner("TEST: renderPageContent");
> +        System.out.format("\nCalling renderPageContent with context as page 
> %s\n", TestConstants.TEST_PAGE);
> +        System.out.format("\nWiki content is:\n%s\n", pageContent);
> +
> +        rpc.storePage(page);
> +
> +        try {
> +            String renderedContent =
> +                    rpc.renderPageContent(page.getId(), page.getContent(), 
> page.getSyntaxId(), "annotatedxhtml/1.0");
> +
> +            System.out.format("\nObtained rendered content:\n%s\n\n", 
> renderedContent);
> +
> +            Assert.assertTrue(renderedContent.contains("Text in Bold"));
> +            Assert.assertTrue(renderedContent.contains("startmacro"));
> +        } finally {
> +            rpc.removePage(page.getId());
> +        }
> +    }
> +}
>
>
> Modified: 
> platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcApiImpl.java
> ===================================================================
> --- 
> platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcApiImpl.java
>  2009-11-26 11:14:42 UTC (rev 25287)
> +++ 
> platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/xmlrpc/XWikiXmlRpcApiImpl.java
>  2009-11-26 15:28:25 UTC (rev 25288)
> @@ -1455,4 +1455,47 @@
>           }
>           return syntaxes;
>       }
> +
> +    /**
> +     * Renders a text in the context of a wiki page.
> +     *
> +     * @param token The authentication token.
> +     * @param pageId The id of the page.
> +     * @param content The context to be rendered.
> +     * @param sourceSyntaxId The syntax of the content.
> +     * @param targetSyntaxId The target syntax of the rendered content
> +     * @return The rendered content.
> +     * @throws Exception If a invalid token is provided, an unsupported 
> syntax id is given or the rendering fails.
> +     */

Make sure a user can't use the programming rights of a page to interpret 
his own text.

> +    public String renderPageContent(String token, String pageId, String 
> content, String sourceSyntaxId,
> +        String targetSyntaxId) throws Exception
> +    {
> +        XWikiXmlRpcUser user = XWikiUtils.checkToken(token, 
> this.xwikiContext);
> +        LOG.debug(String.format("User %s has called renderPageContent()", 
> user.getName()));
> +
> +        Document doc = XWikiUtils.getDocument(this.xwikiApi, pageId, true);
> +
> +        return doc.getRenderedContent(content, sourceSyntaxId, 
> targetSyntaxId);
> +    }
> +
> +    /**
> +     * Gets the rendered content of an existing document.
> +     *
> +     * @param token The authentication token.
> +     * @param pageId The id of the page.
> +     * @param syntaxId The target syntax of the rendered content
> +     * @return The rendered content
> +     * @throws Exception If a invalid token is provided, an unsupported 
> syntax id is given or the rendering fails.
> +     */
> +    public String getRenderedContent(String token, String pageId, String 
> syntaxId) throws Exception
> +    {
> +        XWikiXmlRpcUser user = XWikiUtils.checkToken(token, 
> this.xwikiContext);
> +        LOG.debug(String.format("User %s has called getRenderedContent()", 
> user.getName()));
> +
> +        Document doc = XWikiUtils.getDocument(this.xwikiApi, pageId, true);
> +        SyntaxFactory syntaxFactory = 
> Utils.getComponent(SyntaxFactory.class);
> +        Syntax syntax = syntaxFactory.createSyntaxFromIdString(syntaxId);
> +
> +        return doc.getRenderedContent(syntax);
> +    }
>   }
>
-- 
Sergiu Dumitriu
http://purl.org/net/sergiu/
_______________________________________________
devs mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/devs

Reply via email to