@Test public void hibernateMigrateIgnoresMigrationErrors() throws Exception { this.mocker.registerMockComponent(ComponentManager.class); ComponentManager cm = this.mocker.getInstance(ComponentManager.class); XWikiHibernateStore store = mock(XWikiHibernateStore.class); when(cm.getInstance(XWikiStoreInterface.class, "hibernate")).thenReturn(store); Execution e = this.mocker.getInstance(Execution.class); ExecutionContext ec = mock(ExecutionContext.class); when(e.getContext()).thenReturn(ec); XWikiContext xc = mock(XWikiContext.class); when(ec.getProperty("xwikicontext")).thenReturn(xc); @SuppressWarnings("deprecation") ArgumentCaptor<XWikiHibernateBaseStore.HibernateCallback<Object>> callbackCaptor = new ArgumentCaptor<>(); this.mocker.getComponentUnderTest().migrate(); Mockito.verify(store).executeWrite(Matchers.same(xc), callbackCaptor.capture()); XWiki xwiki = mock(XWiki.class); when(xc.getWiki()).thenReturn(xwiki); XWikiHibernateBaseStore.HibernateCallback<Object> callback = callbackCaptor.getValue(); Assert.assertNotNull(callback); Session session = mock(Session.class); Query q = mock(Query.class); when(session.createQuery(Matchers.anyString())).thenReturn(q); List<String> docs = new ArrayList<>(); docs.add("data.ThrowsDME"); when(q.list()).thenReturn(docs); DocumentReferenceResolver<String> resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "current"); EntityReference rightsClass = new EntityReference("XWikiRights", EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE)); DocumentReference r2 = new DocumentReference("xwiki", "data", "ThrowsDME"); when(resolver.resolve("data.ThrowsDME")).thenReturn(r2); XWikiDocument p2 = mock(XWikiDocument.class); when(xwiki.getDocument(r2, xc)).thenReturn(p2); when(p2.removeXObjects(rightsClass)).thenReturn(true); when(cm.getInstance(XWikiStoreInterface.class, "hibernate")).thenThrow(new ComponentLookupException("Nope")); callback.doInHibernate(session); }
@Override public Object doInHibernate(final Session session) throws HibernateException, XWikiException { XWikiContext context = getXWikiContext(); XWiki xwiki = context.getWiki(); Query q = session.createQuery( "select distinct doc.fullName from XWikiDocument as doc, BaseObject as p, BaseObject as r where " + "p.name = doc.fullName and p.className = '" + this.serializer.serialize(Patient.CLASS_REFERENCE) + "' and r.name = doc.fullName and r.className = '" + this.serializer.serialize(RIGHTS_CLASS) + "'"); @SuppressWarnings("unchecked") List<String> documents = q.list(); this.logger.debug("Found {} patient documents", documents.size()); for (String docName : documents) { try { R71504PhenoTips3226DataMigration.this.logger.debug("Checking [{}]", docName); XWikiDocument doc = xwiki.getDocument(this.resolver.resolve(docName), context); if (doc == null) { continue; } doc.removeXObjects(RIGHTS_CLASS); doc.setComment(this.getDescription()); doc.setMinorEdit(true); session.clear(); ((XWikiHibernateStore) getStore()).saveXWikiDoc(doc, context, false); session.flush(); R71504PhenoTips3226DataMigration.this.logger.debug("Updated [{}]", docName); } catch (DataMigrationException e) { } } return null; }
R71504PhenoTips3226DataMigration extends AbstractHibernateDataMigration implements
    XWikiHibernateBaseStore.HibernateCallback<Object> { @Override public Object doInHibernate(final Session session) throws HibernateException, XWikiException { XWikiContext context = getXWikiContext(); XWiki xwiki = context.getWiki(); Query q = session.createQuery( "select distinct doc.fullName from XWikiDocument as doc, BaseObject as p, BaseObject as r where " + "p.name = doc.fullName and p.className = '" + this.serializer.serialize(Patient.CLASS_REFERENCE) + "' and r.name = doc.fullName and r.className = '" + this.serializer.serialize(RIGHTS_CLASS) + "'"); @SuppressWarnings("unchecked") List<String> documents = q.list(); this.logger.debug("Found {} patient documents", documents.size()); for (String docName : documents) { try { R71504PhenoTips3226DataMigration.this.logger.debug("Checking [{}]", docName); XWikiDocument doc = xwiki.getDocument(this.resolver.resolve(docName), context); if (doc == null) { continue; } doc.removeXObjects(RIGHTS_CLASS); doc.setComment(this.getDescription()); doc.setMinorEdit(true); session.clear(); ((XWikiHibernateStore) getStore()).saveXWikiDoc(doc, context, false); session.flush(); R71504PhenoTips3226DataMigration.this.logger.debug("Updated [{}]", docName); } catch (DataMigrationException e) { } } return null; } }
R71504PhenoTips3226DataMigration extends AbstractHibernateDataMigration implements
    XWikiHibernateBaseStore.HibernateCallback<Object> { @Override public Object doInHibernate(final Session session) throws HibernateException, XWikiException { XWikiContext context = getXWikiContext(); XWiki xwiki = context.getWiki(); Query q = session.createQuery( "select distinct doc.fullName from XWikiDocument as doc, BaseObject as p, BaseObject as r where " + "p.name = doc.fullName and p.className = '" + this.serializer.serialize(Patient.CLASS_REFERENCE) + "' and r.name = doc.fullName and r.className = '" + this.serializer.serialize(RIGHTS_CLASS) + "'"); @SuppressWarnings("unchecked") List<String> documents = q.list(); this.logger.debug("Found {} patient documents", documents.size()); for (String docName : documents) { try { R71504PhenoTips3226DataMigration.this.logger.debug("Checking [{}]", docName); XWikiDocument doc = xwiki.getDocument(this.resolver.resolve(docName), context); if (doc == null) { continue; } doc.removeXObjects(RIGHTS_CLASS); doc.setComment(this.getDescription()); doc.setMinorEdit(true); session.clear(); ((XWikiHibernateStore) getStore()).saveXWikiDoc(doc, context, false); session.flush(); R71504PhenoTips3226DataMigration.this.logger.debug("Updated [{}]", docName); } catch (DataMigrationException e) { } } return null; }  }
R71504PhenoTips3226DataMigration extends AbstractHibernateDataMigration implements
    XWikiHibernateBaseStore.HibernateCallback<Object> { @Override public Object doInHibernate(final Session session) throws HibernateException, XWikiException { XWikiContext context = getXWikiContext(); XWiki xwiki = context.getWiki(); Query q = session.createQuery( "select distinct doc.fullName from XWikiDocument as doc, BaseObject as p, BaseObject as r where " + "p.name = doc.fullName and p.className = '" + this.serializer.serialize(Patient.CLASS_REFERENCE) + "' and r.name = doc.fullName and r.className = '" + this.serializer.serialize(RIGHTS_CLASS) + "'"); @SuppressWarnings("unchecked") List<String> documents = q.list(); this.logger.debug("Found {} patient documents", documents.size()); for (String docName : documents) { try { R71504PhenoTips3226DataMigration.this.logger.debug("Checking [{}]", docName); XWikiDocument doc = xwiki.getDocument(this.resolver.resolve(docName), context); if (doc == null) { continue; } doc.removeXObjects(RIGHTS_CLASS); doc.setComment(this.getDescription()); doc.setMinorEdit(true); session.clear(); ((XWikiHibernateStore) getStore()).saveXWikiDoc(doc, context, false); session.flush(); R71504PhenoTips3226DataMigration.this.logger.debug("Updated [{}]", docName); } catch (DataMigrationException e) { } } return null; }  @Override String getDescription(); @Override XWikiDBVersion getVersion(); @Override Object doInHibernate(final Session session); }
R71504PhenoTips3226DataMigration extends AbstractHibernateDataMigration implements
    XWikiHibernateBaseStore.HibernateCallback<Object> { @Override public Object doInHibernate(final Session session) throws HibernateException, XWikiException { XWikiContext context = getXWikiContext(); XWiki xwiki = context.getWiki(); Query q = session.createQuery( "select distinct doc.fullName from XWikiDocument as doc, BaseObject as p, BaseObject as r where " + "p.name = doc.fullName and p.className = '" + this.serializer.serialize(Patient.CLASS_REFERENCE) + "' and r.name = doc.fullName and r.className = '" + this.serializer.serialize(RIGHTS_CLASS) + "'"); @SuppressWarnings("unchecked") List<String> documents = q.list(); this.logger.debug("Found {} patient documents", documents.size()); for (String docName : documents) { try { R71504PhenoTips3226DataMigration.this.logger.debug("Checking [{}]", docName); XWikiDocument doc = xwiki.getDocument(this.resolver.resolve(docName), context); if (doc == null) { continue; } doc.removeXObjects(RIGHTS_CLASS); doc.setComment(this.getDescription()); doc.setMinorEdit(true); session.clear(); ((XWikiHibernateStore) getStore()).saveXWikiDoc(doc, context, false); session.flush(); R71504PhenoTips3226DataMigration.this.logger.debug("Updated [{}]", docName); } catch (DataMigrationException e) { } } return null; }  @Override String getDescription(); @Override XWikiDBVersion getVersion(); @Override Object doInHibernate(final Session session);  }