org.deephacks.tools4j.config.test.integration.IntegrationValidationTests Maven / Gradle / Ivy
package org.deephacks.tools4j.config.test.integration;
import static org.deephacks.tools4j.config.model.Events.CFG309;
import static org.deephacks.tools4j.config.test.JUnitUtils.toBean;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import java.util.Arrays;
import java.util.Set;
import org.deephacks.tools4j.config.RuntimeContext;
import org.deephacks.tools4j.config.admin.AdminContext;
import org.deephacks.tools4j.config.model.AbortRuntimeException;
import org.deephacks.tools4j.config.model.Bean;
import org.deephacks.tools4j.config.model.Lookup;
import org.deephacks.tools4j.config.test.FeatureTestsRunner;
import org.deephacks.tools4j.config.test.integration.IntegrationTestDataFactory.Jsr303Bean;
import org.deephacks.tools4j.config.test.validation.BinaryTree;
import org.deephacks.tools4j.config.test.validation.BinaryTreeUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(FeatureTestsRunner.class)
public class IntegrationValidationTests {
protected Jsr303Bean jsr303;
private RuntimeContext runtime = Lookup.get().lookup(RuntimeContext.class);
private AdminContext admin = Lookup.get().lookup(AdminContext.class);
private IntegrationTestDataFactory factory = new IntegrationTestDataFactory();
public IntegrationValidationTests() {
}
@Before
public void before() {
jsr303 = factory.getJSR303Validation("jsr303");
}
@Test
public void test_JSR303_validation_success() {
jsr303.prop = "Valid upper value for @FirstUpperValidator";
jsr303.width = 2;
jsr303.height = 2;
Bean jsr303Bean = toBean(jsr303);
admin.create(jsr303Bean);
}
@Test
public void test_JSR303_validation_failures() {
jsr303.prop = "Valid upper value for @FirstUpperValidator";
jsr303.width = 20;
jsr303.height = 20;
Bean jsr303Bean = toBean(jsr303);
try {
admin.create(jsr303Bean);
fail("Area exceeds constraint");
} catch (AbortRuntimeException e) {
assertThat(e.getEvent().getCode(), is(CFG309));
}
jsr303.prop = "test";
jsr303.width = 1;
jsr303.height = 1;
jsr303Bean = toBean(jsr303);
try {
admin.create(jsr303Bean);
fail("Prop does not have first upper case.");
} catch (AbortRuntimeException e) {
assertThat(e.getEvent().getCode(), is(CFG309));
}
jsr303.prop = "T";
jsr303.width = 1;
jsr303.height = 1;
jsr303Bean = toBean(jsr303);
try {
admin.create(jsr303Bean);
fail("Prop must be longer than one char");
} catch (AbortRuntimeException e) {
assertThat(e.getEvent().getCode(), is(CFG309));
}
jsr303.prop = "Valid upper value for @FirstUpperValidator";
jsr303.width = null;
jsr303.height = null;
jsr303Bean = toBean(jsr303);
try {
admin.create(jsr303Bean);
fail("Width and height may not be null.");
} catch (AbortRuntimeException e) {
assertThat(e.getEvent().getCode(), is(CFG309));
}
}
/**
* Test that JSR303 validation can operate on references in order
* to validate that a binary tree is correct after modification.
*
* This is what the test-tree looks like.
*
* _______5______
* / \
* ___4 ___8__
* / / \
* _2 6 10
* / \ \ /
* 1 3 7 9
*/
@Test
public void test_JSR303_reference_validation() {
runtime.register(BinaryTree.class);
// generate the tree seen in the javadoc
Set beans = BinaryTreeUtils.getTree(5, Arrays.asList(8, 4, 10, 2, 5, 1, 9, 6, 3, 7));
admin.create(beans);
BinaryTree root = runtime.get("5", BinaryTree.class);
BinaryTreeUtils.printPretty(root);
/**
* TEST1: a correct delete of 8 where 9 takes its place
*/
Bean eight = BinaryTreeUtils.getBean(8, beans);
Bean ten = BinaryTreeUtils.getBean(10, beans);
// 8: set value 9
eight.setProperty("value", "9");
// 10: remove reference to 9
ten.remove("left");
admin.set(Arrays.asList(eight, ten));
root = runtime.get("5", BinaryTree.class);
// take a look at the tree after delete
BinaryTreeUtils.printPretty(root);
/**
* TEST2: set 4 to 7 should fail
*/
try {
Bean four = BinaryTreeUtils.getBean(4, beans);
four.setProperty("value", "7");
admin.merge(four);
fail("setting 4 to 7 should not be possible.");
} catch (AbortRuntimeException e) {
// BinaryTreeValidator should notice that 7 satisfies left 2
// but not parent 5 (7 should be to right of 5)
assertThat(e.getEvent().getCode(), is(CFG309));
// display error message to prove that validator found the error.
System.out.println(e.getEvent().getMessage());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy