@Override
    public TransferStatus prepare(final Path file, final Local local, final TransferStatus parent, final ProgressListener progress) throws BackgroundException {
        final TransferStatus status = super.prepare(file, local, parent, progress);
        if(status.isExists()) {
            final String filename = file.getName();
            int no = 0;
            do {
                String proposal = String.format("%s-%d", FilenameUtils.getBaseName(filename), ++no);
                if(StringUtils.isNotBlank(Path.getExtension(filename))) {
                    proposal += String.format(".%s", Path.getExtension(filename));
                }
                final Path renamed = new Path(file.getParent(), proposal, file.getType());
                if(options.temporary) {
                    // Adjust final destination when uploading with temporary filename
                    status.getDisplayname().withRemote(renamed).exists(false);
                }
                else {
                    status.withRename(renamed);
                }
            }
            while(find.find(status.getRename().remote));
            if(log.isInfoEnabled()) {
                log.info(String.format("Changed upload target from %s to %s", file, status.getRename().remote));
            }
            if(log.isDebugEnabled()) {
                log.debug(String.format("Clear exist flag for file %s", file));
            }
            status.setExists(false);
        }
        else {
            if(parent.getRename().remote != null) {
                final Path renamed = new Path(parent.getRename().remote, file.getName(), file.getType());
                if(options.temporary) {
                    // Adjust final destination when uploading with temporary filename
                    status.getDisplayname().withRemote(renamed).exists(false);
                }
                else {
                    status.withRename(renamed);
                }
            }
            if(log.isInfoEnabled()) {
                log.info(String.format("Changed upload target from %s to %s", file, status.getRename().remote));
            }
        }
        return status;
    }
@Test
    public void testDirectoryUploadRename() throws Exception {
        final Path directory = new Path("/t", EnumSet.of(Path.Type.directory));
        final Path file = new Path(directory, "f", EnumSet.of(Path.Type.file));
        final AtomicBoolean found = new AtomicBoolean();
        final AttributesFinder attributes = new AttributesFinder() {
            @Override
            public PathAttributes find(final Path file, final ListProgressListener listener) {
                return new PathAttributes();
            }
        };
        final Find find = new Find() {
            @Override
            public boolean find(final Path f, final ListProgressListener listener) {
                if(f.equals(directory)) {
                    found.set(true);
                    return true;
                }
                return false;
            }
        };
        final NullSession session = new NullSession(new Host(new TestProtocol()));
        final RenameFilter f = new RenameFilter(new DisabledUploadSymlinkResolver(), session);
        f.withFinder(find).withAttributes(attributes);
        final TransferStatus directoryStatus = f.prepare(directory, new NullLocal("t"), new TransferStatus().exists(true), new DisabledProgressListener());
        assertTrue(found.get());
        assertFalse(directoryStatus.isExists());
        f.apply(directory, new NullLocal("t"), directoryStatus, new DisabledProgressListener());
        assertNotNull(directoryStatus.getRename());
        assertNotEquals(directory, directoryStatus.getRename().remote);
        assertEquals(new Path("/t-1", EnumSet.of(Path.Type.directory)), directoryStatus.getRename().remote);
        assertNull(directoryStatus.getDisplayname().remote);
        assertNull(directoryStatus.getRename().local);
        assertNotNull(directoryStatus.getRename().remote);
        final TransferStatus fileStatus = f.prepare(file, new NullLocal("t/f"), directoryStatus, new DisabledProgressListener());
        f.apply(file, new NullLocal("t/f"), fileStatus, new DisabledProgressListener());
        assertFalse(fileStatus.isExists());
        assertNotNull(fileStatus.getRename());
        assertEquals(new Path(new Path("/t-1", EnumSet.of(Path.Type.directory)), "f", EnumSet.of(Path.Type.file)), fileStatus.getRename().remote);
        assertNull(fileStatus.getDisplayname().remote);
        assertNull(fileStatus.getRename().local);
        assertNotNull(fileStatus.getRename().remote);
        assertEquals(new Path("/t-1/f", EnumSet.of(Path.Type.file)), fileStatus.getRename().remote);
    }