use of org.xwiki.model.reference.DocumentReferenceResolver in project xwiki-platform by xwiki.
the class UntypedRecordableEventDescriptorComponentBuilderTest method setUp.
@Before
public void setUp() throws Exception {
queryManager = mocker.registerMockComponent(QueryManager.class);
modelBridge = mocker.registerMockComponent(ModelBridge.class);
documentReferenceResolver = mocker.registerMockComponent(DocumentReferenceResolver.class);
authorizationManager = mocker.registerMockComponent(AuthorizationManager.class);
Query query = mock(Query.class);
when(queryManager.createQuery(any(), any())).thenReturn(query);
when(query.execute()).thenReturn(Arrays.asList("e1", "e2", "e3"));
event1 = mock(DocumentReference.class);
event2 = mock(DocumentReference.class);
event3 = mock(DocumentReference.class);
when(this.documentReferenceResolver.resolve("e1")).thenReturn(event1);
when(this.documentReferenceResolver.resolve("e2")).thenReturn(event2);
when(this.documentReferenceResolver.resolve("e3")).thenReturn(event3);
}
use of org.xwiki.model.reference.DocumentReferenceResolver in project xwiki-platform by xwiki.
the class GroupMimeMessageIteratorTest method createMessage.
@Test
public void createMessage() throws Exception {
DocumentReference groupReference = new DocumentReference("xwiki", "XWiki", "Marketing");
DocumentReference userReference1 = new DocumentReference("xwiki", "XWiki", "JohnDoe");
DocumentReference userReference2 = new DocumentReference("xwiki", "XWiki", "JaneDoe");
DocumentReference userReference3 = new DocumentReference("xwiki", "XWiki", "JonnieDoe");
Session session = Session.getInstance(new Properties());
MimeMessageFactory<MimeMessage> factory = new MimeMessageFactory<MimeMessage>() {
@Override
public MimeMessage createMessage(Object source, Map parameters) throws MessagingException {
return new ExtendedMimeMessage();
}
};
Map<String, Object> parameters = new HashMap<>();
parameters.put("parameters", Collections.EMPTY_MAP);
parameters.put("session", session);
DocumentAccessBridge accessBridge = mock(DocumentAccessBridge.class);
when(accessBridge.getProperty(eq(groupReference), any(), eq(0), eq("member"))).thenReturn("XWiki.JohnDoe");
when(accessBridge.getProperty(eq(groupReference), any(), eq(1), eq("member"))).thenReturn("XWiki.JaneDoe");
when(accessBridge.getProperty(eq(groupReference), any(), eq(2), eq("member"))).thenReturn("XWiki.JonnieDoe");
when(accessBridge.getProperty(eq(userReference1), any(), eq("email"))).thenReturn("john@doe.com");
when(accessBridge.getProperty(eq(userReference2), any(), eq("email"))).thenReturn("jane@doe.com");
when(accessBridge.getProperty(eq(userReference3), any(), eq("email"))).thenReturn("jannie@doe.com");
Execution execution = mock(Execution.class);
ExecutionContext executionContext = mock(ExecutionContext.class);
when(execution.getContext()).thenReturn(executionContext);
XWikiContext xwikiContext = mock(XWikiContext.class);
when(executionContext.getProperty("xwikicontext")).thenReturn(xwikiContext);
XWiki xwiki = mock(XWiki.class);
when(xwikiContext.getWiki()).thenReturn(xwiki);
XWikiDocument document = mock(XWikiDocument.class);
when(xwiki.getDocument(eq(groupReference), eq(xwikiContext))).thenReturn(document);
BaseObject object = mock(BaseObject.class);
when(document.getXObjects(any(EntityReference.class))).thenReturn(Arrays.asList(object, object, object));
DocumentReferenceResolver<String> resolver = (DocumentReferenceResolver<String>) mock(DocumentReferenceResolver.class);
when(resolver.resolve("XWiki.JohnDoe")).thenReturn(userReference1);
when(resolver.resolve("XWiki.JaneDoe")).thenReturn(userReference2);
when(resolver.resolve("XWiki.JonnieDoe")).thenReturn(userReference3);
ComponentManager componentManager = mock(ComponentManager.class);
when(componentManager.getInstance(eq(DocumentAccessBridge.class))).thenReturn(accessBridge);
when(componentManager.getInstance(eq(Execution.class))).thenReturn(execution);
when(componentManager.getInstance(eq(DocumentReferenceResolver.TYPE_STRING), eq("current"))).thenReturn(resolver);
GroupMimeMessageIterator iterator = new GroupMimeMessageIterator(groupReference, factory, parameters, componentManager);
assertTrue(iterator.hasNext());
MimeMessage message1 = iterator.next();
assertArrayEquals(message1.getRecipients(Message.RecipientType.TO), InternetAddress.parse("john@doe.com"));
assertTrue(iterator.hasNext());
MimeMessage message2 = iterator.next();
assertArrayEquals(message2.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jane@doe.com"));
assertTrue(iterator.hasNext());
MimeMessage message3 = iterator.next();
assertArrayEquals(message3.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jannie@doe.com"));
assertFalse(iterator.hasNext());
}
use of org.xwiki.model.reference.DocumentReferenceResolver in project xwiki-platform by xwiki.
the class IntegrationTests method initialize.
@RenderingTestSuite.Initialized
public void initialize(MockitoComponentManager componentManager) throws Exception {
// For performance reasons we mock some components to avoid having to draw all oldcore components
// Macro Reference Resolver
DocumentReferenceResolver<String> macroResolver = componentManager.registerMockComponent(new DefaultParameterizedType(null, DocumentReferenceResolver.class, String.class), "macro");
DocumentReference referencedDocumentReference = new DocumentReference("Wiki", "Space", "Page");
when(macroResolver.resolve(eq("Space.Page"), any(MacroBlock.class))).thenReturn(referencedDocumentReference);
// Document Access Bridge mock
// Simulate the XDOM of the referenced document
DocumentAccessBridge dab = componentManager.registerMockComponent(DocumentAccessBridge.class);
DocumentModelBridge dmb = mock(DocumentModelBridge.class);
when(dab.getTranslatedDocumentInstance(referencedDocumentReference)).thenReturn(dmb);
Parser parser = componentManager.getInstance(Parser.class, "xwiki/2.1");
XDOM xdom = parser.parse(new StringReader("= heading1 =\n==heading2=="));
when(dmb.getXDOM()).thenReturn(xdom);
}
use of org.xwiki.model.reference.DocumentReferenceResolver in project xwiki-platform by xwiki.
the class BaseObjectReference method maybeParseName.
private void maybeParseName() {
if (!this.nameParsed) {
String name = getName();
String classReferenceStr;
String objectNumberStr;
Matcher matcher = NUMBERPATTERN.matcher(name);
if (matcher.find()) {
if (matcher.group(1).length() % 2 == 0) {
classReferenceStr = name.substring(0, matcher.end(1));
objectNumberStr = matcher.group(2);
} else {
classReferenceStr = name;
objectNumberStr = null;
}
} else {
classReferenceStr = name;
objectNumberStr = null;
}
this.xclassReference = Utils.<DocumentReferenceResolver<String>>getComponent(DocumentReferenceResolver.TYPE_STRING).resolve(classReferenceStr, getParent());
if (objectNumberStr != null) {
this.objectNumber = Integer.valueOf(objectNumberStr);
}
// Indicate that the name does not need to be parsed
this.nameParsed = true;
}
}
use of org.xwiki.model.reference.DocumentReferenceResolver in project xwiki-platform by xwiki.
the class ModelScriptService method createDocumentReference.
/**
* Create a Document Reference from a passed wiki, list of spaces and page names, which can be empty strings or null
* in which case they are resolved against the Resolver having the hint passed as parameter. Valid hints are for
* example "default", "current", "currentmixed".
*
* @param wiki the wiki reference name to use (can be empty or null)
* @param spaces the spaces list to use (can be empty or null)
* @param page the page reference name to use (can be empty or null)
* @param hint the hint of the Resolver to use in case any parameter is empty or null
* @return the typed Document Reference object or null if no Resolver with the passed hint could be found
*
* @since 7.2M2
*/
public DocumentReference createDocumentReference(String wiki, List<String> spaces, String page, String hint) {
EntityReference reference = null;
if (!StringUtils.isEmpty(wiki)) {
reference = new EntityReference(wiki, EntityType.WIKI);
}
if (spaces != null && !spaces.isEmpty()) {
for (String space : spaces) {
reference = new EntityReference(space, EntityType.SPACE, reference);
}
}
if (!StringUtils.isEmpty(page)) {
reference = new EntityReference(page, EntityType.DOCUMENT, reference);
}
DocumentReference documentReference;
try {
DocumentReferenceResolver<EntityReference> resolver = this.componentManager.getInstance(DocumentReferenceResolver.TYPE_REFERENCE, hint);
documentReference = resolver.resolve(reference);
} catch (ComponentLookupException e) {
try {
// Ensure backward compatibility with older scripts that use hints like "default/reference" because at
// the time they were written we didn't have support for generic types in component role.
DocumentReferenceResolver<EntityReference> drr = this.componentManager.getInstance(DocumentReferenceResolver.class, hint);
documentReference = drr.resolve(reference);
this.logger.warn("Deprecated usage of DocumentReferenceResolver with hint [{}]. " + "Please consider using a DocumentReferenceResolver that takes into account generic types.", hint);
} catch (ComponentLookupException ex) {
documentReference = null;
}
}
return documentReference;
}
Aggregations