use of org.structr.core.graph.Tx in project structr by structr.
the class DirectFileImportTest method testDirectFileImportWithRelativeDirectory.
@Test
public void testDirectFileImportWithRelativeDirectory() {
final PropertyKey<String> pathKey = StructrApp.key(File.class, "path");
final String dirName = "directFileImportTestRelativeDirectory";
final Path base = Paths.get(basePath);
Path testDir = null;
try {
testDir = Files.createDirectory(base.resolve(dirName));
createTestFile(testDir.resolve(Paths.get("test1.txt")), "test file content 1");
createTestFile(testDir.resolve(Paths.get("test2.txt")), "test file content 2");
createTestFile(testDir.resolve(Paths.get("test3.txt")), "test file content 3");
} catch (IOException ioex) {
fail("Unable to create test files.");
}
// do import
try (final Tx tx = app.tx()) {
app.command(DirectFileImportCommand.class).execute(setupParameters(dirName, "/", "COPY", "OVERWRITE", false));
tx.success();
} catch (FrameworkException ex) {
ex.printStackTrace();
fail("Unexpected exception.");
}
// verify successful file import
try (final Tx tx = app.tx()) {
assertNotNull("Folder should have been created by import", app.nodeQuery(Folder.class).and(pathKey, "/" + testDir.getFileName().toString()).getFirst());
final File file1 = app.nodeQuery(File.class).and(pathKey, "/" + testDir.getFileName().toString() + "/test1.txt").getFirst();
final File file2 = app.nodeQuery(File.class).and(pathKey, "/" + testDir.getFileName().toString() + "/test2.txt").getFirst();
final File file3 = app.nodeQuery(File.class).and(pathKey, "/" + testDir.getFileName().toString() + "/test3.txt").getFirst();
assertNotNull("Test file should have been created by import", file1);
assertNotNull("Test file should have been created by import", file2);
assertNotNull("Test file should have been created by import", file3);
assertEquals("Imported test file content does not match source", "test file content 1", getContent(file1));
assertEquals("Imported test file content does not match source", "test file content 2", getContent(file2));
assertEquals("Imported test file content does not match source", "test file content 3", getContent(file3));
tx.success();
} catch (FrameworkException ex) {
ex.printStackTrace();
fail("Unexpected exception.");
}
}
use of org.structr.core.graph.Tx in project structr by structr.
the class DirectFileImportTest method testDirectFileImportWithExistingFileCopyRename.
@Test
public void testDirectFileImportWithExistingFileCopyRename() {
try (final Tx tx = app.tx()) {
FileHelper.createFile(securityContext, "initial content".getBytes("utf-8"), "text/plain", File.class, "test.txt");
tx.success();
} catch (FrameworkException | IOException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
Path testDir = null;
Path importPath = null;
try {
testDir = Files.createTempDirectory(Paths.get(basePath), "directFileImportTestFileCopyRename");
importPath = testDir.resolve(Paths.get("test.txt"));
createTestFile(importPath, "test file content 1");
} catch (IOException ioex) {
fail("Unable to create test files.");
}
try (final Tx tx = app.tx()) {
app.command(DirectFileImportCommand.class).execute(setupParameters(importPath.toString(), "/", "COPY", "RENAME", false));
tx.success();
} catch (FrameworkException ex) {
ex.printStackTrace();
fail("Unexpected exception.");
}
// verify successful file import
try (final Tx tx = app.tx()) {
final List<File> files = app.nodeQuery(File.class).getAsList();
// import mode SKIP => no change, no additional file
assertEquals("Two files should exist after import", 2, files.size());
final File file1 = files.get(0);
final File file2 = files.get(1);
assertNotNull("Test file should have been created by import", file1);
assertEquals("Test file name should be test.txt", "test.txt", file1.getName());
assertNull("Test file should NOT have a parent folder", file1.getParent());
assertEquals("Test file content does not match source", "test file content 1", getContent(file1));
assertNotNull("Test file should have been created by import", file2);
assertNotEquals("Existing file should be renamed", "test.txt", file2.getName());
assertNull("Test file should NOT have a parent folder", file2.getParent());
assertEquals("Test file content does not match source", "initial content", getContent(file2));
tx.success();
} catch (FrameworkException ex) {
ex.printStackTrace();
fail("Unexpected exception.");
}
// verify source file exists after import
assertTrue("Source file should not be deleted after import", importPath.toFile().exists());
}
use of org.structr.core.graph.Tx in project structr by structr.
the class DirectFileImportTest method testDirectFileImportWithNonexistingFileCopyRename.
@Test
public void testDirectFileImportWithNonexistingFileCopyRename() {
Path testDir = null;
Path importPath = null;
try {
testDir = Files.createTempDirectory(Paths.get(basePath), "directFileImportTestFileCopyRename");
importPath = testDir.resolve(Paths.get("test.txt"));
createTestFile(importPath, "test file content 1");
} catch (IOException ioex) {
fail("Unable to create test files.");
}
try (final Tx tx = app.tx()) {
app.command(DirectFileImportCommand.class).execute(setupParameters(importPath.toString(), "/", "COPY", "RENAME", false));
tx.success();
} catch (FrameworkException ex) {
ex.printStackTrace();
fail("Unexpected exception.");
}
// verify successful file import
try (final Tx tx = app.tx()) {
final List<File> files = app.nodeQuery(File.class).getAsList();
// import mode SKIP => no change, no additional file
assertEquals("Two files should exist after import", 1, files.size());
final File file1 = files.get(0);
assertNotNull("Test file should have been created by import", file1);
assertEquals("Test file name should be test.txt", "test.txt", file1.getName());
assertNull("Test file should NOT have a parent folder", file1.getParent());
assertEquals("Test file content does not match source", "test file content 1", getContent(file1));
tx.success();
} catch (FrameworkException ex) {
ex.printStackTrace();
fail("Unexpected exception.");
}
// verify source file exists after import
assertTrue("Source file should not be deleted after import", importPath.toFile().exists());
}
use of org.structr.core.graph.Tx in project structr by structr.
the class DirectoryWatchServiceTest method testDisableWatchKeyRegistration.
@Test
public void testDisableWatchKeyRegistration() {
Path root = null;
java.io.File file1 = null;
java.io.File file2 = null;
java.io.File file3 = null;
try {
logger.info("Creating directory to mount..");
// create some files and folders on disk
root = Files.createTempDirectory("structr-mount-test");
root.resolve("parent1/child1/grandchild1").toFile().mkdirs();
root.resolve("parent2/child1/grandchild1").toFile().mkdirs();
root.resolve("parent3/child1/grandchild1").toFile().mkdirs();
logger.info("Creating files to mount..");
file1 = root.resolve("parent1/child1/grandchild1/test1.txt").toFile();
file2 = root.resolve("parent2/child1/grandchild1/test2.txt").toFile();
file3 = root.resolve("parent3/child1/grandchild1/test3.txt").toFile();
writeFile(file1, "test1 - before change");
writeFile(file2, "test2 - before change");
writeFile(file3, "test3 - before change");
// mount folder
try (final Tx tx = app.tx()) {
logger.info("Mounting directory..");
app.create(Folder.class, new NodeAttribute<>(Folder.name, "mounted3"), new NodeAttribute<>(StructrApp.key(Folder.class, "mountTarget"), root.toString()), new NodeAttribute<>(StructrApp.key(Folder.class, "mountWatchContents"), false));
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception.");
}
// wait some time
try {
Thread.sleep(5000);
} catch (Throwable t) {
}
// check that all files and folders exist
try (final Tx tx = app.tx()) {
logger.info("Checking directory..");
final File check1 = app.nodeQuery(File.class).andName("test1.txt").getFirst();
final File check2 = app.nodeQuery(File.class).andName("test2.txt").getFirst();
final File check3 = app.nodeQuery(File.class).andName("test3.txt").getFirst();
assertEquals("Invalid mount result", "/mounted3/parent1/child1/grandchild1/test1.txt", check1.getPath());
assertEquals("Invalid mount result", "/mounted3/parent2/child1/grandchild1/test2.txt", check2.getPath());
assertEquals("Invalid mount result", "/mounted3/parent3/child1/grandchild1/test3.txt", check3.getPath());
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception.");
}
// test external changes to files
writeFile(file2, "test2 - AFTER change");
// wait some time
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
// check that external changes are NOT recorded
try (final Tx tx = app.tx()) {
logger.info("Checking directory..");
final File check2 = app.nodeQuery(File.class).andName("test2.txt").getFirst();
assertFalse("Invalid checksum of externally modified file", FileHelper.getMD5Checksum(file2).equals(check2.getMd5()));
assertEquals("Invalid content of externally modified file", "test2 - AFTER change", readFile(check2.getFileOnDisk(false)));
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception.");
}
// unmount folder
try (final Tx tx = app.tx()) {
logger.info("Unmounting directory..");
final Folder mounted = app.nodeQuery(Folder.class).and(Folder.name, "mounted3").getFirst();
mounted.setProperty(StructrApp.key(Folder.class, "mountTarget"), null);
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception.");
}
} catch (IOException ioex) {
fail("Unexpected exception.");
} finally {
try {
// cleanup
Files.walkFileTree(root, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
try {
Files.delete(file);
} catch (Throwable t) {
t.printStackTrace();
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
try {
Files.delete(dir);
} catch (Throwable t) {
t.printStackTrace();
}
return FileVisitResult.CONTINUE;
}
});
} catch (Throwable ex) {
ex.printStackTrace();
}
}
}
use of org.structr.core.graph.Tx in project structr by structr.
the class DirectoryWatchServiceTest method testMountedFolderInRoot.
@Test
public void testMountedFolderInRoot() {
final String dirName = "mountServiceTest1";
final Path base = Paths.get(basePath);
Path testDir = null;
try {
testDir = Files.createDirectory(base.resolve(dirName));
createTestFile(testDir.resolve(Paths.get("test1.txt")), "test file content 1");
createTestFile(testDir.resolve(Paths.get("test2.txt")), "test file content 2");
createTestFile(testDir.resolve(Paths.get("test3.txt")), "test file content 3");
} catch (IOException ioex) {
fail("Unable to create test files.");
}
// mount directory
try (final Tx tx = app.tx()) {
app.create(Folder.class, new NodeAttribute<>(Folder.name, "mounted1"), new NodeAttribute<>(StructrApp.key(Folder.class, "mountWatchContents"), true), new NodeAttribute<>(StructrApp.key(Folder.class, "mountTarget"), testDir.toString()));
tx.success();
} catch (FrameworkException ex) {
ex.printStackTrace();
fail("Unexpected exception.");
}
// wait for DirectoryWatchService to start and scan
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
}
// verify mount point
try (final Tx tx = app.tx()) {
assertNotNull("Folder should have been created by import", app.nodeQuery(Folder.class).and(StructrApp.key(File.class, "path"), "/mounted1").getFirst());
final File file1 = app.nodeQuery(File.class).and(StructrApp.key(File.class, "path"), "/mounted1/test1.txt").getFirst();
final File file2 = app.nodeQuery(File.class).and(StructrApp.key(File.class, "path"), "/mounted1/test2.txt").getFirst();
final File file3 = app.nodeQuery(File.class).and(StructrApp.key(File.class, "path"), "/mounted1/test3.txt").getFirst();
assertNotNull("Test file should have been created by import", file1);
assertNotNull("Test file should have been created by import", file2);
assertNotNull("Test file should have been created by import", file3);
assertEquals("Imported test file content does not match source", "test file content 1", getContent(file1));
assertEquals("Imported test file content does not match source", "test file content 2", getContent(file2));
assertEquals("Imported test file content does not match source", "test file content 3", getContent(file3));
tx.success();
} catch (FrameworkException ex) {
ex.printStackTrace();
fail("Unexpected exception.");
}
}
Aggregations