use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.
the class StandardExtendedURLResourceReferenceSerializerTest method serializeWhenNoMatchingSerializer.
@Test
public void serializeWhenNoMatchingSerializer() throws Exception {
TestResourceReference resource = new TestResourceReference();
ComponentManager componentManager = this.mocker.getInstance(ComponentManager.class, "context");
when(componentManager.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, TestResourceReference.class, ExtendedURL.class), "standard")).thenThrow(new ComponentLookupException("error"));
when(componentManager.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, TestResourceReference.class, ExtendedURL.class))).thenThrow(new ComponentLookupException("error"));
try {
this.mocker.getComponentUnderTest().serialize(resource);
fail("Should have thrown an exception here");
} catch (UnsupportedResourceReferenceException expected) {
assertEquals("Failed to find serializer for Resource Reference [type = [test], parameters = []] and " + "URL format [standard]", expected.getMessage());
}
}
use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.
the class DefaultEditorManager method getEditor.
@Override
public <D> Editor<D> getEditor(Type dataType, String hint) {
DefaultParameterizedType editorType = new DefaultParameterizedType(null, Editor.class, dataType);
ComponentManager componentManager = this.componentManagerProvider.get();
if (componentManager.hasComponent(editorType, hint)) {
try {
return componentManager.getInstance(editorType, hint);
} catch (ComponentLookupException e) {
throw new RuntimeException(String.format("Failed to look up the [%s] editor with hint [%s]", dataType.getTypeName(), hint), e);
}
} else {
// No such editor component found.
return null;
}
}
use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.
the class UsersMimeMessageIteratorTest method createMessage.
@Test
public void createMessage() throws Exception {
DocumentReference userReference1 = new DocumentReference("xwiki", "XWiki", "JohnDoe");
DocumentReference userReference2 = new DocumentReference("xwiki", "XWiki", "JaneDoe");
DocumentReference userReference3 = new DocumentReference("xwiki", "XWiki", "JonnieDoe");
List<DocumentReference> userReferences = Arrays.asList(userReference1, userReference2, userReference3);
Session session = Session.getInstance(new Properties());
MimeMessageFactory factory = new MimeMessageFactory() {
@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(userReference1), any(DocumentReference.class), eq("email"))).thenReturn("john@doe.com");
when(accessBridge.getProperty(eq(userReference2), any(DocumentReference.class), eq("email"))).thenReturn("jane@doe.com");
when(accessBridge.getProperty(eq(userReference3), any(DocumentReference.class), eq("email"))).thenReturn("jannie@doe.com");
ComponentManager componentManager = mock(ComponentManager.class);
when(componentManager.getInstance(eq(DocumentAccessBridge.class))).thenReturn(accessBridge);
UsersMimeMessageIterator iterator = new UsersMimeMessageIterator(userReferences, 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.component.manager.ComponentManager in project xwiki-platform by xwiki.
the class UsersAndGroupsMimeMessageFactoryTest method createMessageWhenNotExistingMimeMessageFactory.
@Test
public void createMessageWhenNotExistingMimeMessageFactory() throws Exception {
Session session = Session.getInstance(new Properties());
Map<String, Object> parameters = new HashMap<>();
parameters.put("hint", "factoryHint");
parameters.put("source", "factoryHint");
Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
when(componentManagerProvider.get()).thenReturn(this.mocker);
try {
this.mocker.getComponentUnderTest().createMessage(Collections.<String, Object>emptyMap(), parameters);
fail("Should have thrown an exception");
} catch (MessagingException expected) {
assertEquals("Failed to find a [MimeMessageFactory<MimeMessage>] for hint [factoryHint]", expected.getMessage());
}
}
use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.
the class UsersAndGroupsMimeMessageFactoryTest method createMessage.
@Test
public void createMessage() throws Exception {
DocumentReference userReference = new DocumentReference("userwiki", "userspace", "userpage");
Map<String, Object> source = Collections.<String, Object>singletonMap("users", Collections.singletonList(userReference));
Map<String, Object> parameters = new HashMap<>();
parameters.put("hint", "template");
parameters.put("source", new DocumentReference("templatewiki", "templatespace", "templatepage"));
Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
when(componentManagerProvider.get()).thenReturn(this.mocker);
this.mocker.registerMockComponent(new DefaultParameterizedType(null, MimeMessageFactory.class, MimeMessage.class), "template");
// Setup XWikiContext since this is required internally by the iterator constructor
Execution execution = this.mocker.registerMockComponent(Execution.class);
XWikiContext xwikiContext = mock(XWikiContext.class);
ExecutionContext executionContext = new ExecutionContext();
executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xwikiContext);
when(execution.getContext()).thenReturn(executionContext);
Iterator<MimeMessage> iterator = this.mocker.getComponentUnderTest().createMessage(source, parameters);
assertNotNull(iterator);
}
Aggregations