<commit_before>package com.celements.copydoc;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.easymock.IAnswer;
import org.junit.Before;
import org.junit.Test;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.WikiReference;

import com.celements.common.test.AbstractBridgedComponentTestCase;
import com.celements.model.access.IModelAccessFacade;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.classes.BaseClass;
import com.xpn.xwiki.objects.classes.DateClass;
import com.xpn.xwiki.objects.classes.StringClass;
import com.xpn.xwiki.web.Utils;

public class CopyDocumentServiceTest extends AbstractBridgedComponentTestCase {

  private CopyDocumentService copyDocService;
  
  private List<BaseObject> emptyList;
  private DocumentReference docRef;
  private XWikiDocument docMock;
  private DocumentReference classRef;

  @Before
  public void setUp_CopyDocumentServiceTest() throws Exception {
    copyDocService = (CopyDocumentService) Utils.getComponent(ICopyDocumentRole.class);
    emptyList = new ArrayList<>();
    docRef = new DocumentReference("db", "Space", "SomeDoc");
    docMock = createMockAndAddToDefault(XWikiDocument.class);
    expect(docMock.getDocumentReference()).andReturn(docRef).anyTimes();
    classRef = new DocumentReference("db", "Classes", "SomeClass");
  }
  
  @Test
  public void testCopyObjects_none() throws Exception {
    boolean set = true;
    DocumentReference srcDocRef = new DocumentReference("odb", "Space", "SomeSrcDoc");
    XWikiDocument srcDoc = new XWikiDocument(srcDocRef);
    XWikiDocument trgDoc = new XWikiDocument(docRef);
    Set<BaseObject> toIgnore = Sets.newHashSet();
    
    replayDefault();
    boolean ret = copyDocService.copyObjects(srcDoc, trgDoc, toIgnore, set);
    verifyDefault();
    
    assertFalse(ret);
  }
  
  @Test
  public void testCopyObjects_added() throws Exception {
    boolean set = true;
    String name = "name";
    String val = "val";
    DocumentReference srcDocRef = new DocumentReference("odb", "Space", "SomeSrcDoc");
    DocumentReference srcClassRef = new DocumentReference(classRef);
    srcClassRef.setWikiReference(new WikiReference("odb"));
    XWikiDocument srcDoc = new XWikiDocument(srcDocRef);
    srcDoc.addXObject(createObj(srcClassRef, name, val));
    XWikiDocument trgDoc = new XWikiDocument(docRef);
    Set<BaseObject> toIgnore = Sets.newHashSet();
    BaseClass bClass = expectBaseClass(classRef);
    expect(bClass.get(eq(name))).andReturn(new StringClass()).once();
    
    replayDefault();
    boolean ret = copyDocService.copyObjects(srcDoc, trgDoc, toIgnore, set);
    verifyDefault();<commit_msg>replace setWikiReference for xwiki 4.5.4<commit_after>package com.celements.copydoc;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.easymock.IAnswer;
import org.junit.Before;
import org.junit.Test;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.WikiReference;

import com.celements.common.test.AbstractBridgedComponentTestCase;
import com.celements.model.access.IModelAccessFacade;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.classes.BaseClass;
import com.xpn.xwiki.objects.classes.DateClass;
import com.xpn.xwiki.objects.classes.StringClass;
import com.xpn.xwiki.web.Utils;

public class CopyDocumentServiceTest extends AbstractBridgedComponentTestCase {

  private CopyDocumentService copyDocService;
  
  private List<BaseObject> emptyList;
  private DocumentReference docRef;
  private XWikiDocument docMock;
  private DocumentReference classRef;

  @Before
  public void setUp_CopyDocumentServiceTest() throws Exception {
    copyDocService = (CopyDocumentService) Utils.getComponent(ICopyDocumentRole.class);
    emptyList = new ArrayList<>();
    docRef = new DocumentReference("db", "Space", "SomeDoc");
    docMock = createMockAndAddToDefault(XWikiDocument.class);
    expect(docMock.getDocumentReference()).andReturn(docRef).anyTimes();
    classRef = new DocumentReference("db", "Classes", "SomeClass");
  }
  
  @Test
  public void testCopyObjects_none() throws Exception {
    boolean set = true;
    DocumentReference srcDocRef = new DocumentReference("odb", "Space", "SomeSrcDoc");
    XWikiDocument srcDoc = new XWikiDocument(srcDocRef);
    XWikiDocument trgDoc = new XWikiDocument(docRef);
    Set<BaseObject> toIgnore = Sets.newHashSet();
    
    replayDefault();
    boolean ret = copyDocService.copyObjects(srcDoc, trgDoc, toIgnore, set);
    verifyDefault();
    
    assertFalse(ret);
  }
  
  @Test
  public void testCopyObjects_added() throws Exception {
    boolean set = true;
    String name = "name";
    String val = "val";
    DocumentReference srcDocRef = new DocumentReference("odb", "Space", "SomeSrcDoc");
    //IMPORTANT do not use setWikiReference, because it is dropped in xwiki 4.5.4
    DocumentReference srcClassRef = new DocumentReference("odb",
        classRef.getLastSpaceReference().getName(), classRef.getName());
    XWikiDocument srcDoc = new XWikiDocument(srcDocRef);
    srcDoc.addXObject(createObj(srcClassRef, name, val));
    XWikiDocument trgDoc = new XWikiDocument(docRef);
    Set<BaseObject> toIgnore = Sets.newHashSet();
    BaseClass bClass = expectBaseClass(classRef);
    expect(bClass.get(eq(name))).andReturn(new StringClass()).once();
    
    replayDefault();
    boolean ret = copyDocService.copyObjects(srcDoc, trgDoc, toIgnore, set);
    verifyDefault();
{'fraction_non_alphanumeric': 0.10724925521350546, 'fraction_numerical': 0.0009930486593843098, 'mean_word_length': 5.13502538071066, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3522320', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1815, 'n_words': 379}