Search in sources :

Example 26 with Tx

use of org.structr.core.graph.Tx in project structr by structr.

the class ImporterTest method testWidgetWithScriptTags.

@Test
public void testWidgetWithScriptTags() {
    try (final Tx tx = app.tx()) {
        Settings.JsonIndentation.setValue(true);
        Settings.HtmlIndentation.setValue(true);
        final String source = testImportWidget("<div>\n" + "      <script src=\"/structr/js/lib/jquery-1.11.1.min.js\" type=\"text/javascript\"></script>\n" + "      <script type=\"text/javascript\"></script>\n" + "</div>", RenderContext.EditMode.WIDGET, "https://widgets.structr.org/structr/rest/widgets");
        // System.out.println(source);
        assertEquals("<!DOCTYPE html>\n" + "<html>\n" + "	<head></head>\n" + "	<body>\n" + "		<div>\n" + "			<script src=\"/structr/js/lib/jquery-1.11.1.min.js\" type=\"text/javascript\"></script>\n" + "			<script type=\"text/javascript\"></script>\n" + "		</div>\n" + "	</body>\n" + "</html>", source);
        Script secondScriptElement = (Script) app.nodeQuery(Script.class).blank(StructrApp.key(Script.class, "_html_src")).getFirst();
        assertNull(secondScriptElement.getOutgoingRelationship(StructrApp.getConfiguration().getRelationshipEntityClass("LinkSourceLINKLinkable")));
    } catch (FrameworkException ex) {
        logger.warn("", ex);
    }
}
Also used : Script(org.structr.web.entity.html.Script) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 27 with Tx

use of org.structr.core.graph.Tx 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 28 with Tx

use of org.structr.core.graph.Tx 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 29 with Tx

use of org.structr.core.graph.Tx 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 30 with Tx

use of org.structr.core.graph.Tx 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)

Aggregations

Tx (org.structr.core.graph.Tx)892 FrameworkException (org.structr.common.error.FrameworkException)684 Test (org.junit.Test)461 App (org.structr.core.app.App)201 StructrApp (org.structr.core.app.StructrApp)201 StructrUiTest (org.structr.web.StructrUiTest)139 NodeInterface (org.structr.core.graph.NodeInterface)117 StructrTest (org.structr.common.StructrTest)108 IOException (java.io.IOException)105 PropertyMap (org.structr.core.property.PropertyMap)102 LinkedList (java.util.LinkedList)99 TestOne (org.structr.core.entity.TestOne)98 File (org.structr.web.entity.File)83 Page (org.structr.web.entity.dom.Page)71 Principal (org.structr.core.entity.Principal)65 Folder (org.structr.web.entity.Folder)65 PropertyKey (org.structr.core.property.PropertyKey)62 NodeAttribute (org.structr.core.graph.NodeAttribute)57 SchemaNode (org.structr.core.entity.SchemaNode)45 AbstractFile (org.structr.web.entity.AbstractFile)44