FileOpUtils { public static void recursiveCopy(@NonNull File src, @NonNull File dest, @NonNull FileOp fop, @NonNull ProgressIndicator progress) throws IOException { recursiveCopy(src, dest, false, fop, progress); } private  FileOpUtils(); @NonNull static FileOp create(); static void recursiveCopy(@NonNull File src, @NonNull File dest, @NonNull FileOp fop,
            @NonNull ProgressIndicator progress); static void safeRecursiveOverwrite(@NonNull File src, @NonNull File dest,
            @NonNull FileOp fop, @NonNull ProgressIndicator progress); @Nullable static File getNewTempDir(@NonNull String base, @NonNull FileOp fileOp); @NonNull @VisibleForTesting static File getTempDir(@NonNull String base, int i); @NonNull static File append(@NonNull File base, @NonNull String... segments); @NonNull static File append(@NonNull String base, @NonNull String... segments); @NonNull static String makeRelative(@NonNull File baseDir, @NonNull File toBeRelative, FileOp fop); static void retainTempDirs(Set<File> retain, String base, FileOp mFop);  }
@Test public void recursiveCopySuccess() throws Exception { MockFileOp fop = new MockFileOp(); File s1 = new File("/root/src/a"); File s2 = new File("/root/src/foo/a"); File s3 = new File("/root/src/foo/b"); File s4 = new File("/root/src/foo/bar/a"); File s5 = new File("/root/src/baz/c"); fop.recordExistingFile(s1.getPath(), "content1"); fop.recordExistingFile(s2.getPath(), "content2"); fop.recordExistingFile(s3.getPath(), "content3"); fop.recordExistingFile(s4.getPath(), "content4"); fop.recordExistingFile(s5.getPath(), "content5"); FileOpUtils.recursiveCopy(new File("/root/src/"), new File("/root/dest"), fop, new FakeProgressIndicator()); assertEquals("content1", new String(fop.getContent(new File("/root/dest/a")))); assertEquals("content2", new String(fop.getContent(new File("/root/dest/foo/a")))); assertEquals("content3", new String(fop.getContent(new File("/root/dest/foo/b")))); assertEquals("content4", new String(fop.getContent(new File("/root/dest/foo/bar/a")))); assertEquals("content5", new String(fop.getContent(new File("/root/dest/baz/c")))); assertEquals("content1", new String(fop.getContent(s1))); assertEquals("content2", new String(fop.getContent(s2))); assertEquals("content3", new String(fop.getContent(s3))); assertEquals("content4", new String(fop.getContent(s4))); assertEquals("content5", new String(fop.getContent(s5))); assertEquals(10, fop.getExistingFiles().length); }
@Test public void recursiveCopyAlreadyExists() throws Exception { MockFileOp fop = new MockFileOp(); File s1 = new File("/root/src/a"); File s2 = new File("/root/src/foo/a"); File d1 = new File("/root/dest/b"); fop.recordExistingFile(s1.getPath(), "content1"); fop.recordExistingFile(s2.getPath(), "content2"); fop.recordExistingFile(d1.getPath(), "content3"); try { FileOpUtils.recursiveCopy(new File("/root/src/"), new File("/root/dest"), fop, new FakeProgressIndicator()); fail("Expected exception"); } catch (IOException expected) { } assertEquals("content3", new String(fop.getContent(d1))); assertEquals("content1", new String(fop.getContent(s1))); assertEquals("content2", new String(fop.getContent(s2))); assertEquals(3, fop.getExistingFiles().length); }
@Test public void recursiveCopyMerge() throws Exception { MockFileOp fop = new MockFileOp(); File s1 = new File("/root/src/a"); File s2 = new File("/root/src/foo/a"); File s3 = new File("/root/src/foo/b"); File d1 = new File("/root/dest/b"); File d2 = new File("/root/dest/foo/b"); File d3 = new File("/root/dest/bar/b"); fop.recordExistingFile(s1.getPath(), "content1"); fop.recordExistingFile(s2.getPath(), "content2"); fop.recordExistingFile(s3.getPath(), "content3"); fop.recordExistingFile(d1.getPath(), "content4"); fop.recordExistingFile(d2.getPath(), "content5"); fop.recordExistingFile(d3.getPath(), "content6"); FileOpUtils.recursiveCopy(new File("/root/src/"), new File("/root/dest"), true, fop, new FakeProgressIndicator()); assertEquals("content4", new String(fop.getContent(d1))); assertEquals("content5", new String(fop.getContent(d2))); assertEquals("content6", new String(fop.getContent(d3))); assertEquals("content1", new String(fop.getContent(new File("/root/dest/a")))); assertEquals("content2", new String(fop.getContent(new File("/root/dest/foo/a")))); assertEquals(8, fop.getExistingFiles().length); }
@Test public void recursiveCopyMergeFailed() throws Exception { MockFileOp fop = new MockFileOp(); File s1 = new File("/root/src/a"); File s2 = new File("/root/src/foo/a"); File d1 = new File("/root/dest/a/b"); fop.recordExistingFile(s1.getPath(), "content1"); fop.recordExistingFile(s2.getPath(), "content2"); fop.recordExistingFile(d1.getPath(), "content3"); try { FileOpUtils.recursiveCopy(new File("/root/src/"), new File("/root/dest"), true, fop, new FakeProgressIndicator()); fail(); } catch (IOException expected) { } assertEquals("content1", new String(fop.getContent(s1))); assertEquals("content2", new String(fop.getContent(s2))); assertEquals("content3", new String(fop.getContent(d1))); assertEquals(3, fop.getExistingFiles().length); }