1919
2020import static org .junit .Assert .assertEquals ;
2121import static org .junit .Assert .assertNotNull ;
22- import static org .mockito .ArgumentMatchers .any ;
23- import static org .mockito .ArgumentMatchers .anyInt ;
24- import static org .mockito .ArgumentMatchers .eq ;
25- import static org .mockito .Mockito .never ;
26- import static org .mockito .Mockito .verify ;
27-
28- import java .util .HashMap ;
29- import java .util .Map ;
3022
3123import org .junit .Before ;
3224import org .junit .Rule ;
3325import org .junit .Test ;
3426import org .metafacture .framework .helpers .DefaultStreamReceiver ;
3527import org .metafacture .metamorph .api .Maps ;
3628import org .metafacture .metamorph .api .NamedValueReceiver ;
29+ import org .mockito .ArgumentMatchers ;
3730import org .mockito .Mock ;
31+ import org .mockito .Mockito ;
32+ import org .mockito .exceptions .base .MockitoAssertionError ;
3833import org .mockito .junit .MockitoJUnit ;
3934import org .mockito .junit .MockitoRule ;
4035
36+ import java .util .HashMap ;
37+ import java .util .Map ;
38+ import java .util .function .Consumer ;
39+
4140/**
4241 * Tests for class {@link Metamorph}.
4342 *
@@ -50,7 +49,7 @@ public final class MetamorphTest {
5049 public MockitoRule mockitoRule = MockitoJUnit .rule ();
5150
5251 @ Mock
53- private NamedValueReceiver namedValueReceiver ;
52+ private NamedValueReceiver receiver ;
5453
5554 private Metamorph metamorph ;
5655
@@ -62,81 +61,64 @@ public void createSystemUnderTest() {
6261
6362 @ Test
6463 public void shouldMapMatchingPath () {
65- setupSimpleMappingMorph ();
66-
67- metamorph .startRecord ("" );
68- metamorph .literal ("testEntity.testLiteral" , "testValue" );
69-
70- verify (namedValueReceiver ).receive (eq ("outName" ), eq ("testValue" ),
71- any (), anyInt (), anyInt ());
64+ assertNamedValue (true , i -> {
65+ i .literal ("testEntity.testLiteral" , "testValue" );
66+ });
7267 }
7368
7469 @ Test
7570 public void shouldNotMapNonMatchingPath () {
76- setupSimpleMappingMorph ();
77-
78- metamorph .startRecord ("" );
79- metamorph .literal ("nonMatching.path" , "testValue" );
80-
81- verify (namedValueReceiver , never ()).receive (any (), any (), any (), anyInt (),
82- anyInt ());
71+ assertNamedValue (false , i -> {
72+ i .literal ("nonMatching.path" , "testValue" );
73+ });
8374 }
8475
8576 @ Test
8677 public void shouldMapMatchingLiteralInMatchingEntity () {
87- setupSimpleMappingMorph ();
88-
89- metamorph .startRecord ("" );
90- metamorph .startEntity ("testEntity" );
91- metamorph .literal ("testLiteral" , "testValue" );
92-
93- verify (namedValueReceiver ).receive (eq ("outName" ), eq ("testValue" ),
94- any (), anyInt (), anyInt ());
78+ assertNamedValue (true , i -> {
79+ i .startEntity ("testEntity" );
80+ i .literal ("testLiteral" , "testValue" );
81+ });
9582 }
9683
9784 @ Test
9885 public void shouldNotMapNonMatchingLiteralInMatchingEntity () {
99- setupSimpleMappingMorph ();
100-
101- metamorph .startRecord ("" );
102- metamorph .startEntity ("testEntity" );
103- metamorph .literal ("nonMatching" , "testValue" );
104-
105- verify (namedValueReceiver , never ()).receive (any (), any (), any (), anyInt (),
106- anyInt ());
86+ assertNamedValue (false , i -> {
87+ i .startEntity ("testEntity" );
88+ i .literal ("nonMatching" , "testValue" );
89+ });
10790 }
10891
10992 @ Test
11093 public void shouldNotMapMatchingLiteralInNonMatchingEntity () {
111- setupSimpleMappingMorph ();
112-
113- metamorph .startRecord ("" );
114- metamorph .startEntity ("nonMatching" );
115- metamorph .literal ("testLiteral" , "testValue" );
116-
117- verify (namedValueReceiver , never ()).receive (any (), any (), any (), anyInt (),
118- anyInt ());
94+ assertNamedValue (false , i -> {
95+ i .startEntity ("nonMatching" );
96+ i .literal ("testLiteral" , "testValue" );
97+ });
11998 }
12099 @ Test
121100 public void shouldNotMapLiteralWithoutMatchingEntity () {
122- setupSimpleMappingMorph ();
123-
124- metamorph .startRecord ("" );
125- metamorph .literal ("testLiteral" , "testValue" );
126-
127- verify (namedValueReceiver , never ()).receive (any (), any (), any (), anyInt (),
128- anyInt ());
101+ assertNamedValue (false , i -> {
102+ i .literal ("testLiteral" , "testValue" );
103+ });
129104 }
130105
131- /**
132- * Creates the Metamorph structure that corresponds to the Metamorph XML
133- * statement {@code <data source="testEntity.testLiteral" name="outName" />}.
134- */
135- private void setupSimpleMappingMorph () {
136- final Data data = new Data ();
137- data .setName ("outName" );
138- data .setNamedValueReceiver (namedValueReceiver );
139- metamorph .registerNamedValueReceiver ("testEntity" + '.' + "testLiteral" , data );
106+ @ Test
107+ public void shouldFedbackLiteralsStartingWithAtIntoMetamorph () {
108+ assertNamedValue (true , i -> {
109+ final Data data1 ;
110+ data1 = new Data ();
111+ data1 .setName ("@feedback" );
112+ i .addNamedValueSource (data1 );
113+ i .registerNamedValueReceiver ("testLiteral" , data1 );
114+
115+ final Data data2 = new Data ();
116+ data2 .setName ("outName" );
117+ data2 .setNamedValueReceiver (receiver );
118+ i .registerNamedValueReceiver ("@feedback" , data2 );
119+
120+ i .literal ("testLiteral" , "testValue" );
121+ });
140122 }
141123
142124 @ Test
@@ -160,26 +142,6 @@ public void shouldReturnDefaultValueIfMapIsKnownButNameIsUnknown() {
160142 assertEquals ("defaultValue" , metamorph .getValue ("testMap" , "nameNotInMap" ));
161143 }
162144
163- @ Test
164- public void shouldFedbackLiteralsStartingWithAtIntoMetamorph () {
165- final Data data1 ;
166- data1 = new Data ();
167- data1 .setName ("@feedback" );
168- metamorph .addNamedValueSource (data1 );
169- metamorph .registerNamedValueReceiver ("testLiteral" , data1 );
170-
171- final Data data2 = new Data ();
172- data2 .setName ("outName" );
173- data2 .setNamedValueReceiver (namedValueReceiver );
174- metamorph .registerNamedValueReceiver ("@feedback" , data2 );
175-
176- metamorph .startRecord ("" );
177- metamorph .literal ("testLiteral" , "testValue" );
178-
179- verify (namedValueReceiver ).receive (eq ("outName" ), eq ("testValue" ),
180- any (), anyInt (), anyInt ());
181- }
182-
183145 @ Test (expected =IllegalStateException .class )
184146 public void shouldThrowIllegalStateExceptionIfEntityIsNotClosed () {
185147 metamorph .startRecord ("" );
@@ -189,4 +151,32 @@ public void shouldThrowIllegalStateExceptionIfEntityIsNotClosed() {
189151 metamorph .endRecord (); // Exception expected
190152 }
191153
154+ private void assertNamedValue (final boolean matching , final Consumer <Metamorph > in ) {
155+ /**
156+ * Creates the Metamorph structure that corresponds to the Metamorph XML
157+ * statement {@code <data source="testEntity.testLiteral" name="outName" />}.
158+ */
159+ final Data data = new Data ();
160+ data .setName ("outName" );
161+ data .setNamedValueReceiver (receiver );
162+ metamorph .registerNamedValueReceiver ("testEntity" + '.' + "testLiteral" , data );
163+
164+ metamorph .startRecord ("" );
165+ in .accept (metamorph );
166+
167+ try {
168+ if (matching ) {
169+ Mockito .verify (receiver ).receive (
170+ ArgumentMatchers .eq ("outName" ), ArgumentMatchers .eq ("testValue" ),
171+ ArgumentMatchers .any (), ArgumentMatchers .eq (1 ), ArgumentMatchers .anyInt ());
172+ }
173+
174+ Mockito .verifyNoMoreInteractions (receiver );
175+ }
176+ catch (final MockitoAssertionError e ) {
177+ System .out .println (Mockito .mockingDetails (receiver ).printInvocations ());
178+ throw e ;
179+ }
180+ }
181+
192182}
0 commit comments