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());
}
}
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());
}
}
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;
}
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);
}
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());
}
}
Aggregations