Search in sources :

Example 16 with MacroExecutionException

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

the class IncludeMacroTest method testIncludeMacroWithRecursiveIncludeContextNew.

@Test
public void testIncludeMacroWithRecursiveIncludeContextNew() throws Exception {
    final DocumentDisplayer mockDocumentDisplayer = getMockery().mock(DocumentDisplayer.class);
    this.includeMacro.setDocumentAccessBridge(mockSetup.bridge);
    this.includeMacro.setDocumentDisplayer(mockDocumentDisplayer);
    final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
    final IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("wiki:space.page");
    parameters.setContext(Context.NEW);
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentReferenceResolver).resolve("wiki:space.page", macroContext.getCurrentMacroBlock());
            will(returnValue(new DocumentReference("wiki", "space", "page")));
            allowing(mockSetup.bridge).isDocumentViewable(with(any(DocumentReference.class)));
            will(returnValue(true));
            allowing(mockSetup.bridge).getDocumentInstance(with(any(DocumentReference.class)));
            will(returnValue(null));
            allowing(mockDocumentDisplayer).display(with(same((DocumentModelBridge) null)), with(any(DocumentDisplayerParameters.class)));
            will(new CustomAction("recursively call the include macro again") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    try {
                        includeMacro.execute(parameters, null, macroContext);
                    } catch (Exception expected) {
                        if (expected.getMessage().contains("Found recursive inclusion")) {
                            throw new ExpectedRecursiveInclusionException();
                        }
                    }
                    return true;
                }
            });
        }
    });
    try {
        this.includeMacro.execute(parameters, null, macroContext);
        Assert.fail("The include macro hasn't checked the recursive inclusion");
    } catch (MacroExecutionException expected) {
        if (!(expected.getCause() instanceof ExpectedRecursiveInclusionException)) {
            throw expected;
        }
    }
}
Also used : Expectations(org.jmock.Expectations) DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) DocumentDisplayer(org.xwiki.display.internal.DocumentDisplayer) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) 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) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Test(org.junit.Test)

Example 17 with MacroExecutionException

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

the class IncludeMacroTest method testIncludeMacroWithRecursiveIncludeContextCurrent.

@Test
public void testIncludeMacroWithRecursiveIncludeContextCurrent() throws Exception {
    this.includeMacro.setDocumentAccessBridge(mockSetup.bridge);
    final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
    // Add an Include Macro MarkerBlock as a parent of the include Macro block since this is what would have
    // happened if an Include macro is included in another Include macro.
    final MacroMarkerBlock includeMacroMarker = new MacroMarkerBlock("include", Collections.singletonMap("reference", "space.page"), Collections.<Block>singletonList(macroContext.getCurrentMacroBlock()), false);
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentReferenceResolver).resolve("wiki:space.page", macroContext.getCurrentMacroBlock());
            will(returnValue(new DocumentReference("wiki", "space", "page")));
            allowing(mockDocumentReferenceResolver).resolve("space.page", includeMacroMarker);
            will(returnValue(new DocumentReference("wiki", "space", "page")));
        }
    });
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("wiki:space.page");
    parameters.setContext(Context.CURRENT);
    try {
        this.includeMacro.execute(parameters, null, macroContext);
        Assert.fail("The include macro hasn't checked the recursive inclusion");
    } catch (MacroExecutionException expected) {
        if (!expected.getMessage().startsWith("Found recursive inclusion")) {
            throw expected;
        }
    }
}
Also used : Expectations(org.jmock.Expectations) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 18 with MacroExecutionException

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

the class UserAvatarMacro method execute.

@Override
public List<Block> execute(UserAvatarMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    DocumentReference userReference = this.currentDocumentReferenceResolver.resolve(parameters.getUsername(), new EntityReference(USER_SPACE, EntityType.SPACE));
    // Find the avatar attachment name or null if not defined or an error happened when locating it
    String fileName = null;
    if (this.documentAccessBridge.exists(userReference)) {
        Object avatarProperty = this.documentAccessBridge.getProperty(userReference, new DocumentReference(userReference.getWikiReference().getName(), USER_SPACE, "XWikiUsers"), "avatar");
        if (avatarProperty != null) {
            fileName = avatarProperty.toString();
        }
    } else {
        throw new MacroExecutionException("User [" + this.compactWikiEntityReferenceSerializer.serialize(userReference) + "] is not registered in this wiki");
    }
    // Initialize with the default avatar.
    ResourceReference imageReference = new ResourceReference(this.skinAccessBridge.getSkinFile("icons/xwiki/noavatar.png"), ResourceType.URL);
    // Try to use the configured avatar.
    if (!StringUtils.isBlank(fileName)) {
        AttachmentReference attachmentReference = new AttachmentReference(fileName, userReference);
        // Check if the configured avatar file actually exists.
        try {
            if (documentAccessBridge.getAttachmentVersion(attachmentReference) != null) {
                // Use it.
                imageReference = new ResourceReference(this.compactWikiEntityReferenceSerializer.serialize(attachmentReference), ResourceType.ATTACHMENT);
            }
        } catch (Exception e) {
            // Log and fallback on default.
            logger.warn("Failed to get the avatar for user [{}]: [{}]. Using default.", this.compactWikiEntityReferenceSerializer.serialize(userReference), ExceptionUtils.getRootCauseMessage(e));
        }
    }
    ImageBlock imageBlock = new ImageBlock(imageReference, false);
    imageBlock.setParameter("alt", "Picture of " + userReference.getName());
    imageBlock.setParameter("title", userReference.getName());
    if (parameters.getWidth() != null) {
        imageBlock.setParameter("width", String.valueOf(parameters.getWidth()));
    }
    if (parameters.getHeight() != null) {
        imageBlock.setParameter("height", String.valueOf(parameters.getHeight()));
    }
    List<Block> result = Collections.singletonList(imageBlock);
    return context.isInline() ? result : Collections.singletonList(new GroupBlock(result));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ImageBlock(org.xwiki.rendering.block.ImageBlock) EntityReference(org.xwiki.model.reference.EntityReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) GroupBlock(org.xwiki.rendering.block.GroupBlock) Block(org.xwiki.rendering.block.Block) ImageBlock(org.xwiki.rendering.block.ImageBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 19 with MacroExecutionException

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

the class TableTimeTableXYDatasetBuilder method setParameters.

@Override
public void setParameters(Map<String, String> parameters) throws MacroExecutionException {
    String timePeriodTypeString = parameters.get(TIMEPERIOD_TYPE_PARAM);
    if (timePeriodTypeString != null) {
        this.timePeriodType = TimePeriodType.forName(timePeriodTypeString);
        if (this.timePeriodType == null) {
            throw new MacroExecutionException(String.format("Invalid time period type [%s].", timePeriodTypeString));
        }
    }
    dataset = new TimeTableXYDataset(localeConfiguration.getTimeZone(), localeConfiguration.getLocale());
}
Also used : TimeTableXYDataset(org.jfree.data.time.TimeTableXYDataset) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 20 with MacroExecutionException

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

the class AxisConfigurator method setNumberLimits.

/**
 * Set the limits of a number axis.
 *
 * @param axis The axis.
 * @param index The index of the axis
 * @throws MacroExecutionException if the parameters could not be parsed as numbers.
 */
private void setNumberLimits(ValueAxis axis, int index) throws MacroExecutionException {
    try {
        if (axisLowerLimit[index] != null) {
            Number number = NumberUtils.createNumber(StringUtils.trim(axisLowerLimit[index]));
            axis.setLowerBound(number.doubleValue());
        }
        if (axisUpperLimit[index] != null) {
            Number number = NumberUtils.createNumber(StringUtils.trim(axisUpperLimit[index]));
            axis.setUpperBound(number.doubleValue());
        }
    } catch (NumberFormatException e) {
        throw new MacroExecutionException("Invalid number in axis bound.", e);
    }
}
Also used : MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

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