Search in sources :

Example 26 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class PerformanceTest method testPerformanceOfNodeDeletion.

@Test
public void testPerformanceOfNodeDeletion() {
    final App app = StructrApp.getInstance(setup());
    final List<TestOne> nodes = new LinkedList<>();
    final int number = 1000;
    try {
        try (final Tx tx = app.tx()) {
            nodes.addAll(createNodes(app, TestOne.class, number));
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
    // start measuring
    final long t0 = System.currentTimeMillis();
    try {
        final BulkDeleteCommand cmd = app.command(BulkDeleteCommand.class);
        try (final Tx tx = app.tx()) {
            final Iterator<GraphObject> iterator = (Iterator) nodes.iterator();
            cmd.bulkDelete(iterator);
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
    final long t1 = System.currentTimeMillis();
    DecimalFormat decimalFormat = new DecimalFormat("0.000000000", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
    Double time = (t1 - t0) / 1000.0;
    Double rate = number / ((t1 - t0) / 1000.0);
    logger.info("Deleted {} nodes in {} seconds ({} per s)", number, decimalFormat.format(time), decimalFormat.format(rate));
    assertTrue("Deletion rate of nodes too low, expected > 100, was " + rate, rate > 50);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) BulkDeleteCommand(org.structr.core.graph.BulkDeleteCommand) DecimalFormat(java.text.DecimalFormat) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) Iterator(java.util.Iterator) TestOne(org.structr.web.entity.TestOne) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 27 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class PropertyTest method testCustomProperty.

@Test
public void testCustomProperty() {
    // schema setup
    try (final Tx tx = app.tx()) {
        final JsonSchema schema = StructrSchema.createFromDatabase(app, Arrays.asList("Image"));
        final Gson gson = new GsonBuilder().create();
        final Map<String, Object> str = (Map<String, Object>) gson.fromJson(schema.toString(), Map.class);
        final Map<String, Object> def = (Map<String, Object>) str.get("definitions");
        final Map<String, Object> img = (Map<String, Object>) def.get("Image");
        final Map<String, Object> pro = (Map<String, Object>) img.get("properties");
        final Map<String, Object> tn = (Map<String, Object>) pro.get("tnMid");
        assertEquals("Export of custom property should contain format string.", "300, 300, false", tn.get("format"));
        tx.success();
    } catch (FrameworkException | URISyntaxException t) {
        t.printStackTrace();
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) GsonBuilder(com.google.gson.GsonBuilder) JsonSchema(org.structr.schema.json.JsonSchema) Gson(com.google.gson.Gson) URISyntaxException(java.net.URISyntaxException) Map(java.util.Map) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 28 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class DirectFileImportTest method testDirectFileImportWithExistingFileCopyOverwrite.

@Test
public void testDirectFileImportWithExistingFileCopyOverwrite() {
    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), "directFileImportTestFileCopyOverwrite");
        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", "OVERWRITE", 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("Only one file should exist after import", 1, files.size());
        final File file = files.get(0);
        assertNotNull("Test file should have been created by import", file);
        assertEquals("Test file name should be test.txt", "test.txt", file.getName());
        assertNull("Test file should NOT have a parent folder", file.getParent());
        assertEquals("Test file content does not match source", "test file content 1", getContent(file));
        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());
}
Also used : Path(java.nio.file.Path) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException) DirectFileImportCommand(org.structr.web.maintenance.DirectFileImportCommand) File(org.structr.web.entity.File) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 29 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class DirectFileImportTest method testDirectFileImportWithAbsoluteDirectory.

@Test
public void testDirectFileImportWithAbsoluteDirectory() {
    final PropertyKey<String> pathKey = StructrApp.key(File.class, "path");
    Path testDir = null;
    String importPath = null;
    try {
        testDir = Files.createTempDirectory(Paths.get(basePath), "directFileImportTestAbsoluteDirectory");
        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");
        importPath = testDir.toString();
    } catch (IOException ioex) {
        fail("Unable to create test files.");
    }
    // do import
    final Map<String, Object> attributes = new LinkedHashMap<>();
    attributes.put("source", importPath);
    try (final Tx tx = app.tx()) {
        app.command(DirectFileImportCommand.class).execute(attributes);
        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.");
    }
}
Also used : Path(java.nio.file.Path) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException) DirectFileImportCommand(org.structr.web.maintenance.DirectFileImportCommand) Folder(org.structr.web.entity.Folder) File(org.structr.web.entity.File) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 30 with FrameworkException

use of org.structr.common.error.FrameworkException 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.");
    }
}
Also used : Path(java.nio.file.Path) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException) DirectFileImportCommand(org.structr.web.maintenance.DirectFileImportCommand) Folder(org.structr.web.entity.Folder) File(org.structr.web.entity.File) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Aggregations

FrameworkException (org.structr.common.error.FrameworkException)892 Tx (org.structr.core.graph.Tx)673 Test (org.junit.Test)450 App (org.structr.core.app.App)175 StructrApp (org.structr.core.app.StructrApp)174 StructrUiTest (org.structr.web.StructrUiTest)134 NodeInterface (org.structr.core.graph.NodeInterface)121 StructrTest (org.structr.common.StructrTest)118 PropertyKey (org.structr.core.property.PropertyKey)109 PropertyMap (org.structr.core.property.PropertyMap)105 IOException (java.io.IOException)96 GraphObject (org.structr.core.GraphObject)93 TestOne (org.structr.core.entity.TestOne)92 File (org.structr.web.entity.File)85 SecurityContext (org.structr.common.SecurityContext)78 Principal (org.structr.core.entity.Principal)69 Page (org.structr.web.entity.dom.Page)69 LinkedList (java.util.LinkedList)62 Folder (org.structr.web.entity.Folder)60 NodeAttribute (org.structr.core.graph.NodeAttribute)56