use of org.structr.core.entity.TestOne in project structr by structr.
the class SearchAndSortingTest method test03SortByDate.
@Test
public void test03SortByDate() {
try {
Class type = TestOne.class;
int number = 97;
final List<NodeInterface> nodes = this.createTestNodes(type, number);
final int offset = 10;
Collections.shuffle(nodes, new Random(System.nanoTime()));
try (final Tx tx = app.tx()) {
int i = offset;
String name;
for (NodeInterface node : nodes) {
name = Integer.toString(i);
i++;
node.setProperty(AbstractNode.name, "TestOne-" + name);
node.setProperty(TestOne.aDate, new Date());
// slow down execution speed to make sure distinct changes fall in different milliseconds
try {
Thread.sleep(2);
} catch (Throwable t) {
}
}
tx.success();
}
try (final Tx tx = app.tx()) {
Result result = app.nodeQuery(type).getResult();
assertEquals(number, result.size());
PropertyKey sortKey = TestOne.aDate;
boolean sortDesc = false;
int pageSize = 10;
int page = 1;
result = app.nodeQuery(type).sort(sortKey).order(sortDesc).page(page).pageSize(pageSize).getResult();
logger.info("Raw result size: {}, expected: {}", new Object[] { result.getRawResultCount(), number });
assertTrue(result.getRawResultCount() == number);
logger.info("Result size: {}, expected: {}", new Object[] { result.size(), pageSize });
assertTrue(result.size() == Math.min(number, pageSize));
for (int j = 0; j < Math.min(result.size(), pageSize); j++) {
String expectedName = "TestOne-" + (offset + j);
String gotName = result.get(j).getProperty(AbstractNode.name);
System.out.println(expectedName + ", got: " + gotName);
assertEquals(expectedName, gotName);
}
tx.success();
}
} catch (FrameworkException ex) {
logger.error(ex.toString());
fail("Unexpected exception");
}
}
use of org.structr.core.entity.TestOne in project structr by structr.
the class SystemTest method testTransactionIsolationWithFailures.
@Test
public void testTransactionIsolationWithFailures() {
try {
final TestOne test = createTestNode(TestOne.class);
final ExecutorService executor = Executors.newCachedThreadPool();
final List<FailingTestRunner> tests = new LinkedList<>();
final List<Future> futures = new LinkedList<>();
// create and run test runners
for (int i = 0; i < 25; i++) {
final FailingTestRunner runner = new FailingTestRunner(app, test);
futures.add(executor.submit(runner));
tests.add(runner);
}
// wait for termination
for (final Future future : futures) {
future.get();
System.out.print(".");
}
System.out.println();
// check for success
for (final FailingTestRunner runner : tests) {
assertTrue("Could not validate transaction isolation", runner.success());
}
executor.shutdownNow();
} catch (Throwable fex) {
fail("Unexpected exception");
}
}
use of org.structr.core.entity.TestOne in project structr by structr.
the class SystemTest method testRollbackOnError.
@Test
public void testRollbackOnError() {
final ActionContext ctx = new ActionContext(securityContext, null);
/**
* first the old scripting style
*/
TestOne testNode = null;
try (final Tx tx = app.tx()) {
testNode = createTestNode(TestOne.class);
testNode.setProperty(TestOne.aString, "InitialString");
testNode.setProperty(TestOne.anInt, 42);
tx.success();
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
Scripting.replaceVariables(ctx, testNode, "${ ( set(this, 'aString', 'NewString'), set(this, 'anInt', 'NOT_AN_INTEGER') ) }");
fail("StructrScript: setting anInt to 'NOT_AN_INTEGER' should cause an Exception");
tx.success();
} catch (FrameworkException expected) {
}
try {
try (final Tx tx = app.tx()) {
assertEquals("Property value should still have initial value!", "InitialString", testNode.getProperty(TestOne.aString));
tx.success();
}
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
}
use of org.structr.core.entity.TestOne in project structr by structr.
the class ScriptingTest method testSystemProperties.
@Test
public void testSystemProperties() {
try {
final Principal user = createTestNode(Principal.class);
// create new node
TestOne t1 = createTestNode(TestOne.class, user);
final SecurityContext userContext = SecurityContext.getInstance(user, AccessMode.Frontend);
final App userApp = StructrApp.getInstance(userContext);
try (final Tx tx = userApp.tx()) {
final ActionContext userActionContext = new ActionContext(userContext, null);
assertEquals("node should be of type TestOne", "TestOne", Scripting.replaceVariables(userActionContext, t1, "${(get(this, 'type'))}"));
try {
assertEquals("setting the type should fail", "TestTwo", Scripting.replaceVariables(userActionContext, t1, "${(set(this, 'type', 'TestThree'), get(this, 'type'))}"));
fail("setting a system property should fail");
} catch (FrameworkException fx) {
}
assertEquals("setting the type should work after setting it with unlock_system_properties_once", "TestFour", Scripting.replaceVariables(userActionContext, t1, "${(unlock_system_properties_once(this), set(this, 'type', 'TestFour'), get(this, 'type'))}"));
tx.success();
}
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
}
use of org.structr.core.entity.TestOne in project structr by structr.
the class MaintenanceTest method testBulkSetNodePropertiesCommand.
@Test
public void testBulkSetNodePropertiesCommand() {
final Integer one = 1;
try {
// create test nodes first
createTestNodes(TestOne.class, 100);
try {
// test failure with wrong type
app.command(BulkSetNodePropertiesCommand.class).execute(toMap("type", "NonExistingType"));
fail("Using BulkSetNodePropertiesCommand with a non-existing type should throw an exception.");
} catch (FrameworkException fex) {
// status: 422
assertEquals(422, fex.getStatus());
assertEquals("Invalid type NonExistingType", fex.getMessage());
}
try {
// test failure without type
app.command(BulkSetNodePropertiesCommand.class).execute(toMap("anInt", 1));
fail("Using BulkSetNodePropertiesCommand without a type property should throw an exception.");
} catch (FrameworkException fex) {
// status: 422
assertEquals(422, fex.getStatus());
assertEquals("Type must not be empty", fex.getMessage());
}
// test success
app.command(BulkSetNodePropertiesCommand.class).execute(toMap("type", "TestOne", "anInt", 1, "aString", "one"));
try (final Tx tx = app.tx()) {
// check nodes, we should find 100 TestOnes here, and none TestTwos
assertEquals(0, app.nodeQuery(TestTwo.class).getResult().size());
assertEquals(100, app.nodeQuery(TestOne.class).getResult().size());
// check nodes
for (final TestOne test : app.nodeQuery(TestOne.class)) {
assertEquals(one, test.getProperty(TestOne.anInt));
assertEquals("one", test.getProperty(TestOne.aString));
}
}
// advanced: modify type
app.command(BulkSetNodePropertiesCommand.class).execute(toMap("type", "TestOne", "newType", "TestTwo"));
try (final Tx tx = app.tx()) {
// check nodes, we should find 100 TestTwos here, and none TestOnes
assertEquals(0, app.nodeQuery(TestOne.class).getResult().size());
assertEquals(100, app.nodeQuery(TestTwo.class).getResult().size());
}
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
Aggregations