Skip to content

Commit

Permalink
Running the first tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marigostra committed Apr 17, 2021
1 parent 6989ad8 commit b73b7e8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/luwrain/app/commander/fileops/Copy.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Copy(OperationListener listener, String opName,
NullCheck.notNullItems(copyFrom, "copyFrom");
NullCheck.notEmptyArray(copyFrom, "copyFrom");
NullCheck.notNull(copyTo, "copyTo");
this.copyFrom = copyFrom;
this.copyFrom = copyFrom.clone();
this.copyTo = copyTo;
}

Expand Down
15 changes: 7 additions & 8 deletions src/main/java/org/luwrain/app/commander/fileops/CopyingBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ abstract class CopyingBase extends Operation
return percent;
}


protected Result copy(Path[] toCopy, Path dest) throws IOException
{
NullCheck.notNullItems(toCopy, "toCopy");
Expand All @@ -49,7 +48,7 @@ protected Result copy(Path[] toCopy, Path dest) throws IOException
for(Path p: toCopy)
if (!p.isAbsolute())
throw new IllegalArgumentException("Paths of all source files must be absolute");
//Calculating total size of source files
// Calculating the total size of the source files
totalBytes = 0;
for(Path f: toCopy)
{
Expand All @@ -67,7 +66,7 @@ protected Result copy(Path[] toCopy, Path dest) throws IOException
}
for(Path path: toCopy)
if (d.startsWith(path))
return new Result(Result.Type.SOURCE_PARENT_OF_DEST);
throw new IOException("SOURCE_IS_A_PARENT_OF_THE_DEST");
if (toCopy.length == 1)
return singleSource(toCopy[0], d); else
return multipleSource(toCopy, d);
Expand All @@ -76,16 +75,16 @@ protected Result copy(Path[] toCopy, Path dest) throws IOException
private Result singleSource(Path fileFrom, Path dest) throws IOException
{
status("single source mode:copying " + fileFrom + " to " + dest);
//The destination directory already exists, just copying whatever fileFrom is
// The destination directory already exists, just copying whatever fileFrom is
if (isDirectory(dest, true))
{
status("" + dest + " exists and is a directory (or a symlink to a directory)");
return copyRecurse(new Path[]{fileFrom}, dest);
}
//The destination isn't a directory, maybe even doesn't exist
// The destination isn't a directory, maybe even doesn't exist
if (isDirectory(fileFrom, false))
{
//fileFrom is a directory, we must copy its content to newly created directory
// fileFrom is a directory, we must copy its content to newly created directory
status("" + fileFrom + " is a directory and isn\'t a symlink");
if (exists(dest, false))
{
Expand All @@ -103,7 +102,7 @@ private Result singleSource(Path fileFrom, Path dest) throws IOException
//Copying the content of fileFrom to the newly created directory dest
return copyRecurse(getDirContent(fileFrom), dest);
}
//We sure that fileFrom and dest aren't directories, but dest may exist
// We sure that fileFrom and dest aren't directories, but dest may exist
if (!Files.isSymbolicLink(fileFrom) && !isRegularFile(fileFrom, false))
{
status("" + fileFrom + "is not a symlink and is not a regular file, nothing to do");
Expand Down Expand Up @@ -158,7 +157,7 @@ private Result copyRecurse(Path[] filesFrom, Path fileTo) throws IOException
{
if (!isDirectory(f, false))
{
status("" + f.toString() + " is not a directory");
status("" + f.toString() + " is not a directory, copying it");
final Result res = copyFileToDir(f, fileTo);
if (res.getType() != Result.Type.OK)
return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void run()
}
catch (Exception e)
{
Log.error("linux", name + ":" + e.getClass().getName() + ":" + e.getMessage());
Log.error("fileops", name + ":" + e.getClass().getName() + ":" + e.getMessage());
result = new Result(Result.Type.EXCEPTION, e);
}
}
Expand Down Expand Up @@ -152,7 +152,7 @@ protected Result deleteFileOrDir(Path p) throws IOException

protected void status(String message)
{
Log.debug("linux", message);
Log.debug("fileops", message);
}

protected ConfirmationChoices confirmOverwrite(Path path)
Expand Down
68 changes: 37 additions & 31 deletions src/test/java/org/luwrain/app/commander/fileops/CopyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,35 @@

public class CopyTest extends Assert
{
@Ignore @Test public void singleFileToEmptyDir() throws Exception
private final OperationListener listener = new DummyListener();
private File testDir = null;

@Test public void singleFileToExistingDir() throws Exception
{
final String fileName = "testing.dat";
final File srcFile = createSingleTestingFile(fileName, 5123456);
final File destDir = createDestDir();
final Copy copyOp = new Copy(new DummyListener(), "test", new Path[]{srcFile.toPath()}, destDir.toPath());
final File srcFile = createTestFile("testfile", 1234);
final File destDir = createTestDir("dest");
final Copy copyOp = new Copy(listener, "test", new Path[]{srcFile.toPath()}, destDir.toPath());
copyOp.run();
assertTrue(copyOp.getResult().isOk());
assertTrue(TestingBase.calcSha1(srcFile).equals(TestingBase.calcSha1(new File(destDir, fileName))));
assertEquals(TestingBase.calcSha1(srcFile), TestingBase.calcSha1(new File(destDir, "testfile")));
}

@Ignore @Test public void singleFileToNonExistingPlace() throws Exception
@Test public void singleFileToNonExistingPlace() throws Exception
{
final String fileName = "testing.dat";
final File srcFile = createSingleTestingFile(fileName, 5123456);
final File destDir = createDestDir();
final File destFile = new File(destDir, fileName);
final Copy copyOp = new Copy(new DummyListener(), "test", new Path[]{srcFile.toPath()}, destFile.toPath());
final File srcFile = createTestFile("testfile", 12345);
final File destDir = createTestDir("dest");
final File destFile = new File(destDir, "destfile");
final Copy copyOp = new Copy(listener, "test", new Path[]{srcFile.toPath()}, destFile.toPath());
copyOp.run();
assertTrue(copyOp.getResult().isOk());
assertTrue(TestingBase.calcSha1(srcFile).equals(TestingBase.calcSha1(destFile)));
assertEquals(TestingBase.calcSha1(srcFile), TestingBase.calcSha1(destFile));
}

@Ignore @Test public void singleFileToNonExistingPlaceInNonExistingDir() throws Exception
{
final String fileName = "testing.dat";
final File srcFile = createSingleTestingFile(fileName, 5123456);
final File destDir = createDestDir();
final File srcFile = createTestFile(fileName, 5123456);
final File destDir = createTestDir("dest");
final File nonExistingDir = new File(destDir, "non-existing");
final File destFile = new File(nonExistingDir, fileName);
final Copy copyOp = new Copy(new DummyListener(), "test", new Path[]{srcFile.toPath()}, destFile.toPath());
Expand All @@ -65,9 +66,9 @@ public class CopyTest extends Assert
{
final String fileName1 = "testing1.dat";
final String fileName2 = "testing2.dat";
final File srcFile1 = createSingleTestingFile(fileName1, 5123456);
final File srcFile2 = createSingleTestingFile(fileName2, 5123456);
final File destDir = createDestDir();
final File srcFile1 = createTestFile(fileName1, 5123456);
final File srcFile2 = createTestFile(fileName2, 5123456);
final File destDir = createTestDir("dest");
final Copy copyOp = new Copy(new DummyListener(), "test", new Path[]{srcFile1.toPath(), srcFile2.toPath()}, destDir.toPath());
copyOp.run();
assertTrue(copyOp.getResult().isOk());
Expand All @@ -79,9 +80,9 @@ public class CopyTest extends Assert
{
final String fileName1 = "testing1.dat";
final String fileName2 = "testing2.dat";
final File srcFile1 = createSingleTestingFile(fileName1, 5123456);
final File srcFile2 = createSingleTestingFile(fileName2, 5123456);
final File destDir = createDestDir();
final File srcFile1 = createTestFile(fileName1, 5123456);
final File srcFile2 = createTestFile(fileName2, 5123456);
final File destDir = createTestDir("dest");
final File nonExistingPlace1 = new File(destDir, "non-existing1");
final File nonExistingPlace2 = new File(nonExistingPlace1, "non-existing2");
final Copy copyOp = new Copy(new DummyListener(), "test", new Path[]{srcFile1.toPath(), srcFile2.toPath()}, nonExistingPlace2.toPath());
Expand All @@ -98,22 +99,27 @@ public class CopyTest extends Assert
//FIXME:symlinks
//FIXME:non existing dest, must be an error

private File createSingleTestingFile(String fileName, int len) throws IOException
private File createTestFile(String name, int len) throws IOException
{
TestingBase.TMP_DIR.mkdir();
final File srcDir = new File(TestingBase.TMP_DIR, "src");
srcDir.mkdir();
final File file = new File(srcDir, fileName);
assertNotNull(testDir);
final File file = new File(testDir, name);
final TestingBase base = new TestingBase();
base.writeRandFile(file, len);
return file;
}

private File createDestDir() throws IOException
private File createTestDir(String name) throws IOException
{
assertNotNull(testDir);
final File dir = new File(testDir, name);
dir.mkdir();
return dir;
}

@Before public void createTestDir() throws IOException
{
TestingBase.TMP_DIR.mkdir();
final File destDir = new File(TestingBase.TMP_DIR, "dest");
destDir.mkdir();
return destDir;
testDir = File.createTempFile(".lwr-junit-commander-fileops-", "");
testDir.delete();
testDir.mkdir();
}
}

0 comments on commit b73b7e8

Please sign in to comment.