use of org.xwiki.rendering.block.WordBlock in project xwiki-platform by xwiki.
the class DefaultDocumentSplitterTest method split.
@Test
public void split() throws Exception {
SplittingCriterion splittingCriterion = mock(SplittingCriterion.class);
when(splittingCriterion.shouldSplit(any(Block.class), anyInt())).thenReturn(true, false, false, true);
when(splittingCriterion.shouldIterate(any(SectionBlock.class), anyInt())).thenReturn(true, false, false, false);
NamingCriterion namingCriterion = mock(NamingCriterion.class);
when(namingCriterion.getDocumentName(any(XDOM.class))).thenReturn("Child1", "Child2");
// = Chapter 1 =
// Once upon a time..
// == Section 1.1 ==
// In a kingdom far away..
HeaderBlock header = new HeaderBlock(Arrays.<Block>asList(new WordBlock("Section 1.1")), HeaderLevel.LEVEL2);
ParagraphBlock paragraph = new ParagraphBlock(Arrays.<Block>asList(new WordBlock("In a kingdom far away..")));
SectionBlock subSection = new SectionBlock(Arrays.<Block>asList(header, paragraph));
header = new HeaderBlock(Arrays.<Block>asList(new WordBlock("Chapter 1")), HeaderLevel.LEVEL1);
paragraph = new ParagraphBlock(Arrays.<Block>asList(new WordBlock("Once upon a time..")));
SectionBlock section = new SectionBlock(Arrays.<Block>asList(header, paragraph, subSection));
XDOM xdom = new XDOM(Arrays.<Block>asList(section));
WikiDocument document = new WikiDocument("Space.Page", xdom, null);
List<WikiDocument> result = mocker.getComponentUnderTest().split(document, splittingCriterion, namingCriterion);
assertEquals(3, result.size());
assertSame(document, result.get(0));
assertEquals("Child1", result.get(1).getFullName());
assertSame(document, result.get(1).getParent());
assertEquals("Child2", result.get(2).getFullName());
assertSame(result.get(1), result.get(2).getParent());
ClassBlockMatcher headerMatcher = new ClassBlockMatcher(HeaderBlock.class);
assertTrue(document.getXdom().<LinkBlock>getBlocks(headerMatcher, Axes.DESCENDANT).isEmpty());
assertEquals(1, result.get(1).getXdom().<LinkBlock>getBlocks(headerMatcher, Axes.DESCENDANT).size());
assertEquals(1, result.get(2).getXdom().<LinkBlock>getBlocks(headerMatcher, Axes.DESCENDANT).size());
}
use of org.xwiki.rendering.block.WordBlock in project xwiki-platform by xwiki.
the class DefaultDocumentSplitter method createLink.
/**
* Creates a {@link LinkBlock} suitable to be placed in the parent document.
*
* @param block the {@link Block} that has just been split into a separate document.
* @param target name of the target wiki document.
* @return a {@link LinkBlock} representing the link from the parent document to new document.
*/
private LinkBlock createLink(Block block, String target) {
Block firstBlock = block.getChildren().get(0);
if (firstBlock instanceof HeaderBlock) {
DocumentResourceReference reference = new DocumentResourceReference(target);
// Clone the header block and remove any unwanted stuff
Block clonedHeaderBlock = firstBlock.clone(new BlockFilter() {
@Override
public List<Block> filter(Block block) {
List<Block> blocks = new ArrayList<Block>();
if (block instanceof WordBlock || block instanceof SpaceBlock || block instanceof SpecialSymbolBlock) {
blocks.add(block);
}
return blocks;
}
});
return new LinkBlock(clonedHeaderBlock.getChildren(), reference, false);
} else if (firstBlock instanceof SectionBlock) {
return createLink(firstBlock, target);
} else {
throw new IllegalArgumentException("A SectionBlock should either begin with a HeaderBlock or another SectionBlock.");
}
}
use of org.xwiki.rendering.block.WordBlock in project xwiki-platform by xwiki.
the class WikiUIExtensionTest method createWikiUIExtension.
@Test
public void createWikiUIExtension() {
WikiUIExtension wikiUIX = new WikiUIExtension("roleHint", "id", "epId", objectReference, AUTHOR_REFERENCE, null);
wikiUIX.setScope(WikiComponentScope.WIKI);
Assert.assertEquals("roleHint", wikiUIX.getRoleHint());
Assert.assertEquals("id", wikiUIX.getId());
Assert.assertEquals("epId", wikiUIX.getExtensionPointId());
Assert.assertEquals(AUTHOR_REFERENCE, wikiUIX.getAuthorReference());
Assert.assertEquals(UIExtension.class, wikiUIX.getRoleType());
Assert.assertEquals(WikiComponentScope.WIKI, wikiUIX.getScope());
Assert.assertEquals(new WordBlock(""), wikiUIX.execute());
Assert.assertEquals(MapUtils.EMPTY_MAP, wikiUIX.getParameters());
}
use of org.xwiki.rendering.block.WordBlock in project xwiki-platform by xwiki.
the class LocalizationScriptServiceTest method setUp.
@Before
public void setUp() throws Exception {
componentManager = mock(ComponentManager.class);
Provider<ComponentManager> componentManagerProvider = mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
when(componentManagerProvider.get()).thenReturn(componentManager);
renderer = mock(BlockRenderer.class, Syntax.PLAIN_1_0.toIdString());
when(componentManager.getInstance(BlockRenderer.class, Syntax.PLAIN_1_0.toIdString())).thenReturn(renderer);
localizationContext = mocker.getInstance(LocalizationContext.class);
localizationManager = mocker.getInstance(LocalizationManager.class);
localizationScriptService = (LocalizationScriptService) mocker.getComponentUnderTest();
translation = mock(Translation.class);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
WikiPrinter printer = (WikiPrinter) invocation.getArguments()[1];
printer.print("print result");
return null;
}
}).when(renderer).render(eq(new WordBlock("message")), any(WikiPrinter.class));
when(translation.render(Locale.ROOT, ArrayUtils.EMPTY_OBJECT_ARRAY)).thenReturn(new WordBlock("message"));
when(localizationManager.getTranslation("key", Locale.ROOT)).thenReturn(translation);
when(localizationContext.getCurrentLocale()).thenReturn(Locale.ROOT);
environment = mocker.getInstance(Environment.class);
}
Aggregations