Skip to content
Snippets Groups Projects
Commit 990a3fcd authored by Thomas Mortagne's avatar Thomas Mortagne
Browse files

XWIKI-20462: Add support for content coming from a different source in the code macro

* add tests

(cherry picked from commit 352fd923)
parent 5460158f
No related branches found
No related tags found
No related merge requests found
......@@ -49,5 +49,20 @@
<artifactId>xwiki-platform-mail-general</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-platform-test-oldcore</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-platform-rendering-transformation-macro</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
/*
* 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.rendering.internal.macro.code.source;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.rendering.block.MacroBlock;
import org.xwiki.rendering.block.XDOM;
import org.xwiki.rendering.internal.transformation.macro.CurrentMacroEntityReferenceResolver;
import org.xwiki.rendering.macro.MacroExecutionException;
import org.xwiki.rendering.macro.code.source.CodeMacroSource;
import org.xwiki.rendering.macro.code.source.CodeMacroSourceReference;
import org.xwiki.rendering.transformation.MacroTransformationContext;
import org.xwiki.test.annotation.ComponentList;
import org.xwiki.test.junit5.mockito.InjectMockComponents;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.test.MockitoOldcore;
import com.xpn.xwiki.test.junit5.mockito.InjectMockitoOldcore;
import com.xpn.xwiki.test.junit5.mockito.OldcoreTest;
import com.xpn.xwiki.test.reference.ReferenceComponentList;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Validate the various entity macro source factories.
*
* @version $Id$
*/
@OldcoreTest
@ReferenceComponentList
@ComponentList(value = {CurrentMacroEntityReferenceResolver.class, DocumentAttachmentCodeMacroSourceLoader.class,
DocumentCodeMacroSourceLoader.class, DocumentObjectPropertyCodeMacroSourceLoader.class})
class EntityCodeMacroSourceFactoryTest
{
@InjectMockitoOldcore
private MockitoOldcore oldcore;
@InjectMockComponents
private DocumentCodeMacroSourceFactory factory;
private MacroTransformationContext macroContext;
@BeforeEach
public void beforeEach()
{
this.macroContext = new MacroTransformationContext();
MacroBlock macro = new MacroBlock("code", Map.of(), false);
this.macroContext.setCurrentMacroBlock(macro);
XDOM xdom = new XDOM(List.of(macro));
this.macroContext.setXDOM(xdom);
}
private void assertCodeMacroSource(CodeMacroSourceReference reference, String expectedContent,
String expectedLanguage) throws MacroExecutionException
{
assertEquals(new CodeMacroSource(reference, expectedContent, expectedLanguage),
this.factory.getContent(reference, this.macroContext));
}
@Test
void getContentDocument() throws MacroExecutionException, XWikiException
{
assertCodeMacroSource(new CodeMacroSourceReference("document", "wiki:Space.Document"), "", null);
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Document");
XWikiDocument document =
this.oldcore.getSpyXWiki().getDocument(documentReference, this.oldcore.getXWikiContext());
document.setContent("document content");
this.oldcore.getSpyXWiki().saveDocument(document, this.oldcore.getXWikiContext());
assertCodeMacroSource(new CodeMacroSourceReference("document", "wiki:Space.Document"), "document content",
null);
}
}
......@@ -19,7 +19,11 @@
*/
package org.xwiki.rendering.macro.code.source;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.xwiki.stability.Unstable;
import org.xwiki.text.XWikiToStringBuilder;
/**
* A content to use in a code macro.
......@@ -72,4 +76,52 @@ public String getLanguage()
{
return this.language;
}
@Override
public String toString()
{
ToStringBuilder builder = new XWikiToStringBuilder(this);
builder.append("reference", getReference());
builder.append("language", getLanguage());
builder.append("content", getContent());
return builder.toString();
}
@Override
public boolean equals(Object obj)
{
if (obj != null) {
if (obj == this) {
return true;
}
if (obj instanceof CodeMacroSource) {
CodeMacroSource otherSource = (CodeMacroSource) obj;
EqualsBuilder builder = new EqualsBuilder();
builder.append(getReference(), otherSource.getReference());
builder.append(getLanguage(), otherSource.getLanguage());
builder.append(getContent(), otherSource.getContent());
return builder.isEquals();
}
}
return false;
}
@Override
public int hashCode()
{
HashCodeBuilder builder = new HashCodeBuilder();
builder.append(getReference());
builder.append(getLanguage());
builder.append(getReference());
return builder.build();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment