use of org.xwiki.model.reference.LocalDocumentReference in project xwiki-platform by xwiki.
the class WikiUserFromXEMMigrationTest method upgradeWorkspace.
@Test
public void upgradeWorkspace() throws Exception {
// Mocks about the descriptor
when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("workspace");
XWikiDocument oldDescriptorDocument = mock(XWikiDocument.class);
when(xwiki.getDocument(eq(new DocumentReference("mainWiki", XWiki.SYSTEM_SPACE, "XWikiServerWorkspace")), any(XWikiContext.class))).thenReturn(oldDescriptorDocument);
// Mocks about the old workspace object
BaseObject oldObject = mock(BaseObject.class);
when(oldDescriptorDocument.getXObject(eq(new DocumentReference("mainWiki", "WorkspaceManager", "WorkspaceClass")))).thenReturn(oldObject);
when(oldObject.getStringValue("membershipType")).thenReturn("request");
// Mocks about candidacies
DocumentReference memberGroupRef = new DocumentReference("workspace", XWiki.SYSTEM_SPACE, "XWikiAllGroup");
XWikiDocument memberGroupDoc = mock(XWikiDocument.class);
when(xwiki.getDocument(eq(memberGroupRef), any(XWikiContext.class))).thenReturn(memberGroupDoc);
DocumentReference candidacyOldClass = new DocumentReference("workspace", "XWiki", "WorkspaceCandidateMemberClass");
List<BaseObject> oldCandidacies = new ArrayList<>();
BaseObject oldCandidacy = mock(BaseObject.class);
oldCandidacies.add(oldCandidacy);
when(memberGroupDoc.getXObjects(eq(candidacyOldClass))).thenReturn(oldCandidacies);
LocalDocumentReference newCandidacyClassRef = WikiCandidateMemberClassInitializer.REFERENCE;
BaseObject newCandidacyObject = mock(BaseObject.class);
when(memberGroupDoc.newXObject(eq(newCandidacyClassRef), any(XWikiContext.class))).thenReturn(newCandidacyObject);
when(oldCandidacy.getStringValue("type")).thenReturn("aa");
when(oldCandidacy.getStringValue("status")).thenReturn("bb");
when(oldCandidacy.getStringValue("userName")).thenReturn("cc");
when(oldCandidacy.getLargeStringValue("userComment")).thenReturn("dd");
when(oldCandidacy.getStringValue("reviewer")).thenReturn("ee");
when(oldCandidacy.getLargeStringValue("reviewerComment")).thenReturn("ff");
when(oldCandidacy.getLargeStringValue("reviewerPrivateComment")).thenReturn("gg");
when(oldCandidacy.getDateValue("date")).thenReturn(new Date(2000));
when(oldCandidacy.getDateValue("resolutionDate")).thenReturn(new Date(8000));
// Run
mocker.getComponentUnderTest().hibernateMigrate();
// Verify the user configuration is accurate
WikiUserConfiguration expectedConfiguration = new WikiUserConfiguration();
expectedConfiguration.setUserScope(UserScope.GLOBAL_ONLY);
expectedConfiguration.setMembershipType(MembershipType.REQUEST);
verify(wikiUserConfigurationHelper).saveConfiguration(eq(expectedConfiguration), eq("workspace"));
// Verify the old workspace object has been removed and the descriptor saved
verify(oldDescriptorDocument).removeXObject(oldObject);
verify(xwiki, times(1)).saveDocument(oldDescriptorDocument, "[UPGRADE] Remove the old WorkspaceManager.WorkspaceClass" + " object.", xcontext);
// Verify the candidacy has been upgraded
verify(newCandidacyObject).setStringValue(WikiCandidateMemberClassInitializer.FIELD_TYPE, "aa");
verify(newCandidacyObject).setStringValue(WikiCandidateMemberClassInitializer.FIELD_STATUS, "bb");
verify(newCandidacyObject).setStringValue(WikiCandidateMemberClassInitializer.FIELD_USER, "cc");
verify(newCandidacyObject).setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_USER_COMMENT, "dd");
verify(newCandidacyObject).setStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN, "ee");
verify(newCandidacyObject).setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN_COMMENT, "ff");
verify(newCandidacyObject).setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN_PRIVATE_COMMENT, "gg");
verify(newCandidacyObject).setDateValue(eq(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CREATION), eq(new Date(2000)));
verify(newCandidacyObject).setDateValue(eq(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CLOSURE), eq(new Date(8000)));
// Verify the old candidacy has been removed and the document saved
verify(memberGroupDoc).removeXObject(oldCandidacy);
verify(xwiki, times(1)).saveDocument(memberGroupDoc, "Upgrade candidacies from the old Workspace Application" + " to the new Wiki Application.", xcontext);
}
use of org.xwiki.model.reference.LocalDocumentReference in project xwiki-platform by xwiki.
the class XarPackage method readEntry.
private void readEntry(InputStream stream, String entryName) throws XarException, IOException {
if (entryName.equals(XarModel.PATH_PACKAGE)) {
readDescriptor(stream);
} else {
LocalDocumentReference reference = XarUtils.getReference(stream);
// Get current action associated to the document
int defaultAction = getDefaultAction(reference);
// Create entry
XarEntry xarEntry = new XarEntry(reference, entryName, defaultAction);
// Register entry
this.entries.put(xarEntry, xarEntry);
// Update existing package file entry name
updatePackageFileEntryName(xarEntry);
}
}
use of org.xwiki.model.reference.LocalDocumentReference in project xwiki-platform by xwiki.
the class XarUtils method getReference.
/**
* Extract {@link LocalDocumentReference} from a XAR document XML stream.
*
* @param documentStream the stream to parse
* @return the reference extracted from the stream
* @throws XarException when failing to parse the document stream
* @since 5.4M1
*/
public static LocalDocumentReference getReference(InputStream documentStream) throws XarException {
XMLStreamReader xmlReader;
try {
xmlReader = XML_INPUT_FACTORY.createXMLStreamReader(documentStream);
} catch (XMLStreamException e) {
throw new XarException("Failed to create a XML read", e);
}
EntityReference reference = null;
Locale locale = null;
String legacySpace = null;
String legacyPage = null;
try {
// <xwikidoc>
xmlReader.nextTag();
xmlReader.require(XMLStreamReader.START_ELEMENT, null, XarDocumentModel.ELEMENT_DOCUMENT);
// Reference
String referenceString = xmlReader.getAttributeValue(null, XarDocumentModel.ATTRIBUTE_DOCUMENT_REFERENCE);
if (referenceString != null) {
reference = RESOLVER.resolve(referenceString, EntityType.DOCUMENT);
}
// Locale
String localeString = xmlReader.getAttributeValue(null, XarDocumentModel.ATTRIBUTE_DOCUMENT_LOCALE);
if (localeString != null) {
if (localeString.isEmpty()) {
locale = Locale.ROOT;
} else {
locale = LocaleUtils.toLocale(localeString);
}
}
// Legacy fallback
if (reference == null || locale == null) {
for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
String elementName = xmlReader.getLocalName();
if (XarDocumentModel.ELEMENT_NAME.equals(elementName)) {
if (reference == null) {
legacyPage = xmlReader.getElementText();
if (legacySpace != null && locale != null) {
break;
}
} else if (locale != null) {
break;
}
} else if (XarDocumentModel.ELEMENT_SPACE.equals(elementName)) {
if (reference == null) {
legacySpace = xmlReader.getElementText();
if (legacyPage != null && locale != null) {
break;
}
} else if (locale != null) {
break;
}
} else if (XarDocumentModel.ELEMENT_LOCALE.equals(elementName)) {
if (locale == null) {
String value = xmlReader.getElementText();
if (value.length() == 0) {
locale = Locale.ROOT;
} else {
locale = LocaleUtils.toLocale(value);
}
}
if (reference != null || (legacySpace != null && legacyPage != null)) {
break;
}
} else {
StAXUtils.skipElement(xmlReader);
}
}
}
} catch (XMLStreamException e) {
throw new XarException("Failed to parse document", e);
} finally {
try {
xmlReader.close();
} catch (XMLStreamException e) {
throw new XarException("Failed to close XML reader", e);
}
}
if (reference == null) {
if (legacySpace == null) {
throw new XarException("Missing space element");
}
if (legacyPage == null) {
throw new XarException("Missing page element");
}
reference = new LocalDocumentReference(legacySpace, legacyPage);
}
if (locale == null) {
throw new XarException("Missing locale element");
}
return new LocalDocumentReference(reference, locale);
}
use of org.xwiki.model.reference.LocalDocumentReference in project xwiki-platform by xwiki.
the class CreatePageIT method createTemplateAndTemplateProvider.
/**
* Helper function to Create both a Template and a Template Provider for the tests in this class.
*/
private ViewPage createTemplateAndTemplateProvider(String templateProviderName, String templateContent, String templateTitle, boolean saveAndEdit) throws Exception {
// Cleanup of the test space for any leftovers from previous tests.
getUtil().deleteSpace(getTestClassName());
// Create a Template page
getUtil().rest().savePage(new LocalDocumentReference(getTestClassName(), TEMPLATE_NAME), templateContent, templateTitle);
// Create a Template Provider
TemplatesAdministrationSectionPage sectionPage = TemplatesAdministrationSectionPage.gotoPage();
TemplateProviderInlinePage templateProviderInline = sectionPage.createTemplateProvider(getTestClassName(), templateProviderName);
templateProviderInline.setTemplateName("Test Template");
templateProviderInline.setTemplate(getTestClassName() + "." + TEMPLATE_NAME);
if (saveAndEdit) {
templateProviderInline.setActionOnCreate(TemplateProviderInlinePage.ACTION_SAVEANDEDIT);
}
return templateProviderInline.clickSaveAndView();
}
use of org.xwiki.model.reference.LocalDocumentReference in project xwiki-platform by xwiki.
the class XAROutputFilterStream method beginWikiDocumentLocale.
@Override
public void beginWikiDocumentLocale(Locale locale, FilterEventParameters parameters) throws FilterException {
if (this.writer == null) {
if (this.wikiWriter == null && (this.properties.isForceDocument() || isTargetTextualContent())) {
checkXMLWriter();
} else {
if (this.wikiWriter == null) {
this.wikiWriter = new XARWikiWriter(this.properties.getPackageName() != null ? this.properties.getPackageName() : "package", this.properties);
}
this.writer = new FilterStreamXMLStreamWriter(this.wikiWriter.newEntry(new LocalDocumentReference(this.currentDocumentReference, locale)), this.properties.getEncoding(), this.properties.isFormat(), true);
}
}
this.writer.writeStartDocument(this.properties.getEncoding(), "1.1");
this.writer.writeStartElement(XarDocumentModel.ELEMENT_DOCUMENT);
this.writer.writeAttribute(XarDocumentModel.ATTRIBUTE_DOCUMENT_SPECVERSION, XarDocumentModel.VERSION_CURRENT);
this.writer.writeAttribute(XarDocumentModel.ATTRIBUTE_DOCUMENT_REFERENCE, localSerializer.serialize(this.currentDocumentReference));
this.writer.writeAttribute(XarDocumentModel.ATTRIBUTE_DOCUMENT_LOCALE, toString(locale));
// Legacy space and name
if (this.currentDocumentReference.getParent().getParent() == null) {
// If single space behave as it used to and put the space name instead of the reference to keep
// compatibility when importing in older version
this.writer.writeElement(XarDocumentModel.ELEMENT_SPACE, this.currentDocumentReference.getParent().getName());
} else {
// If nested space put the space reference in the field
this.writer.writeElement(XarDocumentModel.ELEMENT_SPACE, defaultSerializer.serialize(this.currentDocumentReference.getParent()));
}
this.writer.writeElement(XarDocumentModel.ELEMENT_NAME, this.currentDocumentReference.getName());
// Legacy locale
this.writer.writeElement(XarDocumentModel.ELEMENT_LOCALE, toString(locale));
this.writer.writeElement(XarDocumentModel.ELEMENT_DEFAULTLOCALE, toString(this.currentDocumentParameters.get(XWikiWikiDocumentFilter.PARAMETER_LOCALE)));
this.writer.writeElement(XarDocumentModel.ELEMENT_ISTRANSLATION, locale != null && !Locale.ROOT.equals(locale) ? "1" : "0");
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CREATION_AUTHOR)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_CREATION_AUTHOR, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CREATION_AUTHOR));
}
if (parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_CREATION_DATE)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_CREATION_DATE, toString((Date) parameters.get(XWikiWikiDocumentFilter.PARAMETER_CREATION_DATE)));
}
if (this.properties.isPreserveVersion() && parameters.containsKey(XWikiWikiDocumentFilter.PARAMETER_JRCSREVISIONS)) {
this.writer.writeElement(XarDocumentModel.ELEMENT_REVISIONS, (String) parameters.get(XWikiWikiDocumentFilter.PARAMETER_JRCSREVISIONS));
}
}
Aggregations