implement the test cases

pull/1/head
Ivan Olexyn 4 years ago
parent e50aea8a4e
commit ebc4547184

Binary file not shown.

@ -1,20 +0,0 @@
package com.olexyn.ensync;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}

@ -3,8 +3,11 @@ package com.olexyn.ensync.files;
import com.olexyn.ensync.Execute; import com.olexyn.ensync.Execute;
import com.olexyn.ensync.Tools; import com.olexyn.ensync.Tools;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class FileTest { public class FileTest {
@ -16,133 +19,138 @@ public class FileTest {
private static final String fileAPath = "asdf"; private static final String fileAPath = "asdf";
private static final String fileBPath = "asff"; private static final String fileBPath = "asff";
private final File a = new File(fileAPath); private final TestFile a = new TestFile(fileAPath);
private final File b = new File(fileBPath); private final TestFile b = new TestFile(fileBPath);
private void createFile(File file){
}
private void updateFile(File file){
private List<String> createFile(File file) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.println("");
}
return new ArrayList<String>();
} }
private List<String> updateFile(File file) {
private void deleteFile(File file){ try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.println("");
}
return new ArrayList<String>();
} }
private void deleteFile(File file) {
try {
Thread.sleep(10);
public void deleteA(){ } catch (InterruptedException e) {
System.out.println("");
Assert.assertFalse(a.exists()); }
Assert.assertFalse(b.exists());
} }
/** private void cleanDirs() {
* Simulates user activity on disk. deleteFile(a);
*/ deleteFile(b);
void createFiles() throws InterruptedException {
StringBuilder sbA = new StringBuilder("a");
StringBuilder sbB = new StringBuilder("b");
// dv (deleted-void)
// TODO
// dd
tools.writeSbToPath(PATH+"/a/dd", sbA);
Thread.sleep(10);
tools.writeSbToPath(PATH+"/b/dd", sbB);
Thread.sleep(10);Thread.sleep(10);
x.execute(new String[]{"rm", PATH+"/a/dd"});
Thread.sleep(10);
x.execute(new String[]{"rm", PATH+"/b/dd"});
Thread.sleep(10);
// dc
tools.writeSbToPath(PATH+"/a/dc", sbA);
Thread.sleep(10);
x.execute(new String[]{"rm", PATH+"/a/dc"});
Thread.sleep(10);
tools.writeSbToPath(PATH+"/b/dc", sbB);
Thread.sleep(10);
// dm
tools.writeSbToPath(PATH+"/a/dm", sbA);
Thread.sleep(10);
x.execute(new String[]{"rm", PATH+"/a/dm"});
Thread.sleep(10);
tools.writeSbToPath(PATH+"/b/dm", sbB);
Thread.sleep(10);
// dv (deleted-void)
// TODO
// cd
// TODO
// cc
// TODO
// cm
// TODO
// cv (created-void)
// TODO
// md
// TODO
// mc
// TODO
// mm
// TODO
} }
/** /**
* Checks if end-state is as desired. * Perform the 15 test cases in TestCases.xlsx.
* @throws Exception otherwise.
*/ */
void fileTest() throws Exception { @Test
public void doFileTests() {
// Files where the second (= the newer) file was deleted. Thus both files should not exist in the end-state.
String[] arrayToDelete = {"/a/dd", "/b/dd" , "/a/cd", "/b/cd", "/a/md", "/b/md"};
for (String path : arrayToDelete){
if (new TestableFile(path).exists()) throw new Exception();
}
// Files where the second (= the newer) file was created or modified. Thus both files should contain "b" in the end-state.
String[] arrayToB = {"/a/dc", "/b/dc" , "/a/dm", "/b/dm", "/a/cc", "/b/cc"};
for (String path : arrayToB){
if (!new TestableFile(path).hasContent("b")) throw new Exception();
}
List<String> sideloadContentA;
List<String> sideloadContentB;
// 1
createFile(a);
deleteFile(a);
Assert.assertFalse(a.exists());
Assert.assertFalse(b.exists());
cleanDirs();
// 2
createFile(b);
createFile(a);
deleteFile(a);
Assert.assertFalse(a.exists());
Assert.assertFalse(b.exists());
cleanDirs();
// 3
createFile(a);
createFile(b);
deleteFile(a);
deleteFile(b);
Assert.assertFalse(a.exists());
Assert.assertFalse(b.exists());
cleanDirs();
// 4
createFile(a);
deleteFile(a);
sideloadContentB = createFile(b);
Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
cleanDirs();
// 5
createFile(a);
createFile(b);
deleteFile(a);
sideloadContentB = updateFile(b);
Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
cleanDirs();
// 6
sideloadContentA = createFile(a);
Assert.assertEquals(sideloadContentA, b.updateContent().getContent());
// 7
createFile(b);
createFile(a);
Assert.assertEquals(sideloadContentA, b.updateContent().getContent());
// 8
createFile(a);
createFile(b);
deleteFile(b);
Assert.assertFalse(a.exists());
Assert.assertFalse(b.exists());
cleanDirs();
//9
createFile(a);
sideloadContentB = createFile(b);
Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
cleanDirs();
// 10
createFile(b);
createFile(a);
sideloadContentB = updateFile(b);
Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
// 11
createFile(a);
sideloadContentA = updateFile(a);
Assert.assertEquals(sideloadContentA, b.updateContent().getContent());
// 12
createFile(a);
createFile(b);
sideloadContentA = updateFile(a);
Assert.assertEquals(sideloadContentA, b.updateContent().getContent());
// 13
createFile(a);
createFile(b);
updateFile(a);
deleteFile(b);
Assert.assertFalse(a.exists());
Assert.assertFalse(b.exists());
cleanDirs();
// 14
createFile(a);
updateFile(a);
sideloadContentB = createFile(b);
Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
cleanDirs();
// 15
createFile(a);
createFile(b);
updateFile(a);
sideloadContentB = updateFile(b);
Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
cleanDirs();
} }
// Assertion Exception
} }

@ -0,0 +1,58 @@
package com.olexyn.ensync.files;
import com.olexyn.ensync.Tools;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class TestFile extends File {
Tools tools = new Tools();
private List<String> content = new ArrayList<>();
/**
* Wrapper for File that adds tools for assessing it's state.
*/
public TestFile(String pathname) {
super(pathname);
}
public void setContent(List<String> content) {
this.content = content;
}
public List<String> getContent() {
return content;
}
public List<String> copyContent() {
return List.copyOf(content);
}
public TestFile updateContent() {
String line = tools.fileToLines(this).get(0);
this.content.add(line);
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
if (!super.equals(o)) { return false; }
TestFile that = (TestFile) o;
return Objects.equals(tools, that.tools);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), tools);
}
}

@ -1,21 +0,0 @@
package com.olexyn.ensync.files;
import com.olexyn.ensync.Tools;
import java.io.File;
public class TestableFile extends File {
Tools tools = new Tools();
public TestableFile(String pathname) {
super(pathname);
}
public boolean hasContent(String s){
String line = tools.fileToLines(this).get(0);
return line.equals(s);
}
}
Loading…
Cancel
Save