19
19
20
20
import static org .junit .Assert .assertEquals ;
21
21
import 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 ;
30
22
31
23
import org .junit .Before ;
32
24
import org .junit .Rule ;
33
25
import org .junit .Test ;
34
26
import org .metafacture .framework .helpers .DefaultStreamReceiver ;
35
27
import org .metafacture .metamorph .api .Maps ;
36
28
import org .metafacture .metamorph .api .NamedValueReceiver ;
29
+ import org .mockito .ArgumentMatchers ;
37
30
import org .mockito .Mock ;
31
+ import org .mockito .Mockito ;
32
+ import org .mockito .exceptions .base .MockitoAssertionError ;
38
33
import org .mockito .junit .MockitoJUnit ;
39
34
import org .mockito .junit .MockitoRule ;
40
35
36
+ import java .util .HashMap ;
37
+ import java .util .Map ;
38
+ import java .util .function .Consumer ;
39
+
41
40
/**
42
41
* Tests for class {@link Metamorph}.
43
42
*
@@ -50,7 +49,7 @@ public final class MetamorphTest {
50
49
public MockitoRule mockitoRule = MockitoJUnit .rule ();
51
50
52
51
@ Mock
53
- private NamedValueReceiver namedValueReceiver ;
52
+ private NamedValueReceiver receiver ;
54
53
55
54
private Metamorph metamorph ;
56
55
@@ -62,81 +61,64 @@ public void createSystemUnderTest() {
62
61
63
62
@ Test
64
63
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
+ });
72
67
}
73
68
74
69
@ Test
75
70
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
+ });
83
74
}
84
75
85
76
@ Test
86
77
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
+ });
95
82
}
96
83
97
84
@ Test
98
85
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
+ });
107
90
}
108
91
109
92
@ Test
110
93
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
+ });
119
98
}
120
99
@ Test
121
100
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
+ });
129
104
}
130
105
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
+ });
140
122
}
141
123
142
124
@ Test
@@ -160,26 +142,6 @@ public void shouldReturnDefaultValueIfMapIsKnownButNameIsUnknown() {
160
142
assertEquals ("defaultValue" , metamorph .getValue ("testMap" , "nameNotInMap" ));
161
143
}
162
144
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
-
183
145
@ Test (expected =IllegalStateException .class )
184
146
public void shouldThrowIllegalStateExceptionIfEntityIsNotClosed () {
185
147
metamorph .startRecord ("" );
@@ -189,4 +151,32 @@ public void shouldThrowIllegalStateExceptionIfEntityIsNotClosed() {
189
151
metamorph .endRecord (); // Exception expected
190
152
}
191
153
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
+
192
182
}
0 commit comments