### Question:
CompressBackupUtil { public static void tar(@NotNull File source, @NotNull File dest) throws IOException { if (!source.exists()) { throw new IllegalArgumentException("No source file or folder exists: " + source.getAbsolutePath()); } if (dest.exists()) { throw new IllegalArgumentException("Destination refers to existing file or folder: " + dest.getAbsolutePath()); } try (TarArchiveOutputStream tarOut = new TarArchiveOutputStream(new GZIPOutputStream( new BufferedOutputStream(new FileOutputStream(dest)), 0x1000))) { doTar("", source, tarOut); } catch (IOException e) { IOUtil.deleteFile(dest); throw e; } } private  CompressBackupUtil(); @NotNull static File backup(@NotNull final Backupable source, @NotNull final File backupRoot,
                              @Nullable final String backupNamePrefix, final boolean zip); @NotNull static File backup(@NotNull final BackupBean backupBean); @NotNull static File backup(@NotNull final Backupable source,
                              @NotNull final File target, final boolean zip); @NotNull static String getTimeStampedTarGzFileName(); @NotNull static String getTimeStampedZipFileName(); static void tar(@NotNull File source, @NotNull File dest); static void archiveFile(@NotNull final ArchiveOutputStream out,
                                   @NotNull final VirtualFileDescriptor source,
                                   final long fileSize);  }### Answer:
@Test public void testNoSource() throws Exception { try { CompressBackupUtil.tar(new File(randName + ".txt"), dest); } catch (IllegalArgumentException e) { return; } Assert.fail("No source file/folder exists. Should have thrown an exception."); }
@Test public void testDestExists() throws Exception { dest.createNewFile(); try { CompressBackupUtil.tar(new File(randName + ".txt"), dest); } catch (IllegalArgumentException e) { return; } Assert.fail("Destination file/folder already exists. Should have thrown an exception."); }
@Test public void testFileArchived() throws Exception { File src = new File(randName + ".txt"); FileWriter fw = new FileWriter(src); fw.write("12345"); fw.close(); CompressBackupUtil.tar(src, dest); Assert.assertTrue("No destination archive created", dest.exists()); TarArchiveInputStream tai = new TarArchiveInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(dest)))); ArchiveEntry entry = tai.getNextEntry(); Assert.assertNotNull("No entry found in destination archive", entry); Assert.assertEquals("Entry has wrong size", 5, entry.getSize()); }
@Test public void testFolderArchived() throws Exception { File src = new File(randName); src.mkdir(); FileWriter fw = new FileWriter(new File(src, "1.txt")); fw.write("12345"); fw.close(); fw = new FileWriter(new File(src, "2.txt")); fw.write("12"); fw.close(); CompressBackupUtil.tar(src, dest); Assert.assertTrue("No destination archive created", dest.exists()); TarArchiveInputStream tai = new TarArchiveInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(dest)))); ArchiveEntry entry1 = tai.getNextEntry(); ArchiveEntry entry2 = tai.getNextEntry(); if (entry1.getName().compareTo(entry2.getName()) > 0) { ArchiveEntry tmp = entry1; entry1 = entry2; entry2 = tmp; } Assert.assertNotNull("No entry found in destination archive", entry1); Assert.assertEquals("Entry has wrong size", 5, entry1.getSize()); System.out.println(entry1.getName()); Assert.assertEquals("Entry has wrong relative path", src.getName() + "/1.txt", entry1.getName()); System.out.println(entry2.getName()); Assert.assertEquals("Entry has wrong size", 2, entry2.getSize()); Assert.assertEquals("Entry has wrong relative path", src.getName() + "/2.txt", entry2.getName()); }