37
37
import java .util .List ;
38
38
import java .util .concurrent .TimeUnit ;
39
39
40
+ import static org .junit .Assert .assertEquals ;
41
+ import static org .junit .Assert .assertNull ;
42
+ import static org .junit .Assert .assertTrue ;
43
+
40
44
public class TestCommandListener implements CommandListener {
41
45
private final List <CommandEvent > events = new ArrayList <CommandEvent >();
42
- private int firstRequestId = RequestMessage .getCurrentGlobalId ();
43
46
private static final CodecRegistry CODEC_REGISTRY_HACK ;
44
47
45
48
static {
@@ -59,7 +62,6 @@ public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) {
59
62
60
63
public void reset () {
61
64
events .clear ();
62
- firstRequestId = RequestMessage .getCurrentGlobalId ();
63
65
}
64
66
65
67
public List <CommandEvent > getEvents () {
@@ -92,79 +94,54 @@ public void commandFailed(final CommandFailedEvent event) {
92
94
events .add (event );
93
95
}
94
96
95
- public boolean eventsWereDelivered (final List <CommandEvent > expectedEvents ) {
96
- if (expectedEvents .size () != events .size ()) {
97
- return false ;
98
- }
99
- int currentlyExpectedRequestId = firstRequestId ;
97
+ public void eventsWereDelivered (final List <CommandEvent > expectedEvents ) {
98
+ assertEquals (expectedEvents .size (), events .size ());
99
+
100
+ int currentlyExpectedRequestId = 0 ;
100
101
for (int i = 0 ; i < events .size (); i ++) {
101
102
CommandEvent actual = events .get (i );
102
103
CommandEvent expected = expectedEvents .get (i );
103
- if (!actual .getClass ().equals (expected .getClass ())) {
104
- return false ;
105
- }
106
104
107
- if (actual .getRequestId () != currentlyExpectedRequestId ) {
108
- return false ;
109
- }
105
+ assertEquals (expected .getClass (), actual .getClass ());
110
106
111
- if (!(actual instanceof CommandStartedEvent )) {
112
- currentlyExpectedRequestId ++;
107
+ if (actual instanceof CommandStartedEvent ) {
108
+ currentlyExpectedRequestId = actual .getRequestId ();
109
+ } else {
110
+ assertEquals (currentlyExpectedRequestId , actual .getRequestId ());
113
111
}
114
112
115
- if (!actual .getConnectionDescription ().equals (expected .getConnectionDescription ())) {
116
- return false ;
117
- }
113
+ assertEquals (expected .getConnectionDescription (), actual .getConnectionDescription ());
118
114
119
- if (!actual .getCommandName ().equals (expected .getCommandName ())) {
120
- return false ;
121
- }
115
+ assertEquals (expected .getCommandName (), actual .getCommandName ());
122
116
123
117
if (actual .getClass ().equals (CommandStartedEvent .class )) {
124
- if (!isEquivalent ((CommandStartedEvent ) actual , (CommandStartedEvent ) expected )) {
125
- return false ;
126
- }
118
+ assertEquivalence ((CommandStartedEvent ) actual , (CommandStartedEvent ) expected );
127
119
} else if (actual .getClass ().equals (CommandSucceededEvent .class )) {
128
- if (!isEquivalent ((CommandSucceededEvent ) actual , (CommandSucceededEvent ) expected )) {
129
- return false ;
130
- }
120
+ assertEquivalence ((CommandSucceededEvent ) actual , (CommandSucceededEvent ) expected );
131
121
} else if (actual .getClass ().equals (CommandFailedEvent .class )) {
132
- if (!isEquivalent ((CommandFailedEvent ) actual , (CommandFailedEvent ) expected )) {
133
- return false ;
134
- }
122
+ assertEquivalence ((CommandFailedEvent ) actual , (CommandFailedEvent ) expected );
135
123
} else {
136
124
throw new UnsupportedOperationException ("Unsupported event type: " + actual .getClass ());
137
125
}
138
126
}
139
-
140
- return true ;
141
127
}
142
128
143
- private boolean isEquivalent (final CommandFailedEvent actual , final CommandFailedEvent expected ) {
144
- if (!actual .getThrowable ().equals (expected .getThrowable ())) {
145
- return false ;
146
- }
147
- return true ;
129
+ private void assertEquivalence (final CommandFailedEvent actual , final CommandFailedEvent expected ) {
130
+ assertEquals (expected .getThrowable (), actual .getThrowable ());
148
131
}
149
132
150
- private boolean isEquivalent (final CommandSucceededEvent actual , final CommandSucceededEvent expected ) {
133
+ private void assertEquivalence (final CommandSucceededEvent actual , final CommandSucceededEvent expected ) {
151
134
if (actual .getResponse () == null ) {
152
- return expected .getResponse () == null ;
135
+ assertNull (expected .getResponse ());
136
+ } else {
137
+ // ignore extra elements in the actual response
138
+ assertTrue ("Expected response contains elements not in the actual response" ,
139
+ actual .getResponse ().entrySet ().containsAll (expected .getResponse ().entrySet ()));
153
140
}
154
- // ignore extra elements in the actual response
155
- if (!actual .getResponse ().entrySet ().containsAll (expected .getResponse ().entrySet ())) {
156
- return false ;
157
- }
158
- return true ;
159
141
}
160
142
161
- private boolean isEquivalent (final CommandStartedEvent actual , final CommandStartedEvent expected ) {
162
- if (!actual .getDatabaseName ().equals (expected .getDatabaseName ())) {
163
- return false ;
164
- }
165
- if (!actual .getCommand ().equals (expected .getCommand ())) {
166
- return false ;
167
- }
168
- return true ;
143
+ private void assertEquivalence (final CommandStartedEvent actual , final CommandStartedEvent expected ) {
144
+ assertEquals (expected .getDatabaseName (), actual .getDatabaseName ());
145
+ assertEquals (expected .getCommand (), actual .getCommand ());
169
146
}
170
147
}
0 commit comments