@Test public void upgrade() throws Exception { when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("mainWiki"); String document1 = "XWiki.XWikiServerTemplate"; String document2 = "XWiki.XWikiServerNotATemplateAnymore"; String document3 = "XWiki.XWikiServerNotATemplate"; String document4 = "XWiki.XWikiServerTemplateNow"; DocumentReference documentReference1 = new DocumentReference("mainWiki", "XWiki", "XWikiServerTemplate"); DocumentReference documentReference2 = new DocumentReference("mainWiki", "XWiki", "XWikiServerNotATemplateAnymore"); DocumentReference documentReference3 = new DocumentReference("mainWiki", "XWiki", "XWikiServerNotATemplate"); DocumentReference documentReference4 = new DocumentReference("mainWiki", "XWiki", "XWikiServerTemplateNow"); XWikiDocument doc1 = mock(XWikiDocument.class); XWikiDocument doc2 = mock(XWikiDocument.class); XWikiDocument doc3 = mock(XWikiDocument.class); XWikiDocument doc4 = mock(XWikiDocument.class); List<String> documents = new ArrayList<String>(); documents.add(document1); documents.add(document2); documents.add(document3); documents.add(document4); when(query.<String>execute()).thenReturn(documents); when(documentReferenceResolver.resolve(document1)).thenReturn(documentReference1); when(documentReferenceResolver.resolve(document2)).thenReturn(documentReference2); when(documentReferenceResolver.resolve(document3)).thenReturn(documentReference3); when(documentReferenceResolver.resolve(document4)).thenReturn(documentReference4); when(xwiki.getDocument(documentReference1, xcontext)).thenReturn(doc1); when(xwiki.getDocument(documentReference2, xcontext)).thenReturn(doc2); when(xwiki.getDocument(documentReference3, xcontext)).thenReturn(doc3); when(xwiki.getDocument(documentReference4, xcontext)).thenReturn(doc4); DocumentReference templateClassReference = new DocumentReference("mainWiki", "WikiManager", "WikiTemplateClass"); DocumentReference descriptorClassReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerClass"); BaseObject descriptorObj1 = mock(BaseObject.class); BaseObject descriptorObj2 = mock(BaseObject.class); BaseObject descriptorObj3 = mock(BaseObject.class); BaseObject descriptorObj4 = mock(BaseObject.class); when(doc1.getXObject(eq(descriptorClassReference))).thenReturn(descriptorObj1); when(doc2.getXObject(eq(descriptorClassReference))).thenReturn(descriptorObj2); when(doc3.getXObject(eq(descriptorClassReference))).thenReturn(descriptorObj3); when(doc4.getXObject(eq(descriptorClassReference))).thenReturn(descriptorObj4); when(descriptorObj1.getIntValue("iswikitemplate", 0)).thenReturn(1); when(descriptorObj2.getIntValue("iswikitemplate", 0)).thenReturn(1); when(descriptorObj3.getIntValue("iswikitemplate", 0)).thenReturn(0); when(descriptorObj4.getIntValue("iswikitemplate", 0)).thenReturn(0); BaseObject wikitemplateObj1 = mock(BaseObject.class); BaseObject wikitemplateObj2 = mock(BaseObject.class); BaseObject wikitemplateObj3 = mock(BaseObject.class); BaseObject wikitemplateObj4 = mock(BaseObject.class); when(doc1.getXObject(eq(templateClassReference), eq(true), any(XWikiContext.class))).thenReturn(wikitemplateObj1); when(doc2.getXObject(eq(templateClassReference), eq(true), any(XWikiContext.class))).thenReturn(wikitemplateObj2); when(doc3.getXObject(eq(templateClassReference), eq(true), any(XWikiContext.class))).thenReturn(wikitemplateObj3); when(doc4.getXObject(eq(templateClassReference), eq(true), any(XWikiContext.class))).thenReturn(wikitemplateObj4); when(wikitemplateObj1.getIntValue("iswikitemplate", 1)).thenReturn(1); when(wikitemplateObj2.getIntValue("iswikitemplate", 1)).thenReturn(0); when(wikitemplateObj3.getIntValue("iswikitemplate", 0)).thenReturn(0); when(wikitemplateObj4.getIntValue("iswikitemplate", 0)).thenReturn(1); mocker.getComponentUnderTest().hibernateMigrate(); verify(wikitemplateObj1).setIntValue("iswikitemplate", 1); verify(wikitemplateObj2).setIntValue("iswikitemplate", 0); verify(wikitemplateObj3).setIntValue("iswikitemplate", 0); verify(wikitemplateObj4).setIntValue("iswikitemplate", 1); verify(descriptorObj1).removeField("iswikitemplate"); verify(descriptorObj2).removeField("iswikitemplate"); verify(descriptorObj3).removeField("iswikitemplate"); verify(descriptorObj4).removeField("iswikitemplate"); DocumentReference superadmin = new DocumentReference("mainWiki", "XWiki", "superadmin"); verify(doc1).setAuthorReference(eq(superadmin)); verify(doc2).setAuthorReference(eq(superadmin)); verify(doc3).setAuthorReference(eq(superadmin)); verify(doc4).setAuthorReference(eq(superadmin)); verify(xwiki).saveDocument(doc1, "[UPGRADE] Upgrade the template section.", xcontext); verify(xwiki).saveDocument(doc2, "[UPGRADE] Upgrade the template section.", xcontext); verify(xwiki).saveDocument(doc3, "[UPGRADE] Upgrade the template section.", xcontext); verify(xwiki).saveDocument(doc4, "[UPGRADE] Upgrade the template section.", xcontext); }
@Override protected void hibernateMigrate() throws DataMigrationException, XWikiException { XWikiContext context = getXWikiContext(); XWiki xwiki = context.getWiki(); DocumentReference templateClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), WikiTemplateClassDocumentInitializer.DOCUMENT_SPACE, WikiTemplateClassDocumentInitializer.DOCUMENT_NAME); DocumentReference descriptorClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), XWiki.SYSTEM_SPACE, "XWikiServerClass"); DocumentReference superAdmin = new DocumentReference(wikiDescriptorManager.getMainWikiId(), XWiki.SYSTEM_SPACE, "superadmin"); try { String statement = "select distinct doc.fullName " + "from Document doc, doc.object(XWiki.XWikiServerClass) as obj"; Query query = queryManager.createQuery(statement, Query.XWQL); List<String> results = query.execute(); for (String wikiPage : results) { XWikiDocument document = xwiki.getDocument(documentReferenceResolver.resolve(wikiPage), context); BaseObject descriptorObject = document.getXObject(descriptorClassReference); int isTemplate = descriptorObject.getIntValue(OLD_TEMPLATE_PROPERTY, 0); descriptorObject.removeField(OLD_TEMPLATE_PROPERTY); BaseObject object = document.getXObject(templateClassReference, true, context); isTemplate = object.getIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate); object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate); document.setAuthorReference(superAdmin); xwiki.saveDocument(document, "[UPGRADE] Upgrade the template section.", context); } } catch (QueryException e) { throw new DataMigrationException("Failed to get the list of all existing descriptors.", e); } catch (XWikiException e) { throw new DataMigrationException("Failed to upgrade a wiki descriptor.", e); } }
WikiTemplateMigration extends AbstractHibernateDataMigration { @Override protected void hibernateMigrate() throws DataMigrationException, XWikiException { XWikiContext context = getXWikiContext(); XWiki xwiki = context.getWiki(); DocumentReference templateClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), WikiTemplateClassDocumentInitializer.DOCUMENT_SPACE, WikiTemplateClassDocumentInitializer.DOCUMENT_NAME); DocumentReference descriptorClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), XWiki.SYSTEM_SPACE, "XWikiServerClass"); DocumentReference superAdmin = new DocumentReference(wikiDescriptorManager.getMainWikiId(), XWiki.SYSTEM_SPACE, "superadmin"); try { String statement = "select distinct doc.fullName " + "from Document doc, doc.object(XWiki.XWikiServerClass) as obj"; Query query = queryManager.createQuery(statement, Query.XWQL); List<String> results = query.execute(); for (String wikiPage : results) { XWikiDocument document = xwiki.getDocument(documentReferenceResolver.resolve(wikiPage), context); BaseObject descriptorObject = document.getXObject(descriptorClassReference); int isTemplate = descriptorObject.getIntValue(OLD_TEMPLATE_PROPERTY, 0); descriptorObject.removeField(OLD_TEMPLATE_PROPERTY); BaseObject object = document.getXObject(templateClassReference, true, context); isTemplate = object.getIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate); object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate); document.setAuthorReference(superAdmin); xwiki.saveDocument(document, "[UPGRADE] Upgrade the template section.", context); } } catch (QueryException e) { throw new DataMigrationException("Failed to get the list of all existing descriptors.", e); } catch (XWikiException e) { throw new DataMigrationException("Failed to upgrade a wiki descriptor.", e); } } }
WikiTemplateMigration extends AbstractHibernateDataMigration { @Override protected void hibernateMigrate() throws DataMigrationException, XWikiException { XWikiContext context = getXWikiContext(); XWiki xwiki = context.getWiki(); DocumentReference templateClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), WikiTemplateClassDocumentInitializer.DOCUMENT_SPACE, WikiTemplateClassDocumentInitializer.DOCUMENT_NAME); DocumentReference descriptorClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), XWiki.SYSTEM_SPACE, "XWikiServerClass"); DocumentReference superAdmin = new DocumentReference(wikiDescriptorManager.getMainWikiId(), XWiki.SYSTEM_SPACE, "superadmin"); try { String statement = "select distinct doc.fullName " + "from Document doc, doc.object(XWiki.XWikiServerClass) as obj"; Query query = queryManager.createQuery(statement, Query.XWQL); List<String> results = query.execute(); for (String wikiPage : results) { XWikiDocument document = xwiki.getDocument(documentReferenceResolver.resolve(wikiPage), context); BaseObject descriptorObject = document.getXObject(descriptorClassReference); int isTemplate = descriptorObject.getIntValue(OLD_TEMPLATE_PROPERTY, 0); descriptorObject.removeField(OLD_TEMPLATE_PROPERTY); BaseObject object = document.getXObject(templateClassReference, true, context); isTemplate = object.getIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate); object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate); document.setAuthorReference(superAdmin); xwiki.saveDocument(document, "[UPGRADE] Upgrade the template section.", context); } } catch (QueryException e) { throw new DataMigrationException("Failed to get the list of all existing descriptors.", e); } catch (XWikiException e) { throw new DataMigrationException("Failed to upgrade a wiki descriptor.", e); } }  }
WikiTemplateMigration extends AbstractHibernateDataMigration { @Override protected void hibernateMigrate() throws DataMigrationException, XWikiException { XWikiContext context = getXWikiContext(); XWiki xwiki = context.getWiki(); DocumentReference templateClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), WikiTemplateClassDocumentInitializer.DOCUMENT_SPACE, WikiTemplateClassDocumentInitializer.DOCUMENT_NAME); DocumentReference descriptorClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), XWiki.SYSTEM_SPACE, "XWikiServerClass"); DocumentReference superAdmin = new DocumentReference(wikiDescriptorManager.getMainWikiId(), XWiki.SYSTEM_SPACE, "superadmin"); try { String statement = "select distinct doc.fullName " + "from Document doc, doc.object(XWiki.XWikiServerClass) as obj"; Query query = queryManager.createQuery(statement, Query.XWQL); List<String> results = query.execute(); for (String wikiPage : results) { XWikiDocument document = xwiki.getDocument(documentReferenceResolver.resolve(wikiPage), context); BaseObject descriptorObject = document.getXObject(descriptorClassReference); int isTemplate = descriptorObject.getIntValue(OLD_TEMPLATE_PROPERTY, 0); descriptorObject.removeField(OLD_TEMPLATE_PROPERTY); BaseObject object = document.getXObject(templateClassReference, true, context); isTemplate = object.getIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate); object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate); document.setAuthorReference(superAdmin); xwiki.saveDocument(document, "[UPGRADE] Upgrade the template section.", context); } } catch (QueryException e) { throw new DataMigrationException("Failed to get the list of all existing descriptors.", e); } catch (XWikiException e) { throw new DataMigrationException("Failed to upgrade a wiki descriptor.", e); } }  @Override String getDescription(); @Override XWikiDBVersion getVersion(); @Override boolean shouldExecute(XWikiDBVersion startupVersion); }
WikiTemplateMigration extends AbstractHibernateDataMigration { @Override protected void hibernateMigrate() throws DataMigrationException, XWikiException { XWikiContext context = getXWikiContext(); XWiki xwiki = context.getWiki(); DocumentReference templateClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), WikiTemplateClassDocumentInitializer.DOCUMENT_SPACE, WikiTemplateClassDocumentInitializer.DOCUMENT_NAME); DocumentReference descriptorClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), XWiki.SYSTEM_SPACE, "XWikiServerClass"); DocumentReference superAdmin = new DocumentReference(wikiDescriptorManager.getMainWikiId(), XWiki.SYSTEM_SPACE, "superadmin"); try { String statement = "select distinct doc.fullName " + "from Document doc, doc.object(XWiki.XWikiServerClass) as obj"; Query query = queryManager.createQuery(statement, Query.XWQL); List<String> results = query.execute(); for (String wikiPage : results) { XWikiDocument document = xwiki.getDocument(documentReferenceResolver.resolve(wikiPage), context); BaseObject descriptorObject = document.getXObject(descriptorClassReference); int isTemplate = descriptorObject.getIntValue(OLD_TEMPLATE_PROPERTY, 0); descriptorObject.removeField(OLD_TEMPLATE_PROPERTY); BaseObject object = document.getXObject(templateClassReference, true, context); isTemplate = object.getIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate); object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate); document.setAuthorReference(superAdmin); xwiki.saveDocument(document, "[UPGRADE] Upgrade the template section.", context); } } catch (QueryException e) { throw new DataMigrationException("Failed to get the list of all existing descriptors.", e); } catch (XWikiException e) { throw new DataMigrationException("Failed to upgrade a wiki descriptor.", e); } }  @Override String getDescription(); @Override XWikiDBVersion getVersion(); @Override boolean shouldExecute(XWikiDBVersion startupVersion);  }