Skip to content

Commit 344ce03

Browse files
committed
Metamorph macros test should verify that no unexpected interactions occurred.
* Added `startRecord`/`endRecord` events where they were previously unverified.
1 parent e4c6fe5 commit 344ce03

File tree

1 file changed

+119
-131
lines changed

1 file changed

+119
-131
lines changed

metamorph/src/test/java/org/metafacture/metamorph/TestMetamorphMacros.java

Lines changed: 119 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
*/
1616
package org.metafacture.metamorph;
1717

18-
import static org.mockito.Mockito.inOrder;
19-
import static org.mockito.Mockito.verify;
18+
import static org.metafacture.metamorph.TestHelpers.assertMorph;
2019

2120
import org.junit.Rule;
2221
import org.junit.Test;
2322
import org.metafacture.framework.StreamReceiver;
24-
import org.mockito.InOrder;
2523
import org.mockito.Mock;
2624
import org.mockito.junit.MockitoJUnit;
2725
import org.mockito.junit.MockitoRule;
@@ -39,162 +37,152 @@ public class TestMetamorphMacros {
3937
@Mock
4038
private StreamReceiver receiver;
4139

42-
private Metamorph metamorph;
43-
4440
@Test
4541
public void shouldReplaceCallMacroWithMacro() {
46-
metamorph = InlineMorph.in(this)
47-
.with("<macros>")
48-
.with(" <macro name='simple-macro'>")
49-
.with(" <data source='$[in]' name='$[out]' />")
50-
.with(" </macro>")
51-
.with("</macros>")
52-
.with("<rules>")
53-
.with(" <call-macro name='simple-macro' in='in1' out='out1' />")
54-
.with(" <call-macro name='simple-macro' in='in2' out='out2' />")
55-
.with("</rules>")
56-
.createConnectedTo(receiver);
57-
58-
metamorph.startRecord("1");
59-
metamorph.literal("in1", "Hawaii");
60-
metamorph.literal("in2", "Maui");
61-
metamorph.endRecord();
62-
63-
verify(receiver).literal("out1", "Hawaii");
64-
verify(receiver).literal("out2", "Maui");
42+
assertMorph(receiver,
43+
"<macros>" +
44+
" <macro name='simple-macro'>" +
45+
" <data source='$[in]' name='$[out]' />" +
46+
" </macro>" +
47+
"</macros>" +
48+
"<rules>" +
49+
" <call-macro name='simple-macro' in='in1' out='out1' />" +
50+
" <call-macro name='simple-macro' in='in2' out='out2' />" +
51+
"</rules>",
52+
i -> {
53+
i.startRecord("1");
54+
i.literal("in1", "Hawaii");
55+
i.literal("in2", "Maui");
56+
i.endRecord();
57+
},
58+
o -> {
59+
o.get().startRecord("1");
60+
o.get().literal("out1", "Hawaii");
61+
o.get().literal("out2", "Maui");
62+
o.get().endRecord();
63+
}
64+
);
6565
}
6666

6767
@Test
6868
public void shouldAllowCallMacroInEntities() {
69-
metamorph = InlineMorph.in(this)
70-
.with("<macros>")
71-
.with(" <macro name='simple-macro'>")
72-
.with(" <data source='Honolulu' name='Honolulu' />")
73-
.with(" </macro>")
74-
.with("</macros>")
75-
.with("<rules>")
76-
.with(" <entity name='Hawaii'>")
77-
.with(" <call-macro name='simple-macro' />")
78-
.with(" </entity>")
79-
.with("</rules>")
80-
.createConnectedTo(receiver);
81-
82-
processRecordWithSingleLiteral();
83-
84-
verifyEntityWithSingleLiteral();
69+
testSingleLiteral(true,
70+
"<macros>" +
71+
" <macro name='simple-macro'>" +
72+
" <data source='Honolulu' name='Honolulu' />" +
73+
" </macro>" +
74+
"</macros>" +
75+
"<rules>" +
76+
" <entity name='Hawaii'>" +
77+
" <call-macro name='simple-macro' />" +
78+
" </entity>" +
79+
"</rules>"
80+
);
8581
}
8682

8783
@Test
8884
public void shouldAllowNestedMacros() {
89-
metamorph = InlineMorph.in(this)
90-
.with("<macros>")
91-
.with(" <macro name='inner-macro'>")
92-
.with(" <data source='$[literal]' />")
93-
.with(" </macro>")
94-
.with(" <macro name='outer-macro'>")
95-
.with(" <entity name='$[entity]'>")
96-
.with(" <call-macro name='inner-macro' literal='Honolulu' />")
97-
.with(" </entity>")
98-
.with(" </macro>")
99-
.with("</macros>")
100-
.with("<rules>")
101-
.with(" <call-macro name='outer-macro' entity='Hawaii' />")
102-
.with("</rules>")
103-
.createConnectedTo(receiver);
104-
105-
processRecordWithSingleLiteral();
106-
107-
verifyEntityWithSingleLiteral();
85+
testSingleLiteral(true,
86+
"<macros>" +
87+
" <macro name='inner-macro'>" +
88+
" <data source='$[literal]' />" +
89+
" </macro>" +
90+
" <macro name='outer-macro'>" +
91+
" <entity name='$[entity]'>" +
92+
" <call-macro name='inner-macro' literal='Honolulu' />" +
93+
" </entity>" +
94+
" </macro>" +
95+
"</macros>" +
96+
"<rules>" +
97+
" <call-macro name='outer-macro' entity='Hawaii' />" +
98+
"</rules>"
99+
);
108100
}
109101

110102
@Test
111103
public void shouldAllowoForwardReferencingMacros() {
112-
metamorph = InlineMorph.in(this)
113-
.with("<macros>")
114-
.with(" <macro name='referencing'>")
115-
.with(" <entity name='Hawaii'>")
116-
.with(" <call-macro name='forward-referenced' />")
117-
.with(" </entity>")
118-
.with(" </macro>")
119-
.with(" <macro name='forward-referenced'>")
120-
.with(" <data source='Honolulu' />")
121-
.with(" </macro>")
122-
.with("</macros>")
123-
.with("<rules>")
124-
.with(" <call-macro name='referencing' />")
125-
.with("</rules>")
126-
.createConnectedTo(receiver);
127-
128-
processRecordWithSingleLiteral();
129-
130-
verifyEntityWithSingleLiteral();
104+
testSingleLiteral(true,
105+
"<macros>" +
106+
" <macro name='referencing'>" +
107+
" <entity name='Hawaii'>" +
108+
" <call-macro name='forward-referenced' />" +
109+
" </entity>" +
110+
" </macro>" +
111+
" <macro name='forward-referenced'>" +
112+
" <data source='Honolulu' />" +
113+
" </macro>" +
114+
"</macros>" +
115+
"<rules>" +
116+
" <call-macro name='referencing' />" +
117+
"</rules>"
118+
);
131119
}
132120

133121
@Test
134122
public void shouldSupportVariablesInMacroParameters() {
135-
metamorph = InlineMorph.in(this)
136-
.with("<macros>")
137-
.with(" <macro name='inner-macro'>")
138-
.with(" <data source='$[source]' />")
139-
.with(" </macro>")
140-
.with(" <macro name='outer-macro'>")
141-
.with(" <entity name='Hawaii'>")
142-
.with(" <call-macro name='inner-macro' source='$[literal]' />")
143-
.with(" </entity>")
144-
.with(" </macro>")
145-
.with("</macros>")
146-
.with("<rules>")
147-
.with(" <call-macro name='outer-macro' literal='Honolulu' />")
148-
.with("</rules>")
149-
.createConnectedTo(receiver);
150-
151-
processRecordWithSingleLiteral();
152-
153-
verifyEntityWithSingleLiteral();
123+
testSingleLiteral(true,
124+
"<macros>" +
125+
" <macro name='inner-macro'>" +
126+
" <data source='$[source]' />" +
127+
" </macro>" +
128+
" <macro name='outer-macro'>" +
129+
" <entity name='Hawaii'>" +
130+
" <call-macro name='inner-macro' source='$[literal]' />" +
131+
" </entity>" +
132+
" </macro>" +
133+
"</macros>" +
134+
"<rules>" +
135+
" <call-macro name='outer-macro' literal='Honolulu' />" +
136+
"</rules>"
137+
);
154138
}
155139

156140
@Test
157141
public void issue227_shouldSupportXincludeForMacros() {
158-
metamorph = InlineMorph.in(this)
159-
.with("<include href='issue227_should-support-xinclude-for-macros.xml'")
160-
.with(" xmlns='http://www.w3.org/2001/XInclude' />")
161-
.with("<rules>")
162-
.with(" <call-macro name='included-macro' />")
163-
.with("</rules>")
164-
.createConnectedTo(receiver);
165-
166-
processRecordWithSingleLiteral();
167-
168-
verify(receiver).literal("Honolulu", "Aloha");
142+
testSingleLiteral(false,
143+
"<include href='issue227_should-support-xinclude-for-macros.xml'" +
144+
" xmlns='http://www.w3.org/2001/XInclude' />" +
145+
"<rules>" +
146+
" <call-macro name='included-macro' />" +
147+
"</rules>"
148+
);
169149
}
170150

171151
@Test
172152
public void shouldSupportXPointer() {
173-
metamorph = InlineMorph.in(this)
174-
.with("<include href='should-support-xpointer.xml'")
175-
.with(" xmlns='http://www.w3.org/2001/XInclude'")
176-
.with(" xpointer='element(/1/1)' />")
177-
.with("<rules>")
178-
.with(" <call-macro name='included-macro' />")
179-
.with("</rules>")
180-
.createConnectedTo(receiver);
181-
182-
processRecordWithSingleLiteral();
183-
184-
verify(receiver).literal("Honolulu", "Aloha");
185-
}
186-
187-
private void processRecordWithSingleLiteral() {
188-
metamorph.startRecord("1");
189-
metamorph.literal("Honolulu", "Aloha");
190-
metamorph.endRecord();
153+
testSingleLiteral(false,
154+
"<include href='should-support-xpointer.xml'" +
155+
" xmlns='http://www.w3.org/2001/XInclude'" +
156+
" xpointer='element(/1/1)' />" +
157+
"<rules>" +
158+
" <call-macro name='included-macro' />" +
159+
"</rules>"
160+
);
191161
}
192162

193-
private void verifyEntityWithSingleLiteral() {
194-
final InOrder ordered = inOrder(receiver);
195-
ordered.verify(receiver).startEntity("Hawaii");
196-
ordered.verify(receiver).literal("Honolulu", "Aloha");
197-
ordered.verify(receiver).endEntity();
163+
private void testSingleLiteral(final boolean withEntity, final String morphDef) {
164+
assertMorph(receiver, morphDef,
165+
i -> {
166+
i.startRecord("1");
167+
i.literal("Honolulu", "Aloha");
168+
i.endRecord();
169+
},
170+
o -> {
171+
o.get().startRecord("1");
172+
173+
if (withEntity) {
174+
o.get().startEntity("Hawaii");
175+
}
176+
177+
o.get().literal("Honolulu", "Aloha");
178+
179+
if (withEntity) {
180+
o.get().endEntity();
181+
}
182+
183+
o.get().endRecord();
184+
}
185+
);
198186
}
199187

200188
}

0 commit comments

Comments
 (0)