final NativeBroker broker = EasyMock.createMockBuilder(NativeBroker.class)
.addMockedMethod("getCollection")
.addMockedMethod("getSubject")
.createStrictMock();
final Subject subject = EasyMock.createStrictMock(Subject.class);
//grant EXECUTE and READ permissions on the src
expect(srcCollection.getPermissionsNoLock()).andReturn(srcPermissions);
expect(broker.getSubject()).andReturn(subject);
expect(srcPermissions.validate(subject, Permission.EXECUTE | Permission.READ)).andReturn(true);
//grant EXECUTE and WRITE permission on the dest
expect(broker.getCollection(dest)).andReturn(destCollection);
final Capture<XmldbURI> newDestURICapture = new Capture<XmldbURI>();
expect(broker.getCollection(capture(newDestURICapture))).andReturn(newDestCollection);
expect(destCollection.getPermissionsNoLock()).andReturn(destPermissions);
expect(broker.getSubject()).andReturn(subject);
expect(destPermissions.validate(subject, Permission.EXECUTE | Permission.WRITE)).andReturn(true);
expect(newDestCollection.getPermissionsNoLock()).andReturn(newDestPermissions);
expect(broker.getSubject()).andReturn(subject);
expect(newDestPermissions.validate(subject, Permission.EXECUTE | Permission.WRITE)).andReturn(true);
//one sub-document with READ permission
expect(srcCollection.iterator(broker)).andReturn(new ArrayIterator<DocumentImpl>(srcSubDocument));
expect(srcSubDocument.getPermissions()).andReturn(srcSubDocumentPermissions);
expect(broker.getSubject()).andReturn(subject);
expect(srcSubDocumentPermissions.validate(subject, Permission.READ)).andReturn(false);
//expectations for exception that should be thrown
expect(srcCollection.getURI()).andReturn(src);
expect(srcSubDocument.getURI()).andReturn(src.append(newName).append("someSubDocument.xml"));
expect(broker.getSubject()).andReturn(subject);
expect(subject.getName()).andReturn("Fake user");
//no sub-collections
expect(srcCollection.collectionIterator(broker)).andReturn(new EmptyIterator<XmldbURI>());
//test below