Skip to content

Commit 2ebc6a7

Browse files
committed
added child test
1 parent 619b57d commit 2ebc6a7

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

annot/src/test/java/com/predic8/membrane/annot/YAMLParsingTest.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
import static com.predic8.membrane.annot.SpringConfigurationXSDGeneratingAnnotationProcessorTest.MC_MAIN_DEMO;
77
import static com.predic8.membrane.annot.util.CompilerHelper.*;
8-
import static com.predic8.membrane.annot.util.StructureAssertionUtil.assertStructure;
9-
import static com.predic8.membrane.annot.util.StructureAssertionUtil.clazz;
8+
import static com.predic8.membrane.annot.util.StructureAssertionUtil.*;
109

1110
public class YAMLParsingTest {
1211
@Test
@@ -30,6 +29,46 @@ public class DemoElement {
3029
);
3130
}
3231

32+
@Test
33+
public void singleChild() {
34+
var sources = splitSources(MC_MAIN_DEMO + """
35+
package com.predic8.membrane.demo;
36+
import com.predic8.membrane.annot.*;
37+
import java.util.List;
38+
@MCElement(name="demo")
39+
public class DemoElement {
40+
ChildElement child;
41+
42+
public ChildElement getChild() {
43+
return child;
44+
}
45+
46+
@MCChildElement
47+
public void setChild(ChildElement child) {
48+
this.child = child;
49+
}
50+
}
51+
---
52+
package com.predic8.membrane.demo;
53+
import com.predic8.membrane.annot.*;
54+
@MCElement(name="child1", topLevel=false)
55+
public class Child1Element {
56+
}
57+
""");
58+
var result = CompilerHelper.compile(sources, false);
59+
assertCompilerResult(true, result);
60+
61+
assertStructure(
62+
parseYAML(result, """
63+
demo:
64+
child:
65+
child1: {}
66+
"""),
67+
clazz("DemoElement",
68+
property("child", clazz("Child1Element")))
69+
);
70+
}
71+
3372
@Test
3473
public void twoObjects() {
3574
var sources = splitSources(MC_MAIN_DEMO + """

annot/src/test/java/com/predic8/membrane/annot/util/StructureAssertionUtil.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.predic8.membrane.annot.yaml.BeanRegistry;
44

5+
import java.lang.reflect.Method;
6+
57
import static org.junit.jupiter.api.Assertions.assertEquals;
68
import static org.junit.jupiter.api.Assertions.assertTrue;
79

@@ -14,14 +16,36 @@ public static void assertStructure(BeanRegistry registry, Asserter... asserter)
1416
}
1517

1618
public interface Asserter {
17-
void assertStructure(Object o1);
19+
void assertStructure(Object bean);
20+
}
21+
22+
public interface Property {
23+
void assertStructure(Object bean);
1824
}
1925

20-
public static Asserter clazz(String clazzName) {
26+
public static Asserter clazz(String clazzName, Property... properties) {
2127
return new Asserter() {
2228
@Override
23-
public void assertStructure(Object o1) {
24-
assertTrue(o1.getClass().getSimpleName().equals(clazzName));
29+
public void assertStructure(Object bean) {
30+
assertTrue(bean.getClass().getSimpleName().equals(clazzName));
31+
for (Property p : properties) {
32+
p.assertStructure(bean);
33+
}
34+
}
35+
};
36+
}
37+
38+
public static Property property(String name, Asserter asserter) {
39+
return new Property() {
40+
@Override
41+
public void assertStructure(Object bean) {
42+
try {
43+
Method getter = bean.getClass().getMethod("get" + Character.toUpperCase(name.charAt(0)) + name.substring(1));
44+
Object propertyValue = getter.invoke(bean);
45+
asserter.assertStructure(propertyValue);
46+
} catch (NoSuchMethodException | IllegalAccessException | java.lang.reflect.InvocationTargetException e) {
47+
throw new RuntimeException(e);
48+
}
2549
}
2650
};
2751
}

0 commit comments

Comments
 (0)