Search in sources :

Example 26 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class IncludeMacroTest method testIncludeMacroWhenInvalidSectionSpecified.

@Test
public void testIncludeMacroWhenInvalidSectionSpecified() throws Exception {
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("document");
    parameters.setSection("unknown");
    final MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
    final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "document");
    final DocumentModelBridge mockDocument = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            oneOf(mockDocumentReferenceResolver).resolve("document", macroContext.getCurrentMacroBlock());
            will(returnValue(resolvedReference));
            oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
            will(returnValue(true));
            oneOf(mockSetup.bridge).getDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            allowing(mockSetup.bridge).getTranslatedDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            oneOf(mockSetup.bridge).getCurrentDocumentReference();
            will(returnValue(new DocumentReference("wiki", "Space", "IncludingPage")));
            oneOf(mockDocument).getSyntax();
            will(returnValue(Syntax.XWIKI_2_0));
            oneOf(mockDocument).getXDOM();
            will(returnValue(getXDOM("content")));
            allowing(mockDocument).getDocumentReference();
            will(returnValue(resolvedReference));
            allowing(mockDocument).getRealLanguage();
            will(returnValue(""));
        }
    });
    try {
        this.includeMacro.execute(parameters, null, macroContext);
        Assert.fail("Should have raised an exception");
    } catch (MacroExecutionException expected) {
        Assert.assertEquals("Cannot find section [unknown] in document [wiki:space.document]", expected.getMessage());
    }
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 27 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class IncludeMacroTest method testIncludeMacroWithNoDocumentSpecified.

@Test
public void testIncludeMacroWithNoDocumentSpecified() throws Exception {
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    try {
        this.includeMacro.execute(parameters, null, createMacroTransformationContext("whatever", false));
        Assert.fail("An exception should have been thrown");
    } catch (MacroExecutionException expected) {
        Assert.assertEquals("You must specify a 'reference' parameter pointing to the entity to include.", expected.getMessage());
    }
}
Also used : IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Test(org.junit.Test)

Example 28 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class DefaultRomeFeedFactory method createFeed.

@Override
public SyndFeed createFeed(RssMacroParameters parameters) throws MacroExecutionException {
    if (StringUtils.isEmpty(parameters.getFeed())) {
        throw new MacroExecutionException("The required 'feed' parameter is missing");
    }
    SyndFeedInput syndFeedInput = new SyndFeedInput();
    SyndFeed feed;
    try {
        if (StringUtils.startsWith(parameters.getFeed().toLowerCase(), "https")) {
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection) parameters.getFeedURL().openConnection();
            httpsURLConnection.setConnectTimeout(TIMEOUT_MILLISECONDS);
            httpsURLConnection.setRequestProperty(USER_AGENT_HEADER, USER_AGENT);
            feed = syndFeedInput.build(new XmlReader(httpsURLConnection.getInputStream(), true, parameters.getEncoding()));
        } else {
            URLConnection httpURLConnection = parameters.getFeedURL().openConnection();
            httpURLConnection.setConnectTimeout(TIMEOUT_MILLISECONDS);
            httpURLConnection.setRequestProperty(USER_AGENT_HEADER, USER_AGENT);
            feed = syndFeedInput.build(new XmlReader(httpURLConnection.getInputStream(), true, parameters.getEncoding()));
        }
    } catch (SocketTimeoutException ex) {
        throw new MacroExecutionException(MessageFormat.format("Connection timeout when trying to reach [{0}]", parameters.getFeedURL()));
    } catch (Exception ex) {
        throw new MacroExecutionException(MessageFormat.format("Error processing [{0}] : {1}", parameters.getFeedURL(), ex.getMessage()), ex);
    }
    if (feed == null) {
        throw new MacroExecutionException(MessageFormat.format("No feed found at [{0}]", parameters.getFeedURL()));
    }
    return feed;
}
Also used : SyndFeed(com.sun.syndication.feed.synd.SyndFeed) SocketTimeoutException(java.net.SocketTimeoutException) SyndFeedInput(com.sun.syndication.io.SyndFeedInput) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) XmlReader(com.sun.syndication.io.XmlReader) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URLConnection(java.net.URLConnection) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) SocketTimeoutException(java.net.SocketTimeoutException)

Example 29 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class RssMacroTest method testInvalidDocument.

/**
 * Tests the macro's behavior when the server hosting the feeds doesn't respond.
 */
@Test(expected = MacroExecutionException.class)
public void testInvalidDocument() throws Exception {
    // Use a Mock SyndFeedInput to control what it returns for the test.
    Mockery context = new Mockery();
    final RomeFeedFactory mockFactory = context.mock(RomeFeedFactory.class);
    final RssMacroParameters parameters = new RssMacroParameters();
    context.checking(new Expectations() {

        {
            oneOf(mockFactory).createFeed(with(same(parameters)));
            will(throwException(new MacroExecutionException("Error")));
        }
    });
    this.macro.setFeedFactory(mockFactory);
    // Dummy URL since a feed URL is mandatory
    parameters.setFeed("http://www.xwiki.org");
    this.macro.execute(parameters, null, null);
}
Also used : Expectations(org.jmock.Expectations) RssMacroParameters(org.xwiki.rendering.macro.rss.RssMacroParameters) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Mockery(org.jmock.Mockery) Test(org.junit.Test)

Example 30 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class RssMacroTest method testRequiredParameterMissing.

/**
 * Tests whether the macro throws the appropriate exception
 * in cases where the required 'feed' parameter is missing.
 */
@Test
public void testRequiredParameterMissing() throws Exception {
    try {
        this.macro.execute(new RssMacroParameters(), null, null);
        Assert.fail("Should have thrown an exception");
    } catch (MacroExecutionException expected) {
        Assert.assertEquals("The required 'feed' parameter is missing", expected.getMessage());
    }
}
Also used : RssMacroParameters(org.xwiki.rendering.macro.rss.RssMacroParameters) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Test(org.junit.Test)

Aggregations

MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)48 Test (org.junit.Test)12 Block (org.xwiki.rendering.block.Block)12 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)10 DocumentReference (org.xwiki.model.reference.DocumentReference)9 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)7 MacroBlock (org.xwiki.rendering.block.MacroBlock)7 XDOM (org.xwiki.rendering.block.XDOM)7 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)6 Expectations (org.jmock.Expectations)5 DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)5 StringReader (java.io.StringReader)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)4 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)4 IncludeMacroParameters (org.xwiki.rendering.macro.include.IncludeMacroParameters)4 HashMap (java.util.HashMap)3 AttachmentReference (org.xwiki.model.reference.AttachmentReference)3 GroupBlock (org.xwiki.rendering.block.GroupBlock)3 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)3 TableBlock (org.xwiki.rendering.block.TableBlock)3